Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ensure Redshift's _fetch_native_df respects case-sensitivity #3266

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion sqlmesh/core/engine_adapter/redshift.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import typing as t

import pandas as pd
Expand All @@ -26,6 +27,8 @@
from sqlmesh.core._typing import SchemaName, TableName
from sqlmesh.core.engine_adapter.base import QueryOrDF

logger = logging.getLogger(__name__)


@set_catalog()
class RedshiftEngineAdapter(
Expand Down Expand Up @@ -128,7 +131,23 @@ 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()

try:
columns = [column[0] for column in self.cursor.description]
except Exception:
columns = None
logging.warning(
"No row description was found, pandas dataframe will be missing column labels."
)

result = [tuple(row) for row 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},
}