



'''simple docstring'''





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					)	->							Tuple:




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

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

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



 @property
 def lowercase_		(						self					)	->							Union[str, 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 		_snake_case      (			datasets.ArrowBasedBuilder				):





 """simple docstring"""



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




   """simple docstring"""







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




   """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(lowerCamelCase__					) for feature in self.config.features.values()					):
       # cheaper cast
       _A           =    pa.Table.from_arrays([pa_table[field.name] for field in schema]	,    schema=lowerCamelCase__					)
     else:
       # more expensive cast; allows str <-> int/float or str to Audio for example
       _A           =    table_cast(lowerCamelCase__	,    lowerCamelCase__					)
   return pa_table



 def lowercase_		(						self					)	->							List[Any]:




   """simple docstring"""







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




from ..utils import requires_backends


__lowerCamelCase  				=					None



class 		_snake_case      :





 """simple docstring"""



 def __init__(						self	,    *a	,    **a					)	->							Tuple:




   """simple docstring"""







   requires_backends(self	,    ['''detectron2''']					)



 @classmethod
 def lowercase_		(						cls	,    *a	,    **a					)	->							Optional[int]:




   """simple docstring"""







   requires_backends(cls	,    ['''detectron2''']					)