code: def _sparse_feature_from_feature_spec(spec, name, domains):
      """Returns a representation of a SparseFeature from a feature spec."""
      if isinstance(spec.index_key, list):
          assert isinstance(
              spec.size, (list, tuple, tf.TensorShape)
          ), type(spec.size)
          assert len(spec.index_key) == len(spec.size), (
              spec.index_key,
              spec.size,
          )
          spec_size = [
              s.value if isinstance(s, tf.compat.v1.Dimension) else s
              for s in spec.size
          ]
          spec_size = [s if s != -1 else None for s in spec_size]
          int_domains = [
              schema_pb2.IntDomain(min=0, max=size - 1)
              if size is not None
              else None
              for size in spec_size
          ]
          index_feature = [
              schema_pb2.Feature(
                  name=key, type=schema_pb2.INT, int_domain=int_domain
              )
              for (key, int_domain) in zip(spec.index_key, int_domains)
          ]
          index_feature_ref = [
              schema_pb2.SparseFeature.IndexFeature(name=key)
              for key in spec.index_key
          ]
      else:
          # Create a index feature.
          index_feature = [
              schema_pb2.Feature(
                  name=spec.index_key,
                  type=schema_pb2.INT,
                  int_domain=schema_pb2.IntDomain(
                      min=0, max=spec.size - 1
                  ),
              )
          ]
          index_feature_ref = [
              schema_pb2.SparseFeature.IndexFeature(name=spec.index_key)
          ]
  
      # Create a value feature.
      value_feature = schema_pb2.Feature(name=spec.value_key)
      _set_type(name, value_feature, spec.dtype)
      _set_domain(name, value_feature, domains.get(name))
  
      # Create a sparse feature which refers to the index and value features.
      value_feature_ref = schema_pb2.SparseFeature.ValueFeature(
          name=spec.value_key
      )
      sparse_feature = schema_pb2.SparseFeature(
          name=name,
          is_sorted=True if spec.already_sorted else None,
          index_feature=index_feature_ref,
          value_feature=value_feature_ref,
      )
  
      return (index_feature, value_feature, sparse_feature)
  
imports: import dataclasses
  import typing
  from typing import Dict, List, Mapping, Optional, Tuple, Union
  import tensorflow
  from tensorflow_transform import common_types
  from tensorflow_transform.tf_metadata import schema_utils_legacy
  from tfx_bsl.tfxio import tensor_representation_util
  from tensorflow_metadata.proto.v0 import path_pb2
  from tensorflow_metadata.proto.v0 import schema_pb2
label_desc: Use image node_id 3 for calling a global function with example usage: _sparse_feature_from_feature_spec(spec, name, domains) and returns: index_feature,  value_feature,  sparse_feature