Skip to content

feat(executor): split grouped summary builds and merges by group key over worker threads - #1563

Draft
zzylol wants to merge 5 commits into
feat/sil-intentfrom
feat/parallel-query-exec
Draft

zzylol wants to merge 5 commits into
feat/sil-intentfrom
feat/parallel-query-exec

Conversation

@zzylol

@zzylol zzylol commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: a query run's grouped summary build (SummaryAgg per group) and grouped merge (SummaryMerge) run on one thread. The network eval's N1 q1 (a per-(src_ip, src_p24, src_p16) sum over 6.2 M rows) keeps 1 of 16 cores busy, while ClickHouse and TimescaleDB spread one query over every core.
  • Change: RunContext::with_parallelism(p) splits these operators' input rows into p parts by a hash of the group key. The parts are folded on rayon's worker threads and combined in the order one thread produces.
  • Effect: answers stay bit-identical to p = 1 for any p: each group sees exactly its own rows in the same order, so compensated sums add in the same order and sketches see the same items. On a quiet 56-thread host, the network eval's N1 5-minute rounds (6.4 M rows, query-time per-host sums) take a median 1.42 s at 56 threads vs 9.99 s at 1 (7.1x) for 1.4x the CPU, with identical answers.

Review guide

  • Base: feat(sil): plan SIL intents: parse/check, lowering, hole guard, planning modes, group latency (L_G) #1553 (feat/sil-intent).
  • Read first: crates/executor/src/operators/summary/partitioned.rs (split, per-part fold, key-order combine). Then the routing in operators/summary/mod.rs (build_per_group_summary, merge_summary_streaming), StateColumnUpdater::gather and groups_are_independent, and GroupTable::part_keys / label_arrays_of.
  • Mechanical: fold_build_rows is the old per-chunk loop body moved into a function, so one thread and the parts share it.
  • Commit 1 holds the tests and stubs (failing), commit 2 the implementation, and commits 3-5 clippy plus the review fixes.

Behavior: before → after

The N1 intent (SELECT src_p16, sum(value) ... GROUP BY src_p16 ORDER BY b DESC LIMIT 5, planned jointly) builds one query-time per-(src_ip, src_p24, src_p16) sum over the 5-minute window, then merges it by src_p16. Before this PR, both run on one thread. After it, with with_parallelism(16), each 256 k-row wave is split into 16 parts by key hash and folded on 16 threads, and the parts' groups are combined by a k-way merge on key bytes. The top-5 answer and every state are identical to the one-thread run.

Interfaces

crates/executor/src/runtime/context.rs

 impl RunContext {
+    /// Split each grouped summary build and merge into `partitions` parts by group-key hash, on rayon's global pool; answers equal 1 (default).
+    pub fn with_parallelism(self, partitions: usize) -> Self;
+    /// Partition tasks this run's operators ran on worker threads (observability, tests).
+    pub fn parallel_tasks(&self) -> usize;
+    /// CPU time (ns) those tasks used on the worker threads; a caller measuring its own thread adds it to the run's CPU.
+    pub fn parallel_cpu_ns(&self) -> u64;
+    /// Inputs with fewer rows (first wave) stay on one thread; each part gets >= rows/8. Default DEFAULT_MIN_SPLIT_ROWS; 0 splits any input (tests).
+    pub fn with_min_split_rows(self, rows: usize) -> Self;
 }
+/// The default min split rows (16384).
+pub const DEFAULT_MIN_SPLIT_ROWS: usize;
+/// CPU time the calling thread has used, in ns (CLOCK_THREAD_CPUTIME_ID; 0 off unix).
+pub fn thread_cpu_ns() -> u64;

Algorithm changes

Problem: one thread per query run; partitioning rows by range would change each group's addition order, which changes compensated-sum bits and sketch states.

Before: per 1024-row chunk: GroupTable::group_ids → StateColumnUpdater::update per row → seal → groups in key order.

