Skip to content

SQL: ClickHouse's lagInFrame window function needs window-frame modeling #231

Description

@zzylol

Summary

The bgp_jan2024_workload 200-query corpus (#219) has 5 occurrences of ClickHouse's lagInFrame, which currently fails to lower — lower_window_func_kind (crates/frontend-sql/src/sql/mod.rs, ~line 1391) is a closed hand-match over WindowFunctionDefinition::WindowUDF names (row_number, rank, dense_rank, lag, lead, first_value, last_value, nth_value) that rejects anything else via LoweringError::UnsupportedFeature.

Why this isn't a simple catalog addition (unlike #230)

lagInFrame's actual semantic difference from plain LAG is that it respects the window frame bounds (returns NULL/a default when the lookback would cross the frame boundary, rather than reaching arbitrarily far back like ordinary LAG). QueryExpr::SQLWindowFunc / WindowFuncKind (crates/types/src/pre_asap/query_expr.rs) has no frame representation at all — its own doc comment says "Window frames are not modelled yet."

So this needs a design decision before it's mechanical:

  1. Model window frames properly first, then give lagInFrame its own correct semantics (bigger: touches SQLWindowFunc's shape, output_schema, and downstream binding). This is a prerequisite for correctly supporting ClickHouse's whole *InFrame function family, not just this one name, and possibly needed for SQL's own OVER (... ROWS BETWEEN ...) frame clauses generally, which are presumably in the same boat.
  2. Map lagInFrameWindowFuncKind::Lag as an approximation and accept the semantic gap (silently wrong at the frame boundary) until (1) happens. Cheap, but a correctness compromise that should be a deliberate, documented decision, not a quiet shortcut.

Scope of this issue

Settle which of the two directions above (or another) to take, and size the "model window frames properly" work if that's the chosen direction. Not asking for an implementation yet.

Related

Metadata

Metadata

Labels

enhancementNew feature or requestirPre-ASAP or post-ASAP intermediate representationsqlSQL front-end lowering (DataFusion → L2)windowWindow functions, frames, panes, or temporal semantics

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions