Skip to content

Add pipelined operators - #73394

Closed
itsWill wants to merge 5 commits into
ClickHouse:masterfrom
itsWill:add_pipelined_operators
Closed

itsWill wants to merge 5 commits into
ClickHouse:masterfrom
itsWill:add_pipelined_operators

Conversation

@itsWill

@itsWill itsWill commented Dec 16, 2024 •

Copy link
Copy Markdown

Changelog category (leave one):

  • Experimental Feature

This is a proof of concept of a possible approach for adding the ZetaSQL piped syntax into ClickHouse: #69364

The high level overview of the approach is:

  • create a new ZetaSQL dialect (like prql, kusto, or the regular clickhouse)
  • when the dialect is enabled piped syntax is enabled.
  • A ZetSQLQueryParser parses the piped synax into a ZetaSQL Specific Syntax tree.
  • The syntax tree is then translated to a regular clickhouse syntax tree and executed.

In practice it looks like this on the console:

Guilhermes-MacBook-Pro.local :) FROM system.settings 
|> WHERE name like '%merge%' 
|> AGGREGATE count(*) as c GROUP BY name;

SELECT count(*) AS c
FROM
(
    SELECT *
    FROM system.settings
    WHERE name LIKE '%merge%'
)
WHERE name LIKE '%merge%'
GROUP BY name

The main advantages of doing this approach are:

  • ZetaSQL has some idiosyncrasies that differ from regular SQL syntax: wether new keywords like EXTEND or different interpretations of standard SQL, like being able to start a statement using a FROM clause. Separating the ZetaSQL parser from the regular ClickHouse parsing separates the concerns into a clean division we can modify them independently.
  • We can build on top of the existing SQL interpreter.

The disadvantage is that is that errors in the generated sql are not mapped directly to the provided sql.

A picture of the suggested approach looks like this:

image

There are a couple of outstanding questions:

Right now SELECT query is generated when there is an aggregation it looks something like this:

Guilhermes-MacBook-Pro.local :) FROM system.settings 
|> WHERE name like '%merge%' 
|> AGGREGATE count(*) as c GROUP BY name;

SELECT count(*) AS c
FROM
(
    SELECT *
    FROM system.settings
    WHERE name LIKE '%merge%'
)
WHERE name LIKE '%merge%'
GROUP BY name
  • When adding a second aggregation we can either wrap this query in a select subquery or use CTEs. My question is how to handle multiple aggregations?

Personally I prefer a CTE structure it's easier to read and to debug as it flows similar to the pipe syntax. From my initial reading it doesn't seem that we should see a performance benefit from using subqueries.

  • This piece of work requires a few changes, so I'm wondering what's the best way to deliver this feature. By either using a feature branch, or merging piece wise into master.

@CLAassistant

CLAassistant commented Dec 16, 2024 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Guilherme Mansur seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@rschu1ze rschu1ze self-assigned this Dec 18, 2024

@rschu1ze rschu1ze left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR, I like the direction it is taking.
Please find some comments inline.

Regarding your question about aggregation, I second the CTE approach.

About integrating this PR: My main concern is to keep the "blast radius" as small as possible and that means introducing a setting or dialect which isolates pipe syntax from normal SQL. If you do that, and maybe try to add a few other basic and useful operators (ORDER BY, SELECT, maybe JOIN comes to mind) + add some SQL tests (--> tests/queries/0_stateless/), then we can happily merge this PR.

ast = parseQuery(parser, begin, end, "", max_query_size, settings[Setting::max_parser_depth], settings[Setting::max_parser_backtracks]);

#ifndef NDEBUG
if(settings[Setting::dialect] == Dialect::clickhouse)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should definitely have a separate Dialect enum value clickhouse_with_pipes and introduce a separate if branch similar to l. 861-876.

add_headers_and_sources(clickhouse_parsers ./Kusto)
add_headers_and_sources(clickhouse_parsers ./PRQL)
add_headers_and_sources(clickhouse_parsers ./Kusto/KustoFunctions)
add_headers_and_sources(clickhouse_parsers ./ZetaSQL)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look at Google's ZetaSQL repository. My understanding is this:

  • it provides a C++ parser for Google's unified SQL dialect (used by their various internal databases)
  • the parser produces an AST which the individual databases then interpret in various ways (*)
  • the repository comes with an execute binary which implements a extremely simple end-to-end executor for SQL queries on a small set of predefined tables (which makes this a nice utility for testing)
  • the SQL dialect is documented at great detail at literal/identifier/keyword level including SQL operators and functions for DDL/DML (CREATE/UPDATE/DELETE) and DQL (SELECT)

