Skip to content

Commit

Permalink
cherrypick from 2.0.1hotfix to 2.0dev: use fixed function id (#20830)
Browse files Browse the repository at this point in the history
原因:原先的function id用的是iota递增值。如果在中间新增了函数,会导致后面的id发生变化。
修改:functionid 用const值。新增函数追加用不重复的值。

Approved by: @badboynt1, @m-schen, @sukki37, @aunjgr, @ouyuanning
  • Loading branch information
daviszhen authored Dec 19, 2024
1 parent 1c7e50c commit 5b69b1f
Show file tree
Hide file tree
Showing 6 changed files with 861 additions and 355 deletions.
7 changes: 6 additions & 1 deletion pkg/sql/plan/build_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ func rewriteUpdateQueryLastNode(builder *QueryBuilder, planCtxs []*dmlPlanCtx, l
} else {
pos := idx + colIdx
if col.OnUpdate != nil && col.OnUpdate.Expr != nil {
lastNode.ProjectList[pos] = col.OnUpdate.Expr
newDefExpr := DeepCopyExpr(col.OnUpdate.Expr)
err = replaceFuncId(builder.GetContext(), newDefExpr)
if err != nil {
return err
}
lastNode.ProjectList[pos] = newDefExpr
}

if col != nil && col.Typ.Id == int32(types.T_enum) {
Expand Down
29 changes: 28 additions & 1 deletion pkg/sql/plan/build_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/matrixorigin/matrixone/pkg/sql/colexec"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
"github.com/matrixorigin/matrixone/pkg/vm/process"
)

Expand Down Expand Up @@ -478,7 +479,33 @@ func getDefaultExpr(ctx context.Context, d *plan.ColDef) (*Expr, error) {
},
}, nil
}
return d.Default.Expr, nil
newDefExpr := DeepCopyExpr(d.Default.Expr)
err := replaceFuncId(ctx, newDefExpr)
return newDefExpr, err
}

func replaceFuncId(ctx context.Context, expr *Expr) error {
switch fun := expr.Expr.(type) {
case *plan.Expr_F:
for _, arg := range fun.F.Args {
err := replaceFuncId(ctx, arg)
if err != nil {
return err
}
}

fnName := fun.F.Func.ObjName
newFID, err := function.GetFunctionIdByName(ctx, fnName)
if err != nil {
return err
}
oldFID, oldIdx := function.DecodeOverloadID(fun.F.Func.Obj)
if oldFID != newFID {
fun.F.Func.Obj = function.EncodeOverloadID(newFID, oldIdx)
}
default:
}
return nil
}

func judgeUnixTimestampReturnType(timestr string) types.T {
Expand Down
61 changes: 61 additions & 0 deletions pkg/sql/plan/build_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2024 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package plan

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/matrixorigin/matrixone/pkg/pb/plan"
"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
)

func Test_replaceFuncId(t *testing.T) {
case1 := &Expr{
Expr: &plan.Expr_F{
F: &plan.Function{
Func: &ObjectRef{
ObjName: "current_timestamp",
Obj: function.CURRENT_TIMESTAMP,
},
Args: []*Expr{
{
Expr: &plan.Expr_Col{
Col: &plan.ColRef{
RelPos: 1,
ColPos: 10,
Name: "a",
},
},
},
},
},
},
}

err := replaceFuncId(context.Background(), case1)
assert.NoError(t, err)

case1ColDef := &plan.ColDef{
Default: &plan.Default{
Expr: case1,
},
}
case1Expr, err := getDefaultExpr(context.Background(), case1ColDef)
assert.NoError(t, err)
assert.NotNil(t, case1Expr)
}
3 changes: 3 additions & 0 deletions pkg/sql/plan/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,6 @@ func (selectList *FunctionSelectList) Contains(row uint64) bool {
}
return !selectList.SelectList[row]
}

var EncodeOverloadID = encodeOverloadID
var GetFunctionIdByName = getFunctionIdByName
Loading

0 comments on commit 5b69b1f

Please sign in to comment.