Skip to content

Commit 64e4751

Browse files
authored
fix functionid (#20743)
1 parent 44b3761 commit 64e4751

File tree

6 files changed

+850
-349
lines changed

6 files changed

+850
-349
lines changed

pkg/sql/plan/build_update.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ func rewriteUpdateQueryLastNode(builder *QueryBuilder, planCtxs []*dmlPlanCtx, l
145145
} else {
146146
pos := idx + colIdx
147147
if col.OnUpdate != nil && col.OnUpdate.Expr != nil {
148-
lastNode.ProjectList[pos] = col.OnUpdate.Expr
148+
newDefExpr := DeepCopyExpr(col.OnUpdate.Expr)
149+
err = replaceFuncId(builder.GetContext(), newDefExpr)
150+
if err != nil {
151+
return err
152+
}
153+
lastNode.ProjectList[pos] = newDefExpr
149154
}
150155

151156
if col != nil && col.Typ.Id == int32(types.T_enum) {

pkg/sql/plan/build_util.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/matrixorigin/matrixone/pkg/sql/colexec"
3131
"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
3232
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
33+
"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
3334
"github.com/matrixorigin/matrixone/pkg/vm/process"
3435
)
3536

@@ -478,7 +479,33 @@ func getDefaultExpr(ctx context.Context, d *plan.ColDef) (*Expr, error) {
478479
},
479480
}, nil
480481
}
481-
return d.Default.Expr, nil
482+
newDefExpr := DeepCopyExpr(d.Default.Expr)
483+
err := replaceFuncId(ctx, newDefExpr)
484+
return newDefExpr, err
485+
}
486+
487+
func replaceFuncId(ctx context.Context, expr *Expr) error {
488+
switch fun := expr.Expr.(type) {
489+
case *plan.Expr_F:
490+
for _, arg := range fun.F.Args {
491+
err := replaceFuncId(ctx, arg)
492+
if err != nil {
493+
return err
494+
}
495+
}
496+
497+
fnName := fun.F.Func.ObjName
498+
newFID, err := function.GetFunctionIdByName(ctx, fnName)
499+
if err != nil {
500+
return err
501+
}
502+
oldFID, oldIdx := function.DecodeOverloadID(fun.F.Func.Obj)
503+
if oldFID != newFID {
504+
fun.F.Func.Obj = function.EncodeOverloadID(newFID, oldIdx)
505+
}
506+
default:
507+
}
508+
return nil
482509
}
483510

484511
func judgeUnixTimestampReturnType(timestr string) types.T {

pkg/sql/plan/build_util_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2024 Matrix Origin
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package plan
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
23+
"github.com/matrixorigin/matrixone/pkg/pb/plan"
24+
"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
25+
)
26+
27+
func Test_replaceFuncId(t *testing.T) {
28+
case1 := &Expr{
29+
Expr: &plan.Expr_F{
30+
F: &plan.Function{
31+
Func: &ObjectRef{
32+
ObjName: "current_timestamp",
33+
Obj: function.CURRENT_TIMESTAMP,
34+
},
35+
Args: []*Expr{
36+
{
37+
Expr: &plan.Expr_Col{
38+
Col: &plan.ColRef{
39+
RelPos: 1,
40+
ColPos: 10,
41+
Name: "a",
42+
},
43+
},
44+
},
45+
},
46+
},
47+
},
48+
}
49+
50+
err := replaceFuncId(context.Background(), case1)
51+
assert.NoError(t, err)
52+
53+
case1ColDef := &plan.ColDef{
54+
Default: &plan.Default{
55+
Expr: case1,
56+
},
57+
}
58+
case1Expr, err := getDefaultExpr(context.Background(), case1ColDef)
59+
assert.NoError(t, err)
60+
assert.NotNil(t, case1Expr)
61+
}

pkg/sql/plan/function/function.go

+3
Original file line numberDiff line numberDiff line change
@@ -510,3 +510,6 @@ func (selectList *FunctionSelectList) Contains(row uint64) bool {
510510
}
511511
return !selectList.SelectList[row]
512512
}
513+
514+
var EncodeOverloadID = encodeOverloadID
515+
var GetFunctionIdByName = getFunctionIdByName

0 commit comments

Comments
 (0)