@@ -131,6 +131,7 @@ def _copy(
131131 schema : Optional [str ] = None ,
132132 manifest : Optional [bool ] = False ,
133133 sql_copy_extra_params : Optional [List [str ]] = None ,
134+ column_names : Optional [List [str ]] = None ,
134135) -> None :
135136 if schema is None :
136137 table_name : str = f'"{ table } "'
@@ -145,7 +146,9 @@ def _copy(
145146 boto3_session = boto3_session ,
146147 )
147148 ser_json_str : str = " SERIALIZETOJSON" if serialize_to_json else ""
148- sql : str = f"COPY { table_name } \n FROM '{ path } ' { auth_str } \n FORMAT AS PARQUET{ ser_json_str } "
149+ column_names_str : str = f"({ ',' .join (column_names )} )" if column_names else ""
150+ sql = f"COPY { table_name } { column_names_str } \n FROM '{ path } ' { auth_str } \n FORMAT AS PARQUET{ ser_json_str } "
151+
149152 if manifest :
150153 sql += "\n MANIFEST"
151154 if sql_copy_extra_params :
@@ -1250,6 +1253,7 @@ def copy_from_files( # pylint: disable=too-many-locals,too-many-arguments
12501253 boto3_session : Optional [boto3 .Session ] = None ,
12511254 s3_additional_kwargs : Optional [Dict [str , str ]] = None ,
12521255 precombine_key : Optional [str ] = None ,
1256+ column_names : Optional [List [str ]] = None ,
12531257) -> None :
12541258 """Load Parquet files from S3 to a Table on Amazon Redshift (Through COPY command).
12551259
@@ -1352,6 +1356,8 @@ def copy_from_files( # pylint: disable=too-many-locals,too-many-arguments
13521356 When there is a primary_key match during upsert, this column will change the upsert method,
13531357 comparing the values of the specified column from source and target, and keeping the
13541358 larger of the two. Will only work when mode = upsert.
1359+ column_names: List[str], optional
1360+ List of column names to map source data fields to the target columns.
13551361
13561362 Returns
13571363 -------
@@ -1416,6 +1422,7 @@ def copy_from_files( # pylint: disable=too-many-locals,too-many-arguments
14161422 serialize_to_json = serialize_to_json ,
14171423 sql_copy_extra_params = sql_copy_extra_params ,
14181424 manifest = manifest ,
1425+ column_names = column_names ,
14191426 )
14201427 if table != created_table : # upsert
14211428 _upsert (
@@ -1425,6 +1432,7 @@ def copy_from_files( # pylint: disable=too-many-locals,too-many-arguments
14251432 temp_table = created_table ,
14261433 primary_keys = primary_keys ,
14271434 precombine_key = precombine_key ,
1435+ column_names = column_names ,
14281436 )
14291437 if commit_transaction :
14301438 con .commit ()
@@ -1436,7 +1444,7 @@ def copy_from_files( # pylint: disable=too-many-locals,too-many-arguments
14361444 con .autocommit = autocommit_temp
14371445
14381446
1439- def copy ( # pylint: disable=too-many-arguments
1447+ def copy ( # pylint: disable=too-many-arguments,too-many-locals
14401448 df : pd .DataFrame ,
14411449 path : str ,
14421450 con : redshift_connector .Connection ,
@@ -1466,6 +1474,7 @@ def copy( # pylint: disable=too-many-arguments
14661474 s3_additional_kwargs : Optional [Dict [str , str ]] = None ,
14671475 max_rows_by_file : Optional [int ] = 10_000_000 ,
14681476 precombine_key : Optional [str ] = None ,
1477+ use_column_names : bool = False ,
14691478) -> None :
14701479 """Load Pandas DataFrame as a Table on Amazon Redshift using parquet files on S3 as stage.
14711480
@@ -1568,6 +1577,10 @@ def copy( # pylint: disable=too-many-arguments
15681577 When there is a primary_key match during upsert, this column will change the upsert method,
15691578 comparing the values of the specified column from source and target, and keeping the
15701579 larger of the two. Will only work when mode = upsert.
1580+ use_column_names: bool
1581+ If set to True, will use the column names of the DataFrame for generating the INSERT SQL Query.
1582+ E.g. If the DataFrame has two columns `col1` and `col3` and `use_column_names` is True, data will only be
1583+ inserted into the database columns `col1` and `col3`.
15711584
15721585 Returns
15731586 -------
@@ -1592,6 +1605,7 @@ def copy( # pylint: disable=too-many-arguments
15921605 """
15931606 path = path [:- 1 ] if path .endswith ("*" ) else path
15941607 path = path if path .endswith ("/" ) else f"{ path } /"
1608+ column_names = [f'"{ column } "' for column in df .columns ] if use_column_names else []
15951609 session : boto3 .Session = _utils .ensure_session (session = boto3_session )
15961610 if s3 .list_objects (path = path , boto3_session = session , s3_additional_kwargs = s3_additional_kwargs ):
15971611 raise exceptions .InvalidArgument (
@@ -1636,6 +1650,7 @@ def copy( # pylint: disable=too-many-arguments
16361650 s3_additional_kwargs = s3_additional_kwargs ,
16371651 sql_copy_extra_params = sql_copy_extra_params ,
16381652 precombine_key = precombine_key ,
1653+ column_names = column_names ,
16391654 )
16401655 finally :
16411656 if keep_files is False :
0 commit comments