Skip to content

Snowflake: Add support for ->> (pipe) operator for chaining SQL stmts - #2438

Open
BenSatori wants to merge 13 commits into
apache:mainfrom
BenSatori:snowflake-pipe-operator
Open

BenSatori wants to merge 13 commits into
apache:mainfrom
BenSatori:snowflake-pipe-operator

Conversation

@BenSatori

Copy link
Copy Markdown
Contributor

The ->> (flow/pipe) operator chains multiple SQL statements in Snowflake.

Before: Parser rejected pipe chains like SELECT * FROM t1 ->> SELECT * FROM $1

After: Parser correctly handles:

  • Chained statements with ->> operator
  • Pipe result references ($1, $2, $3, etc.)
  • Round-trip SQL serialization

Example:

SELECT * FROM users WHERE active = true ->> SELECT COUNT(*) FROM $1

Changes:

  • Added Statement::Pipe AST variant for chained statements
  • Added TableFactor::PipeResultScan for $n result references
  • Extended Snowflake dialect with pipe operator support
  • Comprehensive test coverage (2 test functions, multiple edge cases)

Snowflake docs:
https://docs.snowflake.com/en/sql-reference/operators-flow#pipe

Comment thread src/dialect/mod.rs Resolved
Comment thread tests/sqlparser_snowflake.rs Resolved
Comment thread src/parser/mod.rs Resolved
Comment thread tests/sqlparser_snowflake.rs Resolved

@LucaCappelletti94 LucaCappelletti94 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a nit pick, then it should me mergiable imho

Comment thread src/parser/mod.rs Resolved
@BenSatori

Copy link
Copy Markdown
Contributor Author

@iffyio do you think we can merge this?

@LucaCappelletti94

Copy link
Copy Markdown
Contributor

@BenSatori GitHub CI / queue is currently chocking, it'll take a while for it to clear.

@LucaCappelletti94 LucaCappelletti94 added the needs rebasing The PR has collisions with main and requires rebasing label Sep 23, 2026
@BenSatori
BenSatori force-pushed the snowflake-pipe-operator branch from ffb2bf2 to f8292c6 Compare September 24, 2026 14:34
@codecov-commenter

codecov-commenter commented Sep 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.77419% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.12%. Comparing base (560a13c) to head (f643bf4).

Files with missing lines Patch % Lines
src/ast/mod.rs 90.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2438      +/-   ##
==========================================
+ Coverage   81.09%   81.12%   +0.03%     
==========================================
  Files          42       42              
  Lines       33684    33746      +62     
  Branches    33684    33746      +62     
==========================================
+ Hits        27316    27378      +62     
+ Misses       2798     2796       -2     
- Partials     3570     3572       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucaCappelletti94 LucaCappelletti94 added waiting on contributor The review needs further refinements by its author and removed needs rebasing The PR has collisions with main and requires rebasing labels Sep 24, 2026
@LucaCappelletti94 LucaCappelletti94 added waiting on reviewer PR needs to be reviewed and removed waiting on contributor The review needs further refinements by its author labels Sep 28, 2026
Comment thread src/parser/mod.rs
if let Token::Placeholder(ref s) = self.peek_token_ref().token.clone() {
if let Some(Ok(index @ 1..)) = s.strip_prefix('$').map(str::parse::<u64>) {
self.next_token(); // consume the $n token
return Ok(TableFactor::PipeResultScan { index });

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Snowflake accepts an alias here: SELECT 1 AS a ->> SELECT t.a FROM $1 AS t and ... ->> SELECT t.a, u.b FROM $2 AS t CROSS JOIN $1 AS u both run on Cloud but are rejected with Expected: end of statement, found: AS. Suggest an alias: Option<TableAlias> on the variant via maybe_parse_table_alias, which also removes the need for the parenthesized-alias error above.

Comment thread src/dialect/snowflake.rs
break;
}
Token::LongArrow => {
// Snowflake pipe operator terminates the statement

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The same end-of-statement loops in parse_copy_into (COPY INTO t FROM @s ->> SELECT * FROM $1), parse_alter_session (ALTER SESSION SET QUERY_TAG = 'x' ->> SELECT 1, valid on Cloud) and parse_key_value_options in src/parser/mod.rs (CREATE WAREHOUSE w ->> SELECT 1, CREATE USER u ->> SELECT 1) still reject ->>. A single end-of-statement predicate on Parser that includes ->> when the dialect supports it would cover all of them.

Comment thread src/dialect/generic.rs
true
}

fn supports_long_arrow_pipe_operator(&self) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In Generic ->> still binds as the JSON operator whenever the previous statement ends in an expression, so SELECT 1 ->> SELECT 2 and SELECT * FROM t WHERE a = 1 ->> SELECT 1 fail while SELECT * FROM t ->> SELECT 1 works. Either drop Generic here or add a test documenting the partial support.

Comment thread src/ast/query.rs
/// Used in FROM clauses of pipe-chained statements to reference previous results.
/// See <https://docs.snowflake.com/en/sql-reference/operators-flow>
PipeResultScan {
/// 1-based index of the previous statement whose result is referenced.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Worth saying this counts back from the current statement ($1 is the immediately preceding one, $2 the one before that), since index reads as absolute.

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

Labels

Snowflake waiting on reviewer PR needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants