'''simple docstring'''




class 	__UpperCAmelCase				:





       '''simple docstring'''



       def __init__(      self )		->     None:
           # numbermap keys are the number and its values are lists of indexes sorted
           # in ascending order
           A_     =   {}
           # indexmap keys are an index and it's values are the number at that index
           A_     =   {}

       def 					__A       (      self    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE )		->     list[int]:
           if isinstance(A__    ,     (range, str) ):
               A_     =   list(A__ )
           elif not isinstance(A__    ,     A__ ):
               raise TypeError(
                   '''binary_search_delete() only accepts either a list, range or str''' )

           A_     =   0
           A_     =   len(A__ ) - 1

           while low <= high:
               A_     =   (low + high) // 2
               if array[mid] == item:
                   array.pop(A__ )
                   return array
               elif array[mid] < item:
                   A_     =   mid + 1
               else:
                   A_     =   mid - 1
           raise ValueError(
               '''Either the item is not in the array or the array was unsorted''' )

       def 					__A       (      self    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE )		->     list[int]:
           if isinstance(A__    ,     (range, str) ):
               A_     =   list(A__ )
           elif not isinstance(A__    ,     A__ ):
               raise TypeError(
                   '''binary_search_insert() only accepts either a list, range or str''' )

           A_     =   0
           A_     =   len(A__ ) - 1

           while low <= high:
               A_     =   (low + high) // 2
               if array[mid] == index:
                   # If the item already exists in the array,
                   # insert it after the existing item
                   array.insert(mid + 1    ,     A__ )
                   return array
               elif array[mid] < index:
                   A_     =   mid + 1
               else:
                   A_     =   mid - 1

        # If the item doesn't exist in the array, insert it at the appropriate position
           array.insert(A__    ,     A__ )
           return array

       def 					__A       (      self    ,     _SCREAMING_SNAKE_CASE    ,     _SCREAMING_SNAKE_CASE )		->     None:
           # Remove previous index
           if index in self.indexmap:
               A_     =   self.indexmap[index]
               if len(self.numbermap[n] ) == 1:
                   del self.numbermap[n]
               else:
                   A_     =   self.binary_search_delete(self.numbermap[n]    ,     A__ )

        # Set new index
           A_     =   number

           # Number not seen before or empty so insert number value
           if number not in self.numbermap:
               A_     =   [index]

           # Here we need to perform a binary search insertion in order to insert
           # The item in the correct place
           else:
               A_     =   self.binary_search_insert(
                   self.numbermap[number]    ,     A__ )

       def 					__A       (      self    ,     _SCREAMING_SNAKE_CASE )		->     int:
           return self.numbermap.get(A__    ,     [-1] )[0]


if __name__ == "__main__":
  import doctest

  doctest.testmod()












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


__SCREAMING_SNAKE_CASE			:      Union[str, Any]      			=datasets.utils.logging.get_logger(__name__)






@dataclass
class 	A	(						datasets.BuilderConfig						):
     """simple docstring"""

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




     def        __snake_case      (			self     :      Optional[Any]	) ->				Union[str, Any]:
           if self.sql is None:
                 raise ValueError('sql must be specified'	)
           if self.con is None:
                 raise ValueError('con must be specified'	)




     def        __snake_case      (			self     :      List[Any]		,     A__     :      dict		,     A__     :      Optional[datasets.Features] = None		,     ) ->				str:
           lowerCamelCase__					     = 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
           lowerCamelCase__					     = 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	):
                             lowerCamelCase__					     = sqlalchemy.create_engine(config_kwargs['con'].split('://'	)[0] + '://'	)
                             lowerCamelCase__					     = str(sql.compile(dialect=engine.dialect	)	)
                             lowerCamelCase__					     = 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}"""	)
           lowerCamelCase__					     = config_kwargs['con']
           if not isinstance(A__		,     A__	):
                 lowerCamelCase__					     = 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        __snake_case      (			self     :      Any	) ->				Union[str, Any]:
           lowerCamelCase__					     = {
               '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						):
     """simple docstring"""

     _snake_case:		List[str]   =SqlConfig




     def        __snake_case      (			self     :      Tuple	) ->				Dict:
           return datasets.DatasetInfo(features=self.config.features	)




     def        __snake_case      (			self     :      List[Any]		,     A__     :      Any	) ->				List[Any]:
           return [datasets.SplitGenerator(name=datasets.Split.TRAIN		,     gen_kwargs={}	)]




     def        __snake_case      (			self     :      Union[str, Any]		,     A__     :      pa.Table	) ->				pa.Table:
           if self.config.features is not None:
                 lowerCamelCase__					     = self.config.features.arrow_schema
                 if all(not require_storage_cast(A__	) for feature in self.config.features.values()	):
                       # cheaper cast
                       lowerCamelCase__					     = 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
                       lowerCamelCase__					     = table_cast(A__		,     A__	)
           return pa_table




     def        __snake_case      (			self     :      int	) ->				List[Any]:
           lowerCamelCase__					     = self.config.chunksize
           lowerCamelCase__					     = pd.read_sql(
               self.config.sql		,     self.config.con		,     chunksize=A__		,     **self.config.pd_read_sql_kwargs	)
           lowerCamelCase__					     = [sql_reader] if chunksize is None else sql_reader
           for chunk_idx, df in enumerate(A__	):
                 lowerCamelCase__					     = pa.Table.from_pandas(A__	)
                 yield chunk_idx, self._cast_table(A__	)