



"""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    SCREAMING_SNAKE_CASE__ (							__UpperCAmelCase   ):

    def __init__(				self			:				Union[str, Any] ,  lowerCAmelCase_			:				Union[str, "sqlalchemy.sql.Selectable"] ,  lowerCAmelCase_			:				Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] ,  lowerCAmelCase_			:				Optional[Features] = None ,  lowerCAmelCase_			:				str = None ,  lowerCAmelCase_			:				bool = False ,  **lowerCAmelCase_			:				Optional[Any] ,  ):







      """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 					_UpperCAmelCase	(				self			:				int):







      """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    SCREAMING_SNAKE_CASE__ :

    def __init__(				self			:				Union[str, Any] ,  lowerCAmelCase_			:				Dataset ,  lowerCAmelCase_			:				str ,  lowerCAmelCase_			:				Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"] ,  lowerCAmelCase_			:				Optional[int] = None ,  lowerCAmelCase_			:				Optional[int] = None ,  **lowerCAmelCase_			:				List[str] ,  ):







      """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 					_UpperCAmelCase	(				self			:				Optional[int]):







      """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 					_UpperCAmelCase	(				self			:				List[str] ,  lowerCAmelCase_			:				Union[str, Any]):







      """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 					_UpperCAmelCase	(				self			:				Union[str, Any] ,  lowerCAmelCase_			:				List[Any] ,  **lowerCAmelCase_			:				Any):







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






"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCAmelCase						:		Any			= {"configuration_xglm": ["XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XGLMConfig"]}

try:
       if not is_sentencepiece_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCAmelCase						:		List[str]			= ["XGLMTokenizer"]

try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCAmelCase						:		Dict			= ["XGLMTokenizerFast"]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCAmelCase						:		Tuple			= [
           "XGLM_PRETRAINED_MODEL_ARCHIVE_LIST",
           "XGLMForCausalLM",
           "XGLMModel",
           "XGLMPreTrainedModel",
       ]


try:
       if not is_flax_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCAmelCase						:		int			= [
           "FlaxXGLMForCausalLM",
           "FlaxXGLMModel",
           "FlaxXGLMPreTrainedModel",
       ]


try:
       if not is_tf_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCAmelCase						:		List[str]			= [
           "TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST",
           "TFXGLMForCausalLM",
           "TFXGLMModel",
           "TFXGLMPreTrainedModel",
       ]


if TYPE_CHECKING:
       from .configuration_xglm import XGLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XGLMConfig

       try:
              if not is_sentencepiece_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_xglm import XGLMTokenizer

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_xglm_fast import XGLMTokenizerFast

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_xglm import XGLM_PRETRAINED_MODEL_ARCHIVE_LIST, XGLMForCausalLM, XGLMModel, XGLMPreTrainedModel

       try:
              if not is_flax_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_flax_xglm import FlaxXGLMForCausalLM, FlaxXGLMModel, FlaxXGLMPreTrainedModel

       try:
              if not is_tf_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tf_xglm import (
                  TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TFXGLMForCausalLM,
                  TFXGLMModel,
                  TFXGLMPreTrainedModel,
              )


else:
       import sys

       UpperCAmelCase						:		Any			= _LazyModule(__name__, globals()["__file__"], _import_structure)

