code: def read_sql(
      sql: str,
      connection_factory: Callable[[], Connection],
      *,
      parallelism: int = -1,
      ray_remote_args: Optional[Dict[str, Any]] = None,
  ) -> Dataset:
      """Read from a database that provides a
      `Python DB API2-compliant <https://peps.python.org/pep-0249/>`_ connector.
  
      .. note::
  
          By default, ``read_sql`` launches multiple read tasks, and each task executes a
          ``LIMIT`` and ``OFFSET`` to fetch a subset of the rows. However, for many
          databases, ``OFFSET`` is slow.
  
          As a workaround, set ``parallelism=1`` to directly fetch all rows in a single
          task. Note that this approach requires all result rows to fit in the memory of
          single task. If the rows don't fit, your program may raise an out of memory
          error.
  
      Examples:
  
          For examples of reading from larger databases like MySQL and PostgreSQL, see
          :ref:`Reading from SQL Databases <reading_sql>`.
  
          .. testcode::
  
              import sqlite3
  
              import ray
  
              # Create a simple database
              connection = sqlite3.connect("example.db")
              connection.execute("CREATE TABLE movie(title, year, score)")
              connection.execute(
                  \"\"\"
                  INSERT INTO movie VALUES
                      ('Monty Python and the Holy Grail', 1975, 8.2),
                      ("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
                      ("Monty Python's Life of Brian", 1979, 8.0),
                      ("Rocky II", 1979, 7.3)
                  \"\"\"
              )
              connection.commit()
              connection.close()
  
              def create_connection():
                  return sqlite3.connect("example.db")
  
              # Get all movies
              ds = ray.data.read_sql("SELECT * FROM movie", create_connection)
              # Get movies after the year 1980
              ds = ray.data.read_sql(
                  "SELECT title, score FROM movie WHERE year >= 1980", create_connection
              )
              # Get the number of movies per year
              ds = ray.data.read_sql(
                  "SELECT year, COUNT(*) FROM movie GROUP BY year", create_connection
              )
  
          .. testcode::
              :hide:
  
              import os
              os.remove("example.db")
  
      Args:
          sql: The SQL query to execute.
          connection_factory: A function that takes no arguments and returns a
              Python DB API2
              `Connection object <https://peps.python.org/pep-0249/#connection-objects>`_.
          parallelism: The requested parallelism of the read. Defaults to -1,
              which automatically determines the optimal parallelism for your
              configuration. You should not need to manually set this value in most cases.
              For details on how the parallelism is automatically determined and guidance
              on how to tune it, see :ref:`Tuning read parallelism
              <read_parallelism>`.
          ray_remote_args: kwargs passed to :meth:`~ray.remote` in the read tasks.
  
      Returns:
          A :class:`Dataset` containing the queried data.
      """
      datasource = SQLDatasource(
          sql=sql, connection_factory=connection_factory
      )
      return read_datasource(
          datasource,
          parallelism=parallelism,
          ray_remote_args=ray_remote_args,
      )
  
imports: import collections
  import logging
  import os
  from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Optional, Tuple, TypeVar, Union
  import numpy
  import ray
  from ray._private.auto_init_hook import wrap_auto_init
  from ray.air.util.tensor_extensions.utils import _create_possibly_ragged_ndarray
  from ray.data._internal.block_list import BlockList
  from ray.data._internal.delegating_block_builder import DelegatingBlockBuilder
  from ray.data._internal.lazy_block_list import LazyBlockList
  from ray.data._internal.logical.operators.from_operators import FromArrow, FromItems, FromNumpy, FromPandas
  from ray.data._internal.logical.operators.read_operator import Read
  from ray.data._internal.logical.optimizers import LogicalPlan
  from ray.data._internal.plan import ExecutionPlan
  from ray.data._internal.remote_fn import cached_remote_fn
  from ray.data._internal.stats import DatasetStats
  from ray.data._internal.util import _autodetect_parallelism, _lazy_import_pyarrow_dataset, _warn_on_high_parallelism, get_table_block_metadata, ndarray_to_block, pandas_df_to_arrow_block
  from ray.data.block import Block, BlockAccessor, BlockExecStats, BlockMetadata
  from ray.data.context import DataContext
  from ray.data.dataset import Dataset, MaterializedDataset
  from ray.data.datasource import BaseFileMetadataProvider, BigQueryDatasource, BinaryDatasource, Connection, CSVDatasource, Datasource, ImageDatasource, JSONDatasource, MongoDatasource, NumpyDatasource, ParquetBaseDatasource, ParquetDatasource, ParquetMetadataProvider, PathPartitionFilter, RangeDatasource, SQLDatasource, TextDatasource, TFRecordDatasource, TorchDatasource, WebDatasetDatasource
  from ray.data.datasource._default_metadata_providers import get_generic_metadata_provider, get_image_metadata_provider, get_parquet_bulk_metadata_provider, get_parquet_metadata_provider
  from ray.data.datasource.datasource import Reader
  from ray.data.datasource.file_based_datasource import _unwrap_arrow_serialization_workaround, _wrap_arrow_serialization_workaround
  from ray.data.datasource.partitioning import Partitioning
  from ray.types import ObjectRef
  from ray.util.annotations import DeveloperAPI, PublicAPI
  from ray.util.scheduling_strategies import NodeAffinitySchedulingStrategy
label_desc: Use image node_id 17 for calling a global function with example usage: read_sql(sql, connection_factory) and returns: read_datasource