Skip to content

Commit be1f741

Browse files
authored
SNOW-652674: normalize name and denormalize name should handle empty string (#349)
1 parent c3274ee commit be1f741

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

DESCRIPTION.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Source code is also available at:
99

1010
# Release Notes
1111

12+
- v1.4.3(Unreleased)
13+
- Fixed a bug that `SnowflakeDialect.normalize_name` and `SnowflakeDialect.denormalize_name` could not handle empty string.
14+
1215
- v1.4.2(Sep 19, 2022)
1316

1417
- Improved performance by standardizing string interpolations to f-strings.

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ universal = 1
33

44
[metadata]
55
name = snowflake-sqlalchemy
6-
version = 1.4.2
6+
version = 1.4.3
77
description = Snowflake SQLAlchemy Dialect
88
long_description = file: DESCRIPTION.md
99
long_description_content_type = text/markdown

src/snowflake/sqlalchemy/snowdialect.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ def _has_object(self, connection, object_type, object_name, schema=None):
252252
def normalize_name(self, name):
253253
if name is None:
254254
return None
255+
if name == "":
256+
return ""
255257
if name.upper() == name and not self.identifier_preparer._requires_quotes(
256258
name.lower()
257259
):
@@ -264,6 +266,8 @@ def normalize_name(self, name):
264266
def denormalize_name(self, name):
265267
if name is None:
266268
return None
269+
if name == "":
270+
return ""
267271
elif name.lower() == name and not self.identifier_preparer._requires_quotes(
268272
name.lower()
269273
):

tests/test_core.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,3 +1718,44 @@ def test_result_type_and_value(engine_testaccount):
17181718
assert data is not None
17191719
data = json.loads(results[-1][29])
17201720
assert data[1]["k"] == 2
1721+
1722+
1723+
def test_normalize_and_denormalize_empty_string_column_name(engine_testaccount):
1724+
with engine_testaccount.connect() as conn:
1725+
table_name = random_string(5)
1726+
conn.exec_driver_sql(
1727+
f"""
1728+
CREATE OR REPLACE TEMP TABLE {table_name}
1729+
(EMPID INT, DEPT TEXT, JAN INT, FEB INT)
1730+
"""
1731+
)
1732+
conn.exec_driver_sql(
1733+
f"""
1734+
INSERT INTO {table_name} VALUES
1735+
(1, 'ELECTRONICS', 100, 200),
1736+
(2, 'CLOTHES', 100, 300)
1737+
"""
1738+
)
1739+
results = conn.exec_driver_sql(
1740+
f'SELECT * FROM {table_name} UNPIVOT(SALES FOR "" IN (JAN, FEB)) ORDER BY EMPID;'
1741+
).fetchall() # normalize_name will be called
1742+
assert results == [
1743+
(1, "ELECTRONICS", "JAN", 100),
1744+
(1, "ELECTRONICS", "FEB", 200),
1745+
(2, "CLOTHES", "JAN", 100),
1746+
(2, "CLOTHES", "FEB", 300),
1747+
]
1748+
1749+
conn.exec_driver_sql(
1750+
f"""
1751+
CREATE OR REPLACE TEMP TABLE {table_name}
1752+
(COL INT, "" INT)
1753+
"""
1754+
)
1755+
inspector = inspect(conn)
1756+
columns = inspector.get_columns(table_name) # denormalize_name will be called
1757+
assert (
1758+
len(columns) == 2
1759+
and columns[0]["name"] == "col"
1760+
and columns[1]["name"] == ""
1761+
)

0 commit comments

Comments
 (0)