Skip to content

Pax: fix IS NULL wrong result after DELETE - #2413

Open
Hayne01 wants to merge 1 commit into
apache:mainfrom
Hayne01:fix/pax-delete-isnull-stats
Open

Hayne01 wants to merge 1 commit into
apache:mainfrom
Hayne01:fix/pax-delete-isnull-stats

Conversation

@Hayne01

@Hayne01 Hayne01 commented Nov 24, 2026 •

Copy link
Copy Markdown

Fixes #2091

What does this PR do?

On a PAX table, after a DELETE removes a row from a micro-partition whose
column is entirely NULL, a query WHERE <col> IS NULL returns fewer rows than
expected — the whole block is skipped. With SET pax.enable_sparse_filter = off
the result is correct. This is a wrong-result bug in the storage layer, surfaced
by SQLancer (QUERY_PARTITIONING / TLP).

Minimal reproducer (from the issue):

CREATE TABLE t(c0 int, c1 boolean) USING pax;
INSERT INTO t(c1) VALUES (TRUE), (FALSE), (FALSE);  -- c0 is all NULL
DELETE FROM t WHERE c1;                              -- deletes 1 row from the only group
SELECT count(*) FROM t WHERE c0 IS NULL;             -- returns 0, expected 2
SET pax.enable_sparse_filter = off;
SELECT count(*) FROM t WHERE c0 IS NULL;             -- returns 2 (correct)

Root cause. When a DELETE touches a micro-partition,
TableDeleter::DeleteWithVisibilityMap recomputes the micro-partition statistics
that the sparse filter relies on. The recompute reader is built with a column
projection limited to the minmax ∪ bloomfilter columns. But
MicroPartitionStats::AddRow maintains null counts
(hasnull / allnull / nonnullrows) for every column, reading
slot->tts_isnull[i] for all i. The two assumptions are incompatible:

  1. For a table with no minmax/bloomfilter columns (the reproducer) the projection
    index set is empty. PaxFilter::SetColumnProjection(cols, natts) treats
    "read all" as cols.size() == natts, so an empty set yields
    proj_ = {false, false} (read nothing), not read-all.
  2. OrcFormatReader::ReadStripe then appends nullptr for every column.
  3. OrcGroup::ReadTuple sees an empty proj_col_index_ and takes the CTID-only
    path (if (!pax_columns[index]) continue;), so it never writes
    slot->tts_isnull. The slot is created once with MakeTupleTableSlot
    (zero-initialized) and never cleared in the updater loop, so tts_isnull
    stays false.
  4. AddRow reads tts_isnull[c0] == false → AddNonNullColumn → the recomputed
    stats claim hasnull = false for the all-NULL column c0.
  5. PaxSparseFilter::ExecNullTests, SK_SEARCHNULL branch:
    if (!hasnull) return false; → the whole block is dropped → IS NULL
    returns 0.

Groups without invisible tuples take the MergeRawInfo branch (old, correct
stats) and are unaffected; the reproducer's only group contains the deleted row,
so it takes the recompute branch. The ORC data itself round-trips correctly
(hence sparse_filter = off returns the right answer) — only the recomputed aux
statistics are wrong.

This is the same class of bug as #1749 (projection not covering columns that
AddRow reads). #1749 covered the minmax ∪ bloomfilter columns; it did not
account for AddRow reading the null flag of all columns.

Fix. The recompute reader must load every column, because null counts are
maintained for every column. Build the projection over all attribute indexes so
that SetColumnProjection clears the projection (read-all). This also subsumes
the #1749 fix. min_max_col_idxs / bf_col_idxs are still passed to
MicroPartitionStatsUpdater::Update → Initialize, so minmax/sum/bloom states
remain gated to the declared columns; only the null-count accounting (which is
per-column anyway) now sees correct tts_isnull values.

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

New regression test contrib/pax_storage/sql/delete_null_stats.sql
(+ expected/delete_null_stats.out, wired into pax_schedule). It covers four
cases under pax.enable_sparse_filter = on:

  1. The minimal issue reproducer (no minmax/bloomfilter columns, c0 all NULL):
    IS NULL must return 2 (returns 0 before the fix).
  2. Larger all-NULL int/text columns with a partial DELETE: IS NULL returns
    the remaining rows, IS NOT NULL returns 0.
  3. A mixed NULL/non-NULL column: checks both hasnull and nonnullrows.
  4. A table whose minmax_columns omit the all-NULL column (non-empty projection
    that still misses the all-NULL column): returns 0 before the fix.

The test fails on unpatched main and passes with the fix.

The PAX regression suite (make -C contrib/pax_storage pax-test, the
pax_schedule suite) was run on the same demo cluster twice — once on unpatched
main with the new test added, once after applying the fix and rebuilding
pax.so. The only difference between the two failure sets is delete_null_stats
going from FAILED to ok; the patched failure set is a strict subset of the
baseline (no new failures). The #1749 test (delete_bloom_stats),
delete_sum_stats, filter, statistics_bloom_filter, statistics/*,
visimap_vec_compact, visimap_vec_storage and update pass in both runs.
(cbdb_parallel, filter_tree_root_quals, cluster, db_size_functions fail
in both runs — they are sensitive to the demo-cluster configuration and unrelated
to this change.)

Impact

Performance: DELETE stats recompute now reads full column data instead of
just the minmax/bloomfilter columns. Correctness takes priority over the previous
projection optimization, which produced corrupt statistics. A follow-up could
read only the PRESENT bitmaps for non-key columns to recover most of the I/O
saving; that is a larger change and is left out of this fix.

User-facing changes: Fixes a wrong-result bug — WHERE <col> IS NULL on a
PAX table after DELETE now returns the correct rows with the sparse filter at its
default (on).

Dependencies: None.

Checklist

Additional Context

Runtime before/after on main (debug build, assertions on, demo cluster without
mirrors), same commit and same cluster: run the reproducer BEFORE the fix, apply
the patch, rebuild contrib/pax_storage, restart, run AFTER the fix.

phase WHERE c0 IS NULL (sparse on, default) sparse off expected (sparse on)
BEFORE fix 0 2 0 (bug)
AFTER fix 2 2 2 (fixed)

Assertion before_count='0' after_count='2' → PASS, and the PAX suite
no-regression assertion → PASS.

On a PAX table whose column is entirely NULL within a micro-partition,
DELETE recomputes the micro-partition statistics used by the sparse
filter. The recompute reader projected only the minmax / bloomfilter
columns, but MicroPartitionStats::AddRow maintains null counts for every
column. For a table with no minmax / bloomfilter columns the projection
is empty, OrcGroup takes the CTID-only read path and never fills
tts_isnull, so the refreshed stats record hasnull=false for the all-NULL
column. ExecNullTests then drops the whole block for an IS NULL qual and
the query returns too few rows (pax.enable_sparse_filter=off is correct).

Project every attribute when refreshing stats after DELETE so the
per-column null counts stay correct. This also subsumes the issue apache#1749
fix (minmax / bloomfilter columns are covered).

Add regression test delete_null_stats covering the minimal reproducer,
larger all-NULL int/text columns, a mixed NULL/non-NULL column, and a
table whose minmax_columns omit the all-NULL column. It fails on
unpatched main and passes with the fix.

See: Issue#2091 <apache#2091>
Signed-off-by: Hayne01 <1518598411@qq.com>

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @Hayne01 welcome!🎊 Thanks for taking the effort to make our project better! 🙌 Keep making such awesome contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] [sqlancer] PAX: IS NULL query returns fewer rows after DELETE

1 participant