Skip to content

Commit 6df8d2e

Browse files
Format instead of bind
1 parent 4bd184e commit 6df8d2e

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

src/snowflake/connector/pandas_tools.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,12 @@ def _do_create_temp_stage(
8787
overwrite: bool,
8888
use_scoped_temp_object: bool,
8989
) -> None:
90-
create_stage_sql = f"CREATE {get_temp_type_for_object(use_scoped_temp_object)} STAGE /* Python:snowflake.connector.pandas_tools.write_pandas() */ identifier(?) FILE_FORMAT=(TYPE=PARQUET COMPRESSION={compression}{' BINARY_AS_TEXT=FALSE' if auto_create_table or overwrite else ''})"
91-
params = (stage_location,)
92-
logger.debug(f"creating stage with '{create_stage_sql}'. params: %s", params)
90+
create_stage_sql = f"CREATE {get_temp_type_for_object(use_scoped_temp_object)} STAGE /* Python:snowflake.connector.pandas_tools.write_pandas() */ identifier('{stage_location}') FILE_FORMAT=(TYPE=PARQUET COMPRESSION={compression}{' BINARY_AS_TEXT=FALSE' if auto_create_table or overwrite else ''})"
91+
logger.debug(f"creating stage with '{create_stage_sql}'.")
9392
cursor.execute(
9493
create_stage_sql,
9594
_is_internal=True,
9695
_force_qmark_paramstyle=True,
97-
params=params,
9896
num_statements=1,
9997
)
10098

@@ -156,17 +154,15 @@ def _do_create_temp_file_format(
156154
use_scoped_temp_object: bool,
157155
) -> None:
158156
file_format_sql = (
159-
f"CREATE {get_temp_type_for_object(use_scoped_temp_object)} FILE FORMAT identifier(?) "
157+
f"CREATE {get_temp_type_for_object(use_scoped_temp_object)} FILE FORMAT identifier('{file_format_location}') "
160158
f"/* Python:snowflake.connector.pandas_tools.write_pandas() */ "
161159
f"TYPE=PARQUET COMPRESSION={compression}{sql_use_logical_type}"
162160
)
163-
params = (file_format_location,)
164-
logger.debug(f"creating file format with '{file_format_sql}'. params: %s", params)
161+
logger.debug(f"creating file format with '{file_format_sql}'.")
165162
cursor.execute(
166163
file_format_sql,
167164
_is_internal=True,
168165
_force_qmark_paramstyle=True,
169-
params=params,
170166
num_statements=1,
171167
)
172168

@@ -475,14 +471,12 @@ def write_pandas(
475471
columns = quote + f"{quote},{quote}".join(snowflake_column_names) + quote
476472

477473
def drop_object(name: str, object_type: str) -> None:
478-
drop_sql = f"DROP {object_type.upper()} IF EXISTS identifier(?) /* Python:snowflake.connector.pandas_tools.write_pandas() */"
479-
params = (name,)
480-
logger.debug(f"dropping {object_type} with '{drop_sql}'. params: %s", params)
474+
drop_sql = f"DROP {object_type.upper()} IF EXISTS identifier('{name}') /* Python:snowflake.connector.pandas_tools.write_pandas() */"
475+
logger.debug(f"dropping {object_type} with '{drop_sql}'.")
481476
cursor.execute(
482477
drop_sql,
483478
_is_internal=True,
484479
_force_qmark_paramstyle=True,
485-
params=params,
486480
num_statements=1,
487481
)
488482

@@ -531,19 +525,15 @@ def drop_object(name: str, object_type: str) -> None:
531525
)
532526

533527
create_table_sql = (
534-
f"CREATE {table_type.upper()} {iceberg}TABLE IF NOT EXISTS identifier(?) "
528+
f"CREATE {table_type.upper()} {iceberg}TABLE IF NOT EXISTS identifier('{target_table_location}') "
535529
f"({create_table_columns}) {iceberg_config_statement}"
536530
f" /* Python:snowflake.connector.pandas_tools.write_pandas() */ "
537531
)
538-
params = (target_table_location,)
539-
logger.debug(
540-
f"auto creating table with '{create_table_sql}'. params: %s", params
541-
)
532+
logger.debug(f"auto creating table with '{create_table_sql}'.")
542533
cursor.execute(
543534
create_table_sql,
544535
_is_internal=True,
545536
_force_qmark_paramstyle=True,
546-
params=params,
547537
num_statements=1,
548538
)
549539
# need explicit casting when the underlying table schema is inferred
@@ -564,14 +554,12 @@ def drop_object(name: str, object_type: str) -> None:
564554

