code: def register_type_if_not_exist(
      metadata_handle: metadata.Metadata,
      metadata_type: MetadataType,
  ) -> MetadataType:
      """Registers a metadata type if not exists.
  
      Uses existing type if schema is superset of what is needed. Otherwise tries
      to register new metadata type.
  
      Args:
        metadata_handle: A handler to access MLMD store.
        metadata_type: The metadata type to register if does not exist.
  
      Returns:
        A MetadataType with id
  
      Raises:
        RuntimeError: If new metadata type conflicts with existing schema in MLMD.
        ValueError: If metadata type is not expected.
      """
      if metadata_type.id:
          return metadata_type
  
      if isinstance(metadata_type, metadata_store_pb2.ArtifactType):
          get_type_handler = metadata_handle.store.get_artifact_type
          put_type_handler = metadata_handle.store.put_artifact_type
      elif isinstance(metadata_type, metadata_store_pb2.ContextType):
          get_type_handler = metadata_handle.store.get_context_type
          put_type_handler = metadata_handle.store.put_context_type
      elif isinstance(metadata_type, metadata_store_pb2.ExecutionType):
          get_type_handler = metadata_handle.store.get_execution_type
          put_type_handler = metadata_handle.store.put_execution_type
      else:
          raise ValueError(
              "Unexpected value type: %s." % type(metadata_type)
          )
  
      try:
          queried_metadata_type = get_type_handler(metadata_type.name)
          if queried_metadata_type is not None:
              assert _assert_metadata_type_match(
                  queried_metadata_type, metadata_type
              )
              return queried_metadata_type
      except (mlmd.errors.NotFoundError, AssertionError):
          res = _put_type_handler(
              get_type_handler, put_type_handler, metadata_type
          )
          return res
  
file_path: tfx/tfx/orchestration/portable/mlmd/common_utils.py
imports: import copy
  from typing import Any, TypeVar
  from absl import logging
  from tfx.orchestration import metadata
  import ml_metadata
  from ml_metadata.proto import metadata_store_pb2
label_desc: Use image node_id 2 for calling a global function with example usage: register_type_if_not_exist(metadata_handle, metadata_type) and returns: metadata_type, queried_metadata_type, res