pub struct Function { /* private fields */ }Expand description
A PostgreSQL function call, with every decoration the grammar hangs off one.
From PostgreSQL 17, 4.2.7 Aggregate Expressions and
4.2.8 Window Function Calls:
name ( [ DISTINCT ] expression [, ...] [ ORDER BY … ] ) [ FILTER ( WHERE … ) ]
name ( expression [, ...] ) WITHIN GROUP ( ORDER BY … ) [ FILTER ( WHERE … ) ]
name ( … ) [ FILTER ( WHERE … ) ] OVER ( window_definition | window_name )plus the FROM-list form, where a set-returning function may name its result
columns:
function_name ( … ) [ AS ] [ alias ] ( column_definition [, ...] )Expr::Func carries only what all three
dialects share, so everything above lives here and reaches core through
Expr::Custom — which is why this is a
keelson-psql type and not a core one.
use keelson_psql::{f, quote, window};
// avg("views") OVER (PARTITION BY "user_id")
let e = f("avg", quote("views")).over(window::partition_by(quote("user_id")));Implementations§
Source§impl Function
impl Function
Sourcepub fn new(
name: impl Into<Cow<'static, str>>,
args: impl IntoExprList,
) -> Function
pub fn new( name: impl Into<Cow<'static, str>>, args: impl IntoExprList, ) -> Function
A call to name with args.
Sourcepub fn distinct(self) -> Function
pub fn distinct(self) -> Function
DISTINCT, for an aggregate that should see each distinct input once.
Sourcepub fn order_by(self, order: impl IntoExpr) -> Function
pub fn order_by(self, order: impl IntoExpr) -> Function
Add a sort key to the aggregate’s own ORDER BY.
Rendered inside the argument list — array_agg(x ORDER BY y) — unless
within_group moved it out.
Sourcepub fn within_group(self) -> Function
pub fn within_group(self) -> Function
Write the ORDER BY as WITHIN GROUP (ORDER BY …), which is what an
ordered-set aggregate such as percentile_cont requires.
Sourcepub fn filter(self, condition: impl IntoExpr) -> Function
pub fn filter(self, condition: impl IntoExpr) -> Function
Add a condition to FILTER (WHERE …). Several are AND-joined.
Sourcepub fn as_table(self, alias: impl Into<Cow<'static, str>>) -> TableFunction
pub fn as_table(self, alias: impl Into<Cow<'static, str>>) -> TableFunction
The alias of a set-returning function used as a from-item: f() AS "t",
or — with columns — f() AS "t" ("a" int).
Not the select-list alias — that is as_. The two must not
meet: gram.y’s func_alias_clause shares one AS between the alias
and the column definitions, so a second alias would be a second AS,
which is a syntax error. That is why this returns a TableFunction,
on which as_ — and the rest of the expression-position decorations —
does not exist.
Sourcepub fn columns<N, T>(
self,
columns: impl IntoIterator<Item = (N, T)>,
) -> TableFunction
pub fn columns<N, T>( self, columns: impl IntoIterator<Item = (N, T)>, ) -> TableFunction
Name and type the columns a set-returning function returns:
json_to_recordset($1) AS ("a" int, "b" text).
The name is quoted; the type is written verbatim, so int, text[] and
numeric(10, 2) all work.
Returns a TableFunction for the same reason
as_table does: the column definitions spend the one
AS the func_alias_clause production has, so the select-list
as_ cannot be allowed to write another.
Sourcepub fn over(self, mods: impl Mod<Window>) -> Expr
pub fn over(self, mods: impl Mod<Window>) -> Expr
Attach OVER (…), built from window mods — psql::window::* and
psql::frame::*.
Ends the builder, because OVER is the last thing in the grammar: it is
written after FILTER, and nothing may follow it. over(()) gives the legal
OVER (), which means the whole partition.
To reference a window declared in the statement’s WINDOW clause, use
over_name — not over(window::based_on(..)), which is
the copying form and cannot copy a frame.
Trait Implementations§
Source§impl Expression for Function
impl Expression for Function
Source§impl From<Function> for TableFunction
impl From<Function> for TableFunction
Source§fn from(function: Function) -> TableFunction
fn from(function: Function) -> TableFunction
Source§impl HasOrderBy for Function
impl HasOrderBy for Function
Source§fn order_by_mut(&mut self) -> &mut OrderBy
fn order_by_mut(&mut self) -> &mut OrderBy
ORDER BY clause to modify.