Skip to content

Only parse FROM identifier in CTE if using Hive#2241

Merged
iffyio merged 2 commits intoapache:mainfrom
Viicos:vp/cte-hive
Apr 15, 2026
Merged

Only parse FROM identifier in CTE if using Hive#2241
iffyio merged 2 commits intoapache:mainfrom
Viicos:vp/cte-hive

Conversation

@Viicos
Copy link
Copy Markdown
Contributor

@Viicos Viicos commented Feb 25, 2026

Fixes #2236.

Comment thread src/parser/mod.rs Outdated
}
};
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!

@Viicos
Copy link
Copy Markdown
Contributor Author

Viicos commented Apr 1, 2026

Hi @iffyio,

Just a gentle ping on this one, as it prevents properly parsing FROM first queries when used with a CTE, which can be fairly common considering FROM first queries are enabled on the generic dialect. Thanks in advance!

Copy link
Copy Markdown
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

Sorry for the delay gettting back to this @Viicos

Comment thread src/parser/mod.rs Outdated
}
};
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.

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?

Comment thread tests/sqlparser_common.rs Outdated
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.

Comment thread tests/sqlparser_common.rs Outdated

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.

Copy link
Copy Markdown
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks @Viicos!

@iffyio iffyio added this pull request to the merge queue Apr 15, 2026
Merged via the queue into apache:main with commit 93bc80c Apr 15, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HiveQL, CTEs and FROM first SELECT statements

2 participants