sabir-akhadov-localstack commented on code in PR #2438:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/2438#discussion_r4391124583


##########
src/parser/mod.rs:
##########
@@ -16862,6 +16892,16 @@ impl<'a> Parser<'a> {
             // Stage reference: @mystage or @namespace.stage (e.g. Snowflake)
             self.parse_snowflake_stage_table_factor()
         } else {
+            // Handle pipe result references ($1, $2, ...) in FROM clause.
+            // See <https://docs.snowflake.com/en/sql-reference/operators-flow>
+            if self.dialect.supports_long_arrow_pipe_operator() {
+                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 });

Review Comment:
   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.



##########
src/dialect/snowflake.rs:
##########
@@ -1100,6 +1106,11 @@ pub fn parse_create_table(
                 parser.prev_token();
                 break;
             }
+            Token::LongArrow => {
+                // Snowflake pipe operator terminates the statement

Review Comment:
   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.



##########
src/ast/query.rs:
##########
@@ -1721,6 +1721,14 @@ pub enum TableFactor {
         /// The alias for the table.
         alias: Option<TableAlias>,
     },
+    /// Snowflake pipe result reference: `$1`, `$2`, etc.
+    ///
+    /// 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.

Review Comment:
   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.



##########
src/dialect/generic.rs:
##########
@@ -329,6 +329,10 @@ impl Dialect for GenericDialect {
         true
     }
 
+    fn supports_long_arrow_pipe_operator(&self) -> bool {

Review Comment:
   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.



-- 
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]

Reply via email to