packages

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.

nabi
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 Query

A 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.

nabi
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] });
  • pub fn select(self, names: List<str>) -> Query§

    Source

    Only the columns names, in that order.

  • pub fn filter(self, e: Expr) -> Query§

    Source

    The rows where e is true (null counts as false).

  • pub fn with_column(self, name: str, e: Expr) -> Query§

    Source

    A computed column, as Table::with_column.

  • pub fn with_columns(self, es: List<(str, Expr)>) -> Query§

    Source

    Several computed columns, each from the input of this step, as Table::with_columns.

  • pub fn group_by(self, keys: List<str>) -> LazyGroupBy§

    Source

    Group by keys; agg on the result gives the query back.

    nabi
    use 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"]);
    
  • pub fn sort(self, keys: List<SortKey>) -> Query§

    Source

    Sorted by keys, as Table::sort.

  • pub fn sort_by(self, names: List<str>) -> Query§

    Source

    Sorted ascending by the columns names.

  • pub fn join(self, o: Query, on: List<str>, how: How) -> Query§

    Source

    Joined with o on the shared key columns on, as Table::join.

    nabi
    let 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);
    
  • pub fn join_on(self, o: Query, left_on: List<str>, right_on: List<str>, how: How) -> Query§

    Source

    Joined with o on key pairs, as Table::join_on.

  • pub fn limit(self, n: usize) -> Query§

    Source

    The first n rows.

  • pub fn head(self, n: usize) -> Query§

    Source

    The first n rows (limit).

  • pub fn collect(self) throws (TableError) -> Table§

    Source

    Optimize the plan and run it.

  • pub fn collect_unoptimized(self) throws (TableError) -> Table§

    Source

    Run the plan as written, without optimizing: the reference collect must agree with.

  • pub fn explain(self) throws (TableError) -> str§

    Source

    The optimized plan, one step per line, the source last.

    nabi
    use 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]"));
    
  • pub fn explain_unoptimized(self) -> str§

    Source

    The plan as written.

LazyGroupBySource

pub struct LazyGroupBy

A query grouped by key columns, waiting for its aggregations.

  • pub fn agg(self, aggs: List<Agg>) -> Query§

    Source

    One row per group: the keys, then each aggregation.

Functions

scan_parquetSource

pub fn scan_parquet(path: str) -> Query

Parquet::new().scan(path).

nabi
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).

nabi
let q = table::scan_arrow("trips.arrow").select(["fare"]);

scan_csvSource

pub fn scan_csv(path: str) -> Query

Csv::new().scan(path).

nabi
let q = table::scan_csv("trips.csv").filter(table::col("fare") .> 50.0);

Implementations

impl Table

  • pub fn lazy(self) -> Query§

    Source

    This table as the source of a lazy query.

    nabi
    let t = table::table!{ "x": [3, 1, 2] };
    assert(try t.lazy().sort_by(["x"]).collect() == table::table!{ "x": [1, 2, 3] });
    

impl Parquet

  • pub fn scan(self, path: str) -> Query§

    Source

    A lazy scan of the Parquet file at path with these options: only the columns the query uses are read, and row groups whose statistics rule out a pushed filter are skipped.

    nabi
    let q = table::Parquet::new().scan("trips.parquet").filter(table::col("fare") .> 50.0);
    

impl Arrow

  • pub fn scan(self, path: str) -> Query§

    Source

    A lazy scan of the Arrow file at path: only the columns the query uses are decoded.

impl Csv

  • pub fn scan(self, path: str) -> Query§

    Source

    A lazy scan of the CSV file at path, read in batches of batch_rows: each batch is filtered and pruned before the next, and reading stops once a limit is met. Column types come from the first infer_rows rows (as reader), not the whole file.