Expressions, filters and maps¶
Three processors evaluate an expression over telemetry:
Processor |
What it does |
|---|---|
|
computes one or more typed fields from expressions |
|
keeps the records a boolean expression selects |
|
projects, renames and drops fields, with optional computed fields |
All three take device_telemetry in and emit device_telemetry, so they may sit
anywhere a processor may: before or after a window aggregate or a fusion node,
or in a chain of their own.
Expressions are written in CEL (the Common Expression Language) under the
profile this page defines — dialect thinglogic-expr/v2. The same profile is
enforced by pipeline validation, by the deploy compiler, by the worker that
executes a deployed pipeline, and by the dry run, so an expression that
validates cannot mean something different at runtime.
Referring to fields¶
A field of the incoming telemetry payload is addressed as in.<field>:
in.value > 30.0 && in.metric == "temperature"
in is reserved: every in.<field> in the author’s text is bound to exactly one
flat variable before CEL sees it. The set of addressable fields is the payload
schema of the upstream port — the telemetry contract, or the output of an
intermediate expression / map node, including the fields it computes and
excluding the fields it drops or renames.
A reference to a field that is not on that input schema is refused with
expression_unknown_field; a field whose type makes the operation invalid is
refused with expression_type_mismatch. Both refusals point at the exact
character range in the author’s expression — see
Checking an expression.
Field names follow ^[A-Za-z_][A-Za-z0-9_]{0,63}$. A payload field whose name
does not match is simply not addressable through in. (it is still carried
through untouched).
Types¶
An expression is statically typed from the input schema and the CEL type inference rules. The JSON-Schema types map as follows:
Schema type |
Expression type |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
anything else / no type |
|
dyn is accepted where CEL can decide at evaluation time. The result of a
computed field must be representable on the telemetry payload: string, int,
double, bool, list, map, or null. A result that is not — a timestamp,
bytes, a map with non-string keys, a non-finite float — is a located
expression_type_unsupported at compile time when the compiler can see it, and
an evaluation error otherwise. A processor can never write a value the delivery
path cannot serialise.
Operators and functions¶
The CEL operator set is available within the profile:
arithmetic
+ - * / %, including+for string concatenation and list concatenation;comparison
== != < <= > >=;logical
&& || !and the ternarycond ? a : b;list membership:
in.metric in ["temperature", "humidity"];membership and indexing on maps and lists with
[];the CEL string functions
contains(),startsWith(),endsWith(),matches()andsize():
in.metric.startsWith("temp")
in.metric.contains("perature")
in.metric.endsWith("ture")
in.metric.matches("^temp.*$")
size(in.metric) > 3
A matches() pattern must be a string literal of at most 512 characters.
A dynamic pattern has no static bound, so it is refused with
expression_forbidden_construct rather than accepted with an unbounded cost.
The profile adds the trigonometric functions sin(), cos(), tan(),
asin(), acos() and atan(). Each takes a double (an INT argument is
accepted as well) and produces a double:
sin(in.value) > 0.0
cos(in.value) - atan(in.value)
They are pure: no clock, no I/O, no randomness. An argument outside a
function’s domain — asin(2.0), for example — produces a non-finite result,
which is an evaluation error on that record: the node counts it and the
pipeline continues, the same way a divide by zero is handled.
Time¶
Two implicit INT variables carry epoch milliseconds:
event_time— the record’s event instant;receipt_time— the instant the runtime processed it.
receipt_time - event_time > 60000
They are not part of the input schema, and a schema that declares either name is
refused, so a payload field can never shadow them. The wire’s ts field is an
ISO-8601 string and there is no permitted way to parse it into an instant in
this profile (the timestamp() / duration() constructors are forbidden); use
event_time for event-time logic.
Bounds¶
Evaluation is bounded statically, before anything runs:
Bound |
Value |
|---|---|
expression length |
4,096 characters |
expression tokens |
512 |
expression nesting depth |
32 |
|
512 characters |
computed fields per |
16 |
An expression over a bound is refused with expression_cost_exceeded, located
at the token where the budget is exhausted. The bound is the profile’s own
lexical budget: the CEL implementation used by the service exposes no cost model,
so the service does not pretend to expose one.
Forbidden constructs¶
These are refused with expression_forbidden_construct:
the CEL macros
all,exists,exists_one,map,filterandhas— the first five expand to comprehensions whose iteration count would escape the static token budget, and this profile has no absent-versus-null distinction forhasto test;the constructors
now(),timestamp()andduration()— their values are not representable on the pipeline payload;raw and bytes string literals (
r"…",b"…");a
matches()pattern that is not a string literal.
Runtime behaviour¶
Expression evaluation never raises into the pipeline. A failure on one
record — divide by zero, a dyn value that turned out to be the wrong type, a
non-finite result — is an error value for that field:
processor.expressionandprocessor.mapomit the field that failed, emit the record anyway withexpression_status: "error", and list every failed field inexpression_errors(field,code=expression_evaluation_error,detail). The record is not dropped and the pipeline is not stopped.processor.filterdrops the record when the expression isfalseor errors — a filter that cannot decide must not pass the record through as though it had. Both outcomes are counted separately, so a broken filter is visible in the node’s metrics rather than silently discarding data.
Per-node counters — records_seen, records_emitted, fields_computed,
evaluation_errors, and for map fields_renamed / fields_dropped, and for
filter filtered_out — are reported in the dry-run receipt and in the runtime
status beacon. The dry run and the worker run the same evaluator, so the counts
describe the same program.
The three processors¶
Compute fields — processor.expression¶
{"id": "e1", "type": "processor", "subtype": "expression",
"config": {"fields": [
{"name": "temp_f", "expression": "in.value * 9.0 / 5.0 + 32.0"},
{"name": "hot", "expression": "in.value > 30.0"}
]}}
fieldsis required and carries 1 to 16{name, expression}entries;namemust be a valid field name and the config is closed (additionalProperties: false).Every expression is evaluated against the input payload; results are added to the record with the computed names.
Replacing
valueis allowed when the expression’s static type is numeric — that is what a math expression is for. The telemetry identity fields (device_id,scope_id,metric,ts) cannot be replaced.The output always carries
expression_status(ok|error) andexpression_errors, written last so an input field of the same name cannot spoof them.
Keep records — processor.filter¶
{"id": "f1", "type": "processor", "subtype": "filter",
"config": {"expression": "in.metric == \"temperature\" && in.value > 10.0"}}
expressionis required and must be statically boolean. An expression whose type could be a string is refused at validation.A passing record is emitted unchanged, with
filter_status: "passed"written last.
Project, rename, drop — processor.map¶
{"id": "m1", "type": "processor", "subtype": "map",
"config": {
"fields": [{"name": "temp_f", "expression": "in.value * 1.8 + 32.0"}],
"rename": {"value": "raw_value"},
"drop": ["device_id"]
}}
At least one of
fields,renameordropmust be present.The order is fixed: computed fields are evaluated against the input payload; the input is then copied with
renameanddropapplied; computed values are written last. A computed field may therefore replace a retained field of the same name (valueincluded, when numeric).renamemaps an input field to a new name. The telemetry required fields (device_id,scope_id,metric,value,ts) cannot be renamed away or dropped; a rename target cannot collide with a retained field, map two sources to one name, or claim a field the runtime writes (expression_status,expression_errors,filter_status). A renamed-away source is removed from the output.dropnames input fields to remove; it cannot name a required telemetry field or a field that is also renamed.The output carries
expression_status/expression_errorsas forprocessor.expression.
Checking an expression¶
POST /api/v1/expressions/check answers what an expression means — validity,
result type, referenced fields and located errors — without saving a pipeline or
executing anything. It is gated by the same thinglogic:Pipeline:Validate
action a pipeline validation needs.
The request is one of two forms: against a published input schema by reference, or against an inline JSON Schema.
{"expression": "in.value > 5.0",
"input_schema_ref": "source.device-mqtt-stream.out",
"dialect": "thinglogic-expr/v2"}
{"expression": "in.value > 5.0",
"input_schema": {"type": "object",
"properties": {"value": {"type": "number"}},
"required": ["value"]}}
Exactly one of
input_schema_reforinput_schemais required;expressionis required;dialectis optional and, when present, must bethinglogic-expr/v2.input_schema_refvalues are the catalog’s own output-port names,<node_type_id>.<port>— for examplesource.device-mqtt-stream.out. CallingGET /api/v1/cataloglists the node types and their ports; a ref is valid when that node type publishes a non-empty schema for that output port.
A well-formed request is answered 200 whether or not the expression is
valid — the answer is the payload. For in.metric > 5 against the telemetry
schema (where metric is a string) the answer is:
{
"valid": false,
"dialect": "thinglogic-expr/v2",
"output_type": null,
"referenced_fields": ["metric"],
"errors": [
{"code": "expression_type_mismatch",
"detail": "found no matching overload for '_>_' applied to '(string, int)'",
"span": {"start": 10, "end": 11, "line": 1, "column": 11}}
]
}
For a valid expression, valid is true, output_type is one of string,
int, double, bool, list, map or null, and errors is empty. The
span is a character range in the author’s own expression — start/end plus
1-based line/column — so an editor can underline exactly the offending
token. input_schema_ref is echoed when the caller supplied one; it is null
for an inline schema.
A request that cannot be posed is refused with 400 and a PLT-0095
application/problem+json document carrying a machine code:
Code |
Meaning |
|---|---|
|
the body is not an object, carries an unknown member, lacks |
|
the inline schema cannot be turned into an expression field map |
|
the ref is not a published output-port schema; the detail names the vocabulary |
|
the named dialect is not |
Parity with StreamPipes¶
The three processors cover the StreamPipes pipeline vocabulary in one language rather than one processor per transformation:
StreamPipes element |
ThingLogic |
|---|---|
Math Expression |
|
Numerical / Text / Boolean / List filters |
|
Projection |
|
Text Filter ( |
filter expressions using |
List membership ( |
the CEL |
Field Renamer |
|
Measurement Unit Converter |
the math operators — conversion is an expression, not a node type |
A later measurement-unit converter node, if one is added, is an ordinary node type; the expression profile does not need to know about units.