gortiz opened a new pull request, #20405:
URL: https://github.com/apache/pinot/pull/20405

   ## Summary
   
   Multi-stage spools can now share two stages that compute the same rows but 
output different columns. The typical case is two scans of the same table, each 
pruned by Calcite to the columns of its own consumer. Before this change, these 
scans never spooled.
   
   The leader stage outputs the union of the columns. Each receiver stage gets 
a `ReceiverSpec` with a column mask, and the sender projects the block for that 
receiver right before the block reaches the mailbox. No consumer stage changes.
   
   **Stacked on 
[apache/pinot#20360](https://github.com/apache/pinot/pull/20360).** The first 
commit of this PR is 
[apache/pinot#20360](https://github.com/apache/pinot/pull/20360). Review the 
last four commits only. I will rebase this PR when 
[apache/pinot#20360](https://github.com/apache/pinot/pull/20360) merges.
   
   ## Why
   
   Before this change, `EquivalentStagesFinder` compared data schemas and 
column ordinals. A query that reads table `t` twice, with different columns 
each time, scanned `t` twice even with `useSpools=true`.
   
   ## How
   
   The PR has four commits. Each one compiles and passes its tests.
   
   1. **`ReceiverSpec` on `MailboxSendNode`.** A new record holds the receiver 
stage id, the distribution, the keys and an optional column mask. Keys and mask 
are ordinals of the sender data schema. `plan.proto` gets `receiverSpecs` 
(field 10). The serializer writes only the specs that differ from the default, 
so plans without specs keep the current wire format. Nothing sets specs in this 
commit.
   2. **Runtime.** `MailboxSendOperator` builds each inner exchange of a 
multi-send from the spec of its receiver. `BlockExchange` routes and splits 
each block with all its columns, so hash keys are ordinals of the full row. 
Then it projects each block, or each split, with `RowHeapDataBlock.project` 
(the lazy view of 
[apache/pinot#20360](https://github.com/apache/pinot/pull/20360)). A remote 
mailbox serializes the masked columns without a copy per row. A serialized 
block is converted to rows once, not once per mailbox.
   3. **Mixed versions.** See [Compatibility](#compatibility).
   4. **Planner.** `EquivalentStagesFinder` compares columns by identity (new 
`ColumnIds`):
      - a scanned column by its name and type;
      - a received column by its ordinal in the union columns of the sender 
group;
      - a computed column by its expression over identities;
      - the output of other nodes (aggregates, joins, windows, set ops and 
more) as opaque columns of the reference stage.
   
      Filters, sorts, keys and collations compare through these identities. 
Nodes with opaque output still need inputs with the same columns in the same 
order. Only projects and scans can differ.
   
      After the substitutions, `EquivalentStagesReplacer` computes the union 
columns of each leader. It rebuilds the leaf chain of the leader with the extra 
columns and gives each receiver that reads other columns a column mask.
   
   ```
   before                                  after
     stage 2: scan t (a, b) ──► join        stage 2: scan t (a, b, c) ──┬─ mask 
[0,1] ──► join
     stage 3: scan t (a, c) ──► join                                    └─ mask 
[0,2] ──► join
   ```
   
   Limits of the planner:
   
   - A member that reads a subset of the columns of its group only needs a mask.
   - A member that needs more columns makes the leader output them. This is 
legal only when the leader is a table scan under projects and filters with a 
project on top, and the new columns only read table columns.
   - Column mapping needs a `HASH`, `BROADCAST` or `RANDOM` distribution 
without pre-partitioning.
   - A stage that calls a volatile function is never the reference of a group.
   
   When every leaf is empty, the broker inlines the leaf stages into the reduce 
stage. `PinotDispatchPlanner` then applies the mask of the receiver with a 
project, so the nodes above the receive read the columns they expect.
   
   ## Compatibility
   
   A server of an older version ignores the specs and sends the same rows and 
columns to every receiver. Two layers prevent this:
   
   - **Capability.** `InstanceCapability.MSE_RECEIVER_SPECS` (server, revision 
1) is the first capability that Pinot defines. `QueryEnvironment` enables 
column mapping only when `RoutingManager.getMinServerCapabilityRevision` 
reports it for every server. Without it, spools only group stages that output 
the same columns in the same order, with the same data schema, so no node needs 
a spec.
   - **Backstop.** For windows that the capability cannot see, for example a 
server that rolls back while the broker still has the old minimum: when a send 
node has specs, the serializer writes the new `DistributionType.PER_RECEIVER` 
in the legacy `distributionType` field and moves the real distribution to the 
new `defaultDistributionType` field (11). An old server then fails the query in 
`PlanNodeDeserializer.convertDistributionType` instead of returning wrong 
results.
   
   Without the capability, the finder is slightly wider than before: the scans 
and projects inside two grouped stages can read the same columns in a different 
order, or read columns that the stage does not output. The output of the 
grouped stages is still identical.
   
   ## Tests
   
   - `EquivalentStagesFinderTest`: column mapping, keys, filters, subsets, 
widening, distributions, upper stages and volatile functions.
   - `EquivalentStagesReplacerTest`: the union columns of the leader, identical 
stages without specs, new expressions through the leaf chain, and rejected 
members.
   - `MailboxSendNodeSerDeTest`: `PER_RECEIVER` round trip, the legacy format 
for uniform nodes, and a `PER_RECEIVER` node without a default distribution.
   - `PinotDispatchPlannerTest`: plans with and without the capability, and the 
inlined reduce stage with masked receivers.
   - `BlockExchangeTest` and `MailboxSendOperatorReceiverSpecTest`: masks with 
hash routing and splits, one conversion of serialized blocks, and OBJECT 
columns sent to a local receiver and a masked remote receiver.
   - `Spool.json`: two new runtime queries (different columns, and filtered 
scans), checked against H2.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to