

"""simple docstring"""




from ..utils import DummyObject, requires_backends
class      _UpperCAmelCase   (							metaclass=_lowerCAmelCase       ):
	a__		: Tuple					=					["torch", "transformers", "onnx"]



	def __init__(  self     :       Optional[int]			,    *_lowercase     :       int			,    **_lowercase     :       Optional[int]						):
		requires_backends(self			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       Tuple			,    *_lowercase     :       List[Any]			,    **_lowercase     :       Tuple						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       List[str]			,    *_lowercase     :       Optional[int]			,    **_lowercase     :       List[Any]						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)






class      _UpperCAmelCase   (							metaclass=_lowerCAmelCase       ):
	a__		: Optional[int]					=					["torch", "transformers", "onnx"]



	def __init__(  self     :       Tuple			,    *_lowercase     :       Optional[Any]			,    **_lowercase     :       List[Any]						):
		requires_backends(self			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       Optional[Any]			,    *_lowercase     :       Tuple			,    **_lowercase     :       Tuple						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       Dict			,    *_lowercase     :       List[Any]			,    **_lowercase     :       Union[str, Any]						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)






class      _UpperCAmelCase   (							metaclass=_lowerCAmelCase       ):
	a__		: Optional[Any]					=					["torch", "transformers", "onnx"]



	def __init__(  self     :       List[str]			,    *_lowercase     :       int			,    **_lowercase     :       List[Any]						):
		requires_backends(self			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       Any			,    *_lowercase     :       str			,    **_lowercase     :       Any						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       int			,    *_lowercase     :       Dict			,    **_lowercase     :       List[Any]						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)






class      _UpperCAmelCase   (							metaclass=_lowerCAmelCase       ):
	a__		: str					=					["torch", "transformers", "onnx"]



	def __init__(  self     :       str			,    *_lowercase     :       str			,    **_lowercase     :       Optional[int]						):
		requires_backends(self			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       Union[str, Any]			,    *_lowercase     :       Any			,    **_lowercase     :       Tuple						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       Tuple			,    *_lowercase     :       Tuple			,    **_lowercase     :       str						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)






class      _UpperCAmelCase   (							metaclass=_lowerCAmelCase       ):
	a__		: int					=					["torch", "transformers", "onnx"]



	def __init__(  self     :       List[str]			,    *_lowercase     :       List[str]			,    **_lowercase     :       Any						):
		requires_backends(self			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       List[str]			,    *_lowercase     :       List[str]			,    **_lowercase     :       Union[str, Any]						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       int			,    *_lowercase     :       Any			,    **_lowercase     :       Optional[int]						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)






class      _UpperCAmelCase   (							metaclass=_lowerCAmelCase       ):
	a__		: Any					=					["torch", "transformers", "onnx"]



	def __init__(  self     :       int			,    *_lowercase     :       Any			,    **_lowercase     :       Optional[int]						):
		requires_backends(self			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       Tuple			,    *_lowercase     :       Any			,    **_lowercase     :       Optional[int]						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)



	@classmethod
	def        a  (  cls     :       Optional[Any]			,    *_lowercase     :       Optional[int]			,    **_lowercase     :       int						):
		requires_backends(cls			,    ['''torch''', '''transformers''', '''onnx''']						)

"""simple docstring"""






import multiprocessing
from typing import TYPE_CHECKING, Optional, Union

from .. import Dataset, Features, config
from ..formatting import query_table
from ..packaged_modules.sql.sql import Sql
from ..utils import logging
from .abc import AbstractDatasetInputStream


if TYPE_CHECKING:
       import sqlitea

       import sqlalchemy





class 					_A		(			lowerCAmelCase      ):

      def __init__(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase = None						,			__lowerCAmelCase = None						,			__lowerCAmelCase = False						,			**__lowerCAmelCase						,			):







            """simple docstring"""



            super().__init__(features=__lowerCAmelCase						,			cache_dir=__lowerCAmelCase						,			keep_in_memory=__lowerCAmelCase						,			**__lowerCAmelCase		)
            lowercase								=      Sql(
                cache_dir=__lowerCAmelCase						,			features=__lowerCAmelCase						,			sql=__lowerCAmelCase						,			con=__lowerCAmelCase						,			**__lowerCAmelCase						,			)







      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      None
            lowercase								=      None
            lowercase								=      None
            lowercase								=      None

            self.builder.download_and_prepare(
                download_config=__lowerCAmelCase						,			download_mode=__lowerCAmelCase						,			verification_mode=__lowerCAmelCase						,			base_path=__lowerCAmelCase						,			)

            # Build dataset for splits
            lowercase								=      self.builder.as_dataset(
                split="""train"""						,			verification_mode=__lowerCAmelCase						,			in_memory=self.keep_in_memory		)
            return dataset






class 					_A		:

      def __init__(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase = None						,			__lowerCAmelCase = None						,			**__lowerCAmelCase						,			):







            """simple docstring"""



            if num_proc is not None and num_proc <= 0:
                  raise ValueError(f'num_proc {num_proc} must be an integer > 0.'		)

            lowercase								=      dataset
            lowercase								=      name
            lowercase								=      con
            lowercase								=      batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
            lowercase								=      num_proc
            lowercase								=      to_sql_kwargs

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.to_sql_kwargs.pop("""sql"""						,			__lowerCAmelCase		)
            lowercase								=      self.to_sql_kwargs.pop("""con"""						,			__lowerCAmelCase		)
            lowercase								=      self.to_sql_kwargs.pop("""index"""						,			__lowerCAmelCase		)

            lowercase								=      self._write(index=__lowerCAmelCase						,			**self.to_sql_kwargs		)
            return written

      def 							A__			(		self						,			__lowerCAmelCase		):







            """simple docstring"""



            lowercase				,				lowercase				,				lowercase								=      args
            lowercase								=      {**to_sql_kwargs, """if_exists""": """append"""} if offset > 0 else to_sql_kwargs
            lowercase								=      query_table(
                table=self.dataset.data						,			key=slice(__lowerCAmelCase						,			offset + self.batch_size		)						,			indices=self.dataset._indices						,			)
            lowercase								=      batch.to_pandas()
            lowercase								=      df.to_sql(self.name						,			self.con						,			index=__lowerCAmelCase						,			**__lowerCAmelCase		)
            return num_rows or len(__lowerCAmelCase		)







      def 							A__			(		self						,			__lowerCAmelCase						,			**__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      0

            if self.num_proc is None or self.num_proc == 1:
                  for offset in logging.tqdm(
                      range(0						,			len(self.dataset		)						,			self.batch_size		)						,			unit="""ba"""						,			disable=not logging.is_progress_bar_enabled()						,			desc="""Creating SQL from Arrow format"""						,			):
                        written += self._batch_sql((offset, index, to_sql_kwargs)		)
            else:
                  lowercase				,				lowercase								=      len(self.dataset		), self.batch_size
                  with multiprocessing.Pool(self.num_proc		) as pool:
                        for num_rows in logging.tqdm(
                            pool.imap(
                                self._batch_sql						,			[(offset, index, to_sql_kwargs) for offset in range(0						,			__lowerCAmelCase						,			__lowerCAmelCase		)]						,			)						,			total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size						,			unit="""ba"""						,			disable=not logging.is_progress_bar_enabled()						,			desc="""Creating SQL from Arrow format"""						,			):
                              written += num_rows

            return written



