Skip to content
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

fix an expression bug for hashbuild operator. #19572

Merged
merged 9 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/sql/colexec/evalExpression.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func NewExpressionExecutor(proc *process.Process, planExpr *plan.Expr) (Expressi
colIndex: int(t.Col.ColPos),
typ: typ,
}
// [issue#19574]
// if < 0, it's special for agg or others.
if ce.relIndex < 0 {
ce.relIndex = 0
}
return ce, nil

case *plan.Expr_P:
Expand Down
61 changes: 44 additions & 17 deletions pkg/sql/compile/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1522,16 +1522,43 @@ func constructJoinBuildOperator(c *Compile, op vm.Operator, mcpu int32) vm.Opera
}
}

// If the join condition is table1.col = table2.col.
// for hash build operator, we only get table2's data, the origin relation index for right-condition is 1 but wrong.
//
// rewriteJoinExprToHashBuildExpr set the relation index to be 0 for resolving this problem.
func rewriteJoinExprToHashBuildExpr(src []*plan.Expr) []*plan.Expr {
var doRelIndexRewrite func(expr *plan.Expr)
doRelIndexRewrite = func(expr *plan.Expr) {
switch t := expr.Expr.(type) {
case *plan.Expr_F:
for i := range t.F.Args {
doRelIndexRewrite(t.F.Args[i])
}
case *plan.Expr_List:
for i := range t.List.List {
doRelIndexRewrite(t.List.List[i])
}
case *plan.Expr_Col:
t.Col.RelPos = 0
}
}

dst := make([]*plan.Expr, len(src))
for i := range src {
dst[i] = plan2.DeepCopyExpr(src[i])
doRelIndexRewrite(dst[i])
}
return dst
}

func constructHashBuild(op vm.Operator, proc *process.Process, mcpu int32) *hashbuild.HashBuild {
// XXX BUG
// relation index of arg.Conditions should be rewritten to 0 here.
ret := hashbuild.NewArgument()

switch op.OpType() {
case vm.Anti:
arg := op.(*anti.AntiJoin)
ret.NeedHashMap = true
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.HashOnPK = arg.HashOnPK
if arg.Cond == nil {
ret.NeedBatches = false
Expand All @@ -1545,7 +1572,7 @@ func constructHashBuild(op vm.Operator, proc *process.Process, mcpu int32) *hash
case vm.Join:
arg := op.(*join.InnerJoin)
ret.NeedHashMap = true
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.HashOnPK = arg.HashOnPK

// to find if hashmap need to keep build batches for probe
Expand All @@ -1569,7 +1596,7 @@ func constructHashBuild(op vm.Operator, proc *process.Process, mcpu int32) *hash
case vm.Left:
arg := op.(*left.LeftJoin)
ret.NeedHashMap = true
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.NeedBatches = true
ret.HashOnPK = arg.HashOnPK
ret.NeedAllocateSels = true
Expand All @@ -1581,7 +1608,7 @@ func constructHashBuild(op vm.Operator, proc *process.Process, mcpu int32) *hash
case vm.Right:
arg := op.(*right.RightJoin)
ret.NeedHashMap = true
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.NeedBatches = true
ret.HashOnPK = arg.HashOnPK
ret.NeedAllocateSels = true
Expand All @@ -1593,7 +1620,7 @@ func constructHashBuild(op vm.Operator, proc *process.Process, mcpu int32) *hash
case vm.RightSemi:
arg := op.(*rightsemi.RightSemi)
ret.NeedHashMap = true
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.NeedBatches = true
ret.HashOnPK = arg.HashOnPK
ret.NeedAllocateSels = true
Expand All @@ -1605,7 +1632,7 @@ func constructHashBuild(op vm.Operator, proc *process.Process, mcpu int32) *hash
case vm.RightAnti:
arg := op.(*rightanti.RightAnti)
ret.NeedHashMap = true
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.NeedBatches = true
ret.HashOnPK = arg.HashOnPK
ret.NeedAllocateSels = true
Expand All @@ -1617,7 +1644,7 @@ func constructHashBuild(op vm.Operator, proc *process.Process, mcpu int32) *hash
case vm.Semi:
arg := op.(*semi.SemiJoin)
ret.NeedHashMap = true
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.HashOnPK = arg.HashOnPK
if arg.Cond == nil {
ret.NeedBatches = false
Expand All @@ -1634,7 +1661,7 @@ func constructHashBuild(op vm.Operator, proc *process.Process, mcpu int32) *hash
case vm.Single:
arg := op.(*single.SingleJoin)
ret.NeedHashMap = true
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.NeedBatches = true
ret.HashOnPK = arg.HashOnPK
ret.NeedAllocateSels = true
Expand Down Expand Up @@ -1674,7 +1701,7 @@ func constructShuffleBuild(op vm.Operator, proc *process.Process) *shufflebuild.
switch op.OpType() {
case vm.Anti:
arg := op.(*anti.AntiJoin)
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.HashOnPK = arg.HashOnPK
if arg.Cond == nil {
ret.NeedBatches = false
Expand All @@ -1691,7 +1718,7 @@ func constructShuffleBuild(op vm.Operator, proc *process.Process) *shufflebuild.

case vm.Join:
arg := op.(*join.InnerJoin)
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.HashOnPK = arg.HashOnPK

// to find if hashmap need to keep build batches for probe
Expand All @@ -1715,7 +1742,7 @@ func constructShuffleBuild(op vm.Operator, proc *process.Process) *shufflebuild.

case vm.Left:
arg := op.(*left.LeftJoin)
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.NeedBatches = true
ret.HashOnPK = arg.HashOnPK
ret.NeedAllocateSels = true
Expand All @@ -1727,7 +1754,7 @@ func constructShuffleBuild(op vm.Operator, proc *process.Process) *shufflebuild.

case vm.Right:
arg := op.(*right.RightJoin)
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.NeedBatches = true
ret.HashOnPK = arg.HashOnPK
ret.NeedAllocateSels = true
Expand All @@ -1739,7 +1766,7 @@ func constructShuffleBuild(op vm.Operator, proc *process.Process) *shufflebuild.

case vm.RightSemi:
arg := op.(*rightsemi.RightSemi)
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.NeedBatches = true
ret.HashOnPK = arg.HashOnPK
ret.NeedAllocateSels = true
Expand All @@ -1751,7 +1778,7 @@ func constructShuffleBuild(op vm.Operator, proc *process.Process) *shufflebuild.

case vm.RightAnti:
arg := op.(*rightanti.RightAnti)
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.NeedBatches = true
ret.HashOnPK = arg.HashOnPK
ret.NeedAllocateSels = true
Expand All @@ -1763,7 +1790,7 @@ func constructShuffleBuild(op vm.Operator, proc *process.Process) *shufflebuild.

case vm.Semi:
arg := op.(*semi.SemiJoin)
ret.Conditions = arg.Conditions[1]
ret.Conditions = rewriteJoinExprToHashBuildExpr(arg.Conditions[1])
ret.HashOnPK = arg.HashOnPK
if arg.Cond == nil {
ret.NeedBatches = false
Expand Down
Loading