iffyio commented on code in PR #2333:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2333#discussion_r3239468137
##########
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:
I think the main goal with the comment was to simplify the cases a bit, so
that ideally we have one branch handling the cte case, and another handling the
cse case. Another way (there might be others) to accomplish that would be to
try to parse a cte and if that fails fallback to cse e.g.
```rust
if let Some(cte) = self.maybe_parse(|parser| parser...)? else {
return cte
} else if self.supprots() {
return self.parse_cse
} else {
self.expected("CTE or CSE")
}
```
maybe above woul be straightforward in that the cases that are covered is
clearer?
--
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]