


'''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    UpperCAmelCase_  (_UpperCAmelCase  ):





    """simple docstring"""






    def __init__(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_ = None			,     SCREAMING_SNAKE_CASE_ = None			,     SCREAMING_SNAKE_CASE_ = False			,     **SCREAMING_SNAKE_CASE_			,     )				->     Tuple:
         super().__init__(features=SCREAMING_SNAKE_CASE_			,     cache_dir=SCREAMING_SNAKE_CASE_			,     keep_in_memory=SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Optional[int]								=		Sql(
             cache_dir=SCREAMING_SNAKE_CASE_			,     features=SCREAMING_SNAKE_CASE_			,     sql=SCREAMING_SNAKE_CASE_			,     con=SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_			,     )







    def       lowercase_				(							self	)				->     Union[str, Any]:
         __lowerCamelCase   :	Dict								=		None
         __lowerCamelCase   :	Tuple								=		None
         __lowerCamelCase   :	Tuple								=		None
         __lowerCamelCase   :	Optional[int]								=		None

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

         # Build dataset for splits
         __lowerCamelCase   :	Optional[int]								=		self.builder.as_dataset(
             split='train'			,     verification_mode=SCREAMING_SNAKE_CASE_			,     in_memory=self.keep_in_memory	)
         return dataset







class    UpperCAmelCase_  :





    """simple docstring"""






    def __init__(							self			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_ = None			,     SCREAMING_SNAKE_CASE_ = None			,     **SCREAMING_SNAKE_CASE_			,     )				->     Union[str, Any]:
         if num_proc is not None and num_proc <= 0:
              raise ValueError(f'num_proc {num_proc} must be an integer > 0.'	)

         __lowerCamelCase   :	List[str]								=		dataset
         __lowerCamelCase   :	str								=		name
         __lowerCamelCase   :	Optional[Any]								=		con
         __lowerCamelCase   :	Optional[Any]								=		batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
         __lowerCamelCase   :	str								=		num_proc
         __lowerCamelCase   :	Optional[int]								=		to_sql_kwargs







    def       lowercase_				(							self	)				->     int:
         __lowerCamelCase   :	int								=		self.to_sql_kwargs.pop('sql'			,     SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	str								=		self.to_sql_kwargs.pop('con'			,     SCREAMING_SNAKE_CASE_	)
         __lowerCamelCase   :	Union[str, Any]								=		self.to_sql_kwargs.pop('index'			,     SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Optional[int]								=		self._write(index=SCREAMING_SNAKE_CASE_			,     **self.to_sql_kwargs	)
         return written







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_	)				->     Dict:
         __lowerCamelCase				,       __lowerCamelCase				,       __lowerCamelCase   :	List[Any]								=		args
         __lowerCamelCase   :	Optional[int]								=		{**to_sql_kwargs, 'if_exists': 'append'} if offset > 0 else to_sql_kwargs
         __lowerCamelCase   :	Optional[Any]								=		query_table(
             table=self.dataset.data			,     key=slice(SCREAMING_SNAKE_CASE_			,     offset + self.batch_size	)			,     indices=self.dataset._indices			,     )
         __lowerCamelCase   :	Union[str, Any]								=		batch.to_pandas()
         __lowerCamelCase   :	Union[str, Any]								=		df.to_sql(self.name			,     self.con			,     index=SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_	)
         return num_rows or len(SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self			,     SCREAMING_SNAKE_CASE_			,     **SCREAMING_SNAKE_CASE_	)				->     int:
         __lowerCamelCase   :	Optional[Any]								=		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:
              __lowerCamelCase				,       __lowerCamelCase   :	Tuple								=		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			,     SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)]			,     )			,     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 . import (
    albert,
    align,
    altclip,
    audio_spectrogram_transformer,
    auto,
    autoformer,
    bark,
    bart,
    barthez,
    bartpho,
    beit,
    bert,
    bert_generation,
    bert_japanese,
    bertweet,
    big_bird,
    bigbird_pegasus,
    biogpt,
    bit,
    blenderbot,
    blenderbot_small,
    blip,
    blip_a,
    bloom,
    bridgetower,
    byta,
    camembert,
    canine,
    chinese_clip,
    clap,
    clip,
    clipseg,
    codegen,
    conditional_detr,
    convbert,
    convnext,
    convnextva,
    cpm,
    cpmant,
    ctrl,
    cvt,
    dataavec,
    deberta,
    deberta_va,
    decision_transformer,
    deformable_detr,
    deit,
    deprecated,
    deta,
    detr,
    dialogpt,
    dinat,
    distilbert,
    dit,
    donut,
    dpr,
    dpt,
    efficientformer,
    efficientnet,
    electra,
    encodec,
    encoder_decoder,
    ernie,
    ernie_m,
    esm,
    falcon,
    flaubert,
    flava,
    fnet,
    focalnet,
    fsmt,
    funnel,
    git,
    glpn,
    gpta,
    gpt_bigcode,
    gpt_neo,
    gpt_neox,
    gpt_neox_japanese,
    gpt_swa,
    gptj,
    gptsan_japanese,
    graphormer,
    groupvit,
    herbert,
    hubert,
    ibert,
    imagegpt,
    informer,
    instructblip,
    jukebox,
    layoutlm,
    layoutlmva,
    layoutlmva,
    layoutxlm,
    led,
    levit,
    lilt,
    llama,
    longformer,
    longta,
    luke,
    lxmert,
    mam_aaa,
    marian,
    markuplm,
    maskaformer,
    maskformer,
    mbart,
    mbartaa,
    mega,
    megatron_bert,
    megatron_gpta,
    mgp_str,
    mluke,
    mobilebert,
    mobilenet_va,
    mobilenet_va,
    mobilevit,
    mobilevitva,
    mpnet,
    mra,
    mta,
    musicgen,
    mvp,
    nat,
    nezha,
    nllb,
    nllb_moe,
    nystromformer,
    oneformer,
    open_llama,
    openai,
    opt,
    owlvit,
    pegasus,
    pegasus_x,
    perceiver,
    phobert,
    pixastruct,
    plbart,
    poolformer,
    prophetnet,
    qdqbert,
    rag,
    realm,
    reformer,
    regnet,
    rembert,
    resnet,
    roberta,
    roberta_prelayernorm,
    roc_bert,
    roformer,
    rwkv,
    sam,
    segformer,
    sew,
    sew_d,
    speech_encoder_decoder,
    speech_to_text,
    speech_to_text_a,
    speechta,
    splinter,
    squeezebert,
    swiftformer,
    swin,
    swinasr,
    swinva,
    switch_transformers,
    ta,
    table_transformer,
    tapas,
    time_series_transformer,
    timesformer,
    timm_backbone,
    transfo_xl,
    trocr,
    tvlt,
    umta,
    unispeech,
    unispeech_sat,
    upernet,
    videomae,
    vilt,
    vision_encoder_decoder,
    vision_text_dual_encoder,
    visual_bert,
    vit,
    vit_hybrid,
    vit_mae,
    vit_msn,
    vivit,
    wavaveca,
    wavaveca_conformer,
    wavaveca_phoneme,
    wavaveca_with_lm,
    wavlm,
    whisper,
    x_clip,
    xglm,
    xlm,
    xlm_prophetnet,
    xlm_roberta,
    xlm_roberta_xl,
    xlnet,
    xmod,
    yolos,
    yoso,
)






