Skip to content

Commit 69c41a2

Browse files
authored
SNOW-1865595: Add currently supported structured type functions (#2790)
1 parent 637905b commit 69c41a2

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
#### New Features
88

99
- Added support for the following functions in `functions.py`
10+
- `array_reverse`
1011
- `divnull`
12+
- `map_cat`
13+
- `map_contains_key`
14+
- `map_keys`
1115
- `nullifzero`
1216
- `snowflake_cortex_sentiment`
1317
- Added `Catalog` class to manage snowflake objects. It can be accessed via `Session.catalog`.

docs/source/snowpark/functions.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,26 @@ Functions
3535
array_construct_compact
3636
array_contains
3737
array_distinct
38+
array_except
3839
array_flatten
3940
array_generate_range
4041
array_insert
4142
array_intersection
43+
array_join
4244
array_max
4345
array_min
4446
array_position
4547
array_prepend
4648
array_remove
49+
array_reverse
4750
array_size
4851
array_slice
4952
array_sort
5053
array_to_string
54+
array_union
5155
array_unique_agg
5256
arrays_overlap
57+
arrays_zip
5358
as_array
5459
as_binary
5560
as_char
@@ -205,6 +210,10 @@ Functions
205210
lpad
206211
ltrim
207212
make_interval
213+
map_cat
214+
map_concat
215+
map_contains_key
216+
map_keys
208217
max
209218
md5
210219
mean

src/snowflake/snowpark/functions.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4785,6 +4785,32 @@ def array_flatten(array: ColumnOrName, _emit_ast: bool = True) -> Column:
47854785
return builtin("array_flatten", _emit_ast=_emit_ast)(array)
47864786

47874787

4788+
@publicapi
4789+
def array_reverse(col: ColumnOrName, _emit_ast: bool = True) -> Column:
4790+
"""Returns an array with the elements of the input array in reverse order.
4791+
4792+
Args:
4793+
col: The source array.
4794+
4795+
Example::
4796+
>>> df = session.sql("select [1, 2, 3, 4] :: ARRAY(INT) as A")
4797+
>>> df.select(array_reverse("A")).show()
4798+
--------------------------
4799+
|"ARRAY_REVERSE(""A"")" |
4800+
--------------------------
4801+
|[ |
4802+
| 4, |
4803+
| 3, |
4804+
| 2, |
4805+
| 1 |
4806+
|] |
4807+
--------------------------
4808+
<BLANKLINE>
4809+
"""
4810+
array = _to_col_if_str(col, "array_reverse")
4811+
return builtin("array_reverse", _emit_ast=_emit_ast)(array)
4812+
4813+
47884814
@publicapi
47894815
def array_sort(
47904816
array: ColumnOrName,
@@ -6933,6 +6959,88 @@ def array_unique_agg(col: ColumnOrName, _emit_ast: bool = True) -> Column:
69336959
return _call_function("array_unique_agg", True, c, _emit_ast=_emit_ast)
69346960

69356961

6962+
@publicapi
6963+
def map_cat(col1: ColumnOrName, col2: ColumnOrName, _emit_ast: bool = True):
6964+
"""Returns the concatenatation of two MAPs.
6965+
6966+
Args:
6967+
col1: The source map
6968+
col2: The map to be appended to col1
6969+
6970+
Example::
6971+
>>> df = session.sql("select {'k1': 'v1'} :: MAP(STRING,STRING) as A, {'k2': 'v2'} :: MAP(STRING,STRING) as B")
6972+
>>> df.select(map_cat("A", "B")).show()
6973+
---------------------------
6974+
|"MAP_CAT(""A"", ""B"")" |
6975+
---------------------------
6976+
|{ |
6977+
| "k1": "v1", |
6978+
| "k2": "v2" |
6979+
|} |
6980+
---------------------------
6981+
<BLANKLINE>
6982+
"""
6983+
m1 = _to_col_if_str(col1, "map_cat")
6984+
m2 = _to_col_if_str(col2, "map_cat")
6985+
return builtin("map_cat", _emit_ast=_emit_ast)(m1, m2)
6986+
6987+
6988+
@publicapi
6989+
def map_contains_key(value: ColumnOrLiteral, col: ColumnOrName, _emit_ast: bool = True):
6990+
"""Determines whether the specified MAP contains the specified key.
6991+
6992+
Args:
6993+
value: The key to find.
6994+
col: The map to be searched.
6995+
6996+
Example 1::
6997+
>>> df = session.sql("select {'k1': 'v1'} :: MAP(STRING,STRING) as M, 'k1' as V")
6998+
>>> df.select(map_contains_key(col("V"), "M")).show()
6999+
------------------------------------
7000+
|"MAP_CONTAINS_KEY(""V"", ""M"")" |
7001+
------------------------------------
7002+
|True |
7003+
------------------------------------
7004+
<BLANKLINE>
7005+
7006+
Example 2::
7007+
>>> df = session.sql("select {'k1': 'v1'} :: MAP(STRING,STRING) as M")
7008+
>>> df.select(map_contains_key("k1", "M")).show()
7009+
-----------------------------------
7010+
|"MAP_CONTAINS_KEY('K1', ""M"")" |
7011+
-----------------------------------
7012+
|True |
7013+
-----------------------------------
7014+
<BLANKLINE>
7015+
"""
7016+
m = _to_col_if_str(col, "map_contains")
7017+
return builtin("map_contains_key", _emit_ast=_emit_ast)(value, m)
7018+
7019+
7020+
@publicapi
7021+
def map_keys(col: ColumnOrName, _emit_ast: bool = True):
7022+
"""Returns the keys in a MAP.
7023+
7024+
Args:
7025+
col: The input map.
7026+
7027+
Example 1::
7028+
>>> df = session.sql("select {'k1': 'v1', 'k2': 'v2'} :: MAP(STRING,STRING) as M")
7029+
>>> df.select(map_keys("M")).show()
7030+
---------------------
7031+
|"MAP_KEYS(""M"")" |
7032+
---------------------
7033+
|[ |
7034+
| "k1", |
7035+
| "k2" |
7036+
|] |
7037+
---------------------
7038+
<BLANKLINE>
7039+
"""
7040+
m = _to_col_if_str(col, "map_keys")
7041+
return builtin("map_keys", _emit_ast=_emit_ast)(m)
7042+
7043+
69367044
@publicapi
69377045
def size(col: ColumnOrName, _emit_ast: bool = True) -> Column:
69387046
"""Returns the size of the input ARRAY, OBJECT or MAP. Returns NULL if the
@@ -10142,6 +10250,9 @@ def sproc(
1014210250
sort_array = array_sort
1014310251
map_from_arrays = arrays_to_object
1014410252
signum = sign
10253+
array_join = array_to_string
10254+
array_union = array_cat
10255+
map_concat = map_cat
1014510256

1014610257

1014710258
@publicapi

0 commit comments

Comments
 (0)