Skip to main content

Module frame

Module frame 

Source
Expand description

Mods for a window frame — which rows around the current one a window function sees.

From https://www.sqlite.org/syntax/frame-spec.html:

{ RANGE | ROWS | GROUPS }
  { UNBOUNDED PRECEDING | expr PRECEDING | CURRENT ROW
  | BETWEEN { UNBOUNDED PRECEDING | expr PRECEDING | CURRENT ROW | expr FOLLOWING }
        AND { expr PRECEDING | CURRENT ROW | expr FOLLOWING | UNBOUNDED FOLLOWING } }
  [ EXCLUDE { NO OTHERS | CURRENT ROW | GROUP | TIES } ]

Read that inner list carefully: UNBOUNDED FOLLOWING appears only as a frame-end and UNBOUNDED PRECEDING only as a frame-start. So there is no from_unbounded_following and no to_unbounded_preceding here — PostgreSQL’s grammar lists both in both positions and leaves the server to refuse them, SQLite’s diagram does not, and a construct a dialect’s grammar lacks should not be representable.

Two of the grammar’s defaults are relied on rather than written: the mode defaults to RANGE, and the start bound to UNBOUNDED PRECEDING. So a to_* mod on its own gives a complete BETWEEN UNBOUNDED PRECEDING AND …, and BETWEEN appears exactly when there is an end bound.

use keelson_sqlite::{arg, f, frame};

// count(*) OVER (ROWS BETWEEN ?1 PRECEDING AND CURRENT ROW EXCLUDE TIES)
let e = f("count", "*").over((
    frame::rows(),
    frame::from_preceding(arg(3i32)),
    frame::to_current_row(),
    frame::exclude_ties(),
));

Functions§

exclude_current_row
EXCLUDE CURRENT ROW.
exclude_group
EXCLUDE GROUP — the current row and all its peers.
exclude_no_others
EXCLUDE NO OTHERS — the default, written out.
exclude_ties
EXCLUDE TIES — the current row’s peers, but not the row itself.
from_current_row
CURRENT ROW as the start bound.
from_following
offset FOLLOWING as the start bound. Only legal inside a BETWEEN, so pair it with a to_* mod.
from_preceding
offset PRECEDING as the start bound.
from_unbounded_preceding
UNBOUNDED PRECEDING as the start bound — the default, written out.
groups
GROUPS — the offsets are counts of peer groups. Requires an ORDER BY on the window.
range
RANGE — the offsets are values compared against the ORDER BY key. The grammar’s default, so this only ever documents intent.
rows
ROWS — the offsets are row counts.
to_current_row
CURRENT ROW as the end bound. Turns the frame into a BETWEEN.
to_following
offset FOLLOWING as the end bound. Turns the frame into a BETWEEN.
to_preceding
offset PRECEDING as the end bound. Turns the frame into a BETWEEN.
to_unbounded_following
UNBOUNDED FOLLOWING as the end bound. Turns the frame into a BETWEEN.