@@ -187,17 +187,22 @@ def _records2df(
187187 records : List [Tuple [Any ]],
188188 cols_names : List [str ],
189189 index : Optional [Union [str , List [str ]]],
190- dtype : Optional [Dict [str , pa .DataType ]] = None ,
190+ safe : bool ,
191+ dtype : Optional [Dict [str , pa .DataType ]],
191192) -> pd .DataFrame :
192193 arrays : List [pa .Array ] = []
193194 for col_values , col_name in zip (tuple (zip (* records )), cols_names ): # Transposing
194195 if (dtype is None ) or (col_name not in dtype ):
195196 try :
196- array : pa .Array = pa .array (obj = col_values , safe = True ) # Creating Arrow array
197+ array : pa .Array = pa .array (obj = col_values , safe = safe ) # Creating Arrow array
197198 except pa .ArrowInvalid as ex :
198199 array = _data_types .process_not_inferred_array (ex , values = col_values ) # Creating Arrow array
199200 else :
200- array = pa .array (obj = col_values , type = dtype [col_name ], safe = True ) # Creating Arrow array with dtype
201+ try :
202+ array = pa .array (obj = col_values , type = dtype [col_name ], safe = safe ) # Creating Arrow array with dtype
203+ except pa .ArrowInvalid :
204+ array = pa .array (obj = col_values , safe = safe ) # Creating Arrow array
205+ array = array .cast (target_type = dtype [col_name ], safe = safe ) # Casting
201206 arrays .append (array )
202207 table = pa .Table .from_arrays (arrays = arrays , names = cols_names ) # Creating arrow Table
203208 df : pd .DataFrame = table .to_pandas ( # Creating Pandas DataFrame
@@ -207,6 +212,7 @@ def _records2df(
207212 integer_object_nulls = False ,
208213 date_as_object = True ,
209214 types_mapper = _data_types .pyarrow2pandas_extension ,
215+ safe = safe ,
210216 )
211217 if index is not None :
212218 df .set_index (index , inplace = True )
@@ -218,13 +224,14 @@ def _iterate_cursor(
218224 chunksize : int ,
219225 cols_names : List [str ],
220226 index : Optional [Union [str , List [str ]]],
221- dtype : Optional [Dict [str , pa .DataType ]] = None ,
227+ safe : bool ,
228+ dtype : Optional [Dict [str , pa .DataType ]],
222229) -> Iterator [pd .DataFrame ]:
223230 while True :
224231 records = cursor .fetchmany (chunksize )
225232 if not records :
226233 break
227- yield _records2df (records = records , cols_names = cols_names , index = index , dtype = dtype )
234+ yield _records2df (records = records , cols_names = cols_names , index = index , safe = safe , dtype = dtype )
228235
229236
230237def _convert_params (sql : str , params : Optional [Union [List [Any ], Tuple [Any , ...], Dict [Any , Any ]]]) -> List [Any ]:
@@ -366,6 +373,7 @@ def read_sql_query(
366373 params : Optional [Union [List [Any ], Tuple [Any , ...], Dict [Any , Any ]]] = None ,
367374 chunksize : Optional [int ] = None ,
368375 dtype : Optional [Dict [str , pa .DataType ]] = None ,
376+ safe : bool = True ,
369377) -> Union [pd .DataFrame , Iterator [pd .DataFrame ]]:
370378 """Return a DataFrame corresponding to the result set of the query string.
371379
@@ -395,6 +403,8 @@ def read_sql_query(
395403 dtype : Dict[str, pyarrow.DataType], optional
396404 Specifying the datatype for columns.
397405 The keys should be the column names and the values should be the PyArrow types.
406+ safe : bool
407+ Check for overflows or other unsafe data type conversions.
398408
399409 Returns
400410 -------
@@ -425,9 +435,11 @@ def read_sql_query(
425435 args = _convert_params (sql , params )
426436 cursor = _con .execute (* args )
427437 if chunksize is None :
428- return _records2df (records = cursor .fetchall (), cols_names = cursor .keys (), index = index_col , dtype = dtype )
438+ return _records2df (
439+ records = cursor .fetchall (), cols_names = cursor .keys (), index = index_col , dtype = dtype , safe = safe
440+ )
429441 return _iterate_cursor (
430- cursor = cursor , chunksize = chunksize , cols_names = cursor .keys (), index = index_col , dtype = dtype
442+ cursor = cursor , chunksize = chunksize , cols_names = cursor .keys (), index = index_col , dtype = dtype , safe = safe
431443 )
432444
433445
@@ -439,6 +451,7 @@ def read_sql_table(
439451 params : Optional [Union [List [Any ], Tuple [Any , ...], Dict [Any , Any ]]] = None ,
440452 chunksize : Optional [int ] = None ,
441453 dtype : Optional [Dict [str , pa .DataType ]] = None ,
454+ safe : bool = True ,
442455) -> Union [pd .DataFrame , Iterator [pd .DataFrame ]]:
443456 """Return a DataFrame corresponding to the result set of the query string.
444457
@@ -471,6 +484,8 @@ def read_sql_table(
471484 dtype : Dict[str, pyarrow.DataType], optional
472485 Specifying the datatype for columns.
473486 The keys should be the column names and the values should be the PyArrow types.
487+ safe : bool
488+ Check for overflows or other unsafe data type conversions.
474489
475490 Returns
476491 -------
@@ -502,7 +517,9 @@ def read_sql_table(
502517 sql : str = f"SELECT * FROM { table } "
503518 else :
504519 sql = f"SELECT * FROM { schema } .{ table } "
505- return read_sql_query (sql = sql , con = con , index_col = index_col , params = params , chunksize = chunksize , dtype = dtype )
520+ return read_sql_query (
521+ sql = sql , con = con , index_col = index_col , params = params , chunksize = chunksize , dtype = dtype , safe = safe
522+ )
506523
507524
508525def get_redshift_temp_engine (
0 commit comments