iffyio commented on code in PR #2241:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/2241#discussion_r3079286327


##########
tests/sqlparser_common.rs:
##########
@@ -16118,6 +16073,129 @@ fn test_select_from_first() {
     }
 }
 
+#[test]
+fn test_select_from_first_with_cte() {
+    let dialects = all_dialects_where(|d| d.supports_from_first_select());
+    let q = "WITH test AS (FROM t SELECT a) FROM test SELECT 1";
+
+    let ast = dialects.verified_query(q);
+
+    let expected = Query {
+        with: Some(With {
+            with_token: AttachedToken::empty(),
+            recursive: false,
+            cte_tables: vec![Cte {
+                alias: TableAlias {
+                    explicit: false,
+                    name: Ident {
+                        value: "test".to_string(),
+                        quote_style: None,
+                        span: Span::empty(),
+                    },
+                    columns: vec![],
+                },
+                query: Box::new(Query {
+                    with: None,
+                    body: Box::new(SetExpr::Select(Box::new(Select {
+                        select_token: AttachedToken::empty(),
+                        optimizer_hints: vec![],
+                        distinct: None,
+                        select_modifiers: None,
+                        top: None,
+                        projection: 
vec![SelectItem::UnnamedExpr(Expr::Identifier(Ident {
+                            value: "a".to_string(),
+                            quote_style: None,
+                            span: Span::empty(),
+                        }))],
+                        exclude: None,
+                        top_before_distinct: false,
+                        into: None,
+                        from: vec![TableWithJoins {
+                            relation: 
table_from_name(ObjectName::from(vec![Ident {
+                                value: "t".to_string(),
+                                quote_style: None,
+                                span: Span::empty(),
+                            }])),
+                            joins: vec![],
+                        }],
+                        lateral_views: vec![],
+                        prewhere: None,
+                        selection: None,
+                        group_by: GroupByExpr::Expressions(vec![], vec![]),
+                        cluster_by: vec![],
+                        distribute_by: vec![],
+                        sort_by: vec![],
+                        having: None,
+                        named_window: vec![],
+                        window_before_qualify: false,
+                        qualify: None,
+                        value_table_mode: None,
+                        connect_by: vec![],
+                        flavor: SelectFlavor::FromFirst,
+                    }))),
+                    order_by: None,
+                    limit_clause: None,
+                    fetch: None,
+                    locks: vec![],
+                    for_clause: None,
+                    settings: None,
+                    format_clause: None,
+                    pipe_operators: vec![],
+                }),
+                from: None,
+                materialized: None,
+                closing_paren_token: AttachedToken::empty(),
+            }],
+        }),
+        body: Box::new(SetExpr::Select(Box::new(Select {
+            select_token: AttachedToken::empty(),
+            optimizer_hints: vec![],
+            distinct: None,
+            select_modifiers: None,
+            top: None,
+            projection: vec![SelectItem::UnnamedExpr(Expr::Value(ValueWithSpan 
{
+                value: test_utils::number("1"),
+                span: Span::empty(),
+            }))],
+            exclude: None,
+            top_before_distinct: false,
+            into: None,
+            from: vec![TableWithJoins {
+                relation: table_from_name(ObjectName::from(vec![Ident {
+                    value: "test".to_string(),
+                    quote_style: None,
+                    span: Span::empty(),
+                }])),
+                joins: vec![],
+            }],
+            lateral_views: vec![],
+            prewhere: None,
+            selection: None,
+            group_by: GroupByExpr::Expressions(vec![], vec![]),
+            cluster_by: vec![],
+            distribute_by: vec![],
+            sort_by: vec![],
+            having: None,
+            named_window: vec![],
+            window_before_qualify: false,
+            qualify: None,
+            value_table_mode: None,
+            connect_by: vec![],
+            flavor: SelectFlavor::FromFirst,
+        }))),
+        order_by: None,
+        limit_clause: None,
+        fetch: None,
+        locks: vec![],
+        for_clause: None,
+        settings: None,
+        format_clause: None,
+        pipe_operators: vec![],
+    };
+    assert_eq!(expected, ast);
+    assert_eq!(ast.to_string(), q);

Review Comment:
   I don't think this should be needed given `verified_query()` does this check?



##########
src/parser/mod.rs:
##########
@@ -13982,7 +13958,7 @@ impl<'a> Parser<'a> {
                 closing_paren_token: closing_paren_token.into(),
             }
         };
-        if self.parse_keyword(Keyword::FROM) {
+        if dialect_of!(self is HiveDialect) && 
self.parse_keyword(Keyword::FROM) {

Review Comment:
   I see, oh yeah afaict it seems to be the case that from [the 
link](https://docs-archive.cloudera.com/HDPDocuments/HDP3/HDP-3.0.1/using-hiveql/content/hive_create_a_table_using_a_cte.html)
 the syntax was misinterpreted, the `FROM` doesn't belong to the CTE, rather 
its a variant of from_first (at least for insert statements).
   
   I think ideally we would introduce a 
`self.dialect.supports_from_first_insert` method that hive flags and the `from` 
being set here moves from the cte to the insert statement. The latter might be 
out of scope for the MR however, so maybe we can just add a comment in the hive 
dialect method on the former mentioning that its incomplete/incorrect?



##########
tests/sqlparser_common.rs:
##########
@@ -16118,6 +16073,129 @@ fn test_select_from_first() {
     }
 }
 
+#[test]
+fn test_select_from_first_with_cte() {
+    let dialects = all_dialects_where(|d| d.supports_from_first_select());
+    let q = "WITH test AS (FROM t SELECT a) FROM test SELECT 1";
+
+    let ast = dialects.verified_query(q);
+
+    let expected = Query {

Review Comment:
   I wonder are we asserting something specific about the AST? It would be good 
to skip the assertion since the struct is quite verbose - thinking if its 
sufficient to only assert that the query parses and round trip. Otherwise if we 
can rewrite/split the assertion to only assert the components we're interested 
in



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