alrevuelta commented on code in PR #2333:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/2333#discussion_r3235396708


##########
src/parser/mod.rs:
##########
@@ -14639,6 +14639,33 @@ impl<'a> Parser<'a> {
         Ok(cte)
     }
 
+    /// Parse a single item in a `WITH` clause.
+    ///
+    /// In standard SQL this is always a CTE (`name [(cols)] AS (query)`).
+    /// Dialects that enable 
[`Dialect::supports_with_clause_scalar_expression`]
+    /// — currently only ClickHouse — also accept the reversed form
+    /// `<expression> AS <identifier>`, which can be freely interleaved with
+    /// CTEs in the same comma-separated list.
+    pub fn parse_with_item(&mut self) -> Result<WithItem, ParserError> {
+        if !self.dialect.supports_with_clause_scalar_expression() {
+            return self.parse_cte().map(WithItem::Cte);
+        }
+
+        // CTE form must start with an identifier. If the leading token
+        // can't begin one (e.g. `42`, `(SELECT …)`, `(x, y) -> …`), this
+        // is unambiguously the named-expression form.
+        if matches!(self.peek_token().token, Token::Word(_)) {
+            if let Some(cte) = self.maybe_parse(|p| p.parse_cte())? {
+                return Ok(WithItem::Cte(cte));
+            }
+        }

Review Comment:
   right, i was mixing things up. can you confirm this `WITH (SELECT max(x) 
FROM t) AS m SELECT m` will work with your approach? i tried and seems that the 
first tokens `(SELECT` make peek_subquery_or_cte_start return true, which 
routes to parse_cte, but parse_cte immediately fails on ( since it expects an 
identifier.



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