You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Function-level capabilities for the portable predicate, and a path to the luma.gl/WGSL lowering
#4066
Hi all, and thanks @ibgreen for pointing me at the common scan architecture. I've been building something close to its deferred Tranche 9 from the other end, and I'd like to compare notes before proposing any code.
Context
duckdb-deck-demo is a query planner for deck.gl-style rendering. It takes a graph of filters and attribute expressions and decides, per node, whether it runs in DuckDB SQL, a generated JS loop, or a fused WGSL compute kernel. The decision comes from a calibrated cost model (FINDINGS §6). As of this week it can also hand a filter to luma.gl's GPU Dataframe for GPU-side row compaction (FINDINGS §12). At 4M rows keeping 5%, a slider tick went from 28.8 ms (requery) to 2.27 ms, with no readback.
So I now have three expression representations talking to each other through ad-hoc code:
shape
parameters
null semantics
loaders.gl ColumnarPredicate
column ⊕ literal comparisons, in, isNull, and/or/not
{{name}}, routed to a SQL bind, a uniform, or a compaction
NaN on the GPU; SQL NULL in DuckDB
The scan architecture's roadmap names the missing bridge: "lowering the same plan to luma.gl/WGSL masks or indices."
The idea: capability per function, not just per operator
The scan architecture already has the right vocabulary: pushdown, pushdown + residual, residual, unsupported. Today that decision is made per operator.
We make it per function, with a table (FUNCTIONS in expr.ts). Each entry records how the function is spelled in SQL, in WGSL and in JS, and null means "this engine has no equivalent". Whether a subtree can run somewhere becomes a walk over the tree (enginesFor), not a pattern match on node types. That is what makes engine assignment a cost decision rather than a rewrite. The GPU Dataframe capability is the same idea in one function: compactable says which predicates luma can evaluate.
Applied to the scan architecture, a backend would declare a support level for each function, not just for filter. A Parquet source can prune on x > 5 but not on sqrt(x) > 5. That predicate becomes residual, and the residual can go to DuckDB, JS, or luma depending on what each supports. The planner splits the tree at the deepest node the backend supports and doesn't need special cases.
What we learned that a shared IR would have to settle
Each of these was a real bug here. Every one compiled and produced a plausible plan, and all were caught by tests that executed each backend and compared numbers (FINDINGS §8):
% on negatives. SQL truncates, and WGSL has no float %. We standardised on floor semantics and emit a - b*floor(a/b).
Null. DuckDB has NULL, while our GPU path has NaN, which comparisons drop. luma has validity masks with three-valued logic. NOT (x > 5) differs between NaN semantics and SQL semantics.
Precision. f64 in SQL, f32 on the GPU. Thresholds near the f32 epsilon disagree.
Literal typing. DuckDB infers DECIMAL for 2.5, and Arrow returns decimals as unscaled integers, so casts must be explicit in generated SQL.
A cross-engine agreement suite (one expression evaluated by every backend, results compared element-wise) is probably the most portable thing we have. It doesn't depend on our syntax at all.
Parameters: a rebind route per predicate
ColumnarPredicateParameter already keeps names unbound until an executor binds them. The next question is what rebinding costs, which is what our planner optimises (FINDINGS §3):
A residual predicate's parameter can be rebound cheaply: a uniform write on the GPU, a prepared-statement bind in SQL.
A pushed predicate's parameter is different. The pruning ladder depends on the value, because row-group statistics prune differently at a new threshold, so a rebind means re-pruning.
The same logical predicate therefore gets a different rebind cost depending on where it was pushed. If a slider drives the threshold, it can be worth keeping the predicate residual on purpose, trading scan bytes for interactivity. If explain reported a rebind route and a rough cost for each parameterised predicate, a planner could make that trade.
What I'd propose, concretely
Discussion first (this post): is a per-function capability table in scope for scan-utils, or should function support stay backend-private?
If yes, a small PR adding an optional function vocabulary to the predicate. The existing column-vs-literal predicates stay valid. Each backend declares support levels per function, and table-query-explain reports which functions made a predicate residual.
Tranche 9 prototype: a lowering from ColumnarPredicate to luma's GPUExpression, returning unsupported when it can't lower. Ours is about 60 lines (toLumaExpr) and is tested against compactable so the two can't drift. It would bring the agreement tests with it.
I'm not proposing to upstream our expression syntax. It's Houdini-flavoured and belongs to the authoring layer. The table, the agreement tests and the lowering are the parts that seem to generalise.
Questions for you:
Does null semantics in the portable predicate follow SQL three-valued logic strictly? That decides whether a NaN-based GPU residual is legal or needs a validity mask.
Is the executor in scan-executor.ts meant to host engine selection (DuckDB vs JS vs GPU for the residual), or is that expected to live above loaders.gl?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Hi all, and thanks @ibgreen for pointing me at the common scan architecture. I've been building something close to its deferred Tranche 9 from the other end, and I'd like to compare notes before proposing any code.
Context
duckdb-deck-demo is a query planner for deck.gl-style rendering. It takes a graph of filters and attribute expressions and decides, per node, whether it runs in DuckDB SQL, a generated JS loop, or a fused WGSL compute kernel. The decision comes from a calibrated cost model (FINDINGS §6). As of this week it can also hand a filter to luma.gl's GPU Dataframe for GPU-side row compaction (FINDINGS §12). At 4M rows keeping 5%, a slider tick went from 28.8 ms (requery) to 2.27 ms, with no readback.
So I now have three expression representations talking to each other through ad-hoc code:
ColumnarPredicatein,isNull,and/or/notColumnarPredicateParameter+bindColumnarPredicateParametersGPUExpressionparameter(name, default), bound at encode time{{name}}, routed to a SQL bind, a uniform, or a compactionThe scan architecture's roadmap names the missing bridge: "lowering the same plan to luma.gl/WGSL masks or indices."
The idea: capability per function, not just per operator
The scan architecture already has the right vocabulary: pushdown, pushdown + residual, residual, unsupported. Today that decision is made per operator.
We make it per function, with a table (
FUNCTIONSin expr.ts). Each entry records how the function is spelled in SQL, in WGSL and in JS, andnullmeans "this engine has no equivalent". Whether a subtree can run somewhere becomes a walk over the tree (enginesFor), not a pattern match on node types. That is what makes engine assignment a cost decision rather than a rewrite. The GPU Dataframe capability is the same idea in one function:compactablesays which predicates luma can evaluate.Applied to the scan architecture, a backend would declare a support level for each function, not just for
filter. A Parquet source can prune onx > 5but not onsqrt(x) > 5. That predicate becomes residual, and the residual can go to DuckDB, JS, or luma depending on what each supports. The planner splits the tree at the deepest node the backend supports and doesn't need special cases.What we learned that a shared IR would have to settle
Each of these was a real bug here. Every one compiled and produced a plausible plan, and all were caught by tests that executed each backend and compared numbers (FINDINGS §8):
%on negatives. SQL truncates, and WGSL has no float%. We standardised on floor semantics and emita - b*floor(a/b).NOT (x > 5)differs between NaN semantics and SQL semantics.DECIMALfor2.5, and Arrow returns decimals as unscaled integers, so casts must be explicit in generated SQL.A cross-engine agreement suite (one expression evaluated by every backend, results compared element-wise) is probably the most portable thing we have. It doesn't depend on our syntax at all.
Parameters: a rebind route per predicate
ColumnarPredicateParameteralready keeps names unbound until an executor binds them. The next question is what rebinding costs, which is what our planner optimises (FINDINGS §3):The same logical predicate therefore gets a different rebind cost depending on where it was pushed. If a slider drives the threshold, it can be worth keeping the predicate residual on purpose, trading scan bytes for interactivity. If
explainreported a rebind route and a rough cost for each parameterised predicate, a planner could make that trade.What I'd propose, concretely
scan-utils, or should function support stay backend-private?table-query-explainreports which functions made a predicate residual.ColumnarPredicateto luma'sGPUExpression, returningunsupportedwhen it can't lower. Ours is about 60 lines (toLumaExpr) and is tested againstcompactableso the two can't drift. It would bring the agreement tests with it.I'm not proposing to upstream our expression syntax. It's Houdini-flavoured and belongs to the authoring layer. The table, the agreement tests and the lowering are the parts that seem to generalise.
Questions for you:
scan-executor.tsmeant to host engine selection (DuckDB vs JS vs GPU for the residual), or is that expected to live above loaders.gl?All reactions