Skip to content

Commit

Permalink
Feat: any_value hive/spark/presto closes #2053
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Aug 14, 2023
1 parent cad6de8 commit c37abfd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions sqlglot/dialects/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ class Generator(generator.Generator):
]
),
exp.Property: _property_sql,
exp.AnyValue: rename_func("FIRST"),
exp.ApproxDistinct: approx_count_distinct_sql,
exp.ArrayConcat: rename_func("CONCAT"),
exp.ArrayJoin: lambda self, e: self.func("CONCAT_WS", e.expression, e.this),
Expand Down
2 changes: 2 additions & 0 deletions sqlglot/dialects/presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class Tokenizer(tokens.Tokenizer):
class Parser(parser.Parser):
FUNCTIONS = {
**parser.Parser.FUNCTIONS,
"ARBITRARY": exp.AnyValue.from_arg_list,
"APPROX_DISTINCT": exp.ApproxDistinct.from_arg_list,
"APPROX_PERCENTILE": _approx_percentile,
"BITWISE_AND": binary_from_function(exp.BitwiseAnd),
Expand Down Expand Up @@ -247,6 +248,7 @@ class Generator(generator.Generator):

TRANSFORMS = {
**generator.Generator.TRANSFORMS,
exp.AnyValue: rename_func("ARBITRARY"),
exp.ApproxDistinct: _approx_distinct_sql,
exp.ApproxQuantile: rename_func("APPROX_PERCENTILE"),
exp.Array: lambda self, e: f"ARRAY[{self.expressions(e, flat=True)}]",
Expand Down
1 change: 1 addition & 0 deletions sqlglot/dialects/spark.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Generator(Spark2.Generator):
"DATEADD", e.args.get("unit") or "DAY", e.expression, e.this
),
}
TRANSFORMS.pop(exp.AnyValue)
TRANSFORMS.pop(exp.DateDiff)
TRANSFORMS.pop(exp.Group)

Expand Down
1 change: 1 addition & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4047,6 +4047,7 @@ class Avg(AggFunc):


class AnyValue(AggFunc):
_sql_names = ["ANY_VALUE", "FIRST"]
arg_types = {"this": True, "having": False, "max": False}


Expand Down
13 changes: 13 additions & 0 deletions tests/dialects/test_presto.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,19 @@ def test_unnest(self):

@mock.patch("sqlglot.helper.logger")
def test_presto(self, logger):
self.validate_all(
"ARBITRARY(x)",
read={
"presto": "ANY_VALUE(x)",
"hive": "FIRST(x)",
},
write={
"hive": "FIRST(x)",
"presto": "ARBITRARY(x)",
"spark": "ANY_VALUE(x)",
},
)

self.validate_identity("SELECT * FROM x OFFSET 1 LIMIT 1")
self.validate_identity("SELECT * FROM x OFFSET 1 FETCH FIRST 1 ROWS ONLY")
self.validate_identity("SELECT BOOL_OR(a > 10) FROM asd AS T(a)")
Expand Down

0 comments on commit c37abfd

Please sign in to comment.