table::lazy
On this page
Lazy queries: a Query records the operations of a table pipeline
as a plan and runs it only on collect, after an optimizer has
moved filters as early as they can go (into a file scan, where
Parquet row groups whose statistics rule a filter out are not read)
and pruned every column nothing downstream uses (a scan reads only
those). explain prints the plan it will run.
use table::{col, desc, sum};
let t = table::table!{
"city": ["Seoul", "Busan", "Seoul", "Daegu"],
"temp": [21.5, 24.0, 19.0, 30.5],
"rain": [0.0, 12.5, 3.0, 0.0],
};
let q = t.lazy()
.with_column("hot", col("temp") .> 20.0)
.filter(col("rain") .< 5.0)
.group_by(["city"]).agg([sum("temp")])
.sort([desc("temp_sum")]);
let out = try q.collect();
assert(out.nrows() == 2 && out.row(0).str("city") == "Seoul");
assert((try q.explain()).contains("FILTER"));
Structs
QuerySource
pub struct QueryA table pipeline that runs on collect. Build it from
Table::lazy or a file scan (scan_parquet, scan_arrow,
scan_csv); each method adds one step and returns a new query.
use table::col;
let q = table::table!{ "x": [1, 2, 3, 4] }.lazy().filter(col("x") .> 2).limit(1);
assert(try q.collect() == table::table!{ "x": [3] });
Only the columns
names, in that order.The rows where
eistrue(null counts asfalse).A computed column, as
Table::with_column.Several computed columns, each from the input of this step, as
Table::with_columns.
Sourcepub fn group_by(§self, keys: List<str>) -> LazyGroupBy Group by
keys;aggon the result gives the query back.nabiuse table::{count, mean}; let t = table::table!{ "g": ["a", "b", "a"], "x": [1.0, 2.0, 5.0] }; let out = try t.lazy().group_by(["g"]).agg([count(), mean("x")]).collect(); assert(out.names() == ["g", "count", "x_mean"]);Sorted by
keys, asTable::sort.Sorted ascending by the columns
names.Joined with
oon the shared key columnson, asTable::join.nabilet people = table::table!{ "id": [1, 2], "name": ["ann", "bob"] }; let orders = table::table!{ "id": [1, 1, 2], "amount": [5.0, 7.0, 2.0] }; let q = people.lazy().join(orders.lazy(), ["id"], table::How::Inner); assert((try q.collect()).nrows() == 3);Joined with
oon key pairs, asTable::join_on.The first
nrows.The first
nrows (limit).
Sourcepub fn collect(§self) throws (TableError) -> Table Optimize the plan and run it.
Sourcepub fn collect_unoptimized(§self) throws (TableError) -> Table Run the plan as written, without optimizing: the reference
collectmust agree with.
Sourcepub fn explain(§self) throws (TableError) -> str The optimized plan, one step per line, the source last.
nabiuse table::col; let q = table::table!{ "a": [1, 2], "b": [3, 4] }.lazy().filter(col("a") .> 1).select(["b"]); let p = try q.explain(); assert(p.starts_with("SELECT [b]"));The plan as written.
LazyGroupBySource
pub struct LazyGroupByA query grouped by key columns, waiting for its aggregations.
One row per group: the keys, then each aggregation.
Functions
scan_parquetSource
pub fn scan_parquet(path: str) -> Query Parquet::new().scan(path).
let top = try table::scan_parquet("trips.parquet")
.filter(table::col("fare") .> 50.0)
.select(["fare", "tip"])
.limit(10)
.collect();
scan_arrowSource
pub fn scan_arrow(path: str) -> Query Arrow::new().scan(path).
let q = table::scan_arrow("trips.arrow").select(["fare"]);
scan_csvSource
pub fn scan_csv(path: str) -> Query Csv::new().scan(path).
let q = table::scan_csv("trips.csv").filter(table::col("fare") .> 50.0);
Implementations
impl Table
This table as the source of a lazy query.
nabilet t = table::table!{ "x": [3, 1, 2] }; assert(try t.lazy().sort_by(["x"]).collect() == table::table!{ "x": [1, 2, 3] });
impl Parquet
A lazy scan of the Parquet file at
pathwith these options: only the columns the query uses are read, and row groups whose statistics rule out a pushed filter are skipped.nabilet q = table::Parquet::new().scan("trips.parquet").filter(table::col("fare") .> 50.0);
impl Arrow
A lazy scan of the Arrow file at
path: only the columns the query uses are decoded.
impl Csv
A lazy scan of the CSV file at
path, read in batches ofbatch_rows: each batch is filtered and pruned before the next, and reading stops once a limit is met. Column types come from the firstinfer_rowsrows (asreader), not the whole file.