From 785338f6717e01364d3fbd3dc909a0531ddef4ec Mon Sep 17 00:00:00 2001 From: Sean Kelly <10122262+skellys@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:07:16 -0400 Subject: [PATCH 1/3] fix(python): DataFrame.write_database() not passing along engine_options when using ADBC --- py-polars/polars/dataframe/frame.py | 1 + .../tests/unit/io/database/test_write.py | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/py-polars/polars/dataframe/frame.py b/py-polars/polars/dataframe/frame.py index 950aebc4331f..98d06ee85281 100644 --- a/py-polars/polars/dataframe/frame.py +++ b/py-polars/polars/dataframe/frame.py @@ -3967,6 +3967,7 @@ def unpack_table_name(name: str) -> tuple[str | None, str | None, str]: mode=mode, catalog_name=catalog, db_schema_name=db_schema, + **(engine_options or {}), ) elif db_schema is not None: adbc_str_version = ".".join(str(v) for v in adbc_version) diff --git a/py-polars/tests/unit/io/database/test_write.py b/py-polars/tests/unit/io/database/test_write.py index 1a995e31df64..6779734ffcd9 100644 --- a/py-polars/tests/unit/io/database/test_write.py +++ b/py-polars/tests/unit/io/database/test_write.py @@ -318,3 +318,33 @@ def test_write_database_sa_commit(tmp_path: str, pass_connection: bool) -> None: ) assert_frame_equal(result, df) + + +def test_write_database_adbc_temporary_table() -> None: + """Confirm that execution_options are passed along to create temporary tables.""" + df = pl.DataFrame({"colx": [1, 2, 3]}) + temp_tbl_name = "should_be_temptable" + expected_temp_table_create_sql = """CREATE TABLE "should_be_temptable" ("colx" INTEGER)""" + + # test with sqlite in memory + conn = _open_adbc_connection("sqlite:///:memory:") + assert ( + df.write_database( + temp_tbl_name, + connection=conn, + if_table_exists="fail", + engine_options={"temporary": True}, + ) + == 3 + ) + temp_tbl_sql_df = pl.read_database( + "select sql from sqlite_temp_master where type='table' and tbl_name = ?", + connection=conn, + execute_options={"parameters": [temp_tbl_name]}, + ) + assert temp_tbl_sql_df.shape[0] == 1, "no temp table created" + actual_temp_table_create_sql = temp_tbl_sql_df["sql"][0] + assert expected_temp_table_create_sql == actual_temp_table_create_sql + + if hasattr(conn, "close"): + conn.close() From d4bd53652a7e5aa77f06fa6b8de0bba5302c6ba9 Mon Sep 17 00:00:00 2001 From: Sean Kelly <10122262+skellys@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:20:05 -0400 Subject: [PATCH 2/3] style: reformat code with ruff --- py-polars/tests/unit/io/database/test_write.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/py-polars/tests/unit/io/database/test_write.py b/py-polars/tests/unit/io/database/test_write.py index 6779734ffcd9..e531e094116c 100644 --- a/py-polars/tests/unit/io/database/test_write.py +++ b/py-polars/tests/unit/io/database/test_write.py @@ -324,7 +324,9 @@ def test_write_database_adbc_temporary_table() -> None: """Confirm that execution_options are passed along to create temporary tables.""" df = pl.DataFrame({"colx": [1, 2, 3]}) temp_tbl_name = "should_be_temptable" - expected_temp_table_create_sql = """CREATE TABLE "should_be_temptable" ("colx" INTEGER)""" + expected_temp_table_create_sql = ( + """CREATE TABLE "should_be_temptable" ("colx" INTEGER)""" + ) # test with sqlite in memory conn = _open_adbc_connection("sqlite:///:memory:") From e70a148ef062ea3e2ed8ddd8343cb069084bbf82 Mon Sep 17 00:00:00 2001 From: Sean Kelly <10122262+skellys@users.noreply.github.com> Date: Wed, 28 Aug 2024 22:45:36 -0400 Subject: [PATCH 3/3] test: skip new adbc test when run on windows or <= python 3.8 --- py-polars/tests/unit/io/database/test_write.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/py-polars/tests/unit/io/database/test_write.py b/py-polars/tests/unit/io/database/test_write.py index e531e094116c..e9b1b726c10e 100644 --- a/py-polars/tests/unit/io/database/test_write.py +++ b/py-polars/tests/unit/io/database/test_write.py @@ -320,6 +320,10 @@ def test_write_database_sa_commit(tmp_path: str, pass_connection: bool) -> None: assert_frame_equal(result, df) +@pytest.mark.skipif( + sys.version_info < (3, 9) or sys.platform == "win32", + reason="adbc not available on Windows or <= Python 3.8", +) def test_write_database_adbc_temporary_table() -> None: """Confirm that execution_options are passed along to create temporary tables.""" df = pl.DataFrame({"colx": [1, 2, 3]})