code: def from_spark(
      df: "pyspark.sql.DataFrame",
      path: Optional[str] = None,
      table_name: Optional[str] = None,
      version: Optional[str] = None,
      sql: Optional[str] = None,
      targets: Optional[str] = None,
      name: Optional[str] = None,
      digest: Optional[str] = None,
  ) -> SparkDataset:
      """
      Given a Spark DataFrame, constructs a
      :py:class:`SparkDataset <mlflow.data.spark_dataset.SparkDataset>` object for use with
      MLflow Tracking.
  
      Args:
          df: The Spark DataFrame from which to construct a SparkDataset.
          path: The path of the Spark or Delta source that the DataFrame originally came from. Note
              that the path does not have to match the DataFrame exactly, since the DataFrame may have
              been modified by Spark operations. This is used to reload the dataset upon request via
              :py:func:`SparkDataset.source.load()
              <mlflow.data.spark_dataset_source.SparkDatasetSource.load>`. If none of ``path``,
              ``table_name``, or ``sql`` are specified, a CodeDatasetSource is used, which will source
              information from the run context.
          table_name: The name of the Spark or Delta table that the DataFrame originally came from.
              Note that the table does not have to match the DataFrame exactly, since the DataFrame
              may have been modified by Spark operations. This is used to reload the dataset upon
              request via :py:func:`SparkDataset.source.load()
              <mlflow.data.spark_dataset_source.SparkDatasetSource.load>`. If none of ``path``,
              ``table_name``, or ``sql`` are specified, a CodeDatasetSource is used, which will source
              information from the run context.
          version: If the DataFrame originally came from a Delta table, specifies the version of the
              Delta table. This is used to reload the dataset upon request via
              :py:func:`SparkDataset.source.load()
              <mlflow.data.spark_dataset_source.SparkDatasetSource.load>`. ``version`` cannot be
              specified if ``sql`` is specified.
          sql: The Spark SQL statement that was originally used to construct the DataFrame. Note that
              the Spark SQL statement does not have to match the DataFrame exactly, since the
              DataFrame may have been modified by Spark operations. This is used to reload the dataset
              upon request via :py:func:`SparkDataset.source.load()
              <mlflow.data.spark_dataset_source.SparkDatasetSource.load>`. If none of ``path``,
              ``table_name``, or ``sql`` are specified, a CodeDatasetSource is used, which will source
              information from the run context.
          targets: Optional. The name of the Data Frame column containing targets (labels) for
              supervised learning.
          name: The name of the dataset. E.g. "wiki_train". If unspecified, a name is automatically
              generated.
          digest: The digest (hash, fingerprint) of the dataset. If unspecified, a digest is
              automatically computed.
  
      Returns:
          An instance of :py:class:`SparkDataset <mlflow.data.spark_dataset.SparkDataset>`.
      """
      from mlflow.data.code_dataset_source import CodeDatasetSource
      from mlflow.data.spark_delta_utils import (
          _is_delta_table,
          _is_delta_table_path,
          _try_get_delta_table_latest_version_from_path,
          _try_get_delta_table_latest_version_from_table_name,
      )
      from mlflow.tracking.context import registry
  
      if (path, table_name, sql).count(None) < 2:
          raise MlflowException(
              "Must specify at most one of `path`, `table_name`, or `sql`.",
              INVALID_PARAMETER_VALUE,
          )
  
      if (sql, version).count(None) == 0:
          raise MlflowException(
              "`version` may not be specified when `sql` is specified. `version` may only be"
              " specified when `table_name` or `path` is specified.",
              INVALID_PARAMETER_VALUE,
          )
  
      if sql is not None:
          source = SparkDatasetSource(sql=sql)
      elif path is not None:
          if _is_delta_table_path(path):
              version = (
                  version
                  or _try_get_delta_table_latest_version_from_path(path)
              )
              source = DeltaDatasetSource(
                  path=path, delta_table_version=version
              )
          elif version is None:
              source = SparkDatasetSource(path=path)
          else:
              raise MlflowException(
                  f"Version '{version}' was specified, but the path '{path}' does not refer"
                  f" to a Delta table.",
                  INVALID_PARAMETER_VALUE,
              )
      elif table_name is not None:
          if _is_delta_table(table_name):
              version = (
                  version
                  or _try_get_delta_table_latest_version_from_table_name(
                      table_name
                  )
              )
              source = DeltaDatasetSource(
                  delta_table_name=table_name,
                  delta_table_version=version,
              )
          elif version is None:
              source = SparkDatasetSource(table_name=table_name)
          else:
              raise MlflowException(
                  f"Version '{version}' was specified, but could not find a Delta table with name"
                  f" '{table_name}'.",
                  INVALID_PARAMETER_VALUE,
              )
      else:
          context_tags = registry.resolve_tags()
          source = CodeDatasetSource(tags=context_tags)
  
      return SparkDataset(
          df=df,
          source=source,
          targets=targets,
          name=name,
          digest=digest,
      )
  
imports: import json
  import logging
  from functools import cached_property
  from typing import TYPE_CHECKING, Any, Dict, Optional, Union
  from mlflow.data.dataset import Dataset
  from mlflow.data.dataset_source import DatasetSource
  from mlflow.data.delta_dataset_source import DeltaDatasetSource
  from mlflow.data.digest_utils import compute_spark_df_digest
  from mlflow.data.pyfunc_dataset_mixin import PyFuncConvertibleDatasetMixin, PyFuncInputsOutputs
  from mlflow.data.spark_dataset_source import SparkDatasetSource
  from mlflow.exceptions import MlflowException
  from mlflow.models.evaluation.base import EvaluationDataset
  from mlflow.protos.databricks_pb2 import INTERNAL_ERROR, INVALID_PARAMETER_VALUE
  from mlflow.types import Schema
  from mlflow.types.utils import _infer_schema
  from mlflow.utils.annotations import experimental
label_desc: Use image node_id 2 for calling a global function with example usage: from_spark(df, path, table_name, version, sql, targets, name, digest) and returns: SparkDataset