The idea of integrating ZetaSQL as a submodule into ClickHouse and going the (*) route has charme but it seems very difficult due to two reasons: 1. ZetaSQL is open-source but they are seemingly not interested in building a community around it, nor that databases outside Google add ZetaSQL as a frontend. The README says they don't even accept pull requests. 2. ClickHouse's existing and their SQL dialects differ in the details, using ZetaSQL as a frontend would create unnecessary surprises for users.

Now that this is out of the way :-) my suggestion is this: We can give credits and link the research paper https://research.google/pubs/pub1005959/ but I'd avoid the term "ZetaSQL" (here, and elsewhere in this PR), and instead call the dialect ClickhouseWithPipes or something similar.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point, I'll refactor this to be under a more general setting ClickHouseWithPipes type setting.

Comment thread src/Parsers/Lexer.cpp
if (pos < end && *pos == '|')
return Token(TokenType::Concatenation, token_begin, ++pos);

if (pos < end && *pos == '>')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever code we add or change, we should do all modifications under a special setting allow_experimental_pipe_syntax etc. (please define such a settings, --> src/Core/Settings.cpp). One could argue that the pipe syntax is only used if SET dialect = [...] was ran before, but for example here, the standard dialect is parsed slightly different as well and we should avoid edge cases that break queries which previously worked fine.

ParserExternalDDLQuery external_ddl_p;
ParserTransactionControl transaction_control_p;
ParserDeleteQuery delete_p;
ZetaSQL::ParserZetaSQLQuery zetasql_parser;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure ... afaik, ZetaSQL pipe syntax only applies to SELECT-type queries. Perhaps we should move the ZetaSQL parser into ParserSelectQuery (l. 82)?

EDIT: after digging a bit more (see my other comment above), I think we better don't modify this file at all and instead introduce a new if branch for a new dialect in executeQuery.cpp

#include "Parsers/ZetaSQL/ParserFromStatement.h"
#include "Parsers/ZetaSQL/ParserPipe.h"

namespace DB::ZetaSQL {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Cosmetic: Let's use namespace DB instead of DB::ZetaSQL - the ClickHouse codebase generally avoids nested namespaces.)

ASTPtr expression;
};

std::list<PipelineStage> stages;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::list<PipelineOperators> operators? That would be the official syntax at least.

@clickhouse-gh

clickhouse-gh Bot commented Mar 11, 2025

Copy link
Copy Markdown
Contributor

Dear @itsWill, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

1 similar comment
@clickhouse-gh

clickhouse-gh Bot commented May 13, 2025

Copy link
Copy Markdown
Contributor

Dear @itsWill, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@alexey-milovidov

Copy link
Copy Markdown
Member

#69364 (comment)

@alexey-milovidov

Copy link
Copy Markdown
Member

Actually, it does almost what I expected...

@clickhouse-gh

clickhouse-gh Bot commented Aug 12, 2025

Copy link
Copy Markdown
Contributor

Dear @itsWill, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@alexey-milovidov

Copy link
Copy Markdown
Member

That's a very good feature, and we should continue.
We should make it without introducing a new dialect.

@clickhouse-gh

clickhouse-gh Bot commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

Dear @itsWill, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

3 similar comments
@clickhouse-gh

clickhouse-gh Bot commented Oct 21, 2025

Copy link
Copy Markdown
Contributor

Dear @itsWill, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Dec 23, 2025

Copy link
Copy Markdown
Contributor

Dear @itsWill, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

Dear @itsWill, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Dear @itsWill, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@alexey-milovidov
alexey-milovidov marked this pull request as ready for review July 20, 2026 20:55
@alexey-milovidov alexey-milovidov self-assigned this Jul 20, 2026
@alexey-milovidov

alexey-milovidov commented Jul 20, 2026 •

Copy link
Copy Markdown
Member

It's strange that we implement it manually as a separate dialect, yet name it ZetaSQL — while not using the zetasql library.

What I actually want is to implement pipeline operators as a syntax extension in the regular ClickHouse dialect. Then any type of query can be pipelined, and inside every stage, the ClickHouse dialect is used. The resulting AST will be the same as an AST with multiple nested subqueries.

Compatibility with BigQuery is not a concern.

@alexey-milovidov alexey-milovidov mentioned this pull request Jul 20, 2026
1 task done
@alexey-milovidov

Copy link
Copy Markdown
Member

The approach described in the comment above is now implemented in #111151: pipe operators as a syntax extension of the regular ClickHouse dialect, without a separate dialect or translator. Each |> operator wraps the query before it into a subquery, so the resulting AST is the same as for the equivalent query with nested subqueries, and the regular ClickHouse syntax is used inside every operator.

Thank you for the initial implementation and for exploring the design space - the new PR supersedes this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants