diff --git a/pkg/sql/opt/constraint/span_test.go b/pkg/sql/opt/constraint/span_test.go index 48bb1d27bb9a..53a0f785161d 100644 --- a/pkg/sql/opt/constraint/span_test.go +++ b/pkg/sql/opt/constraint/span_test.go @@ -864,6 +864,27 @@ func TestSpan_KeyCount(t *testing.T) { span: ParseSpan(&evalCtx, "(/US_WEST - /US_WEST/fix]"), expected: "FAIL", }, + { // 23 + // Fails since the key count overflows. + keyCtx: kcAscAsc, + length: 1, + span: ParseSpan(&evalCtx, "[/0 - /9223372036854775807]"), + expected: "FAIL", + }, + { // 24 + // Succeeds since the key count is int64 max. + keyCtx: kcAscAsc, + length: 1, + span: ParseSpan(&evalCtx, "[/1 - /9223372036854775807]"), + expected: "9223372036854775807", + }, + { // 25 + // Fails since the key count overflows. + keyCtx: kcAscAsc, + length: 1, + span: ParseSpan(&evalCtx, "[/-9223372036854775808 - /9223372036854775807]"), + expected: "FAIL", + }, } for i, tc := range testCases { diff --git a/pkg/sql/opt/memo/testdata/stats/select b/pkg/sql/opt/memo/testdata/stats/select index 61ac9b344def..2c55869aa6c5 100644 --- a/pkg/sql/opt/memo/testdata/stats/select +++ b/pkg/sql/opt/memo/testdata/stats/select @@ -3232,3 +3232,37 @@ select │ <--- 0 ------- 100000000000 └── filters └── x:1 = 10 [type=bool, outer=(1), constraints=(/1: [/10 - /10]; tight), fd=()-->(1)] + +# Regression test for #85499. Avoid overflow when calculating distinct count. +exec-ddl +CREATE TABLE t85499 (c0 INT); +---- + +norm +SELECT t85499.rowid FROM t85499 +WHERE (t85499.rowid) BETWEEN (0) AND ( + CASE WHEN NULL THEN t85499.rowid + ELSE IF(NULL, 1, 9223372036854775807) END +) +UNION SELECT t85499.rowid FROM t85499; +---- +union + ├── columns: rowid:9(int!null) + ├── left columns: t85499.rowid:2(int) + ├── right columns: t85499.rowid:6(int) + ├── stats: [rows=1111.11111, distinct(9)=1111.11111, null(9)=0] + ├── key: (9) + ├── select + │ ├── columns: t85499.rowid:2(int!null) + │ ├── stats: [rows=111.111111, distinct(2)=111.111111, null(2)=0] + │ ├── key: (2) + │ ├── scan t85499 + │ │ ├── columns: t85499.rowid:2(int!null) + │ │ ├── stats: [rows=1000, distinct(2)=1000, null(2)=0] + │ │ └── key: (2) + │ └── filters + │ └── (t85499.rowid:2 >= 0) AND (t85499.rowid:2 <= 9223372036854775807) [type=bool, outer=(2), constraints=(/2: [/0 - /9223372036854775807]; tight)] + └── scan t85499 + ├── columns: t85499.rowid:6(int!null) + ├── stats: [rows=1000, distinct(6)=1000, null(6)=0] + └── key: (6) diff --git a/pkg/sql/sem/tree/datum.go b/pkg/sql/sem/tree/datum.go index befbd7407866..e22eeb15f300 100644 --- a/pkg/sql/sem/tree/datum.go +++ b/pkg/sql/sem/tree/datum.go @@ -5292,10 +5292,10 @@ func MaxDistinctCount(evalCtx *EvalContext, first, last Datum) (_ int64, ok bool return 0, false } - delta := end - start - if delta < 0 { + delta := (end - start) + 1 + if delta <= 0 { // Overflow or underflow. return 0, false } - return delta + 1, true + return delta, true }