Skip to content

Commit 2fb40b6

Browse files
authored
Merge pull request #171264 from virajchogle/width-bucket-equal-bounds-171263
sql: reject equal bounds in width_bucket
2 parents fb59bc5 + f0f6601 commit 2fb40b6

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

pkg/sql/logictest/testdata/logic_test/builtin_function

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,28 @@ SELECT width_bucket('Infinity'::numeric, 1, 10, 0)
29992999
statement error pgcode 2201G pq: count must be greater than zero
30003000
SELECT width_bucket('-Infinity'::numeric, 1, 10, -1)
30013001

3002+
# Match PostgreSQL's "lower bound cannot equal upper bound" rejection
3003+
# for both overloads, across operand positions (above, below, at the
3004+
# degenerate point). Without the guard, the helper divides by zero
3005+
# and casts +/-Inf or NaN to int, producing INT64_MAX, INT64_MIN, or 0.
3006+
statement error pgcode 2201G pq: lower bound cannot equal upper bound
3007+
SELECT width_bucket(7::numeric, 5::numeric, 5::numeric, 10::int)
3008+
3009+
statement error pgcode 2201G pq: lower bound cannot equal upper bound
3010+
SELECT width_bucket(3::numeric, 5::numeric, 5::numeric, 10::int)
3011+
3012+
statement error pgcode 2201G pq: lower bound cannot equal upper bound
3013+
SELECT width_bucket(5::numeric, 5::numeric, 5::numeric, 10::int)
3014+
3015+
statement error pgcode 2201G pq: lower bound cannot equal upper bound
3016+
SELECT width_bucket(7, 5, 5, 10)
3017+
3018+
statement error pgcode 2201G pq: lower bound cannot equal upper bound
3019+
SELECT width_bucket(3, 5, 5, 10)
3020+
3021+
statement error pgcode 2201G pq: lower bound cannot equal upper bound
3022+
SELECT width_bucket(5, 5, 5, 10)
3023+
30023024

30033025
# Sanity check pg_type_is_visible.
30043026
query BBB

pkg/sql/sem/builtins/math_builtins.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,12 @@ var mathBuiltins = map[string]builtinDefinition{
739739
"count must be greater than zero",
740740
)
741741
}
742+
if b1 == b2 {
743+
return nil, pgerror.New(
744+
pgcode.InvalidArgumentForWidthBucketFunction,
745+
"lower bound cannot equal upper bound",
746+
)
747+
}
742748
if math.IsInf(operand, 1) {
743749
return tree.NewDInt(tree.DInt(count + 1)), nil
744750
}
@@ -766,6 +772,12 @@ var mathBuiltins = map[string]builtinDefinition{
766772
"count must be greater than zero",
767773
)
768774
}
775+
if b1 == b2 {
776+
return nil, pgerror.New(
777+
pgcode.InvalidArgumentForWidthBucketFunction,
778+
"lower bound cannot equal upper bound",
779+
)
780+
}
769781
return tree.NewDInt(tree.DInt(widthBucket(operand, b1, b2, count))), nil
770782
},
771783
Info: "return the bucket number to which operand would be assigned in a histogram having count " +

0 commit comments

Comments
 (0)