-
Notifications
You must be signed in to change notification settings - Fork 51
Fix position tracking across ratio, IS NULL, GROUP BY, ALTER and dictionary nodes #283
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 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
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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package parser | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func parseOneStmt(t *testing.T, sql string) Expr { | ||
| t.Helper() | ||
| stmts, err := NewParser(sql).ParseStmts() | ||
| require.NoError(t, err) | ||
| require.Len(t, stmts, 1) | ||
| return stmts[0] | ||
| } | ||
|
|
||
| func TestRatioExprPositions(t *testing.T) { | ||
| sql := "SELECT * FROM t SAMPLE 1/2" | ||
| stmt := parseOneStmt(t, sql).(*SelectQuery) | ||
| sample := stmt.From.Expr.(*JoinTableExpr).SampleRatio | ||
| require.NotNil(t, sample) | ||
| ratio := sample.Ratio | ||
| require.NotNil(t, ratio.Denominator) | ||
| // numerator "1" at offset 23, denominator "2" at offset 25 — the spans | ||
| // must not overlap (the denominator used to inherit the numerator's pos) | ||
| require.Equal(t, Pos(23), ratio.Numerator.Pos()) | ||
| require.Equal(t, Pos(24), ratio.Numerator.End()) | ||
| require.Equal(t, Pos(25), ratio.Denominator.Pos()) | ||
| require.Equal(t, Pos(26), ratio.Denominator.End()) | ||
| } | ||
|
|
||
| func TestGroupByClauseEnd(t *testing.T) { | ||
| sql := "SELECT a FROM t GROUP BY a WITH TOTALS" | ||
| stmt := parseOneStmt(t, sql).(*SelectQuery) | ||
| require.NotNil(t, stmt.GroupBy) | ||
| // the clause ends at the TOTALS keyword, not at the next token's start | ||
| require.Equal(t, Pos(len(sql)), stmt.GroupBy.End()) | ||
|
|
||
| sql = "SELECT a FROM t GROUP BY a" | ||
| stmt = parseOneStmt(t, sql).(*SelectQuery) | ||
| require.Equal(t, Pos(len(sql)), stmt.GroupBy.End()) | ||
|
|
||
| sql = "SELECT a FROM t GROUP BY ALL" | ||
| stmt = parseOneStmt(t, sql).(*SelectQuery) | ||
| require.Equal(t, Pos(len(sql)), stmt.GroupBy.End()) | ||
| } | ||
|
|
||
| func TestIsNullExprPositions(t *testing.T) { | ||
| sql := "SELECT a IS NULL" | ||
| stmt := parseOneStmt(t, sql).(*SelectQuery) | ||
| isNull := stmt.SelectItems[0].Expr.(*IsNullExpr) | ||
| // the node spans `a IS NULL`: from the operand to the end of NULL | ||
| require.Equal(t, Pos(7), isNull.Pos()) | ||
| require.Equal(t, Pos(len(sql)), isNull.End()) | ||
| require.Equal(t, Pos(9), isNull.IsPos) // position of IS | ||
|
|
||
| sql = "SELECT a IS NOT NULL" | ||
| stmt = parseOneStmt(t, sql).(*SelectQuery) | ||
| isNotNull := stmt.SelectItems[0].Expr.(*IsNotNullExpr) | ||
| require.Equal(t, Pos(7), isNotNull.Pos()) | ||
| require.Equal(t, Pos(len(sql)), isNotNull.End()) | ||
| require.Equal(t, Pos(9), isNotNull.IsPos) | ||
| } | ||
|
|
||
| func TestAlterDetachPartitionPos(t *testing.T) { | ||
| sql := "ALTER TABLE t DETACH PARTITION p" | ||
| stmt := parseOneStmt(t, sql).(*AlterTable) | ||
| detach := stmt.AlterExprs[0].(*AlterTableDetachPartition) | ||
| // the clause starts at the DETACH keyword, like every sibling clause | ||
| require.Equal(t, Pos(14), detach.Pos()) | ||
| } | ||
|
|
||
| func TestAlterDropPartitionEndIncludesSettings(t *testing.T) { | ||
| sql := "ALTER TABLE t DROP PARTITION p SETTINGS mutations_sync=1" | ||
| stmt := parseOneStmt(t, sql).(*AlterTable) | ||
| drop := stmt.AlterExprs[0].(*AlterTableDropPartition) | ||
| require.NotNil(t, drop.Settings) | ||
| // End() used to discard the Settings end and stop at the partition | ||
| require.Equal(t, drop.Settings.End(), drop.End()) | ||
| require.Greater(t, drop.End(), drop.Partition.End()) | ||
| } | ||
|
|
||
| func TestDictionaryAttributeEnd(t *testing.T) { | ||
| sql := "CREATE DICTIONARY d (user_id UInt64 IS_OBJECT_ID) PRIMARY KEY user_id SOURCE(CLICKHOUSE()) LAYOUT(FLAT()) LIFETIME(300)" | ||
| stmt := parseOneStmt(t, sql).(*CreateDictionary) | ||
| attrs := stmt.Schema.Attributes | ||
| require.Len(t, attrs, 1) | ||
| // the attribute ends at IS_OBJECT_ID; the old implementation returned | ||
| // NamePos + len("IS_OBJECT_ID"), landing in the middle of the type | ||
| require.Equal(t, "IS_OBJECT_ID", sql[36:48]) | ||
| require.Equal(t, Pos(48), attrs[0].End()) | ||
| } |
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
2 changes: 1 addition & 1 deletion
2
parser/testdata/ddl/output/alter_table_drop_detach_partition.sql.golden.json
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.
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.
When parsing
GROUP BY CUBE(...),ROLLUP(...), orGROUPING SETS(...)with no trailingWITHmodifier,expris aParamExprList, whoseEnd()is the position of the closing)rather than the byte after it. AssigningGroupByEndfromexpr.End()makes bothGroupByClause.End()and the enclosingSelectQuery.End()one byte short (for example,SELECT a FROM t GROUP BY CUBE(a)reports 31 instead oflen(sql)==32), so these supported GROUP BY forms still exclude their final token from the span.Useful? React with 👍 / 👎.