Expand description
Type-safe SQL expression system.
This module provides a type-safe wrapper around SQL expressions that tracks:
- The SQL data type of the expression
- Whether the expression can be NULL
- Whether the expression is an aggregate or scalar
§Example
use drizzle_core::expr::*;
// Type-safe comparisons
let condition = eq(users.id, 10); // OK: Int == Int
// let bad = eq(users.id, "hello"); // ERROR: Int != Text
// Type-safe arithmetic
let total = users.price + users.tax; // OK: both Numeric
// let bad = users.name + users.id; // ERROR: Text + IntStructs§
- Agg
- Marker indicating an aggregate expression (COUNT, SUM, etc.).
- Aliased
Expr - An expression aliased with
AS "name". - AllAgg
- Status indicating all selected expressions are aggregate.
- AllScalar
- Status indicating all selected expressions are scalar (non-aggregate).
- Case
Builder - Builder state after at least one WHEN branch has been added.
- Case
Init - Builder state before the first WHEN branch.
- Excluded
- Wraps a column to reference its value from the proposed insert row (the EXCLUDED row in ON CONFLICT DO UPDATE SET).
- Mixed
Agg - Status indicating a mix of scalar and aggregate expressions.
- NonNull
- Marker indicating an expression cannot be NULL.
- Null
- Marker indicating an expression can be NULL.
- SQLExpr
- A SQL expression that carries type information.
- Scalar
- Marker indicating a scalar (non-aggregate) expression.
- Window
FnExpr - A window function expression that is not yet valid SQL.
- Window
Spec - Builder for a window specification (the content inside
OVER (...)).
Enums§
- Frame
Bound - Specifies a bound for a window frame (ROWS/RANGE BETWEEN).
Traits§
- AggOr
- Combine aggregate kinds: if either input is Agg, output is Agg.
- AggTo
Status - Convert an
AggregateKindto an initialAggStatus. - Aggregate
Kind - Marker trait for expression aggregation state.
- Aggregate
Policy - Dialect-specific aggregate output mapping.
- Alias
Ext - Extension trait providing
.alias()method syntax on any expression. - Boolean
Aggregate Policy - Cast
Target - Input accepted by
cast. - Cast
Type Policy - Additional cast safety policy by dialect.
- Char
Length Policy - Dialect-aware function name for
CHAR_LENGTH. - Combine
AggStatus - Combine two aggregate statuses.
- Comparison
Operand - Type-safe operand for comparison functions.
- Count
Policy - Date
Trunc Policy - Default
Cast Type Name - Default SQL cast type name for a type marker.
- Expr
- An expression in SQL with an associated data type.
- ExprExt
- Extension trait providing method-based comparisons for any
Exprtype. - Float
Policy - HasAgg
Status - Extract the aggregate status of a type that appears in a SELECT list.
- InSubquery
Lhs - Left-hand side of an
IN (subquery)expression. - Length
Policy - NullAnd
- Combine nullability for COALESCE-style fallback behavior.
- NullOr
- Combine nullability: if either input is nullable, output is nullable.
- Nullability
- Marker trait for nullability state.
- Postgres
Aggregate Support - Postgres
Date Time Support - Postgres
Math Support - Postgres
Null Support - Marker trait for dialects that support GREATEST/LEAST (
PostgreSQL). - Postgres
String Support - Random
Policy - Dialect-specific return type for
RANDOM(). - Rounding
Policy - SQLite
Aggregate Support - SQLite
Date Time Support - SQLite
Math Support - SQLite
String Support - Sequence
Support - Statistical
Aggregate Policy - Subquery
Type - Maps a select marker to the SQL type produced by that subquery.
Functions§
- abs
- ABS - returns the absolute value of a number.
- age
- AGE - calculates the interval between two timestamps (
PostgreSQL). - alias
- Create an aliased expression.
- and
- Logical AND of two conditions.
- array_
agg ARRAY_AGG- aggregates values into a SQL array (PostgreSQL).- avg
- AVG(expr) - calculates average of numeric values.
- avg_
distinct - AVG(DISTINCT expr) - calculates average of distinct numeric values.
- between
- BETWEEN comparison.
- bool_
and BOOL_AND- true if all non-null inputs are true (PostgreSQL).- bool_or
BOOL_OR- true if any non-null input is true (PostgreSQL).- case
- Start building a searched CASE expression.
- cast
- Cast an expression to a different type.
- ceil
- CEIL / CEILING - rounds a number up to the nearest integer.
- char_
length CHAR_LENGTH- returns the number of characters in a string.- clock_
timestamp CLOCK_TIMESTAMP- returns the actual wall-clock time (PostgreSQL).- coalesce
- COALESCE - returns first non-null value.
- coalesce_
many - COALESCE with multiple values.
- collate
COLLATE- apply a named collation to a text expression.- concat
- Concatenate two string expressions using || operator.
- concat_
ws CONCAT_WS- concatenates values with a separator, skipping NULLs.- count
- COUNT aggregate.
- count_
distinct - COUNT(DISTINCT expr) - counts distinct non-null values.
- cume_
dist CUME_DIST()— cumulative distribution: fraction of rows <= current row.- current_
date CURRENT_DATE- returns the current date.- current_
time CURRENT_TIME- returns the current time.- current_
timestamp CURRENT_TIMESTAMP- returns the current timestamp with time zone.- currval
- CURRVAL - returns the most recently obtained value from a sequence (
PostgreSQL). - date
- DATE - extracts the date part from a temporal expression (
SQLite). - date_
bin DATE_BIN- bins timestamps into intervals (PostgreSQL14+).- date_
trunc DATE_TRUNC- truncates a timestamp to specified precision (PostgreSQL).- datetime
- DATETIME - creates a datetime from a temporal expression (
SQLite). - dense_
rank DENSE_RANK()— rank without gaps.- distinct
- DISTINCT - marks an expression as DISTINCT.
- eq
- Equality comparison (
=). - every
- EVERY - true if all non-null inputs are true (
PostgreSQL). - excluded
- Reference a column’s value from the proposed insert row (EXCLUDED).
- exists
- EXISTS subquery check.
- exp
- EXP - returns e raised to the power of the argument.
- extract
- EXTRACT - extracts a component from a temporal expression (PostgreSQL/Standard SQL).
- first_
value FIRST_VALUE(expr)— value of expr from the first row of the frame.- floor
- FLOOR - rounds a number down to the nearest integer.
- greatest
- GREATEST - returns the largest of the given values (
PostgreSQL). - group_
concat GROUP_CONCAT- concatenates values into a string (SQLite).- gt
- Greater-than comparison (
>). - gte
- Greater-than-or-equal comparison (
>=). - ifnull
- IFNULL - SQLite/MySQL equivalent of COALESCE with two arguments.
- in_
array - IN array check.
- in_
subquery - IN subquery check.
- initcap
- INITCAP - converts the first letter of each word to uppercase (
PostgreSQL). - instr
- INSTR - finds the position of a substring.
- is_
distinct_ from - IS DISTINCT FROM - NULL-safe inequality comparison.
- is_
false - IS FALSE - tests if a boolean expression is false.
- is_
not_ distinct_ from - IS NOT DISTINCT FROM - NULL-safe equality comparison.
- is_
not_ null - IS NOT NULL check.
- is_null
- IS NULL check.
- is_true
- IS TRUE - tests if a boolean expression is true.
- json_
agg JSON_AGG- aggregates values into a JSON array (PostgreSQL).- json_
object_ agg JSON_OBJECT_AGG- aggregates key/value pairs into a JSON object (PostgreSQL).- jsonb_
agg JSONB_AGG- aggregates values into a JSONB array (PostgreSQL).- jsonb_
object_ agg JSONB_OBJECT_AGG- aggregates key/value pairs into a JSONB object (PostgreSQL).- julianday
- JULIANDAY - converts a temporal expression to Julian day number (
SQLite). - lag
- LAG(expr) — value of expr from the previous row.
- lag_
with_ default - LAG(expr, offset, default) — value of expr from N rows back with a default.
- last_
value LAST_VALUE(expr)— value of expr from the last row of the frame.- lead
- LEAD(expr) — value of expr from the next row.
- lead_
with_ default - LEAD(expr, offset, default) — value of expr from N rows ahead with a default.
- least
- LEAST - returns the smallest of the given values (
PostgreSQL). - left
- LEFT - returns the first n characters of a string (
PostgreSQL). - length
- LENGTH - returns the length of a string.
- like
- LIKE pattern matching.
- ln
- LN - returns the natural logarithm of a number.
- localtime
- LOCALTIME - returns the current time without time zone (
PostgreSQL). - localtimestamp
- LOCALTIMESTAMP - returns the current timestamp without time zone (
PostgreSQL). - log
- LOG - returns the logarithm of a number with a specified base.
- log2
- LOG2 - returns the base-2 logarithm of a number.
- log10
- LOG10 - returns the base-10 logarithm of a number.
- lower
- LOWER - converts string to lowercase.
- lpad
- LPAD - pads a string on the left to a specified length (
PostgreSQL). - lt
- Less-than comparison (
<). - lte
- Less-than-or-equal comparison (
<=). - ltrim
- LTRIM - removes leading whitespace.
- make_
date MAKE_DATE- constructs a date from year, month, day (PostgreSQL).- make_
timestamp MAKE_TIMESTAMP- constructs a timestamp from components (PostgreSQL).- max
- MAX(expr) - finds maximum value.
- min
- MIN(expr) - finds minimum value.
- mod_
- MOD - returns the remainder of division (using % operator).
- ne
- Inequality comparison (
<>). - neq
- Inequality comparison (
<>). - nextval
- NEXTVAL - advances a sequence and returns its new value (
PostgreSQL). - not
- Logical NOT.
- not_
between - NOT BETWEEN comparison.
- not_
exists - NOT EXISTS subquery check.
- not_
in_ array - NOT IN array check.
- not_
in_ subquery - NOT IN subquery check.
- not_
like - NOT LIKE pattern matching.
- now
- NOW - returns the current timestamp with time zone (
PostgreSQL). - nth_
value NTH_VALUE(expr, n) — value of expr from the nth row of the frame.- ntile
- NTILE(n) — divide rows into n roughly equal groups.
- nullif
- NULLIF - returns NULL if arguments are equal, else first argument.
- octet_
length OCTET_LENGTH- returns the number of bytes in a string.- or
- Logical OR of two conditions.
- percent_
rank PERCENT_RANK()— relative rank of the current row: (rank - 1) / (total rows - 1).- pi
- PI - returns the mathematical constant pi (
PostgreSQL). - power
- POWER - raises a number to a power.
- random
- RANDOM - returns a random value.
- rank
RANK()— rank with gaps for ties.- raw
- Create a raw SQL expression with a specified type.
- raw_
non_ null - Create a raw SQL expression with explicit non-null nullability.
- raw_
nullable - Create a raw SQL expression with explicit nullable nullability.
- regexp_
match REGEXP_MATCH- returns captured groups from the first POSIX regex match (PostgreSQL).- regexp_
match_ flags REGEXP_MATCHwith flags (PostgreSQL).- regexp_
replace REGEXP_REPLACE- replaces substrings matching a POSIX regex (PostgreSQL).- regexp_
replace_ flags REGEXP_REPLACEwith flags - replaces substrings matching a POSIX regex (PostgreSQL).- repeat
- REPEAT - repeats a string n times (
PostgreSQL). - replace
- REPLACE - replaces occurrences of a substring.
- reverse
- REVERSE - reverses a string (
PostgreSQL). - right
- RIGHT - returns the last n characters of a string (
PostgreSQL). - round
- ROUND - rounds a number to the nearest integer (or specified precision).
- round_
to - ROUND with precision - rounds a number to specified decimal places.
- row_
number ROW_NUMBER()— sequential row number within the partition.- rpad
- RPAD - pads a string on the right to a specified length (
PostgreSQL). - rtrim
- RTRIM - removes trailing whitespace.
- setval
- SETVAL - sets a sequence’s current value (
PostgreSQL). - sign
- SIGN - returns the sign of a number (-1, 0, or 1).
- split_
part SPLIT_PART- splits a string and returns the nth field (PostgreSQL).- sqrt
- SQRT - returns the square root of a number.
- starts_
with STARTS_WITH- tests if a string starts with a prefix (PostgreSQL).- stddev_
pop STDDEV_POP- population standard deviation.- stddev_
samp STDDEV_SAMP/ STDDEV - sample standard deviation.- strftime
- STRFTIME - formats a temporal expression as text (
SQLite). - string_
agg STRING_AGG- concatenates text values using a delimiter (PostgreSQL).- string_
concat - Concatenate two string expressions using || operator.
- strpos
- STRPOS - finds the position of a substring (
PostgreSQL). - substr
- SUBSTR - extracts a substring from a string.
- sum
- SUM(expr) - sums numeric values.
- sum_
distinct - SUM(DISTINCT expr) - sums distinct numeric values.
- time
- TIME - extracts the time part from a temporal expression (
SQLite). - timediff
- TIMEDIFF - computes the difference between two temporal values (
SQLite3.43+). - to_char
TO_CHAR- formats a temporal expression as text (PostgreSQL).- to_date
TO_DATE- parses a date from text using a format pattern (PostgreSQL).- to_
number TO_NUMBER- parses a number from text using a format pattern (PostgreSQL).- to_
timestamp TO_TIMESTAMP- converts a Unix timestamp to a timestamp (PostgreSQL).- total
- TOTAL - sums numeric values, returning 0.0 for empty sets (
SQLite). - translate
- TRANSLATE - replaces each character in
fromwith the corresponding character into(PostgreSQL). - trim
- TRIM - removes leading and trailing whitespace.
- trunc
- TRUNC - truncates a number towards zero.
- typeof
- Alias for typeof_ (uses Rust raw identifier syntax).
- typeof_
- Get the SQL type of an expression.
- unixepoch
- UNIXEPOCH - converts a temporal expression to Unix timestamp (
SQLite3.38+). - upper
- UPPER - converts string to uppercase.
- var_pop
VAR_POP- population variance.- var_
samp VAR_SAMP/ VARIANCE - sample variance.- variance
- VARIANCE -
PostgreSQLalias for sample variance (VAR_SAMP). - window
- Create an empty window specification.
Type Aliases§
- AggExpr
- An aggregate, non-null expression.
- Nullable
AggExpr - An aggregate, nullable expression.
- Nullable
Expr - A scalar, nullable expression.
- Scalar
Expr - A scalar, non-null expression.