Skip to content

Commit 77ff9d0

Browse files
feat(snowflake)!: annotate types for MAP_* functions (#6605)
* Annotation for Map functions * updated tests * updates to parameters and tests * Rename `expressions` -> `expression` * Rename `key` -> `expressions` --------- Co-authored-by: Jo <[email protected]>
1 parent 62c0ef0 commit 77ff9d0

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

sqlglot/expressions.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7441,6 +7441,36 @@ class MapFromEntries(Func):
74417441
pass
74427442

74437443

7444+
class MapCat(Func):
7445+
arg_types = {"this": True, "expression": True}
7446+
7447+
7448+
class MapContainsKey(Func):
7449+
arg_types = {"this": True, "key": True}
7450+
7451+
7452+
class MapDelete(Func):
7453+
arg_types = {"this": True, "expressions": True}
7454+
is_var_len_args = True
7455+
7456+
7457+
class MapInsert(Func):
7458+
arg_types = {"this": True, "key": False, "value": True, "update_flag": False}
7459+
7460+
7461+
class MapKeys(Func):
7462+
pass
7463+
7464+
7465+
class MapPick(Func):
7466+
arg_types = {"this": True, "expressions": True}
7467+
is_var_len_args = True
7468+
7469+
7470+
class MapSize(Func):
7471+
pass
7472+
7473+
74447474
# https://learn.microsoft.com/en-us/sql/t-sql/language-elements/scope-resolution-operator-transact-sql?view=sql-server-ver16
74457475
class ScopeResolution(Expression):
74467476
arg_types = {"this": False, "expression": True}

sqlglot/typing/snowflake.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def _annotate_str_to_time(self: TypeAnnotator, expression: exp.StrToTime) -> exp
243243
exp.ArrayConstructCompact,
244244
exp.ArrayUniqueAgg,
245245
exp.ArrayUnionAgg,
246+
exp.MapKeys,
246247
exp.RegexpExtractAll,
247248
exp.Split,
248249
exp.StringToArray,
@@ -289,6 +290,7 @@ def _annotate_str_to_time(self: TypeAnnotator, expression: exp.StrToTime) -> exp
289290
exp.BoolxorAgg,
290291
exp.EqualNull,
291292
exp.IsNullValue,
293+
exp.MapContainsKey,
292294
exp.Search,
293295
exp.SearchIp,
294296
exp.ToBoolean,
@@ -385,6 +387,7 @@ def _annotate_str_to_time(self: TypeAnnotator, expression: exp.StrToTime) -> exp
385387
exp.JarowinklerSimilarity,
386388
exp.Length,
387389
exp.Levenshtein,
390+
exp.MapSize,
388391
exp.Minute,
389392
exp.RtrimmedLength,
390393
exp.Second,
@@ -406,6 +409,15 @@ def _annotate_str_to_time(self: TypeAnnotator, expression: exp.StrToTime) -> exp
406409
exp.XMLGet,
407410
}
408411
},
412+
**{
413+
expr_type: {"returns": exp.DataType.Type.MAP}
414+
for expr_type in {
415+
exp.MapCat,
416+
exp.MapDelete,
417+
exp.MapInsert,
418+
exp.MapPick,
419+
}
420+
},
409421
**{
410422
expr_type: {"returns": exp.DataType.Type.FILE}
411423
for expr_type in {

tests/dialects/test_snowflake.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,15 @@ def test_snowflake(self):
309309
self.validate_identity("SELECT rename, replace")
310310
self.validate_identity("SELECT TIMEADD(HOUR, 2, CAST('09:05:03' AS TIME))")
311311
self.validate_identity("SELECT CAST(OBJECT_CONSTRUCT('a', 1) AS MAP(VARCHAR, INT))")
312+
self.validate_identity(
313+
"SELECT MAP_CAT(CAST(col AS MAP(VARCHAR, VARCHAR)), CAST(col AS MAP(VARCHAR, VARCHAR)))"
314+
)
315+
self.validate_identity("SELECT MAP_CONTAINS_KEY('k1', CAST(col AS MAP(VARCHAR, VARCHAR)))")
316+
self.validate_identity("SELECT MAP_DELETE(CAST(col AS MAP(VARCHAR, VARCHAR)), 'k1')")
317+
self.validate_identity("SELECT MAP_INSERT(CAST(col AS MAP(VARCHAR, VARCHAR)), 'b', '2')")
318+
self.validate_identity("SELECT MAP_KEYS(CAST(col AS MAP(VARCHAR, VARCHAR)))")
319+
self.validate_identity("SELECT MAP_PICK(CAST(col AS MAP(VARCHAR, VARCHAR)), 'a', 'c')")
320+
self.validate_identity("SELECT MAP_SIZE(CAST(col AS MAP(VARCHAR, VARCHAR)))")
312321
self.validate_identity("SELECT CAST(OBJECT_CONSTRUCT('a', 1) AS OBJECT(a CHAR NOT NULL))")
313322
self.validate_identity("SELECT CAST([1, 2, 3] AS ARRAY(INT))")
314323
self.validate_identity("SELECT CAST(obj AS OBJECT(x CHAR) RENAME FIELDS)")

tests/fixtures/optimizer/annotate_functions.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,6 +2952,34 @@ VARCHAR;
29522952
LTRIM(NULL);
29532953
VARCHAR;
29542954

2955+
# dialect: snowflake
2956+
MAP_CAT(CAST(col AS MAP(VARCHAR, VARCHAR)), CAST(col AS MAP(VARCHAR, VARCHAR)));
2957+
MAP;
2958+
2959+
# dialect: snowflake
2960+
MAP_CONTAINS_KEY('k1', CAST(col AS MAP(VARCHAR, VARCHAR)));
2961+
BOOLEAN;
2962+
2963+
# dialect: snowflake
2964+
MAP_DELETE(CAST(col AS MAP(VARCHAR, VARCHAR)), 'b');
2965+
MAP;
2966+
2967+
# dialect: snowflake
2968+
MAP_INSERT(CAST(col AS MAP(VARCHAR, VARCHAR)), 'b', '2');
2969+
MAP;
2970+
2971+
# dialect: snowflake
2972+
MAP_KEYS(CAST(col AS MAP(VARCHAR, VARCHAR)));
2973+
ARRAY;
2974+
2975+
# dialect: snowflake
2976+
MAP_PICK(CAST(col AS MAP(VARCHAR, VARCHAR)), 'a', 'c');
2977+
MAP;
2978+
2979+
# dialect: snowflake
2980+
MAP_SIZE(CAST(col AS MAP(VARCHAR, VARCHAR)));
2981+
INT;
2982+
29552983
# dialect: snowflake
29562984
MINUTE(CAST('08:50:57' AS TIME));
29572985
INT;

0 commit comments

Comments
 (0)