-
Notifications
You must be signed in to change notification settings - Fork 2.3k
More union merging #13743
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
More union merging #13743
Changes from all commits
Commits
Show all changes
3 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
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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ import ( | |
| "vitess.io/vitess/go/vt/sqlparser" | ||
| "vitess.io/vitess/go/vt/vtgate/engine" | ||
| "vitess.io/vitess/go/vt/vtgate/planbuilder/operators/ops" | ||
| "vitess.io/vitess/go/vt/vtgate/planbuilder/operators/rewrite" | ||
| "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" | ||
| ) | ||
|
|
||
|
|
@@ -108,40 +109,70 @@ func mergeUnionInputsInOrder(ctx *plancontext.PlanningContext, op *Union) ([]ops | |
| // If they can be merged, a new operator with the merged routing is returned | ||
| // If they cannot be merged, nil is returned. | ||
| // this function is very similar to mergeJoinInputs | ||
| func mergeUnionInputs(ctx *plancontext.PlanningContext, lhs, rhs ops.Operator, lhsExprs, rhsExprs sqlparser.SelectExprs, distinct bool) (ops.Operator, sqlparser.SelectExprs, error) { | ||
| func mergeUnionInputs( | ||
| ctx *plancontext.PlanningContext, | ||
| lhs, rhs ops.Operator, | ||
| lhsExprs, rhsExprs sqlparser.SelectExprs, | ||
| distinct bool, | ||
| ) (ops.Operator, sqlparser.SelectExprs, error) { | ||
| lhsRoute, rhsRoute, routingA, routingB, a, b, sameKeyspace := prepareInputRoutes(lhs, rhs) | ||
| if lhsRoute == nil { | ||
| return nil, nil, nil | ||
| } | ||
|
|
||
| switch { | ||
| // if either side is a dual query, we can always merge them together | ||
| case b == dual: | ||
| // an unsharded/reference route can be merged with anything going to that keyspace | ||
| case b == dual || (b == anyShard && sameKeyspace): | ||
| return createMergedUnion(ctx, lhsRoute, rhsRoute, lhsExprs, rhsExprs, distinct, routingA) | ||
| case a == dual: | ||
| case a == dual || (a == anyShard && sameKeyspace): | ||
|
Comment on lines
+126
to
+128
Member
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. nit: parenthesis around the
Member
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. I think the parenthesis improves readability |
||
| return createMergedUnion(ctx, lhsRoute, rhsRoute, lhsExprs, rhsExprs, distinct, routingB) | ||
| case a == anyShard && sameKeyspace: | ||
|
|
||
| case a == none: | ||
| return createMergedUnion(ctx, lhsRoute, rhsRoute, lhsExprs, rhsExprs, distinct, routingB) | ||
| case b == anyShard && sameKeyspace: | ||
| case b == none: | ||
| return createMergedUnion(ctx, lhsRoute, rhsRoute, lhsExprs, rhsExprs, distinct, routingA) | ||
|
|
||
| case a == sharded && b == sharded && sameKeyspace: | ||
| // If the two routes fully match, they can be merged together. | ||
| tblA := routingA.(*ShardedRouting) | ||
| tblB := routingB.(*ShardedRouting) | ||
| if tblA.RouteOpCode == engine.Scatter && tblB.RouteOpCode == engine.Scatter { | ||
| return createMergedUnion(ctx, lhsRoute, rhsRoute, lhsExprs, rhsExprs, distinct, routingB) | ||
| } | ||
| if tblA.RouteOpCode != engine.EqualUnique || tblB.RouteOpCode != engine.EqualUnique { | ||
| break | ||
| res, exprs, err := tryMergeUnionShardedRouting(ctx, lhsRoute, rhsRoute, lhsExprs, rhsExprs, distinct) | ||
| if err != nil || res != nil { | ||
| return res, exprs, err | ||
| } | ||
| } | ||
| return nil, nil, nil | ||
| } | ||
|
|
||
| func tryMergeUnionShardedRouting( | ||
| ctx *plancontext.PlanningContext, | ||
| routeA, routeB *Route, | ||
| exprsA, exprsB sqlparser.SelectExprs, | ||
| distinct bool, | ||
| ) (ops.Operator, sqlparser.SelectExprs, error) { | ||
| tblA := routeA.Routing.(*ShardedRouting) | ||
| tblB := routeB.Routing.(*ShardedRouting) | ||
|
|
||
| scatterA := tblA.RouteOpCode == engine.Scatter | ||
| scatterB := tblB.RouteOpCode == engine.Scatter | ||
| uniqueA := tblA.RouteOpCode == engine.EqualUnique | ||
| uniqueB := tblB.RouteOpCode == engine.EqualUnique | ||
|
|
||
| switch { | ||
| case scatterA: | ||
| return createMergedUnion(ctx, routeA, routeB, exprsA, exprsB, distinct, tblA) | ||
|
|
||
| case scatterB: | ||
| return createMergedUnion(ctx, routeA, routeB, exprsA, exprsB, distinct, tblB) | ||
|
|
||
| case uniqueA && uniqueB: | ||
| aVdx := tblA.SelectedVindex() | ||
| bVdx := tblB.SelectedVindex() | ||
| aExpr := tblA.VindexExpressions() | ||
| bExpr := tblB.VindexExpressions() | ||
| if aVdx == bVdx && gen4ValuesEqual(ctx, aExpr, bExpr) { | ||
| return createMergedUnion(ctx, lhsRoute, rhsRoute, lhsExprs, rhsExprs, distinct, routingB) | ||
| return createMergedUnion(ctx, routeA, routeB, exprsA, exprsB, distinct, tblA) | ||
| } | ||
| } | ||
|
|
||
| return nil, nil, nil | ||
| } | ||
|
|
||
|
|
@@ -187,3 +218,42 @@ func createMergedUnion( | |
| Routing: routing, | ||
| }, selectExprs, nil | ||
| } | ||
|
|
||
| func compactUnion(u *Union) *rewrite.ApplyResult { | ||
| if u.distinct { | ||
| // first we remove unnecessary DISTINCTs | ||
| for idx, source := range u.Sources { | ||
| d, ok := source.(*Distinct) | ||
| if !ok || !d.Required { | ||
| continue | ||
| } | ||
| u.Sources[idx] = d.Source | ||
| } | ||
| } | ||
|
|
||
| var newSources []ops.Operator | ||
| var newSelects []sqlparser.SelectExprs | ||
| merged := false | ||
|
|
||
| for idx, source := range u.Sources { | ||
| other, ok := source.(*Union) | ||
|
|
||
| if ok && (u.distinct || !other.distinct) { | ||
| newSources = append(newSources, other.Sources...) | ||
| newSelects = append(newSelects, other.Selects...) | ||
| merged = true | ||
| continue | ||
| } | ||
|
|
||
| newSources = append(newSources, source) | ||
| newSelects = append(newSelects, u.Selects[idx]) | ||
| } | ||
|
|
||
| if !merged { | ||
| return rewrite.SameTree | ||
| } | ||
|
|
||
| u.Sources = newSources | ||
| u.Selects = newSelects | ||
| return rewrite.NewTree("merged UNIONs", u) | ||
| } | ||
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
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.
nit: we can reuse types here