diff --git a/src/snowflake/sqlalchemy/snowdialect.py b/src/snowflake/sqlalchemy/snowdialect.py index 3e7928f9..5003f286 100644 --- a/src/snowflake/sqlalchemy/snowdialect.py +++ b/src/snowflake/sqlalchemy/snowdialect.py @@ -252,6 +252,8 @@ def _has_object(self, connection, object_type, object_name, schema=None): def normalize_name(self, name): if name is None: return None + if name == "": + return "" if name.upper() == name and not self.identifier_preparer._requires_quotes( name.lower() ): @@ -264,6 +266,8 @@ def normalize_name(self, name): def denormalize_name(self, name): if name is None: return None + if name == "": + return "" elif name.lower() == name and not self.identifier_preparer._requires_quotes( name.lower() ): diff --git a/tests/test_core.py b/tests/test_core.py index b106bf76..d5c29297 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1718,3 +1718,45 @@ def test_result_type_and_value(engine_testaccount): assert data is not None data = json.loads(results[-1][29]) assert data[1]["k"] == 2 + + +def test_normalize_and_denormalize_empty_string_column_name(engine_testaccount): + with engine_testaccount.connect() as conn: + table_name = random_string(5) + conn.exec_driver_sql( + f""" +CREATE OR REPLACE TEMP TABLE {table_name} +(EMPID INT, DEPT TEXT, JAN INT, FEB INT) +""" + ) + conn.exec_driver_sql( + f""" +INSERT INTO {table_name} VALUES + (1, 'ELECTRONICS', 100, 200), + (2, 'CLOTHES', 100, 300) +""" + ) + results = conn.exec_driver_sql( + f""" +SELECT * FROM {table_name} UNPIVOT(SALES FOR "" IN (JAN, FEB)) ORDER BY EMPID;""" + ).fetchall() # normalize_name will be called + assert results == [ + (1, "electronics", "JAN", 100), + (1, "electronics", "FEB", 200), + (2, "clothes", "JAN", 100), + (2, "clothes", "FEB", 300), + ] + + conn.exec_driver_sql( + f""" +CREATE OR REPLACE TEMP TABLE {table_name} +(COL INT, "" INT) +""" + ) + inspector = inspect(conn) + columns = inspector.get_columns(table_name) # denormalize_name will be called + assert ( + len(columns) == 2 + and columns[0]["name"] == "col" + and columns[1]["name"] == "" + )