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

Open
#21,166 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@zhang2014 is already working on this.

Since Nov 23, 2026.

  • #21220 by @sundy-li — open

Assessment

This issue has not been assessed yet.

Description

C-bug
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

Open in Codespaces

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from databendlabs/databend

All issues in databendlabs/databend

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.