cockroachdb / cockroachdb/cockroach

opt: tables with non-covering partitions can cause incorrect results when avoiding full scans

Open
#137,994 4 comments 0 reactions 0 assignees View on GitHub
branch-release-22.1 branch-release-22.2 branch-release-23.1 branch-release-23.2 branch-release-24.1 branch-release-24.2 branch-release-24.3 branch-release-25.1 C-bug S-2 T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Describe the problem**

If the partitions of a table do not cover all valid values for the partition columns, the optimizer may create a query plan that produces incorrect results. I noticed this issue while making a recent change, but I think the bug has existed for years.

**To Reproduce**

With CockroachDB v24.3 or a prior version, run the following in `cockroach demo`:
```
CREATE TABLE tab (
col1_0 NAME,
col1_1 INT8,
col1_3 INT8,
col1_5 VARCHAR,
PRIMARY KEY (col1_0 ASC),
UNIQUE (col1_1 ASC, col1_3 ASC)
PARTITION BY LIST (col1_1,col1_3) (
PARTITION table1_part_0 VALUES IN (
(
1,
NULL
)
),
PARTITION table1_part_1 VALUES IN (
(
1000000000,
NULL
)
),
PARTITION table1_part_2 VALUES IN (
(
2000000000,
NULL
)
)
)
);

INSERT
INTO
tab
VALUES
('a', 1, NULL, 'foo1'),
('b', 1000000000, NULL, 'foo2'),
('c', 2000000000, NULL, 'foo3'),
('d', 0, NULL, 'foo4'),
('aa', 1, 1, 'foo1'),
('bb', 1000000000, 1, 'foo2'),
('cc', 2000000000, 1, 'foo3'),
('dd', 0, 1, 'foo4');

UPDATE tab
SET col1_5 = 'bar'
WHERE col1_0 ILIKE col1_0
ORDER BY col1_3
LIMIT 84;
```
This should update every row to have `col1_5 = 'bar'`.

Now re-create the table and insert the rows, but change the UPDATE statement to avoid a full scan:
```
UPDATE tab@{NO_FULL_SCAN}
SET col1_5 = 'bar'
WHERE col1_0 ILIKE col1_0
ORDER BY col1_3
LIMIT 84;
```
This will only update 2 rows.

**Expected behavior**

Both UPDATE statements should update all 8 rows in the table.

Jira issue: CRDB-45844

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.