Skip to content

yggdrasil.execution.expr.backends.python

python

Pure-Python compiler for :class:Expression trees.

:func:to_python returns a callable that takes a row mapping (Mapping[str, Any]) and evaluates the expression against it. The result is the natural Python value of the expression — a bool for predicates, a number / string / etc. for scalar expressions.

Filtering helper

For row collections, :func:filter_rows calls the compiled predicate against each row and yields the matches:

>>> from yggdrasil.execution.expr import col
>>> rows = [{"x": 1}, {"x": 2}, {"x": 3}]
>>> from yggdrasil.execution.expr.backends.python import filter_rows
>>> list(filter_rows(col("x") > 1, rows))
[{'x': 2}, {'x': 3}]

NULL semantics

By default, missing columns evaluate to :data:None (SQL three-valued logic): a comparison against None returns None, AND of None and False is False, AND of None and anything else is None. Predicate.to_python(strict=True) raises :class:KeyError instead — useful when the caller wants to detect schema drift loudly.

to_python

to_python(
    expr: Expression, *, strict: bool = False
) -> Callable[[Mapping[str, Any]], Any]

Compile expr to a callable.

Returned callable accepts a row mapping and returns the expression's value. strict=True makes missing columns raise :class:KeyError; the default returns None for them, matching SQL three-valued logic.

filter_rows

filter_rows(
    expr: Expression, rows: Iterable[Mapping[str, Any]], *, strict: bool = False
) -> Iterator[Mapping[str, Any]]

Yield rows from rows that satisfy expr (treated as a predicate).

A non-True evaluation result rejects the row — that includes False and SQL's UNKNOWN (None), so a predicate referencing a missing column rejects the row instead of silently passing it.