Skip to content

Commit

Permalink
Fix: ensure Redshift's _fetch_native_df respects case-sensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas committed Oct 17, 2024
1 parent 115f4e6 commit fb27e0f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion sqlmesh/core/engine_adapter/redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,16 @@ def _fetch_native_df(
) -> pd.DataFrame:
"""Fetches a Pandas DataFrame from the cursor"""
self.execute(query, quote_identifiers=quote_identifiers)
return self.cursor.fetch_dataframe()

# We manually build the `DataFrame` here because the driver's `fetch_dataframe`
# method does not respect the active case-sensitivity configuration.
#
# Context: https://github.com/aws/amazon-redshift-python-driver/issues/238
fetcheddata = self.cursor.fetchall()
columns = [column[0] for column in self.cursor._getDescription() or []]

result = [tuple(column for column in rows) for rows in fetcheddata]
return pd.DataFrame(result, columns=columns)

def _create_table_from_source_queries(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,12 @@ def test_columns(ctx: TestContext):
{k: columns[k] for k in max_cols}
).values()
] == ["CHAR(max)", "CHAR(max)", "CHAR(max)", "VARCHAR(max)", "VARCHAR(max)", "VARCHAR(max)"]


def test_fetch_native_df_respects_case_sensitivity(ctx: TestContext):
adapter = ctx.engine_adapter
adapter.execute("SET enable_case_sensitive_identifier TO true")
assert adapter.fetchdf('WITH t AS (SELECT 1 AS "C", 2 AS "c") SELECT * FROM t').to_dict == {
"C": {0: 1},
"c": {0: 2},
}

0 comments on commit fb27e0f

Please sign in to comment.