Skip to content

Commit 0afbf90

Browse files
Exposing write_pandas keyword arguments in pd_writer to provide better feature parity (#1258)
1 parent 28caf1f commit 0afbf90

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/snowflake/connector/pandas_tools.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,14 @@ def write_pandas(
292292

293293

294294
def make_pd_writer(
295-
quote_identifiers: bool = True,
295+
**kwargs,
296296
) -> Callable[
297297
[
298298
pandas.io.sql.SQLTable,
299299
sqlalchemy.engine.Engine | sqlalchemy.engine.Connection,
300300
Iterable,
301301
Iterable,
302+
Any,
302303
],
303304
None,
304305
]:
@@ -311,24 +312,28 @@ def make_pd_writer(
311312
sf_connector_version_df = pd.DataFrame([('snowflake-connector-python', '1.0')], columns=['NAME', 'NEWEST_VERSION'])
312313
sf_connector_version_df.to_sql('driver_versions', engine, index=False, method=make_pd_writer())
313314
314-
# to use quote_identifiers=False,
315+
# to use parallel=1, quote_identifiers=False,
315316
from functools import partial
316317
sf_connector_version_df.to_sql(
317-
'driver_versions', engine, index=False, method=make_pd_writer(quote_identifiers=False)))
318+
'driver_versions', engine, index=False, method=make_pd_writer(parallel=1, quote_identifiers=False)))
318319
319-
Args:
320-
quote_identifiers: if True (default), the pd_writer will pass quote identifiers to Snowflake.
321-
If False, the created pd_writer will not quote identifiers (and typically coerced to uppercase by Snowflake)
320+
This function takes arguments used by 'pd_writer' (excluding 'table', 'conn', 'keys', and 'data_iter')
321+
Please refer to 'pd_writer' for documentation.
322322
"""
323-
return partial(pd_writer, quote_identifiers=quote_identifiers)
323+
if any(arg in kwargs for arg in ("table", "conn", "keys", "data_iter")):
324+
raise ProgrammingError(
325+
"Arguments 'table', 'conn', 'keys', and 'data_iter' are not supported parameters for make_pd_writer."
326+
)
327+
328+
return partial(pd_writer, **kwargs)
324329

325330

326331
def pd_writer(
327332
table: pandas.io.sql.SQLTable,
328333
conn: sqlalchemy.engine.Engine | sqlalchemy.engine.Connection,
329334
keys: Iterable,
330335
data_iter: Iterable,
331-
quote_identifiers: bool = True,
336+
**kwargs,
332337
) -> None:
333338
"""This is a wrapper on top of write_pandas to make it compatible with to_sql method in pandas.
334339
@@ -339,16 +344,20 @@ def pd_writer(
339344
sf_connector_version_df = pd.DataFrame([('snowflake-connector-python', '1.0')], columns=['NAME', 'NEWEST_VERSION'])
340345
sf_connector_version_df.to_sql('driver_versions', engine, index=False, method=pd_writer)
341346
342-
# to use quote_identifiers=False, see `make_pd_writer`
343-
344347
Args:
345348
table: Pandas package's table object.
346349
conn: SQLAlchemy engine object to talk to Snowflake.
347350
keys: Column names that we are trying to insert.
348351
data_iter: Iterator over the rows.
349-
quote_identifiers: if True (default), quote identifiers passed to Snowflake. If False, identifiers are not
350-
quoted (and typically coerced to uppercase by Snowflake)
352+
353+
More parameters can be provided to be used by 'write_pandas' (excluding 'conn', 'df', 'table_name', and 'schema'),
354+
Please refer to 'write_pandas' for documentation on other available parameters.
351355
"""
356+
if any(arg in kwargs for arg in ("conn", "df", "table_name", "schema")):
357+
raise ProgrammingError(
358+
"Arguments 'conn', 'df', 'table_name', and 'schema' are not supported parameters for pd_writer."
359+
)
360+
352361
sf_connection = conn.connection.connection
353362
df = pandas.DataFrame(data_iter, columns=keys)
354363
write_pandas(
@@ -357,5 +366,5 @@ def pd_writer(
357366
# Note: Our sqlalchemy connector creates tables case insensitively
358367
table_name=table.name.upper(),
359368
schema=table.schema,
360-
quote_identifiers=quote_identifiers,
369+
**kwargs,
361370
)

0 commit comments

Comments
 (0)