Skip to content

Commit

Permalink
Merge pull request cockroachdb#88955 from rytaft/backport21.2-88494
Browse files Browse the repository at this point in the history
release-21.2: opt: fix error due to estimated row count must be non-zero (cockroachdb#88955)
  • Loading branch information
rytaft authored Sep 29, 2022
2 parents c9c8f2e + 35dc68b commit dbc0010
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
21 changes: 21 additions & 0 deletions pkg/sql/opt/constraint/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions pkg/sql/opt/memo/testdata/stats/select
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 3 additions & 3 deletions pkg/sql/sem/tree/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit dbc0010

Please sign in to comment.