-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Prevent Early Ordering Pushdown to Enable Aggregation Pushdown to MySQL #16278
Merged
Merged
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8fcc835
feat: optimise aggregation with ORDER BY
systay fa00ad6
refactor: clean up code
systay f9a4da5
bug: must check error before cleaning out the join columns
systay 7277e3a
bugfix: make sure to include all push down expressions
systay f3bb968
refactor: clean up code
systay 455e1ee
added benchmark
systay f7c0ef7
remove weightstrings from group by sent to mysql
systay 28a6732
undo accidental edit
systay 7579f3e
Merge remote-tracking branch 'upstream/main' into push-down-aggr
systay a6566b8
move UDF checking to operator transformer
systay e6531e6
bugfix: add HAVING aggregations in a cleaner way
systay dc42b54
Revert "remove weightstrings from group by sent to mysql
systay 1a43e29
Merge remote-tracking branch 'upstream/main' into push-down-aggr
systay 102889a
change from unsupported to internal error
systay 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 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 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 |
---|---|---|
|
@@ -202,19 +202,11 @@ func (aj *ApplyJoin) GetOrdering(ctx *plancontext.PlanningContext) []OrderBy { | |
return aj.LHS.GetOrdering(ctx) | ||
} | ||
|
||
func joinColumnToExpr(column applyJoinColumn) sqlparser.Expr { | ||
return column.Original | ||
} | ||
|
||
func (aj *ApplyJoin) getJoinColumnFor(ctx *plancontext.PlanningContext, orig *sqlparser.AliasedExpr, e sqlparser.Expr, addToGroupBy bool) (col applyJoinColumn) { | ||
defer func() { | ||
col.Original = orig.Expr | ||
}() | ||
lhs := TableID(aj.LHS) | ||
rhs := TableID(aj.RHS) | ||
both := lhs.Merge(rhs) | ||
deps := ctx.SemTable.RecursiveDeps(e) | ||
col.GroupBy = addToGroupBy | ||
|
||
switch { | ||
case deps.IsSolvedBy(lhs): | ||
|
@@ -224,9 +216,12 @@ func (aj *ApplyJoin) getJoinColumnFor(ctx *plancontext.PlanningContext, orig *sq | |
case deps.IsSolvedBy(both): | ||
col = breakExpressionInLHSandRHS(ctx, e, TableID(aj.LHS)) | ||
default: | ||
panic(vterrors.VT13002(sqlparser.String(e))) | ||
panic(vterrors.VT13001(fmt.Sprintf("expression depends on tables outside this join: %s", sqlparser.String(e)))) | ||
} | ||
|
||
col.GroupBy = addToGroupBy | ||
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. This is another bug that was not related to the issue being found, but it caused some plans to be missing group by expressions |
||
col.Original = orig.Expr | ||
|
||
return | ||
} | ||
|
||
|
@@ -323,33 +318,15 @@ func (aj *ApplyJoin) planOffsets(ctx *plancontext.PlanningContext) Operator { | |
} | ||
|
||
func (aj *ApplyJoin) planOffsetFor(ctx *plancontext.PlanningContext, col applyJoinColumn) { | ||
if col.DTColName != nil { | ||
// If DTColName is set, then we already pushed the parts of the expression down while planning. | ||
// We need to use this name and ask the correct side of the join for it. Nothing else is required. | ||
if col.IsPureLeft() { | ||
offset := aj.LHS.AddColumn(ctx, true, col.GroupBy, aeWrap(col.DTColName)) | ||
aj.addOffset(ToLeftOffset(offset)) | ||
} else { | ||
for _, lhsExpr := range col.LHSExprs { | ||
offset := aj.LHS.AddColumn(ctx, true, col.GroupBy, aeWrap(lhsExpr.Expr)) | ||
aj.Vars[lhsExpr.Name] = offset | ||
} | ||
offset := aj.RHS.AddColumn(ctx, true, col.GroupBy, aeWrap(col.DTColName)) | ||
aj.addOffset(ToRightOffset(offset)) | ||
} | ||
return | ||
} | ||
for _, lhsExpr := range col.LHSExprs { | ||
offset := aj.LHS.AddColumn(ctx, true, col.GroupBy, aeWrap(lhsExpr.Expr)) | ||
if col.RHSExpr == nil { | ||
// if we don't have an RHS expr, it means that this is a pure LHS expression | ||
aj.addOffset(ToLeftOffset(offset)) | ||
} else { | ||
if col.IsPureLeft() { | ||
offset := aj.LHS.AddColumn(ctx, true, col.GroupBy, aeWrap(col.GetPureLeftExpr())) | ||
aj.addOffset(ToLeftOffset(offset)) | ||
} else { | ||
for _, lhsExpr := range col.LHSExprs { | ||
offset := aj.LHS.AddColumn(ctx, true, col.GroupBy, aeWrap(lhsExpr.Expr)) | ||
aj.Vars[lhsExpr.Name] = offset | ||
} | ||
} | ||
if col.RHSExpr != nil { | ||
offset := aj.RHS.AddColumn(ctx, true, col.GroupBy, aeWrap(col.RHSExpr)) | ||
offset := aj.RHS.AddColumn(ctx, true, col.GroupBy, aeWrap(col.GetRHSExpr())) | ||
aj.addOffset(ToRightOffset(offset)) | ||
} | ||
} | ||
|
@@ -443,17 +420,17 @@ func (aj *ApplyJoin) findOrAddColNameBindVarName(ctx *plancontext.PlanningContex | |
return bvName | ||
} | ||
|
||
func (a *ApplyJoin) LHSColumnsNeeded(ctx *plancontext.PlanningContext) (needed sqlparser.Exprs) { | ||
func (aj *ApplyJoin) LHSColumnsNeeded(ctx *plancontext.PlanningContext) (needed sqlparser.Exprs) { | ||
f := func(from BindVarExpr) sqlparser.Expr { | ||
return from.Expr | ||
} | ||
for _, jc := range a.JoinColumns.columns { | ||
for _, jc := range aj.JoinColumns.columns { | ||
needed = append(needed, slice.Map(jc.LHSExprs, f)...) | ||
} | ||
for _, jc := range a.JoinPredicates.columns { | ||
for _, jc := range aj.JoinPredicates.columns { | ||
needed = append(needed, slice.Map(jc.LHSExprs, f)...) | ||
} | ||
needed = append(needed, slice.Map(a.ExtraLHSVars, f)...) | ||
needed = append(needed, slice.Map(aj.ExtraLHSVars, f)...) | ||
return ctx.SemTable.Uniquify(needed) | ||
} | ||
|
||
|
@@ -462,7 +439,11 @@ func (jc applyJoinColumn) String() string { | |
lhs := slice.Map(jc.LHSExprs, func(e BindVarExpr) string { | ||
return sqlparser.String(e.Expr) | ||
}) | ||
return fmt.Sprintf("[%s | %s | %s]", strings.Join(lhs, ", "), rhs, sqlparser.String(jc.Original)) | ||
if jc.DTColName == nil { | ||
return fmt.Sprintf("[%s | %s | %s]", strings.Join(lhs, ", "), rhs, sqlparser.String(jc.Original)) | ||
} | ||
|
||
return fmt.Sprintf("[%s | %s | %s | %s]", strings.Join(lhs, ", "), rhs, sqlparser.String(jc.Original), sqlparser.String(jc.DTColName)) | ||
} | ||
|
||
func (jc applyJoinColumn) IsPureLeft() bool { | ||
|
@@ -477,6 +458,20 @@ func (jc applyJoinColumn) IsMixedLeftAndRight() bool { | |
return len(jc.LHSExprs) > 0 && jc.RHSExpr != nil | ||
} | ||
|
||
func (jc applyJoinColumn) GetPureLeftExpr() sqlparser.Expr { | ||
if jc.DTColName != nil { | ||
return jc.DTColName | ||
} | ||
return jc.LHSExprs[0].Expr | ||
} | ||
|
||
func (jc applyJoinColumn) GetRHSExpr() sqlparser.Expr { | ||
if jc.DTColName != nil { | ||
return jc.DTColName | ||
} | ||
return jc.RHSExpr | ||
} | ||
|
||
func (bve BindVarExpr) String() string { | ||
if bve.Name == "" { | ||
return sqlparser.String(bve.Expr) | ||
|
This file contains 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 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 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.
This bug was not related to the rest of the PR, but it caused tests to fail, so it had to go.