---
url: /dev/api/sklink.primitives.md
description: >-
  API reference of sklink.primitives. Aggregation primitives: what an expression
  computes over related rows.
---

::: v-pre

# `sklink.primitives` {#sklink.primitives}

Aggregation primitives: what an expression computes over related rows.

**Classes:**

* [**KernelContext**](#sklink.primitives.KernelContext): Extra information a kernel may need besides the rows it aggregates.
* [**Primitive**](#sklink.primitives.Primitive): Description of an aggregation primitive.

**Functions:**

* [**default_primitives**](#sklink.primitives.default_primitives): Primitives generated by default for a column of the given semantic type.
* [**get_kernel**](#sklink.primitives.get_kernel): Return the implementation of a primitive for a backend (NumPy fallback).
* [**get_primitive**](#sklink.primitives.get_primitive): Return a registered primitive by name.
* [**list_primitives**](#sklink.primitives.list_primitives): Names of the registered primitives, sorted.
* [**register_primitive**](#sklink.primitives.register_primitive): Register a primitive and its implementation per backend.

## `sklink.primitives.KernelContext` {#sklink.primitives.KernelContext}

```python
KernelContext(all_group: np.ndarray, cutoff_seconds: np.ndarray | None = None, row_seconds: np.ndarray | None = None) -> None
```

Extra information a kernel may need besides the rows it aggregates.

**Attributes:**

* [**all_group**](#sklink.primitives.KernelContext.all_group) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html)): Group of every reached row before the `where` conditions.
* [**cutoff_seconds**](#sklink.primitives.KernelContext.cutoff_seconds) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html) | None): Cutoff of every group in seconds since the epoch, in temporal mode.
* [**row_seconds**](#sklink.primitives.KernelContext.row_seconds) ([ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html) | None): Time of every aggregated row in seconds since the epoch, aligned with

### `sklink.primitives.KernelContext.all_group` {#sklink.primitives.KernelContext.all_group}

```python
all_group: np.ndarray
```

Group of every reached row before the `where` conditions.

### `sklink.primitives.KernelContext.cutoff_seconds` {#sklink.primitives.KernelContext.cutoff_seconds}

```python
cutoff_seconds: np.ndarray | None = None
```

Cutoff of every group in seconds since the epoch, in temporal mode.

### `sklink.primitives.KernelContext.row_seconds` {#sklink.primitives.KernelContext.row_seconds}

```python
row_seconds: np.ndarray | None = None
```

Time of every aggregated row in seconds since the epoch, aligned with
`group` (and `values`); given in temporal mode to primitives with
`requires_time` on a table with a time column, `None` otherwise.

## `sklink.primitives.Primitive` {#sklink.primitives.Primitive}

```python
Primitive(name: str, input_types: frozenset[str], output_type: str, empty_value: Literal['zero', 'false', 'missing'], decomposable: bool, requires_time: bool = False, params: tuple[str, ...] = (), needs_where: bool = False) -> None
```

Description of an aggregation primitive.

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

**Parameters:**

* **name** ([str](https://docs.python.org/3/library/stdtypes.html#str)): Lowercase identifier used in canonical names.
* **input_types** (frozenset of str): Semantic types of the aggregated column it accepts, or `{"rows"}` for
  primitives that work on rows without a column (`count`).
* **output_type** ([str](https://docs.python.org/3/library/stdtypes.html#str)): Semantic type of the result, or `"same"` for the input type.
* **empty_value** ((zero, false, missing)): Result when no row is reached.
* **decomposable** ([bool](https://docs.python.org/3/library/functions.html#bool)): Whether it can be computed from cumulative sums; only those are combined
  with learned conditions by relational trees.
* **requires_time** ([bool](https://docs.python.org/3/library/functions.html#bool)): Whether it needs temporal mode.
* **params** (tuple of str): Names of the parameters it takes, such as `("q",)`.
* **needs_where** ([bool](https://docs.python.org/3/library/functions.html#bool)): Whether it is meaningless without conditions (`proportion`).

**Functions:**

* [**result_type**](#sklink.primitives.Primitive.result_type): Semantic type of the output for a given input type.

**Attributes:**

* [**decomposable**](#sklink.primitives.Primitive.decomposable) ([bool](https://docs.python.org/3/library/functions.html#bool)):
* [**empty_value**](#sklink.primitives.Primitive.empty_value) ([Literal](https://docs.python.org/3/library/typing.html#typing.Literal)\['zero', 'false', 'missing']):
* [**input_types**](#sklink.primitives.Primitive.input_types) ([frozenset](https://docs.python.org/3/library/stdtypes.html#frozenset)\[[str](https://docs.python.org/3/library/stdtypes.html#str)]):
* [**name**](#sklink.primitives.Primitive.name) ([str](https://docs.python.org/3/library/stdtypes.html#str)):
* [**needs_where**](#sklink.primitives.Primitive.needs_where) ([bool](https://docs.python.org/3/library/functions.html#bool)):
* [**output_type**](#sklink.primitives.Primitive.output_type) ([str](https://docs.python.org/3/library/stdtypes.html#str)):
* [**params**](#sklink.primitives.Primitive.params) ([tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[str](https://docs.python.org/3/library/stdtypes.html#str), ...]):
* [**requires_time**](#sklink.primitives.Primitive.requires_time) ([bool](https://docs.python.org/3/library/functions.html#bool)):
* [**takes_column**](#sklink.primitives.Primitive.takes_column) ([bool](https://docs.python.org/3/library/functions.html#bool)): Whether the primitive aggregates a column rather than counting rows.

### `sklink.primitives.Primitive.decomposable` {#sklink.primitives.Primitive.decomposable}

```python
decomposable: bool
```

### `sklink.primitives.Primitive.empty_value` {#sklink.primitives.Primitive.empty_value}

```python
empty_value: Literal['zero', 'false', 'missing']
```

### `sklink.primitives.Primitive.input_types` {#sklink.primitives.Primitive.input_types}

```python
input_types: frozenset[str]
```

### `sklink.primitives.Primitive.name` {#sklink.primitives.Primitive.name}

```python
name: str
```

### `sklink.primitives.Primitive.needs_where` {#sklink.primitives.Primitive.needs_where}

```python
needs_where: bool = False
```

### `sklink.primitives.Primitive.output_type` {#sklink.primitives.Primitive.output_type}

```python
output_type: str
```

### `sklink.primitives.Primitive.params` {#sklink.primitives.Primitive.params}

```python
params: tuple[str, ...] = ()
```

### `sklink.primitives.Primitive.requires_time` {#sklink.primitives.Primitive.requires_time}

```python
requires_time: bool = False
```

### `sklink.primitives.Primitive.result_type` {#sklink.primitives.Primitive.result_type}

```python
result_type(input_type: str | None) -> str
```

Semantic type of the output for a given input type.

### `sklink.primitives.Primitive.takes_column` {#sklink.primitives.Primitive.takes_column}

```python
takes_column: bool
```

Whether the primitive aggregates a column rather than counting rows.

## `sklink.primitives.default_primitives` {#sklink.primitives.default_primitives}

```python
default_primitives(input_type: str) -> tuple[str, ...]
```

Primitives generated by default for a column of the given semantic type.

`"rows"` gives the row primitives. Primitives that require time
(`time_since_*`, `last` and `trend`) are used only in temporal mode
and on tables with a time column; the caller drops them otherwise.

## `sklink.primitives.get_kernel` {#sklink.primitives.get_kernel}

```python
get_kernel(name: str, backend: str = 'numpy') -> Kernel
```

Return the implementation of a primitive for a backend (NumPy fallback).

## `sklink.primitives.get_primitive` {#sklink.primitives.get_primitive}

```python
get_primitive(name: str) -> Primitive
```

Return a registered primitive by name.

## `sklink.primitives.list_primitives` {#sklink.primitives.list_primitives}

```python
list_primitives() -> list[str]
```

Names of the registered primitives, sorted.

## `sklink.primitives.register_primitive` {#sklink.primitives.register_primitive}

```python
register_primitive(primitive: Primitive, *, backends: Mapping[str, Kernel], overwrite: bool = False) -> None
```

Register a primitive and its implementation per backend.

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

**Parameters:**

* **primitive** ([Primitive](#sklink.primitives.Primitive)): The description.
* **backends** (mapping of str to callable): Implementation for each backend. A `"numpy"` implementation is
  required: it is the reference every backend can fall back on.
* **overwrite** ([bool](https://docs.python.org/3/library/functions.html#bool)): Replace an existing primitive with the same name.

:::
