Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sqlglot/dialects/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,11 @@ class Parser(parser.Parser):
"LEVENSHTEIN_LESS_EQUAL": _build_levenshtein_less_equal,
"JSON_OBJECT_AGG": lambda args: exp.JSONObjectAgg(expressions=args),
"JSONB_OBJECT_AGG": exp.JSONBObjectAgg.from_arg_list,
"WIDTH_BUCKET": lambda args: exp.WidthBucket(
this=seq_get(args, 0), threshold=seq_get(args, 1)
)
if len(args) == 2
else exp.WidthBucket.from_arg_list(args),
}

NO_PAREN_FUNCTIONS = {
Expand Down
8 changes: 7 additions & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8279,7 +8279,13 @@ class Skewness(AggFunc):


class WidthBucket(Func):
arg_types = {"this": True, "min_value": True, "max_value": True, "num_buckets": True}
arg_types = {
"this": True,
"min_value": False,
"max_value": False,
"num_buckets": False,
"threshold": False,
}


class CovarSamp(Binary, AggFunc):
Expand Down
6 changes: 6 additions & 0 deletions tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,12 @@ def test_postgres(self):
"SELECT SLOPE(CAST('(4,4)' AS POINT), CAST('(0,0)' AS POINT))",
)

width_bucket = self.validate_identity("WIDTH_BUCKET(10, ARRAY[5, 15])")
self.assertIsNotNone(width_bucket.args.get("threshold"))

width_bucket = self.validate_identity("WIDTH_BUCKET(10, 5, 15, 25)")
self.assertIsNone(width_bucket.args.get("threshold"))

def test_ddl(self):
# Checks that user-defined types are parsed into DataType instead of Identifier
self.parse_one("CREATE TABLE t (a udt)").this.expressions[0].args["kind"].assert_is(
Expand Down