Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13958,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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I follow the intent of the PR, is it rather that hive should support supports_from_first_select (i.e. what's the difference in behavior from hive vs other dialects)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave more context in the issue I created; I'm not knowledgeable enough about Hive to understand if it should support FROM first selects, and it is unclear (see also #235 (comment)) why this FROM token parsing was introduced here. Maybe the intent was indeed to support FROM first selects, but I'd rather not break things here so that's why I only gated this FROM parsing for Hive.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, oh yeah afaict it seems to be the case that from the link 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with this option, I can also create a follow up issue along with the comment (or just the comment if you are happy with it).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue sounds great as well thanks!

cte.from = Some(self.parse_identifier()?);
}
Ok(cte)
Expand Down
123 changes: 123 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16073,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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified to only assert the query body's projection and from clauses.

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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the existing test_select_from_first() test for this one, but this makes sense. I removed the assertion from both tests.

}

#[test]
fn test_geometric_unary_operators() {
// Number of points in path or polygon
Expand Down