



'''simple docstring'''






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


UpperCamelCase__:       Optional[int]							     =      datasets.utils.logging.get_logger(__name__)

@dataclass
class     SCREAMING_SNAKE_CASE(							datasets.BuilderConfig      ):







	"""simple docstring"""






	lowerCamelCase__      			=	None
	lowerCamelCase__      			=	None
	lowerCamelCase__      			=	None
	lowerCamelCase__      			=	True
	lowerCamelCase__      			=	None
	lowerCamelCase__      			=	None
	lowerCamelCase__      			=	None
	lowerCamelCase__      			=	10_000
	lowerCamelCase__      			=	None







	def 					A     (						self      :					Union[str, 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 					A     (						self      :					List[str]		,      __snake_case      :					dict		,      __snake_case      :					Optional[datasets.Features] = None		,      )      ->		str:
								UpperCAmelCase			:	Union[str, Any]										=		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
								UpperCAmelCase			:	Any										=		config_kwargs['''sql''']
								if not isinstance(__snake_case		,      __snake_case		):
															if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
																						import sqlalchemy

																						if isinstance(__snake_case		,      sqlalchemy.sql.Selectable		):
																													UpperCAmelCase			:	List[str]										=		sqlalchemy.create_engine(config_kwargs['''con'''].split('''://'''		)[0] + '''://'''		)
																													UpperCAmelCase			:	Any										=		str(sql.compile(dialect=engine.dialect		)		)
																													UpperCAmelCase			:	int										=		sql_str
																						else:
																													raise TypeError(
																													    F"""Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(__snake_case		)}: {sql}"""		)
															else:
																						raise TypeError(
																						    F"""Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(__snake_case		)}: {sql}"""		)
								UpperCAmelCase			:	Optional[int]										=		config_kwargs['''con''']
								if not isinstance(__snake_case		,      __snake_case		):
															UpperCAmelCase			:	str										=		id(__snake_case		)
															logger.info(
															    F"""SQL connection 'con' of type {type(__snake_case		)} couldn't be hashed properly. To enable hashing, specify 'con' as URI string instead."""		)

								return super().create_config_id(__snake_case		,      custom_features=__snake_case		)







	@property
	def 					A     (						self      :					Tuple		)      ->		Any:
								UpperCAmelCase			:	Union[str, Any]										=		{
								    '''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     SCREAMING_SNAKE_CASE(							datasets.ArrowBasedBuilder      ):







	"""simple docstring"""






	lowerCamelCase__      			=	SqlConfig







	def 					A     (						self      :					Tuple		)      ->		int:
								return datasets.DatasetInfo(features=self.config.features		)







	def 					A     (						self      :					Dict		,      __snake_case      :					Union[str, Any]		)      ->		Dict:
								return [datasets.SplitGenerator(name=datasets.Split.TRAIN		,      gen_kwargs={}		)]







	def 					A     (						self      :					Optional[int]		,      __snake_case      :					pa.Table		)      ->		pa.Table:
								if self.config.features is not None:
															UpperCAmelCase			:	Optional[int]										=		self.config.features.arrow_schema
															if all(not require_storage_cast(__snake_case		) for feature in self.config.features.values()		):
																						# cheaper cast
																						UpperCAmelCase			:	int										=		pa.Table.from_arrays([pa_table[field.name] for field in schema]		,      schema=__snake_case		)
															else:
																						# more expensive cast; allows str <-> int/float or str to Audio for example
																						UpperCAmelCase			:	Dict										=		table_cast(__snake_case		,      __snake_case		)
								return pa_table







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










'''simple docstring'''






import importlib.metadata
import subprocess
import sys


def 			snake_case_					( _lowerCAmelCase  :						bool = False  )				->       str:
							UpperCAmelCase			:	List[str]										=		False
							if "IPython" in sys.modules:
														UpperCAmelCase			:	Optional[Any]										=		'''google.colab''' in str(sys.modules['''IPython'''].get_ipython()  )

							if in_colab:
														if upgrade:
																					UpperCAmelCase			:	List[Any]										=		['''pip''', '''install''', '''-U''', '''torch''']
																					subprocess.run(_lowerCAmelCase       ,				check=_lowerCAmelCase  )
														# get the current version of torch
														UpperCAmelCase			:	Dict										=		importlib.metadata.version('''torch'''  )
														UpperCAmelCase			:	int										=		torch_version[: torch_version.rindex('''.'''  )]
														UpperCAmelCase			:	Any										=		f"""https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl"""
														UpperCAmelCase			:	List[str]										=		['''pip''', '''install''', xla_wheel]
														subprocess.run(_lowerCAmelCase       ,				check=_lowerCAmelCase  )
							else:
														raise RuntimeError('''`install_xla` utility works only on google colab.'''  )