565555
try:
566556
if overwrite and (not auto_create_table):
567-
truncate_sql = "TRUNCATE TABLE identifier(?) /* Python:snowflake.connector.pandas_tools.write_pandas() */"
568-
params = (target_table_location,)
569-
logger.debug(f"truncating table with '{truncate_sql}'. params: %s", params)
557+
truncate_sql = f"TRUNCATE TABLE identifier('{target_table_location}') /* Python:snowflake.connector.pandas_tools.write_pandas() */"
558+
logger.debug(f"truncating table with '{truncate_sql}'")
570559
cursor.execute(
571560
truncate_sql,
572561
_is_internal=True,
573562
_force_qmark_paramstyle=True,
574-
params=params,
575563
num_statements=1,
576564
)
577565

@@ -605,14 +593,12 @@ def drop_object(name: str, object_type: str) -> None:
605593
quote_identifiers=quote_identifiers,
606594
)
607595
drop_object(original_table_location, "table")
608-
rename_table_sql = "ALTER TABLE identifier(?) RENAME TO identifier(?) /* Python:snowflake.connector.pandas_tools.write_pandas() */"
609-
params = (target_table_location, original_table_location)
610-
logger.debug(f"rename table with '{rename_table_sql}'. params: %s", params)
596+
rename_table_sql = f"ALTER TABLE identifier('{target_table_location}') RENAME TO identifier('{original_table_location}') /* Python:snowflake.connector.pandas_tools.write_pandas() */"
597+
logger.debug(f"rename table with '{rename_table_sql}'.")
611598
cursor.execute(
612599
rename_table_sql,
613600
_is_internal=True,
614601
_force_qmark_paramstyle=True,
615-
params=params,
616602
num_statements=1,
617603
)
618604
except ProgrammingError:

test/integ/pandas/test_pandas_tools.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ def test_table_location_building(
535535

536536
def mocked_execute(*args, **kwargs):
537537
if len(args) >= 1 and args[0].startswith("COPY INTO"):
538-
assert kwargs["params"][0] == expected_location
538+
assert expected_location in args[0]
539539
cur = SnowflakeCursor(cnx)
540540
cur._result = iter([])
541541
return cur
@@ -566,6 +566,8 @@ def mocked_execute(*args, **kwargs):
566566
(None, "schema", False, "schema"),
567567
(None, None, True, ""),
568568
(None, None, False, ""),
569+
("data'base", "schema", True, '"data\\\'base"."schema"'),
570+
("data'base", "schema", False, '"data\\\'base".schema'),
569571
],
570572
)
571573
def test_stage_location_building(
@@ -581,9 +583,11 @@ def test_stage_location_building(
581583
with conn_cnx() as cnx:
582584

583585
def mocked_execute(*args, **kwargs):
584-
if len(args) >= 1 and args[0].startswith("create temporary stage"):
585-
db_schema = ".".join(args[0].split(" ")[-1].split(".")[:-1])
586-
assert db_schema == expected_db_schema
586+
if len(args) >= 1 and args[0].lower().startswith("create temp stage"):
587+
location_identifier = re.search(
588+
r"identifier\(\'(.*?)\)", args[0]
589+
).group(1)
590+
assert location_identifier.startswith(expected_db_schema)
587591
cur = SnowflakeCursor(cnx)
588592
cur._result = iter([])
589593
return cur
@@ -998,7 +1002,7 @@ def test_no_create_internal_object_privilege_in_target_schema(
9981002
def mock_execute(*args, **kwargs):
9991003
if (
10001004
f"CREATE TEMP {object_type}" in args[0]
1001-
and "target_schema_no_create_" in kwargs["params"][0]
1005+
and "target_schema_no_create_" in args[0]
10021006
):
10031007
raise ProgrammingError("Cannot create temp object in target schema")
10041008
cursor = cnx.cursor()

0 commit comments

Comments
 (0)