Skip to content

Commit a05053c

Browse files
VaggelisDgeorgesittas
authored andcommitted
fix(postgres): Fix exp.WidthBucket required args (#6621)
1 parent 4daf21f commit a05053c

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

sqlglot/dialects/postgres.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,11 @@ class Parser(parser.Parser):
447447
"LEVENSHTEIN_LESS_EQUAL": _build_levenshtein_less_equal,
448448
"JSON_OBJECT_AGG": lambda args: exp.JSONObjectAgg(expressions=args),
449449
"JSONB_OBJECT_AGG": exp.JSONBObjectAgg.from_arg_list,
450+
"WIDTH_BUCKET": lambda args: exp.WidthBucket(
451+
this=seq_get(args, 0), threshold=seq_get(args, 1)
452+
)
453+
if len(args) == 2
454+
else exp.WidthBucket.from_arg_list(args),
450455
}
451456

452457
NO_PAREN_FUNCTIONS = {

sqlglot/expressions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8279,7 +8279,13 @@ class Skewness(AggFunc):
82798279

82808280

82818281
class WidthBucket(Func):
8282-
arg_types = {"this": True, "min_value": True, "max_value": True, "num_buckets": True}
8282+
arg_types = {
8283+
"this": True,
8284+
"min_value": False,
8285+
"max_value": False,
8286+
"num_buckets": False,
8287+
"threshold": False,
8288+
}
82838289

82848290

82858291
class CovarSamp(Binary, AggFunc):

tests/dialects/test_postgres.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,12 @@ def test_postgres(self):
10161016
"SELECT SLOPE(CAST('(4,4)' AS POINT), CAST('(0,0)' AS POINT))",
10171017
)
10181018

1019+
width_bucket = self.validate_identity("WIDTH_BUCKET(10, ARRAY[5, 15])")
1020+
self.assertIsNotNone(width_bucket.args.get("threshold"))
1021+
1022+
width_bucket = self.validate_identity("WIDTH_BUCKET(10, 5, 15, 25)")
1023+
self.assertIsNone(width_bucket.args.get("threshold"))
1024+
10191025
def test_ddl(self):
10201026
# Checks that user-defined types are parsed into DataType instead of Identifier
10211027
self.parse_one("CREATE TABLE t (a udt)").this.expressions[0].args["kind"].assert_is(

0 commit comments

Comments
 (0)