






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


__a :    Optional[Any]								=				datasets.utils.logging.get_logger(__name__)


@dataclass
class  __lowercase (		datasets.BuilderConfig					):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE				  =			None
     SCREAMING_SNAKE_CASE				  =			None
     SCREAMING_SNAKE_CASE				  =			None
     SCREAMING_SNAKE_CASE				  =			True
     SCREAMING_SNAKE_CASE				  =			None
     SCREAMING_SNAKE_CASE				  =			None
     SCREAMING_SNAKE_CASE				  =			None
     SCREAMING_SNAKE_CASE				  =			1_00_00
     SCREAMING_SNAKE_CASE				  =			None




     def 						lowerCAmelCase_       (			self    :     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 						lowerCAmelCase_       (			self    :     Union[str, Any]	,		UpperCamelCase_    :     dict	,		UpperCamelCase_    :     Optional[datasets.Features] = None	,		):


         """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(UpperCamelCase_	,		UpperCamelCase_			):
             if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
                 import sqlalchemy

                 if isinstance(UpperCamelCase_	,		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(UpperCamelCase_			)}: {sql}"			)
             else:
                 raise TypeError(
                     F"Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(UpperCamelCase_			)}: {sql}"			)
         __A             =		config_kwargs["""con"""]
         if not isinstance(UpperCamelCase_	,		UpperCamelCase_			):
             __A             =		id(UpperCamelCase_			)
             logger.info(
                 F"SQL connection 'con' of type {type(UpperCamelCase_			)} couldn't be hashed properly. To enable hashing, specify 'con' as URI string instead."			)

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





     @property
     def 						lowerCAmelCase_       (			self    :     Any			):


         """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  __lowercase (		datasets.ArrowBasedBuilder					):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE				  =			SqlConfig




     def 						lowerCAmelCase_       (			self    :     Any			):


         """simple docstring"""



         return datasets.DatasetInfo(features=self.config.features			)




     def 						lowerCAmelCase_       (			self    :     Optional[int]	,		UpperCamelCase_    :     int			):


         """simple docstring"""



         return [datasets.SplitGenerator(name=datasets.Split.TRAIN	,		gen_kwargs={}			)]




     def 						lowerCAmelCase_       (			self    :     int	,		UpperCamelCase_    :     pa.Table			):


         """simple docstring"""



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





     def 						lowerCAmelCase_       (			self    :     Any			):


         """simple docstring"""



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












from ..utils import DummyObject, requires_backends


__a :    int								=				None



def      _SCREAMING_SNAKE_CASE  (	*__lowercase				:     Any     ,			**__lowercase				:     Optional[int]      )  -> Tuple:





    """simple docstring"""

    requires_backends(__lowercase     ,			["""sentencepiece""", """tokenizers"""]      )




