



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__							,       a__       )    ->			Union[str, Any]:
							assert isinstance(__SCREAMING_SNAKE_CASE							,       __SCREAMING_SNAKE_CASE       )
							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__							,       a__							,       a__							,       a__       )    ->			Any:
							__SCREAMING_SNAKE_CASE									= tmp_path / "cache"
							__SCREAMING_SNAKE_CASE									= {"col_1": "string", "col_2": "int64", "col_3": "float64"}
							with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
														__SCREAMING_SNAKE_CASE									= SqlDatasetReader(
														    'dataset'							,       'sqlite:///' + sqlite_path							,       cache_dir=__SCREAMING_SNAKE_CASE							,       keep_in_memory=__SCREAMING_SNAKE_CASE       ).read()
							_check_sql_dataset(__SCREAMING_SNAKE_CASE							,       __SCREAMING_SNAKE_CASE       )






@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__							,       a__							,       a__							,       a__       )    ->			Dict:
							__SCREAMING_SNAKE_CASE									= tmp_path / "cache"
							__SCREAMING_SNAKE_CASE									= {"col_1": "string", "col_2": "int64", "col_3": "float64"}
							__SCREAMING_SNAKE_CASE									= features.copy() if features else default_expected_features
							__SCREAMING_SNAKE_CASE									= (
							    Features({feature: Value(__SCREAMING_SNAKE_CASE       ) for feature, dtype in features.items()}       ) if features is not None else None
							)
							__SCREAMING_SNAKE_CASE									= SqlDatasetReader('dataset'							,       'sqlite:///' + sqlite_path							,       features=__SCREAMING_SNAKE_CASE							,       cache_dir=__SCREAMING_SNAKE_CASE       ).read()
							_check_sql_dataset(__SCREAMING_SNAKE_CASE							,       __SCREAMING_SNAKE_CASE       )






def      __lowercase       (			a__       )    ->			Optional[int]:
							with contextlib.closing(sqlitea.connect(__SCREAMING_SNAKE_CASE       )       ) as con:
														__SCREAMING_SNAKE_CASE									= con.cursor()
														cur.execute('SELECT * FROM dataset'       )
														for row in cur:
																					yield row






@require_sqlalchemy
def      __lowercase       (			a__							,       a__							,       a__       )    ->			List[str]:
							__SCREAMING_SNAKE_CASE									= tmp_path / "cache"
							__SCREAMING_SNAKE_CASE									= os.path.join(__SCREAMING_SNAKE_CASE							,       'tmp.sql'       )
							__SCREAMING_SNAKE_CASE									= SqlDatasetReader('dataset'							,       'sqlite:///' + sqlite_path							,       cache_dir=__SCREAMING_SNAKE_CASE       ).read()
							SqlDatasetWriter(__SCREAMING_SNAKE_CASE							,       'dataset'							,       'sqlite:///' + output_sqlite_path							,       num_proc=1       ).write()

							__SCREAMING_SNAKE_CASE									= iter_sql_file(__SCREAMING_SNAKE_CASE       )
							__SCREAMING_SNAKE_CASE									= iter_sql_file(__SCREAMING_SNAKE_CASE       )

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






@require_sqlalchemy
def      __lowercase       (			a__							,       a__							,       a__       )    ->			Dict:
							__SCREAMING_SNAKE_CASE									= tmp_path / "cache"
							__SCREAMING_SNAKE_CASE									= os.path.join(__SCREAMING_SNAKE_CASE							,       'tmp.sql'       )
							__SCREAMING_SNAKE_CASE									= SqlDatasetReader('dataset'							,       'sqlite:///' + sqlite_path							,       cache_dir=__SCREAMING_SNAKE_CASE       ).read()
							SqlDatasetWriter(__SCREAMING_SNAKE_CASE							,       'dataset'							,       'sqlite:///' + output_sqlite_path							,       num_proc=2       ).write()

							__SCREAMING_SNAKE_CASE									= iter_sql_file(__SCREAMING_SNAKE_CASE       )
							__SCREAMING_SNAKE_CASE									= iter_sql_file(__SCREAMING_SNAKE_CASE       )

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






@require_sqlalchemy
def      __lowercase       (			a__							,       a__							,       a__       )    ->			str:
							__SCREAMING_SNAKE_CASE									= tmp_path / "cache"
							__SCREAMING_SNAKE_CASE									= os.path.join(__SCREAMING_SNAKE_CASE							,       'tmp.sql'       )
							__SCREAMING_SNAKE_CASE									= SqlDatasetReader('dataset'							,       'sqlite:///' + sqlite_path							,       cache_dir=__SCREAMING_SNAKE_CASE       ).read()
							with pytest.raises(__SCREAMING_SNAKE_CASE       ):
														SqlDatasetWriter(__SCREAMING_SNAKE_CASE							,       'dataset'							,       'sqlite:///' + output_sqlite_path							,       num_proc=0       ).write()











import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows






def      __lowercase       (			a__       )    ->			List[Any]:  # picklable for multiprocessing
							return i + 1






@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def      __lowercase       (			)    ->			Any:
							with parallel_backend('spark'       ):
														assert ParallelBackendConfig.backend_name == "spark"

							__SCREAMING_SNAKE_CASE									= [1, 2, 3]
							with pytest.raises(a__       ):
														with parallel_backend('unsupported backend'       ):
																					map_nested(a__							,       a__							,       num_proc=2       )

							with pytest.raises(a__       ):
														with parallel_backend('unsupported backend'       ):
																					map_nested(a__							,       a__							,       num_proc=-1       )






@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize('num_proc'							,       [2, -1]       )
def      __lowercase       (			a__       )    ->			Union[str, Any]:
							__SCREAMING_SNAKE_CASE									= [1, 2]
							__SCREAMING_SNAKE_CASE									= {'a': 1, 'b': 2}
							__SCREAMING_SNAKE_CASE									= {'a': [1, 2], 'b': [3, 4]}
							__SCREAMING_SNAKE_CASE									= {'a': {'1': 1}, 'b': 2}
							__SCREAMING_SNAKE_CASE									= {'a': 1, 'b': 2, 'c': 3, 'd': 4}
							__SCREAMING_SNAKE_CASE									= [2, 3]
							__SCREAMING_SNAKE_CASE									= {'a': 2, 'b': 3}
							__SCREAMING_SNAKE_CASE									= {'a': [2, 3], 'b': [4, 5]}
							__SCREAMING_SNAKE_CASE									= {'a': {'1': 2}, 'b': 3}
							__SCREAMING_SNAKE_CASE									= {'a': 2, 'b': 3, 'c': 4, 'd': 5}

							with parallel_backend('spark'       ):
														assert map_nested(a__							,       a__							,       num_proc=a__       ) == expected_map_nested_sa
														assert map_nested(a__							,       a__							,       num_proc=a__       ) == expected_map_nested_sa
														assert map_nested(a__							,       a__							,       num_proc=a__       ) == expected_map_nested_sa
														assert map_nested(a__							,       a__							,       num_proc=a__       ) == expected_map_nested_sa
														assert map_nested(a__							,       a__							,       num_proc=a__       ) == expected_map_nested_sa






