Pipe Syntax (|>)
Function
The Pipe syntax (pipeline operator |>) is a SQL syntax extension in Spark 4.0 that allows organizing SQL queries in a pipeline fashion. The Pipe syntax changes the query style from nested function calls to a top-to-bottom linear flow, making complex queries easier to read and write.
The Pipe syntax is fully compatible with existing SQL syntax and can be mixed in any query.
Syntax
FROM table_source |> WHERE condition |> SELECT select_expr [, ...] |> ORDER BY col_name [ASC|DESC] [, ...] |> LIMIT num_rows
Parameters
| Operator | Syntax | Description |
|---|---|---|
| FROM / TABLE | FROM or TABLE | Starts from the source table, returning all rows. |
| SELECT | |> SELECT [[AS] alias], ... | Computes expressions and generates new columns (does not support aggregate functions; use AGGREGATE for aggregation). |
| EXTEND | |> EXTEND [[AS] alias], ... | Appends new columns to the input table. |
| SET | |> SET col_name = expression [, col_name = expression, ...] | Replaces the values of existing columns. |
| DROP | |> DROP , ... | Deletes specified columns. |
| AS | |> AS | Introduces a new alias for the input table. |
| WHERE | |> WHERE | Filters rows (no separate HAVING or QUALIFY needed). |
| LIMIT | |> [LIMIT ] [OFFSET ] | Limits the number of rows returned and skips rows. |
| AGGREGATE | |> AGGREGATE <agg_expr> GROUP BY <group_expr> | Performs aggregation (full table or grouped). |
| JOIN | |> [LEFT|RIGHT|FULL|CROSS|SEMI|ANTI] JOIN ON | Performs a join operation. |
| ORDER BY | |> ORDER BY [ASC|DESC], ... | Performs sorting. |
| UNION ALL | |> UNION ALL | Merges result sets. |
| TABLESAMPLE | |> TABLESAMPLE ( {ROWS|PERCENT}) | Performs sampling. |
| PIVOT | |> PIVOT (agg_expr FOR col IN (val1, ...)) | Rotates rows to columns. |
| UNPIVOT | |> UNPIVOT (value_col FOR key_col IN (col1, ...)) | Rotates columns to rows. |
Precautions
- The Pipe syntax starts with a FROM clause.
- Each |> operator processes the output of the previous step, forming a pipeline chain.
- The WHERE operator can be used anywhere. No separate HAVING or QUALIFY is needed.
- The Pipe syntax does not change the semantics of the query; it only provides a different way of writing it.
- The EXTEND/JOIN operators may trigger a toAttribute on unresolved object internal error on simple inline queries. You are advised to use a sub-table alias approach like SELECT * FROM (subquery) AS alias |> ....
- The |> SET operator only supports single-segment keys (e.g., key_name), not multi-segment configuration keys (e.g., spark.sql.ansi.enabled).
Example 1: Basic Pipeline Query
FROM employees |> WHERE department = 'Engineering' |> SELECT name, salary |> ORDER BY salary DESC |> LIMIT 10;
Equivalent to:
SELECT name, salary FROM employees WHERE department = 'Engineering' ORDER BY salary DESC LIMIT 10;
Example 2: Using EXTEND to Add a Computed Column
VALUES (0), (1) tab(col) |> EXTEND col * 2 AS result;
Result:
| col | result |
|---|---|
| 0 | 0 |
| 1 | 2 |
Example 3: Using SET to Modify Column Values
VALUES (0), (1) tab(col) |> SET col = col * 2;
Result:
| col |
|---|
| 0 |
| 2 |
Example 4: Using AGGREGATE for Grouped Aggregation
-- Full table aggregation VALUES (0), (1) tab(col) |> AGGREGATE COUNT(col) AS count; -- Grouped aggregation VALUES (0, 1), (0, 2) tab(col1, col2) |> AGGREGATE COUNT(col2) AS count GROUP BY col1;
Example 5: Using JOIN
SELECT 0 AS a, 1 AS b |> AS lhs |> JOIN VALUES (0, 2) rhs(a, b) ON (lhs.a = rhs.a);
Example 6: Complex Pipeline (TPC-H Q13 Rewritten)
Standard SQL:
SELECT c_count, COUNT(*) AS custdist
FROM
(SELECT c_custkey, COUNT(o_orderkey) c_count
FROM customer
LEFT OUTER JOIN orders ON c_custkey = o_custkey
AND o_comment NOT LIKE '%unusual%packages%' GROUP BY c_custkey
) AS c_orders
GROUP BY c_count
ORDER BY custdist DESC, c_count DESC; Pipeline syntax:
FROM customer |> LEFT OUTER JOIN orders ON c_custkey = o_custkey AND o_comment NOT LIKE '%unusual%packages%' |> AGGREGATE COUNT(o_orderkey) c_count GROUP BY c_custkey |> AGGREGATE COUNT(*) AS custdist GROUP BY c_count |> ORDER BY custdist DESC, c_count DESC;
Core Features
Independence and Interoperability
- Each pipeline operator receives an input table and independently operates on the rows.
- For any valid chain of N pipeline operators, the first M operators (M ≤ N) also form a valid query.
- Pipeline operators can be appended to any valid standard SQL query.
- Table subqueries can be written using standard SQL syntax or pipeline syntax.
- Views, DDL, and DML statements can all contain pipeline syntax queries.
Projection operations
- SELECT: Computes new expressions and generates a new table.
- EXTEND: Appends new columns while retaining the original columns.
- SET: Replaces the values of existing columns.
- DROP: Deletes specified columns.
Aggregation operations
- AGGREGATE combines the functionality of GROUP BY + SELECT, eliminating the need to repeat expressions.
- Grouping expressions support specifying aliases directly in the GROUP BY clause.
- The output columns automatically include the grouping columns and the aggregate columns.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot