





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__    ):


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



									"""simple docstring"""



									super().__init__(features=__A							,				cache_dir=__A							,				keep_in_memory=__A							,				**__A		)
									SCREAMING_SNAKE_CASE__			=      Sql(
									    cache_dir=__A							,				features=__A							,				sql=__A							,				con=__A							,				**__A							,				)


							def      _snake_case  (							self    :Tuple		)   ->   List[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      None
									SCREAMING_SNAKE_CASE__			=      None
									SCREAMING_SNAKE_CASE__			=      None
									SCREAMING_SNAKE_CASE__			=      None

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

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




class   UpperCamelCase_       :


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



									"""simple docstring"""



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

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


							def      _snake_case  (							self    :Tuple		)   ->   int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      self.to_sql_kwargs.pop("""sql"""							,				__A		)
									SCREAMING_SNAKE_CASE__			=      self.to_sql_kwargs.pop("""con"""							,				__A		)
									SCREAMING_SNAKE_CASE__			=      self.to_sql_kwargs.pop("""index"""							,				__A		)

									SCREAMING_SNAKE_CASE__			=      self._write(index=__A							,				**self.to_sql_kwargs		)
									return written


							def      _snake_case  (							self    :Dict							,				__A    :Tuple		)   ->   Tuple:



									"""simple docstring"""



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


							def      _snake_case  (							self    :List[str]							,				__A    :Optional[int]							,				**__A    :Dict		)   ->   int:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__			=      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:
											SCREAMING_SNAKE_CASE__	,						SCREAMING_SNAKE_CASE__			=      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							,				__A							,				__A		)]							,				)							,				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






# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowerCamelCase   				=			{
    'configuration_xmod': [
        'XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'XmodConfig',
        'XmodOnnxConfig',
    ],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_lowerCamelCase   				=			[
	    'XMOD_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'XmodForCausalLM',
	    'XmodForMaskedLM',
	    'XmodForMultipleChoice',
	    'XmodForQuestionAnswering',
	    'XmodForSequenceClassification',
	    'XmodForTokenClassification',
	    'XmodModel',
	    'XmodPreTrainedModel',
	]

if TYPE_CHECKING:
	from .configuration_xmod import XMOD_PRETRAINED_CONFIG_ARCHIVE_MAP, XmodConfig, XmodOnnxConfig

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_xmod import (
		    XMOD_PRETRAINED_MODEL_ARCHIVE_LIST,
		    XmodForCausalLM,
		    XmodForMaskedLM,
		    XmodForMultipleChoice,
		    XmodForQuestionAnswering,
		    XmodForSequenceClassification,
		    XmodForTokenClassification,
		    XmodModel,
		    XmodPreTrainedModel,
		)

else:
	import sys

	_lowerCamelCase   				=			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)