'''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


UpperCamelCase       :     int			=    datasets.utils.logging.get_logger(__name__)



@dataclass
class   UpperCamelCase					(       datasets.BuilderConfig       ):







    """simple docstring"""





    A     :    Union[str, "sqlalchemy.sql.Selectable"]				=			None
    A     :    Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]				=			None
    A     :    Optional[Union[str, List[str]]]				=			None
    A     :    bool				=			True
    A     :    Optional[Union[List, Tuple, Dict]]				=			None
    A     :    Optional[Union[List, Dict]]				=			None
    A     :    Optional[List[str]]				=			None
    A     :    Optional[int]				=			1_0000
    A     :    Optional[datasets.Features]				=			None





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


         """simple docstring"""







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


         """simple docstring"""







         a					:							Union[str, Any]							= {
             '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   UpperCamelCase					(       datasets.ArrowBasedBuilder       ):







    """simple docstring"""





    A     :    Tuple				=			SqlConfig





    def    SCREAMING_SNAKE_CASE_     (					self     :						str):


         """simple docstring"""







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





    def    SCREAMING_SNAKE_CASE_     (					self     :						List[Any]							,							UpperCAmelCase_     :						str):


         """simple docstring"""







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





    def    SCREAMING_SNAKE_CASE_     (					self     :						List[Any]							,							UpperCAmelCase_     :						pa.Table):


         """simple docstring"""







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







    def    SCREAMING_SNAKE_CASE_     (					self     :						List[Any]):


         """simple docstring"""







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





from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


__SCREAMING_SNAKE_CASE	:							str           =  {
    '''configuration_ernie_m''': ['''ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ErnieMConfig'''],
}

try:
		if not is_sentencepiece_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__SCREAMING_SNAKE_CASE	:							Any           =  ['''ErnieMTokenizer''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__SCREAMING_SNAKE_CASE	:							Tuple           =  [
		    '''ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ErnieMForMultipleChoice''',
		    '''ErnieMForQuestionAnswering''',
		    '''ErnieMForSequenceClassification''',
		    '''ErnieMForTokenClassification''',
		    '''ErnieMModel''',
		    '''ErnieMPreTrainedModel''',
		    '''ErnieMForInformationExtraction''',
		]


if TYPE_CHECKING:
		from .configuration_ernie_m import ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieMConfig

		try:
				if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_ernie_m import ErnieMTokenizer

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_ernie_m import (
				    ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ErnieMForInformationExtraction,
				    ErnieMForMultipleChoice,
				    ErnieMForQuestionAnswering,
				    ErnieMForSequenceClassification,
				    ErnieMForTokenClassification,
				    ErnieMModel,
				    ErnieMPreTrainedModel,
				)


else:
		import sys

		__SCREAMING_SNAKE_CASE	:							str           =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

