Skip to content

Commit 6e57619

Browse files
authored
fix(snowflake): Transpile ISOWEEK to WEEKISO (#5139)
1 parent 993919d commit 6e57619

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

sqlglot/dialects/snowflake.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ class Snowflake(Dialect):
372372
"ff6": "%f",
373373
}
374374

375+
DATE_PART_MAPPING = {
376+
**Dialect.DATE_PART_MAPPING,
377+
"ISOWEEK": "WEEKISO",
378+
}
379+
375380
def quote_identifier(self, expression: E, identify: bool = True) -> E:
376381
# This disables quoting DUAL in SELECT ... FROM DUAL, because Snowflake treats an
377382
# unquoted DUAL keyword in a special way and does not map it to a user-defined table
@@ -1033,7 +1038,9 @@ class Generator(generator.Generator):
10331038
exp.DayOfWeekIso: rename_func("DAYOFWEEKISO"),
10341039
exp.DayOfYear: rename_func("DAYOFYEAR"),
10351040
exp.Explode: rename_func("FLATTEN"),
1036-
exp.Extract: rename_func("DATE_PART"),
1041+
exp.Extract: lambda self, e: self.func(
1042+
"DATE_PART", map_date_part(e.this, self.dialect), e.expression
1043+
),
10371044
exp.FileFormatProperty: lambda self,
10381045
e: f"FILE_FORMAT=({self.expressions(e, 'expressions', sep=' ')})",
10391046
exp.FromTimeZone: lambda self, e: self.func(

tests/dialects/test_snowflake.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,14 @@ def test_snowflake(self):
10711071
},
10721072
)
10731073

1074+
self.validate_all(
1075+
"SELECT DATE_PART(WEEKISO, CAST('2013-12-25' AS DATE))",
1076+
read={
1077+
"bigquery": "SELECT EXTRACT(ISOWEEK FROM CAST('2013-12-25' AS DATE))",
1078+
"snowflake": "SELECT DATE_PART(WEEKISO, CAST('2013-12-25' AS DATE))",
1079+
},
1080+
)
1081+
10741082
def test_null_treatment(self):
10751083
self.validate_all(
10761084
r"SELECT FIRST_VALUE(TABLE1.COLUMN1) OVER (PARTITION BY RANDOM_COLUMN1, RANDOM_COLUMN2 ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS MY_ALIAS FROM TABLE1",

0 commit comments

Comments
 (0)