Skip to content

Commit

Permalink
feat: snowflake array_construct_compact to spark
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed May 17, 2024
1 parent 5b64475 commit c811adb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sqlglot/dialects/spark.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ class Generator(Spark2.Generator):

TRANSFORMS = {
**Spark2.Generator.TRANSFORMS,
exp.ArrayConstructCompact: lambda self, e: self.func(
"ARRAY_COMPACT", self.func("ARRAY", *e.expressions)
),
exp.Create: preprocess(
[
remove_unique_constraints,
Expand Down
5 changes: 5 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4775,6 +4775,11 @@ class ArrayConcat(Func):
is_var_len_args = True


class ArrayConstructCompact(Func):
arg_types = {"expressions": True}
is_var_len_args = True


class ArrayContains(Binary, Func):
pass

Expand Down
8 changes: 8 additions & 0 deletions tests/dialects/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class TestSnowflake(Validator):
dialect = "snowflake"

def test_snowflake(self):
self.validate_all(
"ARRAY_CONSTRUCT_COMPACT(1, null, 2)",
write={
"spark": "ARRAY_COMPACT(ARRAY(1, NULL, 2))",
"snowflake": "ARRAY_CONSTRUCT_COMPACT(1, NULL, 2)",
},
)

expr = parse_one("SELECT APPROX_TOP_K(C4, 3, 5) FROM t")
expr.selects[0].assert_is(exp.AggFunc)
self.assertEqual(expr.sql(dialect="snowflake"), "SELECT APPROX_TOP_K(C4, 3, 5) FROM t")
Expand Down

0 comments on commit c811adb

Please sign in to comment.