Skip to main content

Function

Struct Function 

Source
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

Source

pub fn new( name: impl Into<Cow<'static, str>>, args: impl IntoExprList, ) -> Function

A call to name with args.

Source

pub fn distinct(self) -> Function

DISTINCT, for an aggregate that should see each distinct input once.

Source

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.

Source

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.

Source

pub fn filter(self, condition: impl IntoExpr) -> Function

Add a condition to FILTER (WHERE …). Several are AND-joined.

Source

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 columnsf() 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.

Source

pub fn columns<N, T>( self, columns: impl IntoIterator<Item = (N, T)>, ) -> TableFunction
where N: Into<Cow<'static, str>>, T: Into<Cow<'static, str>>,

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.

Source

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.

Source

pub fn over_name(self, name: impl Into<Cow<'static, str>>) -> Expr

Attach OVER "w" — a reference to a window in the statement’s WINDOW clause.

Unparenthesised, which is what makes it a reference rather than a copy: the parenthesised form is refused outright when the named window has a frame.

Source

pub fn as_(self, alias: impl Into<Cow<'static, str>>) -> Expr

f(…) AS "alias" — the select-list alias.

Ends the builder for the same reason Chain::as_ does: an alias is not an operand.

Trait Implementations§

Source§

impl Clone for Function

Source§

fn clone(&self) -> Function

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Function

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Function

Source§

fn default() -> Function

Returns the “default value” for a type. Read more
Source§

impl Expression for Function

Source§

fn write_sql(&self, w: &mut SqlWriter<'_>)

Append this fragment to w. Read more
Source§

impl From<Function> for TableFunction

Source§

fn from(function: Function) -> TableFunction

Converts to this type from the input type.
Source§

impl HasOrderBy for Function

Source§

fn order_by_mut(&mut self) -> &mut OrderBy

The ORDER BY clause to modify.
Source§

impl IntoExpr for Function

Source§

fn into_expr(self) -> Expr

Perform the conversion.
Source§

impl IntoExprList for Function

Source§

fn into_expr_list(self) -> Vec<Expr>

Perform the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.