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 :     List[Any]          =  datasets.utils.logging.get_logger(__name__)






@dataclass
class 		a__						( datasets.BuilderConfig ):
					A     	=							None
					A     	=							None
					A     	=							None
					A     	=							True
					A     	=							None
					A     	=							None
					A     	=							None
					A     	=							10000
					A     	=							None

					def     __UpperCamelCase       (   self				:	List[Any]   ):







							"""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     __UpperCamelCase       (   self				:	Dict,_A				:	Any,_A				:	List[Any] = None,):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Tuple    				=   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
							SCREAMING_SNAKE_CASE_  :		int    				=   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   ):
													SCREAMING_SNAKE_CASE_  :		Any    				=   sqlalchemy.create_engine(config_kwargs["con"].split("://"   )[0] + "://"   )
													SCREAMING_SNAKE_CASE_  :		int    				=   str(sql.compile(dialect=engine.dialect   )   )
													SCREAMING_SNAKE_CASE_  :		int    				=   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}'   )
							SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   config_kwargs["con"]
							if not isinstance(_A,_A   ):
									SCREAMING_SNAKE_CASE_  :		Dict    				=   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     __UpperCamelCase       (   self				:	Tuple   ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Tuple    				=   {
							    "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 		a__						( datasets.ArrowBasedBuilder ):
					A     	=							SqlConfig

					def     __UpperCamelCase       (   self				:	Optional[Any]   ):







							"""simple docstring"""






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

					def     __UpperCamelCase       (   self				:	str,_A				:	Optional[int]   ):







							"""simple docstring"""






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

					def     __UpperCamelCase       (   self				:	Optional[Any],_A				:	Any   ):







							"""simple docstring"""






							if self.config.features is not None:
									SCREAMING_SNAKE_CASE_  :		Optional[int]    				=   self.config.features.arrow_schema
									if all(not require_storage_cast(_A   ) for feature in self.config.features.values()   ):
											# cheaper cast
											SCREAMING_SNAKE_CASE_  :		Union[str, Any]    				=   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
											SCREAMING_SNAKE_CASE_  :		str    				=   table_cast(_A,_A   )
							return pa_table






					def     __UpperCamelCase       (   self				:	List[str]   ):







							"""simple docstring"""






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

from ..utils import DummyObject, requires_backends






class 		a__						( metaclass=A__ ):
					A     	=							['tensorflow_text']

					def __init__(   self				:	List[str],*_A				:	Optional[Any],**_A				:	int   ):







							"""simple docstring"""






							requires_backends(self,["tensorflow_text"]   )
