Reading inputs¶
A model reads other models and source tables. pipeloom must know every input before it runs
anything: inputs order the run, select descendants under state:modified and show in lineage. Since
1.4 there are three ways to name an input, and one model may use all of them
(ADR-0069).
| Way | Best for |
|---|---|
Parameters (and inputs=) |
a few inputs |
ctx.read in the body |
many inputs, or code that names its tables as it goes |
ctx.spark.table and ctx.spark.sql |
PySpark code brought from a notebook |
Parameters¶
Each parameter of the model function is an input, bound by name to a model or a source table, or
explicitly through inputs=:
from pipeloom import model, source
@model(inputs={"raw": source("tpch", "orders")})
def stg_orders(ctx, raw, customer):
return raw.join(customer, left_on="o_custkey", right_on="c_custkey")
ctx.read¶
ctx.read names an input inside the body. It returns what a parameter of that name would
receive: the native frame, or a Narwhals frame under frame_api: narwhals.
from pipeloom import ModelContext, model, source
@model(materialized="incremental", incremental={"input": "orders", "column": "o_orderdate"})
def orders_enriched(ctx: ModelContext):
orders = ctx.read(source("tpch", "orders")) # alias "orders", filtered
customer = ctx.read("customer") # the model customer
return orders.join(customer, left_on="o_custkey", right_on="c_custkey")
ctx.read("x")reads the input with aliasxif the model has one; otherwise the modelx, else the one source tablex(PLR210 when several groups have it, PLR201 when nothing does). The new alias isx.ctx.read(source("g", "t"))reads tabletof groupg. The new alias ist; if another input already has that alias, that is PLR211: declare the input under another alias withinputs={"tpch_orders": source("tpch", "orders")}and callctx.read("tpch_orders").- The incremental input comes after the incremental filter, and for an interval model it is
limited to the window.
incremental.inputmay name an alias a read created. - Repeated reads return the same frame. Aliases bound only by reads are never passed as arguments.
ctx.sqlsees read aliases like any input; table names inctx.sqlstay aliases.pipeloom testgives rows to read aliases like any input, keyed by alias.
Reads must be literal¶
The compiler finds reads by reading the function's code, not by running it. The argument must
be a string literal, or source() with string literals; anything else is PLK607. A read inside
a helper function outside the model function is not seen, and fails at run time with PLX123.
A factory that builds models declares the input in its decorator and reads the alias:
from pipeloom import model, ref
def latest(name, upstream):
@model(name=name, inputs={"src": ref(upstream)})
def body(ctx):
return ctx.read("src")
return body
ctx.spark¶
On the Spark engine ctx.spark is a thin wrapper around the session (see the
Spark plugin). table(name) reads an input. sql(query)
replaces each model or source name in the query with its physical name on the current target, so
the same code runs on the filesystem catalog and on Unity Catalog:
@model
def revenue_by_nation(ctx):
return ctx.spark.sql("""
select n.n_name as nation, sum(l.net_revenue) as revenue
from lineitem_revenue l
join orders_enriched o on l.order_key = o.order_key
join tpch.nation n on o.nation = n.n_name
group by n.n_name
""")
- A one-part name that is an alias of the model, a model or a source table is an input; a
two-part name
group.tableof a declared source is an input. Other names (three-part names, other schemas, temp views) pass through and declare nothing. Names compare case-insensitively. - A CTE named like a model is the CTE. Statements that are not queries pass through untouched.
- The incremental filter is never applied: filter with
ctx.lower_boundorctx.interval. ctx.sparkreturns native Spark objects whateverframe_apisays, so a model that uses it is not portable. On another engine it is PLC515.