Skip to main content

Module expr

Module expr 

Source
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 + Int

Structs§

Agg
Marker indicating an aggregate expression (COUNT, SUM, etc.).
AliasedExpr
An expression aliased with AS "name".
AllAgg
Status indicating all selected expressions are aggregate.
AllScalar
Status indicating all selected expressions are scalar (non-aggregate).
CaseBuilder
Builder state after at least one WHEN branch has been added.
CaseInit
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).
MixedAgg
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.
WindowFnExpr
A window function expression that is not yet valid SQL.
WindowSpec
Builder for a window specification (the content inside OVER (...)).

Enums§

FrameBound
Specifies a bound for a window frame (ROWS/RANGE BETWEEN).

Traits§

AggOr
Combine aggregate kinds: if either input is Agg, output is Agg.
AggToStatus
Convert an AggregateKind to an initial AggStatus.
AggregateKind
Marker trait for expression aggregation state.
AggregatePolicy
Dialect-specific aggregate output mapping.
AliasExt
Extension trait providing .alias() method syntax on any expression.
BooleanAggregatePolicy
CastTarget
Input accepted by cast.
CastTypePolicy
Additional cast safety policy by dialect.
CharLengthPolicy
Dialect-aware function name for CHAR_LENGTH.
CombineAggStatus
Combine two aggregate statuses.
ComparisonOperand
Type-safe operand for comparison functions.
CountPolicy
DateTruncPolicy
DefaultCastTypeName
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 Expr type.
FloatPolicy
HasAggStatus
Extract the aggregate status of a type that appears in a SELECT list.
InSubqueryLhs
Left-hand side of an IN (subquery) expression.
LengthPolicy
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.
PostgresAggregateSupport
PostgresDateTimeSupport
PostgresMathSupport
PostgresNullSupport
Marker trait for dialects that support GREATEST/LEAST (PostgreSQL).
PostgresStringSupport
RandomPolicy
Dialect-specific return type for RANDOM().
RoundingPolicy
SQLiteAggregateSupport
SQLiteDateTimeSupport
SQLiteMathSupport
SQLiteStringSupport
SequenceSupport
StatisticalAggregatePolicy
SubqueryType
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 (PostgreSQL 14+).
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_MATCH with flags (PostgreSQL).
regexp_replace
REGEXP_REPLACE - replaces substrings matching a POSIX regex (PostgreSQL).
regexp_replace_flags
REGEXP_REPLACE with 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 (SQLite 3.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 from with the corresponding character in to (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 (SQLite 3.38+).
upper
UPPER - converts string to uppercase.
var_pop
VAR_POP - population variance.
var_samp
VAR_SAMP / VARIANCE - sample variance.
variance
VARIANCE - PostgreSQL alias for sample variance (VAR_SAMP).
window
Create an empty window specification.

Type Aliases§

AggExpr
An aggregate, non-null expression.
NullableAggExpr
An aggregate, nullable expression.
NullableExpr
A scalar, nullable expression.
ScalarExpr
A scalar, non-null expression.