Ray's Knowledge Base
Unlock

Decorrelate equality subqueries into keyed sides at lowering

DecisionVerified 30 Sep 2026Holds project: dftracer-utils
Decision. The context, the choice, why, and the options that were turned down.

Context#

Correlated duql subqueries must retain scalar or membership semantics without rerunning a scan for every outer row.

Decision#

Copy the subquery with print and parse, extract inner == ^.outer where terms as keys, and add those keys to the last select, group or agg. Each outer row reads the keyed side.

For a scalar subquery ending in agg, a missing key gets the aggregate over no rows, including count zero. Build that default row by running the subquery with where false and concatenate it diagonally to the keyed side at table build.

Why#

This avoids per-row quadratic evaluation and preserves the outer row count. Inbox notes 0f6d901d23 and 6e74758c50 record the design and empty-input behavior.

Rejected options#

Per-row evaluation is quadratic. A regular join stage can change row count. Inequality correlation was rejected with a pointer to asof or overlap. A union stage for the default row failed on JSON-typed key columns, so diagonal concatenation is used instead.

Also found by searching

  • correlated subquery in duql reruns the scan for every outer row, quadratic slow
  • scalar subquery count returns null instead of 0 for missing keys
  • how do I decorrelate an equality correlated subquery into a keyed join
  • subquery where inner == ^.outer rewrite, default row with where false
  • duql correlated subquery reruns a scan per outer row and is quadratic
  • scalar subquery ending in agg should give count 0 for missing key, how is it lowered?
  • how are correlated subqueries with inner == ^.outer decorrelated?
  • correlated subquery equality where term to keyed side, inequality correlation rejected, asof or overlap