Ray's Knowledge Base
Unlock

DUQL range-correlated sub-queries use a per-key sorted index with a segment tree

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

Context#

A sub-query correlated through <, <=, >, >= or between on one inner expression must compute count, sum, min or max over the matching range for each outer row.

Decision#

LookupTable::range holds a per-key sorted index and an iterative segment tree. It keeps count, an __int128 integer sum, a float sum and arg-min/arg-max. The outer row queries it by binary search.

Why#

The tree answers min and max over any range, which a running total cannot do.

Rejected options#

  • Prefix arrays: float band sums by difference lose precision, and min/max do not subtract.
  • A window pass: it has no running min/max.
  • The asof stage: strict bounds and bands do not fit it.

Also found by searching

  • duql sub-query correlated with ^.ts between start and end: need count, sum, min, max per outer row
  • how is a range-correlated subquery evaluated efficiently without precision loss in float sums?
  • what data structure does duql use for sub-queries correlated by < <= > >= or between?
  • LookupTable range segment tree vs prefix sums vs window pass vs asof for band joins
  • duql correlated subquery with < or between on outer row is slow, how is it computed?
  • how are range-correlated sub-queries implemented, count sum min max over matching range per outer row
  • LookupTable::range sorted index segment tree design
  • need min and max over ranges for each row, why not a running total