Skip to content

Commit

Permalink
feat: support write_pandas with partial columns
Browse files Browse the repository at this point in the history
  • Loading branch information
tekumara committed Jun 3, 2023
1 parent 6d3e82a commit 02fd6a4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 4 additions & 3 deletions fakesnow/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,11 @@ def execute_string(
]
return cursors if return_cursors else []

def insert_df(
def _insert_df(
self, df: pd.DataFrame, table_name: str, database: str | None = None, schema: str | None = None
) -> int:
self._duck_conn.execute(f"INSERT INTO {table_name} SELECT * FROM df")
cols = ",".join(df.columns.to_list())
self._duck_conn.execute(f"INSERT INTO {table_name}({cols}) SELECT * FROM df")
return self._duck_conn.fetchall()[0][0]


Expand Down Expand Up @@ -463,7 +464,7 @@ def write_pandas(
table_type: Literal["", "temp", "temporary", "transient"] = "",
**kwargs: Any,
) -> WritePandasResult:
count = conn.insert_df(df, table_name, database, schema)
count = conn._insert_df(df, table_name, database, schema) # noqa: SLF001

# mocks https://docs.snowflake.com/en/sql-reference/sql/copy-into-table.html#output
mock_copy_results = [("fakesnow/file0.txt", "LOADED", count, count, 1, 0, None, None, None, None)]
Expand Down
18 changes: 18 additions & 0 deletions tests/test_fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,21 @@ def test_write_pandas(conn: snowflake.connector.SnowflakeConnection):
cur.execute("select id, first_name, last_name from customers")

assert cur.fetchall() == [(1, "Jenny", "P"), (2, "Jasper", "M")]


def test_write_pandas_partial_columns(conn: snowflake.connector.SnowflakeConnection):
with conn.cursor() as cur:
cur.execute("create table customers (ID int, FIRST_NAME varchar, LAST_NAME varchar)")

df = pd.DataFrame.from_records(
[
{"ID": 1, "FIRST_NAME": "Jenny"},
{"ID": 2, "FIRST_NAME": "Jasper"},
]
)
snowflake.connector.pandas_tools.write_pandas(conn, df, "customers")

cur.execute("select id, first_name, last_name from customers")

# columns not in dataframe will receive their default value
assert cur.fetchall() == [(1, "Jenny", None), (2, "Jasper", None)]

0 comments on commit 02fd6a4

Please sign in to comment.