



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 			A_      (	__a     :				str ,		__a     :				Dict		):

				"""simple docstring"""
				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 			A_      (	__a     :				int ,		__a     :				Dict ,		__a     :				Optional[int] ,		__a     :				int		):

				"""simple docstring"""
				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 			A_      (	__a     :				int ,		__a     :				Any ,		__a     :				Optional[int] ,		__a     :				List[str]		):

				"""simple docstring"""
				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 			A_      (	__a     :				List[str]		):

				"""simple docstring"""
				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 			A_      (	__a     :				List[Any] ,		__a     :				Optional[Any] ,		__a     :				Union[str, Any]		):

				"""simple docstring"""
				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 			A_      (	__a     :				str ,		__a     :				int ,		__a     :				Tuple		):

				"""simple docstring"""
				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 			A_      (	__a     :				List[Any] ,		__a     :				Dict ,		__a     :				Dict		):

				"""simple docstring"""
				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()






# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import sys

import transformers


UpperCAmelCase							 =					"""3"""

print("""Python version:""", sys.version)
print("""transformers version:""", transformers.__version__)

try:
	import torch

	print("""Torch version:""", torch.__version__)
	print("""Cuda available:""", torch.cuda.is_available())
	print("""Cuda version:""", torch.version.cuda)
	print("""CuDNN version:""", torch.backends.cudnn.version())
	print("""Number of GPUs available:""", torch.cuda.device_count())
	print("""NCCL version:""", torch.cuda.nccl.version())
except ImportError:
	print("""Torch version:""", None)

try:
	import deepspeed

	print("""DeepSpeed version:""", deepspeed.__version__)
except ImportError:
	print("""DeepSpeed version:""", None)

try:
	import tensorflow as tf

	print("""TensorFlow version:""", tf.__version__)
	print("""TF GPUs available:""", bool(tf.config.list_physical_devices("""GPU""")))
	print("""Number of TF GPUs available:""", len(tf.config.list_physical_devices("""GPU""")))
except ImportError:
	print("""TensorFlow version:""", None)

