Liking cljdoc? Tell your friends :D

datahike.pg.window

Window function post-processing engine.

Operates on query result rows (vectors of values) after the main Datalog query has executed: partition, sort, resolve each row's FRAME, and compute the window value over it.

Two rules govern the whole file.

The frame is the unit of computation. PostgreSQL evaluates a window aggregate over the frame -- the slice of the sorted partition the frame clause names -- not over the partition. The default frame when a window has an ORDER BY is RANGE UNBOUNDED PRECEDING AND CURRENT ROW, which ends at the last PEER of the current row (every row with an equal sort key), so sum(v) OVER (ORDER BY k) on tied keys gives tied rows the same total. Reading that default as ROWS -- as this engine did -- makes every tie wrong, and dropping the frame entirely makes every running aggregate wrong.

The aggregate is the one in datahike.pg.sql.fns. A window aggregate is the SAME function as the plain one, applied to the frame's values instead of the group's: the translator resolves it through the same sql-aggregate->datalog map and the same precision-variant rule, and hands the symbol down in the spec. This file therefore has no arithmetic of its own. It used to, and every one of those private implementations had drifted from its counterpart -- sum accumulated at double so sum(numeric) lost its scale and sum(real) gained float8 noise, avg over a running frame answered an integer where PostgreSQL answers NUMERIC, min/max over any frame but the whole partition answered NULL, and every aggregate the private case did not name -- array_agg, string_agg, stddev, the bool aggregates -- answered NULL for every row.

Window spec format: {:op :row_number/:rank/:dense_rank/:ntile/:sum/:avg/…/:lag/:lead :col-idx int — result-tuple index of the first argument :arg2-idx int — index of the second (string_agg delimiter, …) :count-star? bool — COUNT(*): counts frame ROWS, no argument :agg-sym sym — fully-qualified aggregate fn (aggregates only) :partition-by [idx …] — column indices for partitioning :order-by [[idx :asc/:desc] …] :frame {:type :rows/:range :start bound :end bound} :offset-n int — LAG/LEAD/NTH_VALUE offset (default 1) :default-val val — LAG/LEAD default :ntile-n int}

Window function post-processing engine.

Operates on query result rows (vectors of values) after the main Datalog
query has executed: partition, sort, resolve each row's FRAME, and compute
the window value over it.

Two rules govern the whole file.

The frame is the unit of computation. PostgreSQL evaluates a window
aggregate over the frame -- the slice of the sorted partition the frame
clause names -- not over the partition. The default frame when a window
has an ORDER BY is `RANGE UNBOUNDED PRECEDING AND CURRENT ROW`, which
ends at the last PEER of the current row (every row with an equal sort
key), so `sum(v) OVER (ORDER BY k)` on tied keys gives tied rows the same
total. Reading that default as ROWS -- as this engine did -- makes every
tie wrong, and dropping the frame entirely makes every running aggregate
wrong.

The aggregate is the one in `datahike.pg.sql.fns`. A window aggregate is
the SAME function as the plain one, applied to the frame's values instead
of the group's: the translator resolves it through the same
`sql-aggregate->datalog` map and the same precision-variant rule, and
hands the symbol down in the spec. This file therefore has no arithmetic
of its own. It used to, and every one of those private implementations
had drifted from its counterpart -- `sum` accumulated at double so
`sum(numeric)` lost its scale and `sum(real)` gained float8 noise, `avg`
over a running frame answered an integer where PostgreSQL answers
NUMERIC, `min`/`max` over any frame but the whole partition answered
NULL, and every aggregate the private `case` did not name -- array_agg,
string_agg, stddev, the bool aggregates -- answered NULL for every row.

Window spec format:
  {:op :row_number/:rank/:dense_rank/:ntile/:sum/:avg/…/:lag/:lead
   :col-idx int          — result-tuple index of the first argument
   :arg2-idx int         — index of the second (string_agg delimiter, …)
   :count-star? bool     — COUNT(*): counts frame ROWS, no argument
   :agg-sym sym          — fully-qualified aggregate fn (aggregates only)
   :partition-by [idx …] — column indices for partitioning
   :order-by [[idx :asc/:desc] …]
   :frame {:type :rows/:range :start bound :end bound}
   :offset-n int         — LAG/LEAD/NTH_VALUE offset (default 1)
   :default-val val      — LAG/LEAD default
   :ntile-n int}
raw docstring

execute-window-functionsclj

(execute-window-functions rows window-specs)

Apply window function specs to query result rows. rows: seq of result tuples (vectors) Returns rows with one window value appended per spec.

Apply window function specs to query result rows.
rows: seq of result tuples (vectors)
Returns rows with one window value appended per spec.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close