LucaCappelletti94 commented on code in PR #2438:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2438#discussion_r3801456133
##########
src/dialect/mod.rs:
##########
@@ -726,6 +726,14 @@ pub trait Dialect: Debug + Any {
false
}
+ /// Does the dialect support the Snowflake `-->` flow/pipe operator for
chaining
Review Comment:
This looks like a typo
##########
tests/sqlparser_snowflake.rs:
##########
@@ -4912,3 +4912,61 @@ fn test_select_dollar_column_from_stage() {
// With table function args, without alias
snowflake().verified_stmt("SELECT $1, $2 FROM @mystage1(file_format =>
'myformat')");
}
+
+#[test]
+fn test_snowflake_pipe_operator() {
+ // Basic pipe: two SELECT statements chained
+ snowflake().verified_stmt("SELECT * FROM tablename ->> SELECT * FROM $1");
+
+ // Three statements chained
+ snowflake().verified_stmt(
+ "SELECT * FROM dept WHERE dname = 'SALES' ->> SELECT * FROM emp WHERE
deptno IN (SELECT deptno FROM $1) ->> SELECT ename, sal FROM $1 ORDER BY 2
DESC",
+ );
+
+ // Reference to a non-adjacent prior result using $2
+ snowflake().verified_stmt("SELECT a FROM t ->> SELECT b FROM t2 ->> SELECT
$1 FROM $2");
+
+ // Non-SELECT statements in the chain (CREATE/INSERT)
+ snowflake()
+ .verified_stmt("CREATE TABLE t (id INT) ->> INSERT INTO t VALUES (1)
->> SELECT * FROM $1");
+
+ // Pipe operator is not parsed in non-Snowflake dialects
+ use sqlparser::dialect::GenericDialect;
+ use sqlparser::parser::Parser;
+ // In a generic dialect, ->> is a binary operator, not a pipe
+ let stmts = Parser::parse_sql(&GenericDialect {}, "SELECT 1").unwrap();
Review Comment:
What is this even meant to test? Also, `GenericDialect` would generally be a
dialect that supports all syntaxes, and should therefore likely also support
this one.
##########
src/parser/mod.rs:
##########
@@ -16706,6 +16735,18 @@ impl<'a> Parser<'a> {
// Stage reference: @mystage or @namespace.stage (e.g. Snowflake)
self.parse_snowflake_stage_table_factor()
} else {
+ // Handle Snowflake pipe result references ($1, $2, ...) in FROM
clause.
+ // See <https://docs.snowflake.com/en/sql-reference/operators-flow>
+ if self.dialect.supports_snowflake_pipe_operator() {
+ if let Token::Placeholder(ref s) =
self.peek_token_ref().token.clone() {
+ if let Some(index_str) = s.strip_prefix('$') {
+ if let Ok(index) = index_str.parse::<u64>() {
Review Comment:
Here you are currently accepting `$0`, maybe replace with:
```
if let Ok(index @ 1..) = index_str.parse::<u64>() {
```
##########
tests/sqlparser_snowflake.rs:
##########
@@ -4912,3 +4912,61 @@ fn test_select_dollar_column_from_stage() {
// With table function args, without alias
snowflake().verified_stmt("SELECT $1, $2 FROM @mystage1(file_format =>
'myformat')");
}
+
+#[test]
+fn test_snowflake_pipe_operator() {
Review Comment:
Several cases that I am not sure are currently corrected supported, such as
the following ones, should be added to the tests. These are ones from the
Snowflake docs:
```
// Exact documented SHOW shape.
verified_stmt(
r#"SHOW WAREHOUSES
->> SELECT "name", "state", "type", "size" FROM $1"#,
);
// A Snowflake dialect override that currently scans until EOF/semicolon.
verified_stmt("CREATE DATABASE d ->> SELECT 1");
// Existing happy path.
verified_stmt(
"CREATE TABLE t (id INT)
->> INSERT INTO t VALUES (1)
->> SELECT * FROM $1"
);
// Error paths.
assert_parse_error("SELECT 1 ->>");
assert_parse_error("SELECT * FROM $0");
// GenericDialect ambiguity.
verified_generic_expr("SELECT payload ->> 'name'");
verified_generic_pipe("SELECT 1 ->> SELECT 2");
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]