'''simple docstring'''


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  _lowerCamelCase   (				lowerCamelCase_ :							Any     ,					lowerCamelCase_ :							str       ):



     """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  _lowerCamelCase   (				lowerCamelCase_ :							List[Any]     ,					lowerCamelCase_ :							Union[str, Any]     ,					lowerCamelCase_ :							int     ,					lowerCamelCase_ :							Optional[int]       ):



     """simple docstring"""





     UpperCAmelCase_							:       str       =				tmp_path / 'cache'
     UpperCAmelCase_							:       Optional[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():
          UpperCAmelCase_							:       Optional[int]       =				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  _lowerCamelCase   (				lowerCamelCase_ :							Dict     ,					lowerCamelCase_ :							List[str]     ,					lowerCamelCase_ :							List[str]     ,					lowerCamelCase_ :							Tuple       ):



     """simple docstring"""





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


def  _lowerCamelCase   (				lowerCamelCase_ :							Optional[Any]       ):



     """simple docstring"""





     with contextlib.closing(sqlitea.connect(_A       )       ) as con:
          UpperCAmelCase_							:       Any       =				con.cursor()
          cur.execute('SELECT * FROM dataset'       )
          for row in cur:
               yield row


@require_sqlalchemy
def  _lowerCamelCase   (				lowerCamelCase_ :							Tuple     ,					lowerCamelCase_ :							Optional[Any]     ,					lowerCamelCase_ :							str       ):



     """simple docstring"""





     UpperCAmelCase_							:       Tuple       =				tmp_path / 'cache'
     UpperCAmelCase_							:       Union[str, Any]       =				os.path.join(_A     ,					'tmp.sql'       )
     UpperCAmelCase_							:       Any       =				SqlDatasetReader('dataset'     ,					'sqlite:///' + sqlite_path     ,					cache_dir=_A       ).read()
     SqlDatasetWriter(_A     ,					'dataset'     ,					'sqlite:///' + output_sqlite_path     ,					num_proc=1       ).write()

     UpperCAmelCase_							:       int       =				iter_sql_file(_A       )
     UpperCAmelCase_							:       List[str]       =				iter_sql_file(_A       )

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


@require_sqlalchemy
def  _lowerCamelCase   (				lowerCamelCase_ :							List[str]     ,					lowerCamelCase_ :							List[str]     ,					lowerCamelCase_ :							int       ):



     """simple docstring"""





     UpperCAmelCase_							:       str       =				tmp_path / 'cache'
     UpperCAmelCase_							:       Any       =				os.path.join(_A     ,					'tmp.sql'       )
     UpperCAmelCase_							:       int       =				SqlDatasetReader('dataset'     ,					'sqlite:///' + sqlite_path     ,					cache_dir=_A       ).read()
     SqlDatasetWriter(_A     ,					'dataset'     ,					'sqlite:///' + output_sqlite_path     ,					num_proc=2       ).write()

     UpperCAmelCase_							:       Any       =				iter_sql_file(_A       )
     UpperCAmelCase_							:       List[Any]       =				iter_sql_file(_A       )

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





@require_sqlalchemy
def  _lowerCamelCase   (				lowerCamelCase_ :							Optional[Any]     ,					lowerCamelCase_ :							Tuple     ,					lowerCamelCase_ :							Union[str, Any]       ):



     """simple docstring"""





     UpperCAmelCase_							:       Optional[Any]       =				tmp_path / 'cache'
     UpperCAmelCase_							:       Optional[int]       =				os.path.join(_A     ,					'tmp.sql'       )
     UpperCAmelCase_							:       List[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()







'''simple docstring'''


def  _lowerCamelCase   (				lowerCamelCase_ :							int     ,					lowerCamelCase_ :							int       ):



     """simple docstring"""





     return int(input_a == input_a == 0       )


def  _lowerCamelCase   (				):



     """simple docstring"""





     print('Truth Table of NOR Gate:'       )
     print('|   Input 1   |  Input 2  | Output |'       )
     print(F'''|      0      |    0      |  {nor_gate(0     ,					0       )}     |'''       )
     print(F'''|      0      |    1      |  {nor_gate(0     ,					1       )}     |'''       )
     print(F'''|      1      |    0      |  {nor_gate(1     ,					0       )}     |'''       )
     print(F'''|      1      |    1      |  {nor_gate(1     ,					1       )}     |'''       )


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 main()






