bug: range pruning treats a block of a nullable ARRAY column whose arrays are all empty (or hold only NULL elements) as an all-NULL block; `length(tags) = 0`, `tags = []`, even `tags IS NOT NULL` skip the block, and DELETE/UPDATE miss the rows
Maintainers usually reply within 1 day
Assessment
This issue has not been assessed yet.
Description
Search before asking
- I had searched in the issues and found no similar issues.
Version
main 057efa66e6 (nightly docker image v1.2.961-057efa66e6, 2026-11-08); also v1.2.960-nightly-3c9357c866.
What's Wrong?
For an ARRAY(T) column declared without NOT NULL (the default), a block in which every array is empty, or
contains only NULL elements ([NULL], [NULL, NULL]), is described by range pruning as "all values are NULL".
Every predicate that is false for a NULL array then prunes the block before any row is read: length(tags) = 0,
tags = [], array_to_string(tags, ',') = '', and even tags IS NOT NULL. SELECT misses the rows, DELETE
and UPDATE with such a predicate report fewer affected rows and leave the rows unchanged, and
CREATE TABLE … AS SELECT … WHERE … drops them. One INSERT of empty arrays is enough to create such a block.
No error, default settings. Empty tag/label lists are common, and a whole block is skipped at a time.
Cause: column statistics exist only for leaf columns; for tags ARRAY(STRING) the only leaf is the element column.
RangeIndex builds the domain of tags from the leaf statistics
(range_index.rs#L151)
and statistics_to_domain starts with
(range_index.rs#L342-L353):
DataType::Nullable(deref!(inner_ty)) => {
if stats.len() == 1 && (stats[0].min.is_null() || stats[0].max.is_null()) {
return Domain::Nullable(NullableDomain { has_null: true, value: None }); // "all NULL"
}
The element column's min/max are NULL whenever the block has no non-NULL element, i.e. when every array is empty
or holds only NULLs, but the arrays themselves are not NULL. The comment a few lines below ("Only leaf columns have
statistics, nested columns are treated as having nullable values") shows the intent, but the early return comes
first. The same leaf-versus-field confusion was fixed for the optimizer's count(nested) folding in #20544; range
pruning was not changed. The early return should only apply when the column itself is not nested (or should use
the field's own null count).
How to Reproduce?
CREATE TABLE posts (id INT, tags ARRAY(STRING));
INSERT INTO posts VALUES (1, []), (2, []); -- a block holding only empty arrays
INSERT INTO posts VALUES (3, ['db']), (4, []); -- a mixed block
SELECT count(*) FROM posts WHERE length(tags) = 0;
SELECT count_if(length(tags) = 0) FROM posts;
SELECT count(*) FROM posts WHERE tags = [];
SELECT count(*) FROM posts WHERE tags IS NOT NULL;
EXPLAIN SELECT count(*) FROM posts WHERE length(tags) = 0;
DELETE FROM posts WHERE length(tags) = 0;
SELECT id, tags FROM posts ORDER BY id;
CREATE TABLE readings (id INT, vals ARRAY(INT));
INSERT INTO readings VALUES (1, [NULL]), (2, [NULL, NULL]);
INSERT INTO readings VALUES (3, [5]), (4, [NULL]);
SELECT count(*) FROM readings WHERE vals IS NOT NULL;
SELECT count(*) FROM readings WHERE length(vals) = 2;
Result on main 057efa66e6:
1 <- count(*) WHERE length(tags) = 0 (count_if: 3)
1 <- WHERE tags = [] (expected 3)
2 <- WHERE tags IS NOT NULL (expected 4)
pruning stats: [segments: <... range pruning: 2 to 1 ...>, blocks: <range pruning: 1 to 1 ...>]
1 <- rows deleted
1 | []
2 | []
3 | ["db"] <- ids 1 and 2 match the DELETE predicate and are still there
2 <- readings WHERE vals IS NOT NULL (expected 4)
0 <- readings WHERE length(vals) = 2 (expected 1)
The same with UPDATE posts SET tags = ['untagged'] WHERE length(tags) = 0 (1 row updated instead of 3) and
CREATE TABLE posts_nonnull AS SELECT * FROM posts WHERE tags IS NOT NULL (2 of 4 rows stored).
Expected: every count equals the corresponding count_if (3, 3, 4, 4, 1), the DELETE removes ids 1, 2 and 4.
On columns declared ARRAY(INT NOT NULL) NOT NULL and on MAP columns the same pruning panics instead
(called Result::unwrap() on an Err value: BadDataValueType … failed to downcast scalar NULL and
called Result::unwrap() on an Err value: Null, code 1104), which may be what #20625 reports.
Single node, default settings, HTTP handler.
Are you willing to submit PR?
- Yes I am willing to submit a PR!
- Dominant language
- Rust
- Stars
- 9.5k
- Forks
- 903
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 204
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from databendlabs/databend
-
bug: segment pruning skips rows when only some blocks have statistics of an added columnPossibly taken @zhyass claimed this today. OpenC-bug
databendlabs/databend#21244 · 1 assignee ·
Maintainers usually reply within 1 day
-
bug: ORDER BY ... NULLS LAST LIMIT returns wrong rows when an inverted index answers the filter and the sort column has NULLsPossibly taken @b41sh claimed this today. OpenC-bug
databendlabs/databend#21243 · 1 assignee ·
Maintainers usually reply within 1 day
-
Feature: Reduce role-graph work for explicit secondary rolesPossibly taken @TCeason claimed this 1 day ago. OpenC-feature
databendlabs/databend#21226 · 1 assignee ·
Maintainers usually reply within 1 day
-
Feature: Key role-dependent query results by authorization contextPossibly taken @TCeason claimed this 1 day ago. OpenC-feature
databendlabs/databend#21225 · 1 assignee ·
Maintainers usually reply within 1 day
-
bug: filters folded to false on block statistics drop matching rows: cast-eliminated equality under NOT, float arithmetic over NaN and infinities, variant IS DISTINCT FROMPossibly taken @forsaken628 claimed this 1 day ago. OpenC-bug
databendlabs/databend#21216 · 1 assignee ·
Maintainers usually reply within 1 day
All issues in databendlabs/databend
Similar issues
-
marketing
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
eliasstravik/herdr-projects#1605 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
jlcodes99/cockpit-tools#3332 ·
Maintainers usually reply within 3 days
-
Cognito: the email MFA code (EMAIL_OTP challenge after a password flow) has 8 digits; AWS delivers 6Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
aws/aws-emulator#692 ·
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 82/100
pamburus/hl#1779 · 1 comment ·
Maintainers usually reply within 1 day