




import contextlib
import os
import sqlitea

import pytest

from datasets import Dataset, Features, Value
from datasets.io.sql import SqlDatasetReader, SqlDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases, require_sqlalchemy







def 		lowercase__   (		__A:	Union[str, Any]							,__A:	Union[str, Any]				):





					'''simple docstring'''






					assert isinstance(__A							,__A				)
					assert dataset.num_rows == 4
					assert dataset.num_columns == 3
					assert dataset.column_names == ["col_1", "col_2", "col_3"]
					for feature, expected_dtype in expected_features.items():
										assert dataset.features[feature].dtype == expected_dtype







@require_sqlalchemy
@pytest.mark.parametrize('''keep_in_memory'''							,[False, True]				)
def 		lowercase__   (		__A:	Union[str, Any]							,__A:	Optional[Any]							,__A:	Any							,__A:	str				):





					'''simple docstring'''






					__magic_name__							:	Tuple    							=							tmp_path / '''cache'''
					__magic_name__							:	Any    							=							{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										__magic_name__							:	Union[str, Any]    							=							SqlDatasetReader(
										    '''dataset'''							,'''sqlite:///''' + sqlite_path							,cache_dir=__A							,keep_in_memory=__A				).read()
					_check_sql_dataset(__A							,__A				)







@require_sqlalchemy
@pytest.mark.parametrize(
    '''features'''							,[
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ]							,)
def 		lowercase__   (		__A:	List[Any]							,__A:	Optional[int]							,__A:	Optional[int]							,__A:	str				):





					'''simple docstring'''






					__magic_name__							:	Dict    							=							tmp_path / '''cache'''
					__magic_name__							:	Tuple    							=							{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
					__magic_name__							:	Optional[Any]    							=							features.copy() if features else default_expected_features
					__magic_name__							:	Dict    							=							(
					    Features({feature: Value(__A				) for feature, dtype in features.items()}				) if features is not None else None
					)
					__magic_name__							:	List[Any]    							=							SqlDatasetReader('''dataset'''							,'''sqlite:///''' + sqlite_path							,features=__A							,cache_dir=__A				).read()
					_check_sql_dataset(__A							,__A				)







def 		lowercase__   (		__A:	List[Any]				):





					'''simple docstring'''






					with contextlib.closing(sqlitea.connect(__A				)				) as con:
										__magic_name__							:	List[str]    							=							con.cursor()
										cur.execute('''SELECT * FROM dataset'''				)
										for row in cur:
															yield row







@require_sqlalchemy
def 		lowercase__   (		__A:	List[str]							,__A:	Optional[int]							,__A:	Optional[int]				):





					'''simple docstring'''






					__magic_name__							:	Dict    							=							tmp_path / '''cache'''
					__magic_name__							:	int    							=							os.path.join(__A							,'''tmp.sql'''				)
					__magic_name__							:	Dict    							=							SqlDatasetReader('''dataset'''							,'''sqlite:///''' + sqlite_path							,cache_dir=__A				).read()
					SqlDatasetWriter(__A							,'''dataset'''							,'''sqlite:///''' + output_sqlite_path							,num_proc=1				).write()

					__magic_name__							:	Any    							=							iter_sql_file(__A				)
					__magic_name__							:	Tuple    							=							iter_sql_file(__A				)

					for rowa, rowa in zip(__A							,__A				):
										assert rowa == rowa







@require_sqlalchemy
def 		lowercase__   (		__A:	Any							,__A:	Union[str, Any]							,__A:	Dict				):





					'''simple docstring'''






					__magic_name__							:	List[Any]    							=							tmp_path / '''cache'''
					__magic_name__							:	str    							=							os.path.join(__A							,'''tmp.sql'''				)
					__magic_name__							:	Optional[int]    							=							SqlDatasetReader('''dataset'''							,'''sqlite:///''' + sqlite_path							,cache_dir=__A				).read()
					SqlDatasetWriter(__A							,'''dataset'''							,'''sqlite:///''' + output_sqlite_path							,num_proc=2				).write()

					__magic_name__							:	str    							=							iter_sql_file(__A				)
					__magic_name__							:	int    							=							iter_sql_file(__A				)

					for rowa, rowa in zip(__A							,__A				):
										assert rowa == rowa







@require_sqlalchemy
def 		lowercase__   (		__A:	int							,__A:	str							,__A:	List[Any]				):





					'''simple docstring'''






					__magic_name__							:	List[str]    							=							tmp_path / '''cache'''
					__magic_name__							:	Any    							=							os.path.join(__A							,'''tmp.sql'''				)
					__magic_name__							:	Optional[Any]    							=							SqlDatasetReader('''dataset'''							,'''sqlite:///''' + sqlite_path							,cache_dir=__A				).read()
					with pytest.raises(__A				):
										SqlDatasetWriter(__A							,'''dataset'''							,'''sqlite:///''' + output_sqlite_path							,num_proc=0				).write()









from typing import TYPE_CHECKING

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


__lowerCamelCase   :						List[Any]				=			{
    '''configuration_speech_to_text''': ['''SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Speech2TextConfig'''],
    '''processing_speech_to_text''': ['''Speech2TextProcessor'''],
}

try:
	if not is_sentencepiece_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowerCamelCase   :						Any				=			['''Speech2TextTokenizer''']

try:
	if not is_speech_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowerCamelCase   :						Optional[Any]				=			['''Speech2TextFeatureExtractor''']

try:
	if not is_tf_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowerCamelCase   :						Any				=			[
	    '''TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''',
	    '''TFSpeech2TextForConditionalGeneration''',
	    '''TFSpeech2TextModel''',
	    '''TFSpeech2TextPreTrainedModel''',
	]

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	__lowerCamelCase   :						Any				=			[
	    '''SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST''',
	    '''Speech2TextForConditionalGeneration''',
	    '''Speech2TextModel''',
	    '''Speech2TextPreTrainedModel''',
	]


if TYPE_CHECKING:
	from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig
	from .processing_speech_to_text import SpeechaTextProcessor

	try:
		if not is_sentencepiece_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .tokenization_speech_to_text import SpeechaTextTokenizer

	try:
		if not is_speech_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor

	try:
		if not is_tf_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_tf_speech_to_text import (
		    TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFSpeechaTextForConditionalGeneration,
		    TFSpeechaTextModel,
		    TFSpeechaTextPreTrainedModel,
		)

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_speech_to_text import (
		    SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    SpeechaTextForConditionalGeneration,
		    SpeechaTextModel,
		    SpeechaTextPreTrainedModel,
		)

else:
	import sys

	__lowerCamelCase   :						List[str]				=			_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



