Skip to content

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

Closed

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 lagInFrame → WindowFuncKind::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

Activity

  1. milindsrivastava1997 commented on Aug 23, 2026

    @milindsrivastava1997
    Collaborator

    Split into two independent issues:

    Neither depends on the other. #267 alone unblocks the 5 `lagInFrame` corpus failures (all default-frame usages, so it's exact for the corpus as-is); #268 is the general fix for the wider silent gap affecting every frame-sensitive `WindowFuncKind` (`Sum`/`Avg`/`Count`/`Min`/`Max`/`FirstValue`/`LastValue`/`NthValue`), not just `lagInFrame`.

  2. added
    enhancementNew feature or request
    sqlSQL front-end lowering (DataFusion → L2)
    windowWindow functions, frames, panes, or temporal semantics
    irPre-ASAP or post-ASAP intermediate representation
    on Aug 26, 2026
  3. zzylol commented on Aug 26, 2026

    @zzylol
    ContributorAuthor

    Related open work

  4. zzylol commented on Aug 26, 2026

    @zzylol
    ContributorAuthor

    Closing because this issue's stated scope was to choose a direction and size/split the window-frame work, not to implement an execution engine. That work is complete:

    Keeping this design/splitting issue open would duplicate those focused follow-ups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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