After (when p > 1, there is at least one group column, and the family's groups are independent):

  1. Collect a wave of input batches (≤ 256 k rows).
  2. In parallel over batches: hash each row's group key (a dictionary text key is hashed once per dictionary entry; key-equal values hash alike) and take the rows into p order-preserving sub-batches.
  3. In parallel over parts: part i folds its sub-batch of every batch, in input order, with the same code one thread runs (fold_build_rows), into its own table and updater.
  4. Charge memory and check cancellation after each wave.
  5. At the end, order the parts' disjoint groups by a k-way merge on key bytes (the order of groups_in_key_order), gather the labels (label_arrays_of) and the states (StateColumnUpdater::gather), and seal once.

Grouped merges of exact records work the same way over the whole held input. If any state is a keyed exact object, the merge falls back to the streaming one-thread fold.

Correctness: a group's rows all land in one part and keep their input order, so its state is the one-thread state. The output order is the one-thread key order. Tested bit for bit at p ∈ {2, 3, 5, 8} for Sum/Count/Min/Max/DDSketch/HLL builds, exact merges, filters, NULLs, and float keys with zero signs and NaN payloads.

Stays on one thread: builds with no group column, counters (reorder buffer), HydraCMS (one grid shared by all groups), keyed and all-groups builds, and sketch (object) merges.

Review fixes (second round)

  • CPU accounting. Each partition task measures its worker thread's CPU, and the run is charged with it (parallel_cpu_ns). The deployment adds this to the query phase measured on the caller's thread.
  • Memory. Split merges now fold wave by wave instead of holding the whole input, and they charge their parts' tables and records. A wave is capped at 262144 rows and 64 MB of charged batch bytes, so many window batches sharing one dictionary are never all held at once.
  • Small inputs. An input with fewer than 16384 rows in its first wave stays on one thread, and each part gets at least 2048 rows.
  • Dictionary hashing. Dictionary keys are hashed once per entry the batch uses, not once per dictionary entry.
  • Keyed exact states. A merge that meets a keyed exact state folds the rest of its input on one thread, after the records merged so far. Each group's records still fold in input order.

Tests

cargo test -p asap-executor --test executor parallel_execution (7 tests: adds small_inputs_stay_on_one_thread and split_runs_charge_worker_cpu_and_span_waves, a 300 k-row input over two waves). The existing executor suites are unchanged because the default parallelism is 1.

Deferred

  • Parallel sketch-object merges (KLL/DDSketch merges by key).
  • A tree merge for many states into few groups.
  • Parallel Filter/TimeRange operators ahead of the build.
  • Keyed (CMS-with-heap) and all-groups builds.

🤖 Generated with Claude Code

https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi

zzylol and others added 4 commits December 3, 2026 22:20
…e thread (failing)

RunContext::with_parallelism / parallel_tasks are stubs: the partition
count is stored but unused and no task is counted, so the split tests'
task assertions fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
…over worker threads

RunContext::with_parallelism(p) splits each wave of a per-group build's
input rows (and a grouped merge of exact records) into p parts by a hash
of the group key, folds the parts on rayon's global pool with the code
one thread runs, and combines the parts' disjoint groups in key order
(k-way merge on key bytes), gathering labels and states and sealing once.
Each group sees exactly its rows in their input order, so answers and
states are identical to p = 1. No group column, counters and HydraCMS
stay on one thread; a merge meeting a keyed exact state falls back to the
one-thread fold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
…wave, keep small inputs on one thread

Review findings on the key-hash split:
- CPU: each partition task measures its worker thread's CPU
  (runtime::thread_cpu_ns) and the run is charged it
  (RunContext::parallel_cpu_ns), so a caller measuring its own thread can
  count the work done for it on the pool.
- Memory: a split merge folds wave by wave instead of holding (and
  charging) the whole input, and charges its parts' tables and records;
  waves are capped at 262144 rows and 64 MB of charged batch bytes (a
  dictionary shared by many window batches is charged per batch).
- Inputs with fewer than 16384 rows in their first wave stay on one
  thread (RunContext::with_min_split_rows); each part gets >= 1/8 of that.
- Dictionary text keys are hashed once per entry the batch uses (dense or
  map, as GroupTable resolves them), not once per dictionary entry.
A merge meeting a keyed exact state folds the rest on one thread after
the records merged so far: a group's records still fold in input order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1M6boFpwkpMtRc1FnZuYi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request optimizer Workload optimization and plan selection

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant