



import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
    import sqlitea

    import sqlalchemy


__lowerCamelCase      =    datasets.utils.logging.get_logger(__name__)



@dataclass
class 						_snake_case					(	datasets.BuilderConfig ):





 """simple docstring"""



 lowerCamelCase_								=	None
 lowerCamelCase_								=	None
 lowerCamelCase_								=	None
 lowerCamelCase_								=	True
 lowerCamelCase_								=	None
 lowerCamelCase_								=	None
 lowerCamelCase_								=	None
 lowerCamelCase_								=	1_0_0_0_0
 lowerCamelCase_								=	None
 def lowercase_						(  self    )		->							Optional[int]:




   """simple docstring"""







   if self.sql is None:
     raise ValueError('''sql must be specified'''    )
   if self.con is None:
     raise ValueError('''con must be specified'''    )
 def lowercase_						(  self		,			a		,			a = None		,			)		->							str:




   """simple docstring"""







   _A							=			config_kwargs.copy()
   # We need to stringify the Selectable object to make its hash deterministic

   # The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
   _A							=			config_kwargs['''sql''']
   if not isinstance(a		,			a    ):
     if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
       import sqlalchemy

       if isinstance(a		,			sqlalchemy.sql.Selectable    ):
         _A							=			sqlalchemy.create_engine(config_kwargs['''con'''].split('''://'''    )[0] + '''://'''    )
         _A							=			str(sql.compile(dialect=engine.dialect    )    )
         _A							=			sql_str
       else:
         raise TypeError(
             f'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(a    )}: {sql}'''    )
     else:
       raise TypeError(
           f'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(a    )}: {sql}'''    )
   _A							=			config_kwargs['''con''']
   if not isinstance(a		,			a    ):
     _A							=			id(a    )
     logger.info(
         f'''SQL connection \'con\' of type {type(a    )} couldn\'t be hashed properly. To enable hashing, specify \'con\' as URI string instead.'''    )

   return super().create_config_id(a		,			custom_features=a    )



 @property
 def lowercase_						(  self    )		->							Tuple:




   """simple docstring"""







   _A							=			{
       '''index_col''': self.index_col,
       '''columns''': self.columns,
       '''params''': self.params,
       '''coerce_float''': self.coerce_float,
       '''parse_dates''': self.parse_dates,
   }
   return pd_read_sql_kwargs







class 						_snake_case					(	datasets.ArrowBasedBuilder ):





 """simple docstring"""



 lowerCamelCase_								=	SqlConfig
 def lowercase_						(  self    )		->							int:




   """simple docstring"""







   return datasets.DatasetInfo(features=self.config.features    )
 def lowercase_						(  self		,			a    )		->							List[str]:




   """simple docstring"""







   return [datasets.SplitGenerator(name=datasets.Split.TRAIN		,			gen_kwargs={}    )]
 def lowercase_						(  self		,			a    )		->							pa.Table:




   """simple docstring"""







   if self.config.features is not None:
     _A							=			self.config.features.arrow_schema
     if all(not require_storage_cast(a    ) for feature in self.config.features.values()    ):
       # cheaper cast
       _A							=			pa.Table.from_arrays([pa_table[field.name] for field in schema]		,			schema=a    )
     else:
       # more expensive cast; allows str <-> int/float or str to Audio for example
       _A							=			table_cast(a		,			a    )
   return pa_table



 def lowercase_						(  self    )		->							str:




   """simple docstring"""







   _A							=			self.config.chunksize
   _A							=			pd.read_sql(
       self.config.sql		,			self.config.con		,			chunksize=a		,			**self.config.pd_read_sql_kwargs    )
   _A							=			[sql_reader] if chunksize is None else sql_reader
   for chunk_idx, df in enumerate(a    ):
     _A							=			pa.Table.from_pandas(a    )
     yield chunk_idx, self._cast_table(a    )
from typing import TypeVar

from .arrow_dataset import Dataset, _split_by_node_map_style_dataset
from .iterable_dataset import IterableDataset, _split_by_node_iterable_dataset


lowerCamelCase_				:		List[Any]       						=       TypeVar("""DatasetType""", Dataset, IterableDataset)
def 							lowerCAmelCase(	__lowerCamelCase      ,				__lowerCamelCase      ,				__lowerCamelCase						):
					if isinstance(__lowerCamelCase      ,				__lowerCamelCase						):
										return _split_by_node_map_style_dataset(__lowerCamelCase      ,				rank=__lowerCamelCase      ,				world_size=__lowerCamelCase						)
					else:
										return _split_by_node_iterable_dataset(__lowerCamelCase      ,				rank=__lowerCamelCase      ,				world_size=__lowerCamelCase						)




