


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






@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       (			__lowerCAmelCase							:    Union[str, Any] ,   __lowerCAmelCase							:    Optional[int] ,   __lowerCAmelCase							:    int ,   __lowerCAmelCase							:    Tuple       ):
							a__					=					tmp_path / 'cache'
							a__					=					{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
							a__					=					features.copy() if features else default_expected_features
							a__					=					(
							    Features({feature: Value(__lowerCAmelCase       ) for feature, dtype in features.items()}       ) if features is not None else None
							)
							a__					=					SqlDatasetReader('dataset' ,   'sqlite:///' + sqlite_path ,   features=__lowerCAmelCase ,   cache_dir=__lowerCAmelCase       ).read()
							_check_sql_dataset(__lowerCAmelCase ,   __lowerCAmelCase       )






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






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

							a__					=					iter_sql_file(__lowerCAmelCase       )
							a__					=					iter_sql_file(__lowerCAmelCase       )

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






@require_sqlalchemy
def 							__lowercase       (			__lowerCAmelCase							:    List[Any] ,   __lowerCAmelCase							:    int ,   __lowerCAmelCase							:    Tuple       ):
							a__					=					tmp_path / 'cache'
							a__					=					os.path.join(__lowerCAmelCase ,   'tmp.sql'       )
							a__					=					SqlDatasetReader('dataset' ,   'sqlite:///' + sqlite_path ,   cache_dir=__lowerCAmelCase       ).read()
							SqlDatasetWriter(__lowerCAmelCase ,   'dataset' ,   'sqlite:///' + output_sqlite_path ,   num_proc=2       ).write()

							a__					=					iter_sql_file(__lowerCAmelCase       )
							a__					=					iter_sql_file(__lowerCAmelCase       )

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






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








from math import ceil, sqrt






def 							__lowercase       (			__lowerCAmelCase							:    int = 1_0_0_0_0_0_0       ):
							a__					=					0

							for outer_width in range(3 ,   (limit // 4) + 2       ):
														if outer_width**2 > limit:
																					a__					=					max(ceil(sqrt(outer_width**2 - limit       )       ) ,   1       )
														else:
																					a__					=					1
														if (outer_width - hole_width_lower_bound) % 2:
																					hole_width_lower_bound += 1

														answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1

							return answer


if __name__ == "__main__":
						print(f"""{solution() = }""")




