Skip to content

perf(executor): hash equi-join, KMV merge by sorted union, group-table entry cache - #1569

Merged
zzylol merged 2 commits into
eval/2026-12-plannerfrom
perf/ingest-x10
Dec 4, 2026
Merged

zzylol merged 2 commits into
eval/2026-12-plannerfrom
perf/ingest-x10

Conversation

@zzylol

@zzylol zzylol commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem. At 10x scale (suite_v2 observability/security intents, 840k rows/s), three executor operators cost far more than the data needed. SELECT node, avg(value) FROM cpu … GROUP BY node is planned as a sum view JOINed with a count view on node. The relational join was a nested loop over 100k × 100k pairs: about 144 s per evaluation. A KMV merge scanned the retained hashes for a duplicate on every inserted hash, O(k²) per merge. count(DISTINCT value) … GROUP BY service over 10 minutes (60 windows × 5,000 services) took about 112 s. The group table re-hashed the dictionary texts in every 1024-row slice of a batch.
  • Change. (1) A hash path for equi-joins: a conjunction of SQL column equalities across the two sides, on text, integer, timestamp, date or bool keys. (2) A KMV merge by one sorted union. (3) A per-column cache of dictionary-entry → part that lives across slices and batches sharing a dictionary.
  • Effect. Answers and row order are unchanged. Measured on a 56-thread server at x10, 1x rate: OU_avg_node_* fell from 144 s to 1.1-1.9 s, and H_traces_per_service_10m from 112 s to 23 s. With the backend half (ASAPQuery-backend#908), precompute CPU per million rows fell from 2.12 to 1.26 core-s.

Review guide

  • Base: eval/2026-12-planner 39a25ee (eval build 3). Two commits; the backend draft PR pins this branch.
  • Read first: crates/executor/src/operators/joins/mod.rs: relational_join (new equi branch), equi_join_keys, EquiIndex, emit_matches. These need careful review: null handling, which key types are accepted, output order.
  • Then: crates/executor/src/summary_kernels/kmv.rs union_of, and crates/executor/src/operators/group_table.rs ColumnParts::entry_parts.
  • Mechanical: the nested-loop emission moved unchanged into emit_matches.

Behavior: before → after

The query OU_avg_node_1h: SELECT node, avg(value) AS v FROM cpu WHERE ts > $now - INTERVAL '1 hour' GROUP BY node runs at x10 (100k nodes). The data plane executes it as Join(Inner, node = node) over FinalizeExactAccumulator(sum view by node) and FinalizeExactAccumulator(count view by node).

before after
join work 10^10 predicate evaluations (each left row × every right row) 100k hash inserts + 100k lookups
latency at x10 (56-thread server, round latency incl. queueing) 142-145 s 1.1-1.9 s
latency on the c1 golden input (10k nodes) 1.7 s 0.1 s
answer 100k rows the same rows, same order

Same for H_traces_per_service_10m (KMV merge of 60 windows × 5,000 services): 112 s → 20-23 s.

Interfaces

None. All changes are private functions; no type, field, flag or format changes.

Algorithm changes

Hash equi-join (joins::relational_join)

  • Problem: every join evaluated its predicate on all |L|·|R| pairs; 100k × 100k took ~144 s.
  • Before: for each left row, build the pairs with every right row and evaluate the predicate column.
  • After: equi_join_keys accepts a predicate that is Compare{Column a, Eq, Column b, Sql}, or a non-empty BoolAnd of such comparisons, where each comparison has one column on each side and both columns have the same type among Utf8/Int64/Timestamp/Date/Bool. EquiIndex hashes each right row's key tuple (Value::key bytes, each length-prefixed) to its right rows in ascending order. A left row's matches are the rows listed under its key. A NULL in any key column matches nothing. Any other predicate, and Cross, keeps the nested loop.
  • Correctness: for the accepted types, SQL = is equality of Value::key, and NULL = anything is not true. So a pair matches under the index exactly when the predicate is true. Matches are emitted through the same emit_matches in ascending right-row order, so outer, semi and anti rows come out in the nested loop's order. Floats are excluded (-0.0 = 0.0, NaN). Test equi_join_matches_the_nested_loop_row_for_row compares all six join kinds row for row against the forced nested loop, with duplicate keys and NULLs on both sides and a reversed equality.
  • Complexity: O(|L|·|R|) → O(|L| + |R| + output) time; O(|R|) extra memory for the index.
  • Limits: non-equality conjuncts, mixed key types and float keys still use the nested loop.

KMV merge (kmv::union_of)

  • Problem: Kmv::merge pushes each hash through update_with_hash, which scans the retained hashes linearly for a duplicate: O(k²).
  • After: concatenate both retained sets, sort, dedup, keep the k smallest, and push them largest first into a new heap.
  • Correctness: both the old merge and the sorted union retain the k smallest distinct hashes of the union, so the estimate is identical. Test merge_keeps_the_library_merges_hashes checks this below, at and above k against the library's merge.
  • Complexity: O(k²) → O(k log k) per merge.

Dictionary-entry cache (GroupTable::column_parts)

  • Problem: summary builds fold their input in 1024-row slices (row_chunks). The entry → part cache was per call, so each slice hashed its entries' texts again: about one text hash per row.
  • After: the cache (ColumnParts::entry_parts) keeps the dictionary values array (an Arc, so its address identifies it) and one part per entry, and is reused while batches share that dictionary.
  • Correctness: an entry's part depends only on its text. New parts are still assigned in first-encounter row order, so part ids, groups and group order are unchanged. Covered by the existing group-table and summary tests.

Decisions and open questions

None.

Verification

scope result notes
cargo test -p asap-executor --lib --test executor (release) 170 + 240 passed, 0 failed, 2 ignored includes the 2 new tests
backend golden check (fixed suite_v2.c1r1h2 input, 15 rounds, 68 queries) 822 of 828 evaluations identical to eval build 3 the 6 others are round 1, where the eval build fails with "raw samples … evicted" because the unpaced test preload runs ahead of round 1; this branch answers them, and gives the same answers as the eval build when the preload stops before the eviction (GLEN=130)
x10 measurement see the ASAPQuery-backend#908 table
  • equi_join_matches_the_nested_loop_row_for_row: the hash path returns the nested loop's rows in its order for every join kind, with duplicates and NULLs.
  • merge_keeps_the_library_merges_hashes: the sorted-union KMV merge keeps the library merge's hashes and estimate.

Follow-ups

  • SE_anomalous (sum(CASE WHEN ts > now()-10m …)/count(*) over 1 h) is planned as a query-time raw scan: 360M rows at x10 with 1 h of history (54 s with 10 minutes of history). Planner capability gap: split it into a 10-minute count and a 1 h count.
  • OB_fast_5m / OF_q2 (~30 s): the bound service IN $X filter is applied after SummaryMerge of all groups. Pushing it below the merge would merge only the bound groups.

🤖 Generated with Claude Code

zzylol and others added 2 commits December 4, 2026 09:31
…ry up once per batch

A relational join evaluated its predicate on every (left, right) pair. A
predicate that is a conjunction of SQL equalities between a left and a right
column of one key type (text, integer, timestamp, date, bool) now finds each
left row's matches through a hash index of the right rows; the matches and
their order are the nested loop's. Example: SELECT node, avg(value) FROM cpu
... GROUP BY node, planned as sum view JOIN count view on node, joined 100k x
100k pairs per evaluation (~144 s at x10); it is now linear.

GroupTable kept its dictionary-entry cache per call, and a summary build
folds its input in 1024-row slices, so every slice looked its dictionary
entries up again (one text hash per row). The cache now lives in the column's
parts and is reused while the dictionary is the same array.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Kmv::merge inserts each hash of the other sketch through update_with_hash,
which scans the retained hashes for a duplicate: O(k^2) per merge. A query
merging 60 windows of KMV states for 5000 groups (count(DISTINCT value) ...
GROUP BY service over 10 minutes at x10) spent ~110 s there. The merge now
sorts both retained sets once and keeps the k smallest distinct hashes, the
same set the library's merge keeps (test).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zzylol zzylol added enhancement New feature or request sql SQL front-end lowering (DataFusion → L2) labels Dec 4, 2026
@zzylol
zzylol merged commit 709f1ef into eval/2026-12-planner Dec 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request sql SQL front-end lowering (DataFusion → L2)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant