-
Notifications
You must be signed in to change notification settings - Fork 714
Make clickhouse tuples work #2337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alrevuelta
wants to merge
1
commit into
apache:main
Choose a base branch
from
alrevuelta:tuples-clickhouse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1381,10 +1381,18 @@ impl<'a> Tokenizer<'a> { | |
| return Ok(Some(Token::HexStringLiteral(s2))); | ||
| } | ||
|
|
||
| // match one period | ||
| if let Some('.') = chars.peek() { | ||
| s.push('.'); | ||
| chars.next(); | ||
| // match one period. if we've just consumed an integer | ||
| // and the previous token is `.`, we're inside a ClickHouse | ||
| // tuple element access chain, and the trailing dot belongs | ||
| // to the chain, not to this number. | ||
| let in_tuple_chain = self.dialect.supports_tuple_element_access() | ||
| && prev_token == Some(&Token::Period) | ||
| && !s.is_empty(); | ||
| if !in_tuple_chain { | ||
| if let Some('.') = chars.peek() { | ||
| s.push('.'); | ||
| chars.next(); | ||
| } | ||
| } | ||
|
|
||
| // If the dialect supports identifiers that start with a numeric prefix | ||
|
|
@@ -1398,6 +1406,26 @@ impl<'a> Tokenizer<'a> { | |
| } | ||
| } | ||
|
|
||
| // ClickHouse-style positional tuple element access: emit `.` as a | ||
| // standalone Period when it follows the LHS of a chain (an | ||
| // identifier, `]`, `)`, or another integer already in the chain), | ||
| // so e.g. `arr[1].1` and `t.1.2` parse as `CompoundFieldAccess` | ||
| // instead of being fused into a decimal literal. | ||
| if s == "." | ||
| && self.dialect.supports_tuple_element_access() | ||
| && matches!( | ||
| prev_token, | ||
| Some( | ||
| Token::Word(_) | ||
| | Token::RBracket | ||
| | Token::RParen | ||
| | Token::Number(_, _) | ||
| ) | ||
| ) | ||
| { | ||
| return Ok(Some(Token::Period)); | ||
| } | ||
|
|
||
| // Consume fractional digits. | ||
| s += &peeking_next_take_while(chars, |ch, next_ch| { | ||
| ch.is_ascii_digit() || is_number_separator(ch, next_ch) | ||
|
|
@@ -4303,6 +4331,98 @@ mod tests { | |
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tokenize_clickhouse_tuple_element_access() { | ||
| let dialects = all_dialects_where(|dialect| dialect.supports_tuple_element_access()); | ||
|
|
||
| // After a Word, RBracket, or RParen, `.<digit>` is split into `Period` | ||
| // and a separate integer `Number`, so the parser can build a | ||
| // CompoundFieldAccess instead of seeing a single decimal literal. | ||
| dialects.tokenizes_to( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure I follow the patch/tests - are we saying that without this PR, a sql like |
||
| "t.1", | ||
| vec![ | ||
| Token::make_word("t", None), | ||
| Token::Period, | ||
| Token::Number("1".to_string(), false), | ||
| ], | ||
| ); | ||
|
|
||
| dialects.tokenizes_to( | ||
| "arr[1].2", | ||
| vec![ | ||
| Token::make_word("arr", None), | ||
| Token::LBracket, | ||
| Token::Number("1".to_string(), false), | ||
| Token::RBracket, | ||
| Token::Period, | ||
| Token::Number("2".to_string(), false), | ||
| ], | ||
| ); | ||
|
|
||
| dialects.tokenizes_to( | ||
| "(1,2).2", | ||
| vec![ | ||
| Token::LParen, | ||
| Token::Number("1".to_string(), false), | ||
| Token::Comma, | ||
| Token::Number("2".to_string(), false), | ||
| Token::RParen, | ||
| Token::Period, | ||
| Token::Number("2".to_string(), false), | ||
| ], | ||
| ); | ||
|
|
||
| // Nested access `tup.1.2` (Tuple of Tuple) — the rule must re-fire on | ||
| // the second dot, and the integer between the two dots must not eat | ||
| // the trailing dot as a decimal fraction. | ||
| dialects.tokenizes_to( | ||
| "t.1.2", | ||
| vec![ | ||
| Token::make_word("t", None), | ||
| Token::Period, | ||
| Token::Number("1".to_string(), false), | ||
| Token::Period, | ||
| Token::Number("2".to_string(), false), | ||
| ], | ||
| ); | ||
|
|
||
| // Decimal literals must remain untouched: the previous token is | ||
| // either whitespace or a number, never the LHS of an access chain. | ||
| dialects.tokenizes_to( | ||
| "SELECT 0.5", | ||
| vec![ | ||
| Token::make_keyword("SELECT"), | ||
| Token::Whitespace(Whitespace::Space), | ||
| Token::Number("0.5".to_string(), false), | ||
| ], | ||
| ); | ||
|
|
||
| dialects.tokenizes_to( | ||
| "SELECT .5", | ||
| vec![ | ||
| Token::make_keyword("SELECT"), | ||
| Token::Whitespace(Whitespace::Space), | ||
| Token::Number(".5".to_string(), false), | ||
| ], | ||
| ); | ||
|
|
||
| // Regression: dialects without the flag keep the old behavior. The | ||
| // dot and digit fuse into a single decimal-shaped Number token. | ||
| let tokens = Tokenizer::new(&GenericDialect {}, "arr[1].2") | ||
| .tokenize() | ||
| .unwrap(); | ||
| assert_eq!( | ||
| tokens, | ||
| vec![ | ||
| Token::make_word("arr", None), | ||
| Token::LBracket, | ||
| Token::Number("1".to_string(), false), | ||
| Token::RBracket, | ||
| Token::Number(".2".to_string(), false), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn tokenize_period_underscore() { | ||
| let sql = String::from("SELECT table._col"); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this behavior differ from
parse_compound_exprhandling (if so how, on a first glance they look identical syntax wise)?