impulse_query_engine.analyze.query.optimizer.pushdown
Building blocks of Spark pushdown rules.
VARIANT_COL
Column tagging each channel-data row with the series variant it belongs to.
VARIANTS_COL
Column listing the variant ids a matched channel ships, one row per channel.
RAW_VARIANT
Variant id of the unmodified (raw) channel data.
PRESENCE_VARIANT
Variant id of the presence rows: one row per channel shipping a variant of a rule
that may emit nothing (:attr:PushdownRule.may_emit_nothing), so that its container
still reaches the UDF. No node reads these rows.
FACTOR_COL
Optional column of the variants frame: the channel's unit-conversion factor.
TIME_TYPES
Spark types of time columns the rules compute on: integral times convert to double
exactly as numpy converts them.
VALUE_TYPES
Spark types of value columns the rules compute on, as double like SampleSeries.
real_scalar
def real_scalar(value: Any) -> float | None
value as the float numpy computes with, or None unless Spark can use it as is.
Accepts integers and floats (Python or numpy, up to double precision); booleans are not numbers here.
check_name
def check_name(kind: str, name: str) -> str
Return name if it is a valid rule, rule column or run name, else raise.
Names are lowercase letters, digits and single underscores, starting with a
letter. Pipeline columns join them with __, which a name never contains,
so names of different rules cannot collide.
PushdownContext
class PushdownContext()
Plan-time facts about the channel data a pushdown would run on.
Arguments:
tstart_type, tend_type(pyspark.sql.types.DataType): Spark types of the channel-datatstart/tendcolumns.value_type(pyspark.sql.types.DataType): Spark type of the channel-data value column; defaults to double.
from_schema
def from_schema(schema: T.StructType, config: SolverConfig) -> PushdownContext
Build the context from the column-mapped channel-data schema.
numeric_times
def numeric_times() -> bool
Whether both time columns have a type of :data:TIME_TYPES.
Pushdown
class Pushdown()
A subtree that a :class:PushdownRule computes natively in Spark.
Expression nodes overload ==, so pushdowns compare by identity; use
:attr:variant_key to identify equivalent pushdowns.
A pushdown reads the samples of its channel, possibly absorbing pushed
operands into that pass, or computes on the result of another pushdown, its
:attr:source. The pipeline computes the former only.
Arguments:
rule(PushdownRule): The rule that matched the subtree.selector(TimeSeriesSelector): The physical channel selector the subtree reads.params(tuple of (str, Any)): Canonical, hashable operation parameters (e.g.(("sample_rate", 1e6),)).consumed(tuple of TimeSeriesSelector): Every selector occurrence inside the subtree (itsget_selectors()).source(Pushdown): The pushdown whose result this one computes on;Nonewhen it reads the channel's samples.
variant_key
def variant_key() -> tuple
Identity of the derived series, independent of the input channel.
describe
def describe() -> str
Human-readable rule(param=value, ...) label.
Operand
Classification of an operand of the node a rule matches: the selector of a
channel, the :class:Pushdown computing the operand, or None when the
operand is evaluated in pandas or is not an expression.
PushdownRule
class PushdownRule(abc.ABC)
Recognizes a subtree and computes it in Spark ahead of the pandas UDF.
Planning and decoding run in Python: :meth:match recognizes a node on the
driver from its operands' classification; :meth:decode builds, in the UDF,
the object the subtree evaluates to from its variant's derived rows. The
result must equal evaluating the subtree in pandas on the same channel data.
Only built-in rules can be decoded in the UDF (:func:~.query_optimizer.builtin_rules).
The Spark side is a set of hooks returning columns, evaluated by the shared
pushdown pipeline (:mod:.pipeline) for the samples of the rule's variants,
one series (channel and variant) at a time, with a :class:SeriesContext:
- :meth:
row_columnsper sample; - :meth:
keepselects the samples that go on; - :meth:
kept_columnsper kept sample; - :meth:
emitthe derived rows of a kept sample.
A rule that sets :attr:finishes also gets a second pass over its derived
rows, all of a series in order, once they are grouped by container
(:func:~.pipeline.finish_rows, with a :class:RowsContext):
- :meth:
finish_columnsper derived row; - :meth:
finish_keepselects the rows that are shipped; - :meth:
finish_rowthe shipped row.
Rules never handle DataFrames, so every rule shares the pipeline's reads and
shuffles. Derived rows have the channel-data shape
(container_id, channel_id, variant, tstart, tend, value).
name
Unique rule name, part of every :attr:Pushdown.variant_key; lowercase
letters, digits and single underscores (see :func:check_name).
ordered
Whether the rule needs its series in order (neighbours, runs, series
facts). Per-sample rules set False and skip the series scan.
may_emit_nothing
Whether a series may emit no derived rows although it has samples (e.g. a
comparison that never holds). The pipeline then ships a presence row per
channel (:data:PRESENCE_VARIANT), so the container still reaches the UDF.
Only ordered rules may set it (checked by :class:~.query_optimizer.QueryOptimizer).
finishes
Whether the rule has the finishing pass (:meth:finish_columns,
match
def match(node: TimeSeriesExpression, operand: Operand) -> Pushdown | None
Return the pushdown for node, or None if the rule does not apply.
Nodes are classified bottom-up: operand returns the classification of
any operand of node. A pushdown reading the channel's samples sets no
source; when it absorbs a pushed operand into that pass, its
consumed includes the operand's and :meth:supports covers both. A
pushdown computing on a pushed operand's result sets source to it.
Purely syntactic and driver-only: checks of the channel data belong in
supports
def supports(pushdown: Pushdown, context: PushdownContext) -> bool
Whether pushdown can run on channel data described by context.
result_type
def result_type(pushdown: Pushdown) -> type
Core-model class :meth:decode returns for pushdown.
needs_factor
def needs_factor(pushdown: Pushdown) -> bool
Whether pushdown computes on unit-converted values in Spark.
The pipeline then provides the channel's conversion factor, and
runs
def runs() -> Mapping[str, RunBreak]
Run definitions beyond the built-in continuous blocks, by name.
Each maps (prev, cur) samples to whether cur starts a new run;
row_columns
def row_columns(ctx: SeriesContext) -> Mapping[str, Column]
Columns computed for every sample, in order; refer to them via ctx.col.
Column names follow :func:check_name and only need to be unique within
the rule.
keep
def keep(ctx: SeriesContext) -> Column
Whether a sample can emit rows; the others are dropped early.
kept_columns
def kept_columns(ctx: SeriesContext) -> Mapping[str, Column]
Columns computed for the kept samples, in order (ctx.block_end available).
finish_columns
def finish_columns(ctx: RowsContext) -> Mapping[str, Column]
Columns computed for every derived row in the finishing pass, in order.
finish_keep
def finish_keep(ctx: RowsContext) -> Column
Whether a derived row is shipped after the finishing pass.
finish_row
def finish_row(ctx: RowsContext) -> Column
The shipped row: one :data:~.context.ELEMENT_TYPE struct.
Times left out keep the row's own tstart / tend; the value is the
element's.
emit
def emit(ctx: SeriesContext) -> Column
The derived rows of a kept sample.
Returns:
Column: An array of :data:~.context.ELEMENT_TYPE structs built with
:meth:SeriesContext.element; empty or null emits nothing. Times
left out keep the sample's own tstart / tend.
decode
def decode(pushed: PushedNode, cache: TimeSeriesCache) -> Any
Build a pushed-down node's result in the UDF from its variant's derived rows.
selector.build(cache, variant=pushed.variant) loads them like raw rows. Only
called when each selector's channel has derived rows in the container; otherwise
the node is evaluated normally.
emits_result_rows
def emits_result_rows(pushdown: Pushdown) -> bool
Whether pushdown's derived rows are its result's samples as they are.
True only if :meth:decode returns them unchanged as a :class:SampleSeries
(apart from the alias conversion factor). Calculated channels that are such a
pushdown as a whole are then emitted from Spark, without :meth:decode.
has_native_value
def has_native_value(pushdown: Pushdown) -> bool
Whether :meth:native_value can produce pushdown's solve() result in Spark.
A solve whose every selection is such a pushdown as a whole runs without the pandas UDF.
native_value
def native_value(pushdown: Pushdown, rows: Column) -> Column
pushdown's solve() result for one container, built in Spark.
Arguments:
pushdown(Pushdown): A pushdown for which :meth:has_native_valueholds.rows(Column): The series' shipped rows: an array of :data:~.context.ELEMENT_TYPEstructs in series order; empty when the container has none.
Returns:
Column: The value the UDF would emit for the selection, NaN encoded as the UDF's
Arrow output encodes it.