[Feature] Route fixed-bucket IN/OR predicates per bucket in PrimaryKeySortedIndexScan
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
- 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). - 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).
- Plan a batch point lookup query with
WHERE pk IN (k1, k2, ..., k200)viaPrimaryKeyBatchScan.
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
Mmatched buckets (~100 buckets for 200 random keys on a 128-bucket table). - However,
PrimaryKeySortedIndexScanevaluates the entireIN (200 keys)predicate against every one of theMbuckets. - This amplifies
200logical key lookups into100 × 200 = 20,000B-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,000probes): scan planning takes 8.2–12.8 s for a singleIN (200)query. - With per-bucket predicate routing (
200probes, 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(...):
- Extract target bucket(s) for each literal in
EQUAL,IN, andORbranches (including insideANDconjunctions) using the table'sBucketFunctionandBinaryRowserialization. - For bucket
b, narrowIN (...)/ORdown to only the literals that hash tob(and short-circuit to empty result if no literal matchesb). - 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!