-
Notifications
You must be signed in to change notification settings - Fork 13
[release-19.0-github] Improve UNION query merging
#163
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
[release-19.0-github] Improve UNION query merging
#163
Conversation
Signed-off-by: Arthur Schreiber <[email protected]>
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.
Pull Request Overview
Backport of UNION query merging improvements to release-19.0, enabling more aggressive merging of UNION and UNION ALL on sharded keyspaces and deduplicating IN-list bind variables.
- Added new test cases covering IN-list, mixed IN/
=, and repeated IN scenarios inselect_cases.jsonandnormalizer_test.go - Extended
tryMergeUnionShardedRoutinginunion_merging.goto merge onEqualUnique,Equal, andINop codes - Enhanced normalizer (
normalizer.go) to cache tuple bind-vars (tupleVals) and updated route planning (route_planning.go) to compareValTupleandListArg - Updated executor tests (
executor_select_test.go) to expect mergedUNION ALLpushdown
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| select_cases.json | Added test cases for UNION merging with IN lists and mixed filters |
| union_merging.go | Expanded merge criteria to include EqualUnique, Equal, and IN |
| route_planning.go | Added support for ValTuple and ListArg equality in gen4ValEqual |
| executor_select_test.go | Updated expected queries for merged UNION ALL scenarios |
| normalizer_test.go | Added test for repeated IN clause bind variable deduplication |
| normalizer.go | Implemented caching of tuple bind variables to avoid duplicate binds |
Comments suppressed due to low confidence (2)
go/vt/sqlparser/normalizer.go:323
- This
:=shadows the outerbvname, so after this blockbvnamemay remain empty. Use a new var (e.g.existing, found := nz.tupleVals[string(key)]) and then assignbvname = existingto avoid shadowing.
if bvname, ok := nz.tupleVals[string(key)]; !ok {
go/vt/sqlparser/normalizer.go:50
- [nitpick] The name
tupleValsis a bit vague; consider renaming to something liketupleBindNameCacheto clarify that it maps serialized tuple keys to bind-variable names.
tupleVals map[string]string
| return createMergedUnion(ctx, routeA, routeB, exprsA, exprsB, distinct, tblB) | ||
|
|
||
| case uniqueA && uniqueB: | ||
| case tblA.RouteOpCode == engine.EqualUnique && tblB.RouteOpCode == engine.EqualUnique: |
Copilot
AI
May 26, 2025
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.
[nitpick] The repeated direct comparisons with fallthrough could be refactored into a helper (e.g. isMergeableOp(op)) or a slice lookup to improve readability and reduce duplication.
| case sqlparser.ValTuple: | ||
| if b, ok := b.(sqlparser.ValTuple); ok { | ||
| return gen4ValuesEqual(ctx, a, b) | ||
| } | ||
|
|
||
| return false | ||
|
|
||
| case sqlparser.ListArg: | ||
| if b, ok := b.(sqlparser.ListArg); ok { | ||
| return a == b | ||
| } | ||
|
|
Copilot
AI
May 26, 2025
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.
[nitpick] Consider combining the ValTuple and ListArg cases into a single case sqlparser.ValTuple, sqlparser.ListArg: block to reduce duplication and centralize the tuple/list comparison logic.
| case sqlparser.ValTuple: | |
| if b, ok := b.(sqlparser.ValTuple); ok { | |
| return gen4ValuesEqual(ctx, a, b) | |
| } | |
| return false | |
| case sqlparser.ListArg: | |
| if b, ok := b.(sqlparser.ListArg); ok { | |
| return a == b | |
| } | |
| case sqlparser.ValTuple, sqlparser.ListArg: | |
| if b, ok := b.(type); ok && a == b { | |
| switch a := a.(type) { | |
| case sqlparser.ValTuple: | |
| return gen4ValuesEqual(ctx, a, b) | |
| case sqlparser.ListArg: | |
| return a == b | |
| } | |
| } | |
| return false |
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.
This suggestion makes no sense and is not valid Go code. b.(type) can not be used outside of a switch statement.
3e8aa8f
into
release-19.0-github
Description
This is a backport of vitessio#18289.
Related Issue(s)
Checklist
Deployment Notes