-
Notifications
You must be signed in to change notification settings - Fork 52
Split keywords into reserved and non-reserved sets #282
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package parser | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestReservedKeywordsAreKeywords(t *testing.T) { | ||
| for _, kw := range reservedKeywords.Members() { | ||
| require.True(t, keywords.Contains(kw), | ||
| "reserved keyword %q is missing from the keywords set", kw) | ||
| } | ||
| } | ||
|
|
||
| // TestReservedKeywordRejectedAsIdentifier asserts that a reserved keyword no | ||
| // longer silently fills an identifier slot: a missing name before a clause | ||
| // keyword must fail at the keyword instead of swallowing it (the bug class | ||
| // behind #268/#269: e.g. `SELECT a FROM WHERE b = 1` used to parse FROM as a | ||
| // bare alias of `a`). | ||
| func TestReservedKeywordRejectedAsIdentifier(t *testing.T) { | ||
| cases := []string{ | ||
| "SELECT a FROM WHERE b = 1", // FROM must not become an alias of `a` | ||
| "SELECT a, b FROM GROUP BY a", // FROM must not become an alias of `b` | ||
| "INSERT INTO SELECT 1", // SELECT must not become a table name | ||
| "SELECT a AS FROM t", // explicit alias FROM consumes the clause keyword | ||
| "SELECT a FROM t JOIN ON a = b", // ON must not become a table name | ||
| "CREATE TABLE t (from String) ENGINE=Memory", // reserved keyword as column name needs quoting | ||
| } | ||
| for _, sql := range cases { | ||
| t.Run(sql, func(t *testing.T) { | ||
| _, err := NewParser(sql).ParseStmts() | ||
| require.Error(t, err) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestNonReservedKeywordAsIdentifier asserts that non-reserved keywords keep | ||
| // working as identifiers anywhere an identifier is expected. | ||
| func TestNonReservedKeywordAsIdentifier(t *testing.T) { | ||
| cases := []string{ | ||
| "SELECT key FROM t", | ||
| "SELECT date, first, last, timestamp FROM t", | ||
| "CREATE TABLE t (key String, date Date) ENGINE=Memory", | ||
| "SELECT t.key FROM t", | ||
| "SELECT if(a, 1, 2), any(b), left(c, 1) FROM t", | ||
| "SELECT * FROM t WHERE key = 1", | ||
| } | ||
| for _, sql := range cases { | ||
| t.Run(sql, func(t *testing.T) { | ||
| _, err := NewParser(sql).ParseStmts() | ||
| require.NoError(t, err) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestReservedKeywordInDisambiguatedPositions asserts that reserved keywords | ||
| // are still accepted as names where context proves they cannot start a clause: | ||
| // after AS, after a dot in a qualified name, lookahead-disambiguated select | ||
| // items, query parameters, window names, and GRANT options. | ||
| func TestReservedKeywordInDisambiguatedPositions(t *testing.T) { | ||
| cases := []string{ | ||
| "SELECT 1 AS from", | ||
| "SELECT 1 AS interval, 2 AS from, 3 AS limit", | ||
| "SELECT * FROM t AS from", | ||
| "SELECT a FROM db.from", | ||
| "SELECT t.from FROM t", | ||
| "SELECT a, limit FROM t", | ||
| "SELECT case;", | ||
| "SELECT limit", | ||
| "SELECT a FROM t WHERE ts < {end:UInt32}", | ||
| "SELECT sum(x) OVER (order) FROM t WINDOW order AS (PARTITION BY team)", | ||
| "SELECT sum(x) OVER order FROM t WINDOW order AS (PARTITION BY team)", | ||
| "GRANT SELECT(x) ON db.table TO john WITH GRANT OPTION", | ||
| } | ||
| for _, sql := range cases { | ||
| t.Run(sql, func(t *testing.T) { | ||
| _, err := NewParser(sql).ParseStmts() | ||
| require.NoError(t, err) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestReservedOperatorKeywordsAreCallable covers the regressions from the | ||
| // review of #275: operator keywords double as ordinary ClickHouse function | ||
| // names and must stay callable when followed by '('. | ||
| func TestReservedOperatorKeywordsAreCallable(t *testing.T) { | ||
| inputs := []string{ | ||
| "SELECT and(a, b) FROM t", | ||
| "SELECT or(a, b) FROM t", | ||
| "SELECT in(1, [1])", | ||
| "SELECT like(s, '%a%') FROM t", | ||
| "SELECT ilike(s, '%a%') FROM t", | ||
| } | ||
| for _, sql := range inputs { | ||
| t.Run(sql, func(t *testing.T) { | ||
| stmts, err := NewParser(sql).ParseStmts() | ||
| require.NoError(t, err) | ||
| require.Len(t, stmts, 1) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestReservedKeywordAliasesAfterAs covers the review regressions of #275: | ||
| // AS proves the next token is an alias name, so even reserved keywords are | ||
| // accepted in expression lists, ORDER BY, and non-parenthesized CTEs. | ||
| func TestReservedKeywordAliasesAfterAs(t *testing.T) { | ||
| inputs := []string{ | ||
| "SELECT (1 AS from)", | ||
| "SELECT sum(x AS from) FROM t", | ||
| "SELECT a FROM t ORDER BY x AS from", | ||
| "WITH 1 AS from SELECT from", | ||
| } | ||
| for _, sql := range inputs { | ||
| t.Run(sql, func(t *testing.T) { | ||
| stmts, err := NewParser(sql).ParseStmts() | ||
| require.NoError(t, err) | ||
| require.Len(t, stmts, 1) | ||
| }) | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.