Pipes (proposed extension)
Proposed, in development, not released: OpenFlange pipes as the TugQL extension mechanism. Draft syntax, the closed-query rule, pushdown and limits.
Status: proposed, in development
Proposed · in development. Pipes are not released. Nothing on this page is part of the published TugQL specification, and the syntax may change before it is. A query that uses pipes runs only where a host supplies a pipe runner; everywhere else it is refused. DataTug is the first host, and that work is in development too.
Why pipes
A query language can't do everything, and shouldn't. Statistics in R, masking an email, calling a model: until now that meant exporting rows and writing glue code around the query.
TugQL does not grow a built-in function library to cover those cases. It hands the rows to a pipe: a named, versioned unit of work with a declared contract for its inputs, outputs and parameters. Pipes are defined by OpenFlange, an open standard, so the same pipe runs from a query, the command line, HTTP or a visual pipeline.
Declare pipes, then use them after a bar
pipes (
lower = 'openflange/lowercase@^0.1'
stats = 'openflange/r-summary@^0.1'
)
with Stat as (
from Customers as c
where c.Country = 'IE'
select c.Id, c.Total
) | stats
from Stat as s
select s.mean
The pipes ( block sits in the query header, like databases (. Each line binds an alias to a pipe reference with a version range. The rest of the query uses the alias only. The host pins every pipe it resolves by version and integrity, so a saved query keeps running the same pipe.
A bar, |, sends the rows of the query before it into the pipe named after it. Here the name Stat binds to the output of stats, not to the rows of the query in the parentheses.
The closed-query rule
A bar chain follows a complete, closed query. It never appears between the clauses of an open query. There are two places for it:
- after the closing parenthesis of a common table expression body, as above;
- after the last clause of the top-level query.
The rule answers the question of what runs before the pipe and what runs after it. Everything inside the parentheses runs first. Everything outside sees the pipe's output.
Chains, arguments and output ports
) | lower | stats(digits = 2)
Stages chain left to right: the output of one stage is the input of the next. A stage takes named scalar arguments in parentheses. A pipe with more than one output has a default; .port after the alias selects another one.
pipes (
lower = 'openflange/lowercase@^0.1'
mask = 'openflange/mask-email@^0.1'
)
with Masked as (
from Customers as c
where c.Country = 'IE'
select c.Email
) | lower | mask
from Masked as m
select m.email
With this chain the value Alice@Example.COM comes back as a***e@example.com.
What is pushed down
Everything inside the parentheses is pushed down to the database as usual: the from, the where, the projection. A pipe changes nothing about that part of the query.
Everything after the bar sees the pipe's output. It is not pushed down, because the database never sees those rows. Filter inside the parentheses, so that the pipe receives only the rows it needs.
Output columns
The pipe's declared output schema becomes the columns of the result. In the first example s.mean is a column because the pipe declares it, not because the query selected it from Customers.
Limits
- One input per bar. A bar feeds one stream of rows into one pipe. A pipe that needs several inputs cannot be fed by a bar.
- Typed outputs are required. A pipe whose selected output declares no schema has no columns to resolve, so the query is refused.
- No pipes inside scalar subqueries. A bar chain belongs to a common table expression or to the top-level query.
- A pipe runner is required. Without one, a query that uses pipes is refused. It is never run with the pipe silently skipped.
Where to read more
OpenFlange defines what a pipe is: its manifest, ports, parameters, versions and permissions. Pipes are an open standard and are not specific to TugQL. For the released language, start with the TugQL syntax cheat sheet.