table::expr
On this page
Column expressions: a small tree built with the dot operators
(col("x") .* 2 .+ col("y"), col("a") .> 3 .&& col("b") .== "k")
and evaluated column at a time against a table.
Structs
ExprSource
pub struct ExprA computed column: names columns with col, scalars with lit (or
a plain number, string, bool or date on either side of an
operator), and combines them with the dot operators. Nulls
propagate; .&& / .|| use three-valued logic (false .&& null
is false).
| Operators | Result |
|---|---|
.+ .- .* .% | i64 for two i64, else f64 |
./ .** | f64 |
.== .!= .< .<= .> .>= | bool |
| `.&& . | |
prefix - | the operand's type |
use table::{col, lit};
let t = table::table!{ "x": [1.0, 2.0, 3.0], "y": [4.0, 5.0, 6.0] };
let e = (col("x") .+ col("y")) ./ 2 .- lit(1.0);
let u = try t.with_column("m", e) catch _ { t };
assert(u.row(0).f64("m") == 1.5);
truewhere the value is null.nabilet t = table::table!{ "x": [1, none] }; assert((try t.filter(table::col("x").is_null()) catch _ { t }).nrows() == 1);truewhere there is a value.Nulls replaced by
v.The values as type
t(Column::cast).truewhere the value is one ofvs.nabiuse table::Value; let t = table::table!{ "c": ["a", "b", "c"] }; let u = try t.filter(table::col("c").is_in([Value::Str("a"), Value::Str("c")])) catch _ { t }; assert(u.nrows() == 2);|x|, keeping the type.Square root, as
f64.eto the value, asf64.Natural logarithm.
Base-10 logarithm, as
f64.Half away from zero, as
f64::round.Round down (an
i64column stays as it is).Round up (an
i64column stays as it is).The year of a date or timestamp, as
i64.nabilet t = table::table!{ "d": [date"2024-03-01", date"1999-12-31"] }; let u = try t.with_column("y", table::col("d").year()) catch _ { t }; assert(u.row(1).i64("y") == 1999);The month of a date or timestamp, 1 to 12.
The day of the month, 1 to 31.
Monday 0 to Sunday 6.
The length of a string in code points.
The text in upper case.
The text in lower case.
truewhere the text containss.truewhere the text starts withs.truewhere the text ends withs.Running sum down the column; nulls stay null and are skipped.
nabilet t = table::table!{ "x": [1, 2, 3] }; let u = try t.with_column("s", table::col("x").cum_sum()) catch _ { t }; assert(u.row(2).i64("s") == 6);Running product; nulls stay null and are skipped.
Running minimum; nulls stay null and are skipped.
Running maximum; nulls stay null and are skipped.
How many non-null values so far.
Sum of the last
windowrows (this one included); null untilwindowvalues have been seen. Neumaier-compensated running sum, O(n).nabilet t = table::table!{ "x": [1.0, 2.0, 3.0, 4.0] }; let u = try t.with_column("r", table::col("x").rolling_mean(2)) catch _ { t }; assert(u.row(3).f64("r") == 3.5 && u.row(0).f64("r") is none);Mean of the last
windowrows (compensated running sum).Minimum over the window, monotonic deque, O(n).
Maximum over the window, monotonic deque, O(n).
Sample standard deviation over the window (Welford update with removal, O(n)).
A rolling statistic that starts once
min_periodsvalues of the window are present (nulls do not count).The column moved down by
krows (up for a negativek), nulls filling the gap.x - x.shift(k).fof each cell (nulls included), as one column; the result type comes from the cells (Column::from_values).nabiuse table::Value; let t = table::table!{ "s": ["a", "bb"] }; let e = table::col("s").map(|v| Value::Int((v.as_str() ?? "").len() as i64)); assert((try t.with_column("n", e) catch _ { t }).row(1).i64("n") == 2);fof each non-null number, asf64.The names of the columns this expression reads.
Sourcepub fn eval(§self, t: Table) throws (TableError) -> Column The expression's values over
t, as a column oft.nrows()rows.nabilet t = table::table!{ "x": [1, 2] }; let c = try (table::col("x") .* 10).eval(t) catch _ { table::Column::i64s([]) }; assert(c.get(1) == table::Value::Int(20));
WhenSource
pub struct WhenA when(..) waiting for its then.
The value where the condition holds.
ThenSource
pub struct ThenA when(..).then(..) waiting for its otherwise.
The value elsewhere (null condition included); the finished expression.
Traits
IntoExprSource
pub trait IntoExprWhat can stand on either side of an expression operator: an Expr,
a number, string, bool, date or Value.
Functions
colSource
pub fn col(name: str) -> Expr Column name of the table the expression runs against.
let e = table::col("price") .* 1.1;
assert(f"{e}" == "(col(\"price\") .* 1.1)");
litSource
pub fn lit<T: IntoExpr>(v: T) -> Expr A scalar, the same in every row.
let e = table::lit(2.5);
assert(f"{e}" == "2.5");
whenSource
pub fn when<C: IntoExpr>(cond: C) -> When then where cond is true, else otherwise; start with when.
use table::{col, when};
let t = table::table!{ "x": [-1.0, 2.0] };
let u = try t.with_column("pos", when(col("x") .> 0).then(col("x")).otherwise(0.0)) catch _ { t };
assert(u.row(0).f64("pos") == 0.0);