---
url: /stable/api/sklink.expressions.md
description: >-
  API reference of sklink.expressions. Expressions: the shared language of
  relational features.
---

::: v-pre

# `sklink.expressions` {#sklink.expressions}

Expressions: the shared language of relational features.

An expression describes one value per sample computed by following
relationships from the entity table, such as `mean(orders.amount)` or
`count(orders[type == 'express'])`. Featurizers, trees, inspection and
persistence all speak in expressions; the engine evaluates them.

**Classes:**

* [**Aggregate**](#sklink.expressions.Aggregate): A primitive applied to the rows reached through a `to_many` path.
* [**Attribute**](#sklink.expressions.Attribute): A column of the entity table, or of a table reached with `to_one` hops.
* [**Condition**](#sklink.expressions.Condition): Filter on the rows of the last table of a path. Conditions are and-ed.
* [**Expression**](#sklink.expressions.Expression): Base class of every expression.
* [**Hop**](#sklink.expressions.Hop): One step along a relationship.
* [**LeafSum**](#sklink.expressions.LeafSum): Learned weights of the rows reached through a `to_many` path, summed.
* [**Path**](#sklink.expressions.Path): Sequence of hops starting at the entity table. Empty means the entity itself.
* [**SampleFeature**](#sklink.expressions.SampleFeature): A column of the population table `X` itself.
* [**Window**](#sklink.expressions.Window): Time window ending at the cutoff: `cutoff - duration <= time < cutoff`.

**Functions:**

* [**enumerate_expressions**](#sklink.expressions.enumerate_expressions): List the default unconditioned expressions for an entity.
* [**enumerate_lookups**](#sklink.expressions.enumerate_lookups): List the lookups that conditions on the rows of `path` may follow.
* [**enumerate_paths**](#sklink.expressions.enumerate_paths): List every path from `entity` allowed by the visibility rules.
* [**expression_from_dict**](#sklink.expressions.expression_from_dict): Rebuild an expression from `Expression.to_dict` output.
* [**format_expression**](#sklink.expressions.format_expression): Canonical name of an expression.
* [**make_hop**](#sklink.expressions.make_hop): Build a hop from `current` along `relationship`, deciding its spelling.
* [**output_type**](#sklink.expressions.output_type): Semantic type of the value an expression produces.
* [**parse**](#sklink.expressions.parse): Read an expression from its canonical name.
* [**validate_expression**](#sklink.expressions.validate_expression): Check that an expression can be evaluated on a database with `schema`.

## `sklink.expressions.Aggregate` {#sklink.expressions.Aggregate}

```python
Aggregate(primitive: str, path: Path, column: str | None = None, where: tuple[Condition, ...] = (), window: Window | None = None, params: tuple[tuple[str, Scalar], ...] = ()) -> None
```

Bases: [Expression](#sklink.expressions.Expression)

A primitive applied to the rows reached through a `to_many` path.

**Functions:**

* [**columns**](#sklink.expressions.Aggregate.columns): Return the aggregated column and the condition columns.
* [**relationships**](#sklink.expressions.Aggregate.relationships): Relationships of the path and of the condition lookups.
* [**tables**](#sklink.expressions.Aggregate.tables): Tables of the path and of the condition lookups.
* [**to_dict**](#sklink.expressions.Aggregate.to_dict): JSON-serializable form.
* [**with_where**](#sklink.expressions.Aggregate.with_where): Return the same aggregate with additional conditions.

**Attributes:**

* [**column**](#sklink.expressions.Aggregate.column) ([str](https://docs.python.org/3/library/stdtypes.html#str) | None):
* [**name**](#sklink.expressions.Aggregate.name) ([str](https://docs.python.org/3/library/stdtypes.html#str)): Canonical, stable name. See [`sklink.expressions.parse`](#sklink.expressions.parse).
* [**params**](#sklink.expressions.Aggregate.params) ([tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[str](https://docs.python.org/3/library/stdtypes.html#str), Scalar], ...]):
* [**path**](#sklink.expressions.Aggregate.path) ([Path](#sklink.expressions.Path)):
* [**primitive**](#sklink.expressions.Aggregate.primitive) ([str](https://docs.python.org/3/library/stdtypes.html#str)):
* [**where**](#sklink.expressions.Aggregate.where) ([tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[Condition](#sklink.expressions.Condition), ...]):
* [**window**](#sklink.expressions.Aggregate.window) ([Window](#sklink.expressions.Window) | None):

### `sklink.expressions.Aggregate.column` {#sklink.expressions.Aggregate.column}

```python
column: str | None = None
```

### `sklink.expressions.Aggregate.columns` {#sklink.expressions.Aggregate.columns}

```python
columns(entity: str) -> frozenset[tuple[str, str]]
```

Return the aggregated column and the condition columns.

Condition columns of a lookup belong to the table the lookup reaches.

### `sklink.expressions.Aggregate.name` {#sklink.expressions.Aggregate.name}

```python
name: str
```

Canonical, stable name. See [`sklink.expressions.parse`](#sklink.expressions.parse).

### `sklink.expressions.Aggregate.params` {#sklink.expressions.Aggregate.params}

```python
params: tuple[tuple[str, Scalar], ...] = field(default=(), compare=False)
```

### `sklink.expressions.Aggregate.path` {#sklink.expressions.Aggregate.path}

```python
path: Path
```

### `sklink.expressions.Aggregate.primitive` {#sklink.expressions.Aggregate.primitive}

```python
primitive: str
```

### `sklink.expressions.Aggregate.relationships` {#sklink.expressions.Aggregate.relationships}

```python
relationships() -> frozenset[str]
```

Relationships of the path and of the condition lookups.

### `sklink.expressions.Aggregate.tables` {#sklink.expressions.Aggregate.tables}

```python
tables(entity: str) -> frozenset[str]
```

Tables of the path and of the condition lookups.

### `sklink.expressions.Aggregate.to_dict` {#sklink.expressions.Aggregate.to_dict}

```python
to_dict() -> dict[str, Any]
```

JSON-serializable form.

### `sklink.expressions.Aggregate.where` {#sklink.expressions.Aggregate.where}

```python
where: tuple[Condition, ...] = ()
```

### `sklink.expressions.Aggregate.window` {#sklink.expressions.Aggregate.window}

```python
window: Window | None = None
```

### `sklink.expressions.Aggregate.with_where` {#sklink.expressions.Aggregate.with_where}

```python
with_where(*conditions: Condition) -> Aggregate
```

Return the same aggregate with additional conditions.

## `sklink.expressions.Attribute` {#sklink.expressions.Attribute}

```python
Attribute(path: Path, column: str) -> None
```

Bases: [Expression](#sklink.expressions.Expression)

A column of the entity table, or of a table reached with `to_one` hops.

**Functions:**

* [**columns**](#sklink.expressions.Attribute.columns): Return the column read.
* [**relationships**](#sklink.expressions.Attribute.relationships): Relationships followed by the expression.
* [**tables**](#sklink.expressions.Attribute.tables): Tables read by the expression.
* [**to_dict**](#sklink.expressions.Attribute.to_dict): JSON-serializable form.

**Attributes:**

* [**column**](#sklink.expressions.Attribute.column) ([str](https://docs.python.org/3/library/stdtypes.html#str)):
* [**name**](#sklink.expressions.Attribute.name) ([str](https://docs.python.org/3/library/stdtypes.html#str)): Canonical, stable name. See [`sklink.expressions.parse`](#sklink.expressions.parse).
* [**path**](#sklink.expressions.Attribute.path) ([Path](#sklink.expressions.Path)):

### `sklink.expressions.Attribute.column` {#sklink.expressions.Attribute.column}

```python
column: str
```

### `sklink.expressions.Attribute.columns` {#sklink.expressions.Attribute.columns}

```python
columns(entity: str) -> frozenset[tuple[str, str]]
```

Return the column read.

### `sklink.expressions.Attribute.name` {#sklink.expressions.Attribute.name}

```python
name: str
```

Canonical, stable name. See [`sklink.expressions.parse`](#sklink.expressions.parse).

### `sklink.expressions.Attribute.path` {#sklink.expressions.Attribute.path}

```python
path: Path
```

### `sklink.expressions.Attribute.relationships` {#sklink.expressions.Attribute.relationships}

```python
relationships() -> frozenset[str]
```

Relationships followed by the expression.

### `sklink.expressions.Attribute.tables` {#sklink.expressions.Attribute.tables}

```python
tables(entity: str) -> frozenset[str]
```

Tables read by the expression.

### `sklink.expressions.Attribute.to_dict` {#sklink.expressions.Attribute.to_dict}

```python
to_dict() -> dict[str, Any]
```

JSON-serializable form.

## `sklink.expressions.Condition` {#sklink.expressions.Condition}

```python
Condition(column: str, op: Operator, value: Scalar | tuple[Scalar, ...] | None = None, lookup: Path = Path()) -> None
```

Filter on the rows of the last table of a path. Conditions are and-ed.

The column is one of the last table itself, or, with `lookup`, a column
of the row reached from each of its rows by following many-to-one hops (a
*lookup*, such as the product of an order line). A row without such a
parent row, or whose parent is not visible at the cutoff, has a missing
value there: only `is_null` holds for it.

**Functions:**

* [**from_dict**](#sklink.expressions.Condition.from_dict): Inverse of `to_dict`.
* [**sort_key**](#sklink.expressions.Condition.sort_key): Canonical ordering: lookup, column, operator, then value.
* [**table**](#sklink.expressions.Condition.table): Table of the column, for rows of table `start`.
* [**to_dict**](#sklink.expressions.Condition.to_dict): JSON-serializable form; `"lookup"` only when there is one.

**Attributes:**

* [**column**](#sklink.expressions.Condition.column) ([str](https://docs.python.org/3/library/stdtypes.html#str)):
* [**lookup**](#sklink.expressions.Condition.lookup) ([Path](#sklink.expressions.Path)):
* [**op**](#sklink.expressions.Condition.op) (Operator):
* [**value**](#sklink.expressions.Condition.value) (Scalar | [tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[Scalar, ...] | None):

### `sklink.expressions.Condition.column` {#sklink.expressions.Condition.column}

```python
column: str
```

### `sklink.expressions.Condition.from_dict` {#sklink.expressions.Condition.from_dict}

```python
from_dict(data: Mapping[str, Any]) -> Condition
```

Inverse of `to_dict`.

### `sklink.expressions.Condition.lookup` {#sklink.expressions.Condition.lookup}

```python
lookup: Path = Path()
```

### `sklink.expressions.Condition.op` {#sklink.expressions.Condition.op}

```python
op: Operator
```

### `sklink.expressions.Condition.sort_key` {#sklink.expressions.Condition.sort_key}

```python
sort_key() -> tuple[Any, ...]
```

Canonical ordering: lookup, column, operator, then value.

Conditions without a lookup come first, in their usual order.

### `sklink.expressions.Condition.table` {#sklink.expressions.Condition.table}

```python
table(start: str) -> str
```

Table of the column, for rows of table `start`.

### `sklink.expressions.Condition.to_dict` {#sklink.expressions.Condition.to_dict}

```python
to_dict() -> dict[str, Any]
```

JSON-serializable form; `"lookup"` only when there is one.

### `sklink.expressions.Condition.value` {#sklink.expressions.Condition.value}

```python
value: Scalar | tuple[Scalar, ...] | None = field(default=None, compare=False)
```

## `sklink.expressions.Expression` {#sklink.expressions.Expression}

Base class of every expression.

Expressions are immutable values: equal content means equal name and hash.
They hold no data and no learned statistics, only explicit constants.

**Functions:**

* [**columns**](#sklink.expressions.Expression.columns): `(table, column)` pairs read by the expression (keys excluded).
* [**relationships**](#sklink.expressions.Expression.relationships): Relationships followed by the expression.
* [**tables**](#sklink.expressions.Expression.tables): Tables read by the expression.
* [**to_dict**](#sklink.expressions.Expression.to_dict): JSON-serializable form; see `expression_from_dict`.

**Attributes:**

* [**name**](#sklink.expressions.Expression.name) ([str](https://docs.python.org/3/library/stdtypes.html#str)): Canonical, stable name. See [`sklink.expressions.parse`](#sklink.expressions.parse).
* [**path**](#sklink.expressions.Expression.path) ([Path](#sklink.expressions.Path)): Path from the entity table.

### `sklink.expressions.Expression.columns` {#sklink.expressions.Expression.columns}

```python
columns(entity: str) -> frozenset[tuple[str, str]]
```

`(table, column)` pairs read by the expression (keys excluded).

### `sklink.expressions.Expression.name` {#sklink.expressions.Expression.name}

```python
name: str
```

Canonical, stable name. See [`sklink.expressions.parse`](#sklink.expressions.parse).

### `sklink.expressions.Expression.path` {#sklink.expressions.Expression.path}

```python
path: Path
```

Path from the entity table.

### `sklink.expressions.Expression.relationships` {#sklink.expressions.Expression.relationships}

```python
relationships() -> frozenset[str]
```

Relationships followed by the expression.

### `sklink.expressions.Expression.tables` {#sklink.expressions.Expression.tables}

```python
tables(entity: str) -> frozenset[str]
```

Tables read by the expression.

### `sklink.expressions.Expression.to_dict` {#sklink.expressions.Expression.to_dict}

```python
to_dict() -> dict[str, Any]
```

JSON-serializable form; see `expression_from_dict`.

## `sklink.expressions.Hop` {#sklink.expressions.Hop}

```python
Hop(relationship: str, direction: Direction, target: str, qualified: bool = False, self_link: bool = False) -> None
```

One step along a relationship.

`to_many` goes from a parent row to its child rows; `to_one` goes from a
child row to its parent row. `target` is the table reached and
`qualified` tells whether the canonical name must spell the relationship
because several relationships lead to the same table.

**Attributes:**

* [**direction**](#sklink.expressions.Hop.direction) (Direction):
* [**qualified**](#sklink.expressions.Hop.qualified) ([bool](https://docs.python.org/3/library/functions.html#bool)):
* [**relationship**](#sklink.expressions.Hop.relationship) ([str](https://docs.python.org/3/library/stdtypes.html#str)):
* [**self_link**](#sklink.expressions.Hop.self_link) ([bool](https://docs.python.org/3/library/functions.html#bool)):
* [**target**](#sklink.expressions.Hop.target) ([str](https://docs.python.org/3/library/stdtypes.html#str)):

### `sklink.expressions.Hop.direction` {#sklink.expressions.Hop.direction}

```python
direction: Direction
```

### `sklink.expressions.Hop.qualified` {#sklink.expressions.Hop.qualified}

```python
qualified: bool = field(default=False, compare=False)
```

### `sklink.expressions.Hop.relationship` {#sklink.expressions.Hop.relationship}

```python
relationship: str
```

### `sklink.expressions.Hop.self_link` {#sklink.expressions.Hop.self_link}

```python
self_link: bool = field(default=False, compare=False)
```

### `sklink.expressions.Hop.target` {#sklink.expressions.Hop.target}

```python
target: str = field(compare=False)
```

## `sklink.expressions.LeafSum` {#sklink.expressions.LeafSum}

```python
LeafSum(primitive: str, path: Path, terms: tuple[tuple[Aggregate, float], ...]) -> None
```

Bases: [Expression](#sklink.expressions.Expression)

Learned weights of the rows reached through a `to_many` path, summed.

The value of an entity is `sum_l weight_l * count(path[where_l])` over
the `terms` (an `Aggregate` of primitive `"count"` on `path` and
its weight), divided by `count(path)` for the primitive `"mean"` (and
missing without rows). The terms of a fitted model are the leaves of a
small tree over the rows of the path, as in getML's Relboost: each row
gets the weight of its leaf, and the entity the sum or mean of them.
Its name, such as `mean_weights(orders: +0.4 [type == 'pickup'] -0.1 [type in ('economy', 'express', 'standard')])`, is written to be read:
[`parse`](#sklink.expressions.parse) does not read it back, and
`expression_from_dict` rebuilds it from `to_dict`.

**Functions:**

* [**columns**](#sklink.expressions.LeafSum.columns): Return the condition columns of the terms.
* [**relationships**](#sklink.expressions.LeafSum.relationships): Relationships of the path and of the condition lookups.
* [**tables**](#sklink.expressions.LeafSum.tables): Tables of the path and of the condition lookups.
* [**to_dict**](#sklink.expressions.LeafSum.to_dict): JSON-serializable form.

**Attributes:**

* [**name**](#sklink.expressions.LeafSum.name) ([str](https://docs.python.org/3/library/stdtypes.html#str)): Canonical, stable name. See [`sklink.expressions.parse`](#sklink.expressions.parse).
* [**path**](#sklink.expressions.LeafSum.path) ([Path](#sklink.expressions.Path)):
* [**primitive**](#sklink.expressions.LeafSum.primitive) ([str](https://docs.python.org/3/library/stdtypes.html#str)):
* [**terms**](#sklink.expressions.LeafSum.terms) ([tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[Aggregate](#sklink.expressions.Aggregate), [float](https://docs.python.org/3/library/functions.html#float)], ...]):

### `sklink.expressions.LeafSum.columns` {#sklink.expressions.LeafSum.columns}

```python
columns(entity: str) -> frozenset[tuple[str, str]]
```

Return the condition columns of the terms.

### `sklink.expressions.LeafSum.name` {#sklink.expressions.LeafSum.name}

```python
name: str
```

Canonical, stable name. See [`sklink.expressions.parse`](#sklink.expressions.parse).

### `sklink.expressions.LeafSum.path` {#sklink.expressions.LeafSum.path}

```python
path: Path
```

### `sklink.expressions.LeafSum.primitive` {#sklink.expressions.LeafSum.primitive}

```python
primitive: str
```

### `sklink.expressions.LeafSum.relationships` {#sklink.expressions.LeafSum.relationships}

```python
relationships() -> frozenset[str]
```

Relationships of the path and of the condition lookups.

### `sklink.expressions.LeafSum.tables` {#sklink.expressions.LeafSum.tables}

```python
tables(entity: str) -> frozenset[str]
```

Tables of the path and of the condition lookups.

### `sklink.expressions.LeafSum.terms` {#sklink.expressions.LeafSum.terms}

```python
terms: tuple[tuple[Aggregate, float], ...]
```

### `sklink.expressions.LeafSum.to_dict` {#sklink.expressions.LeafSum.to_dict}

```python
to_dict() -> dict[str, Any]
```

JSON-serializable form.

## `sklink.expressions.Path` {#sklink.expressions.Path}

```python
Path(hops: tuple[Hop, ...] = ()) -> None
```

Sequence of hops starting at the entity table. Empty means the entity itself.

**Functions:**

* [**from_list**](#sklink.expressions.Path.from_list): Inverse of `to_list`.
* [**last_table**](#sklink.expressions.Path.last_table): Table reached at the end of the path.
* [**tables**](#sklink.expressions.Path.tables): Tables visited, starting with `entity`.
* [**to_list**](#sklink.expressions.Path.to_list): JSON-serializable form.
* [**validate**](#sklink.expressions.Path.validate): Check that each hop starts where the previous one ended.

**Attributes:**

* [**depth**](#sklink.expressions.Path.depth) ([int](https://docs.python.org/3/library/functions.html#int)): Number of hops.
* [**hops**](#sklink.expressions.Path.hops) ([tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[Hop](#sklink.expressions.Hop), ...]):
* [**is_to_one**](#sklink.expressions.Path.is_to_one) ([bool](https://docs.python.org/3/library/functions.html#bool)): Whether every hop is many-to-one (the path reaches at most one row).

### `sklink.expressions.Path.depth` {#sklink.expressions.Path.depth}

```python
depth: int
```

Number of hops.

### `sklink.expressions.Path.from_list` {#sklink.expressions.Path.from_list}

```python
from_list(data: list[Mapping[str, Any]]) -> Path
```

Inverse of `to_list`.

### `sklink.expressions.Path.hops` {#sklink.expressions.Path.hops}

```python
hops: tuple[Hop, ...] = ()
```

### `sklink.expressions.Path.is_to_one` {#sklink.expressions.Path.is_to_one}

```python
is_to_one: bool
```

Whether every hop is many-to-one (the path reaches at most one row).

### `sklink.expressions.Path.last_table` {#sklink.expressions.Path.last_table}

```python
last_table(entity: str) -> str
```

Table reached at the end of the path.

### `sklink.expressions.Path.tables` {#sklink.expressions.Path.tables}

```python
tables(entity: str) -> tuple[str, ...]
```

Tables visited, starting with `entity`.

### `sklink.expressions.Path.to_list` {#sklink.expressions.Path.to_list}

```python
to_list() -> list[dict[str, Any]]
```

JSON-serializable form.

### `sklink.expressions.Path.validate` {#sklink.expressions.Path.validate}

```python
validate(schema: Schema, entity: str) -> None
```

Check that each hop starts where the previous one ended.

## `sklink.expressions.SampleFeature` {#sklink.expressions.SampleFeature}

```python
SampleFeature(column: str) -> None
```

Bases: [Expression](#sklink.expressions.Expression)

A column of the population table `X` itself.

**Functions:**

* [**columns**](#sklink.expressions.SampleFeature.columns): No column of the database is read.
* [**relationships**](#sklink.expressions.SampleFeature.relationships): Relationships followed by the expression.
* [**tables**](#sklink.expressions.SampleFeature.tables): No table of the database is read.
* [**to_dict**](#sklink.expressions.SampleFeature.to_dict): JSON-serializable form.

**Attributes:**

* [**column**](#sklink.expressions.SampleFeature.column) ([str](https://docs.python.org/3/library/stdtypes.html#str)):
* [**name**](#sklink.expressions.SampleFeature.name) ([str](https://docs.python.org/3/library/stdtypes.html#str)): Canonical, stable name. See [`sklink.expressions.parse`](#sklink.expressions.parse).
* [**path**](#sklink.expressions.SampleFeature.path) ([Path](#sklink.expressions.Path)): Empty path: the value comes from `X`.

### `sklink.expressions.SampleFeature.column` {#sklink.expressions.SampleFeature.column}

```python
column: str
```

### `sklink.expressions.SampleFeature.columns` {#sklink.expressions.SampleFeature.columns}

```python
columns(entity: str) -> frozenset[tuple[str, str]]
```

No column of the database is read.

### `sklink.expressions.SampleFeature.name` {#sklink.expressions.SampleFeature.name}

```python
name: str
```

Canonical, stable name. See [`sklink.expressions.parse`](#sklink.expressions.parse).

### `sklink.expressions.SampleFeature.path` {#sklink.expressions.SampleFeature.path}

```python
path: Path
```

Empty path: the value comes from `X`.

### `sklink.expressions.SampleFeature.relationships` {#sklink.expressions.SampleFeature.relationships}

```python
relationships() -> frozenset[str]
```

Relationships followed by the expression.

### `sklink.expressions.SampleFeature.tables` {#sklink.expressions.SampleFeature.tables}

```python
tables(entity: str) -> frozenset[str]
```

No table of the database is read.

### `sklink.expressions.SampleFeature.to_dict` {#sklink.expressions.SampleFeature.to_dict}

```python
to_dict() -> dict[str, Any]
```

JSON-serializable form.

## `sklink.expressions.Window` {#sklink.expressions.Window}

```python
Window(duration: dt.timedelta) -> None
```

Time window ending at the cutoff: `cutoff - duration <= time < cutoff`.

**Attributes:**

* [**duration**](#sklink.expressions.Window.duration) ([timedelta](https://docs.python.org/3/library/datetime.html#datetime.timedelta)):

### `sklink.expressions.Window.duration` {#sklink.expressions.Window.duration}

```python
duration: dt.timedelta
```

## `sklink.expressions.enumerate_expressions` {#sklink.expressions.enumerate_expressions}

```python
enumerate_expressions(schema: Schema, *, entity: str, max_relationship_depth: int, primitives: str | Sequence[str] = 'default', allow_reverse_paths: bool = False, temporal: bool = False, max_lookup_depth: int = 0) -> list[Expression]
```

List the default unconditioned expressions for an entity.

Attributes come from the entity table and from tables reached with
many-to-one hops; aggregates from every path with a one-to-many hop, with the
primitives that match each column's semantic type. Primitives that require
time are used only with `temporal=True` and on tables with a time column.

With `max_lookup_depth > 0`, the columns of the tables that the rows of
the longest paths look up (see `enumerate_lookups`) are aggregated
too, such as `mean(orders.order_items.products.price)` at
`max_relationship_depth=2`: a lookup reaches one row per row, so it does
not count as a relationship hop. Primitives that require time are not used
on them. Expressions with
conditions on category values are added by estimators, because they depend
on vocabularies learned from training data.

**Returns:**

* **expressions** (list of Expression): Ordered by path depth, then canonical name. Deterministic.

## `sklink.expressions.enumerate_lookups` {#sklink.expressions.enumerate_lookups}

```python
enumerate_lookups(schema: Schema, path: Path, *, entity: str, max_relationship_depth: int, max_lookup_depth: int, allow_reverse_paths: bool = False) -> list[Path]
```

List the lookups that conditions on the rows of `path` may follow.

A lookup is a chain of one to `max_lookup_depth` many-to-one hops from
the last table of `path`, such as from an order line to its product. It
reaches at most one row per row, so it never adds rows to an aggregate and
does not count as a hop of `max_relationship_depth`.

Lookups continue a path where `max_relationship_depth` stops: only paths
of exactly that depth get them. A shorter path reaches the same parent
tables as a longer path with to_one hops, whose conditions are on its last
table, so a lookup there would repeat a question that is already asked.
As in `enumerate_paths`, a lookup never walks back the relationship
the path just followed down (the row it came from) and, unless
`allow_reverse_paths=True`, never reaches the entity table, whose other
rows are other samples.

**Returns:**

* **lookups** (list of Path): Non-empty to_one paths relative to the last table of `path`, ordered
  by depth, then canonical spelling. Empty for a path without a
  one-to-many hop or shorter than `max_relationship_depth`.

## `sklink.expressions.enumerate_paths` {#sklink.expressions.enumerate_paths}

```python
enumerate_paths(schema: Schema, *, entity: str, max_relationship_depth: int, allow_reverse_paths: bool = False) -> list[Path]
```

List every path from `entity` allowed by the visibility rules.

By default a path goes down one-to-many relationships first and then only up
many-to-one relationships: it never returns to the entity table and never
goes down again after going up. This keeps a sample from reading rows of
other entities through a shared table (for example, the other customers who
bought the same product). `allow_reverse_paths=True` lifts the rule except
for immediately walking back the relationship just followed.

The result is ordered by depth, then canonical spelling, and includes the
empty path.

## `sklink.expressions.expression_from_dict` {#sklink.expressions.expression_from_dict}

```python
expression_from_dict(data: Mapping[str, Any]) -> Expression
```

Rebuild an expression from `Expression.to_dict` output.

## `sklink.expressions.format_expression` {#sklink.expressions.format_expression}

```python
format_expression(expression: Expression) -> str
```

Canonical name of an expression.

## `sklink.expressions.make_hop` {#sklink.expressions.make_hop}

```python
make_hop(schema: Schema, current: str, relationship: str, direction: str) -> Hop
```

Build a hop from `current` along `relationship`, deciding its spelling.

## `sklink.expressions.output_type` {#sklink.expressions.output_type}

```python
output_type(expression: Expression, schema: Schema | None, entity: str | None) -> str
```

Semantic type of the value an expression produces.

## `sklink.expressions.parse` {#sklink.expressions.parse}

```python
parse(name: str, schema: Schema, *, entity: str) -> Expression
```

Read an expression from its canonical name.

**Guides:** [Custom primitives and expressions](../tutorials/custom-primitives)

**Parameters:**

* **name** ([str](https://docs.python.org/3/library/stdtypes.html#str)): Canonical name, such as `"count(orders[type == 'express'])"`.
* **schema** ([Schema](./sklink.database#sklink.database.Schema)): Schema the expression refers to.
* **entity** ([str](https://docs.python.org/3/library/stdtypes.html#str)): Table the samples belong to.

**Returns:**

* **expression** ([Expression](#sklink.expressions.Expression)):

**Raises:**

* [ValueError](https://docs.python.org/3/library/exceptions.html#ValueError): If the text does not follow the grammar; the message gives the position.
* [SchemaError](./sklink.exceptions#sklink.exceptions.SchemaError): If a table, column or relationship does not exist.

## `sklink.expressions.validate_expression` {#sklink.expressions.validate_expression}

```python
validate_expression(expression: Expression, schema: Schema, entity: str, *, temporal: bool = False) -> None
```

Check that an expression can be evaluated on a database with `schema`.

**Raises:**

* [SchemaError](./sklink.exceptions#sklink.exceptions.SchemaError): If a relationship, table or column is missing, a column type is not
  accepted by the primitive, or time is used without temporal mode.

:::
