paimon icon indicating copy to clipboard operation
paimon copied to clipboard

[Feature] Route fixed-bucket IN/OR predicates per bucket in PrimaryKeySortedIndexScan

Open tzphh opened this issue 13 hours ago • 0 comments

Search before asking

  • [x] I searched in the issues and found nothing similar.

Motivation

Compute Engine

Java core scan planning (affects any engine querying primary-key tables with B-Tree / scalar sorted indexes via PrimaryKeyBatchScan, e.g., StarRocks, Flink, Spark).

Minimal reproduce step

  1. Create a fixed-bucket primary-key table with bucket = 128, deletion-vectors.enabled = true, and a B-Tree index on the primary key / bucket key column (pk).
  2. Write enough data so that each bucket has a non-trivial B-Tree SST index file (e.g., ~50M rows / ~2 GB SST per bucket).
  3. Plan a batch point lookup query with WHERE pk IN (k1, k2, ..., k200) via PrimaryKeyBatchScan.

In PrimaryKeySortedIndexScan.evaluate() (PrimaryKeySortedIndexScan.java:293), the full unsplit predicate is passed directly to every matched bucket's evaluator:

resultFutures.add(evaluator.evaluateAsync(predicate));

What doesn't meet your expectations?

For a fixed-bucket table where the indexed column is the bucket key, each key in IN (k1, ..., k200) deterministically belongs to exactly one bucket (bucketFunction.bucket(key, totalBuckets)).

Currently:

  • Bucket pruning narrows the scan to M matched buckets (~100 buckets for 200 random keys on a 128-bucket table).
  • However, PrimaryKeySortedIndexScan evaluates the entire IN (200 keys) predicate against every one of the M buckets.
  • This amplifies 200 logical key lookups into 100 × 200 = 20,000 B-Tree SST probes (99% are guaranteed cross-bucket misses).
  • When SST data blocks are not resident in cache (and bloom filters are disabled by default), each wasted probe triggers a 64 KB random DFS/object-store read (~1.28 GB of wasted index reads for a single 200-key query).

Observed impact (6.8B-row, 128-bucket table, ~2.07 GB B-Tree SST per bucket):

  • Current behavior (20,000 probes): scan planning takes 8.2–12.8 s for a single IN (200) query.
  • With per-bucket predicate routing (200 probes, 100× fewer): combined with block-cache reuse, planning drops to ~350 ms (~36× faster).

Solution

For fixed-bucket primary-key tables (bucket > 0, single-column bucket key matching the predicate field), rewrite the predicate per bucket before calling evaluator.evaluateAsync(...):

  1. Extract target bucket(s) for each literal in EQUAL, IN, and OR branches (including inside AND conjunctions) using the table's BucketFunction and BinaryRow serialization.
  2. For bucket b, narrow IN (...) / OR down to only the literals that hash to b (and short-circuit to empty result if no literal matches b).
  3. Leave any unsupported expressions (range filters, dynamic/postpone buckets, multi-column bucket keys, non-bucket-key predicates, IS NULL / NOT IN, etc.) untouched so query semantics remain 100% identical.

Anything else?

No response

Are you willing to submit a PR?

  • [x] I'm willing to submit a PR!

tzphh avatar Dec 04 '26 06:12 tzphh