github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/auto_increase.go (about) 1 // Copyright 2021 - 2022 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 function 16 17 import ( 18 "context" 19 20 "github.com/matrixorigin/matrixone/pkg/common/moerr" 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 22 "github.com/matrixorigin/matrixone/pkg/defines" 23 "github.com/matrixorigin/matrixone/pkg/incrservice" 24 "github.com/matrixorigin/matrixone/pkg/sql/plan/function/functionUtil" 25 "github.com/matrixorigin/matrixone/pkg/vm/engine" 26 "github.com/matrixorigin/matrixone/pkg/vm/process" 27 ) 28 29 // XXX confused function. 30 func builtInInternalAutoIncrement(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error { 31 p1 := vector.GenerateFunctionStrParameter(parameters[0]) 32 p2 := vector.GenerateFunctionStrParameter(parameters[1]) 33 rs := vector.MustFunctionResult[uint64](result) 34 35 eng := proc.Ctx.Value(defines.EngineKey{}).(engine.Engine) 36 for i := uint64(0); i < uint64(length); i++ { 37 s1, null1 := p1.GetStrValue(i) 38 s2, null2 := p2.GetStrValue(i) 39 if null1 || null2 { 40 return moerr.NewInvalidInput(proc.Ctx, "unsupported parameter `null` for internal_auto_increment") 41 } 42 43 dbName := functionUtil.QuickBytesToStr(s1) 44 tableName := functionUtil.QuickBytesToStr(s2) 45 46 database, err := eng.Database(proc.Ctx, dbName, proc.TxnOperator) 47 if err != nil { 48 return moerr.NewInvalidInput(proc.Ctx, "Database '%s' does not exist", dbName) 49 } 50 relation, err := database.Relation(proc.Ctx, tableName, nil) 51 if err != nil { 52 return moerr.NewInvalidInput(proc.Ctx, "Table '%s' does not exist in database '%s'", tableName, dbName) 53 } 54 tableId := relation.GetTableID(proc.Ctx) 55 engineDefs, err := relation.TableDefs(proc.Ctx) 56 if err != nil { 57 return err 58 } 59 autoIncrCol := getTableAutoIncrCol(engineDefs, tableName) 60 if autoIncrCol != "" { 61 autoIncrement, err := getCurrentValue( 62 proc.Ctx, 63 tableId, 64 autoIncrCol) 65 if err != nil { 66 return err 67 } 68 if err = rs.Append(autoIncrement, false); err != nil { 69 return err 70 } 71 } else { 72 if err = rs.Append(0, true); err != nil { 73 return err 74 } 75 } 76 } 77 return nil 78 } 79 80 func getTableAutoIncrCol( 81 engineDefs []engine.TableDef, 82 tableName string) string { 83 for _, def := range engineDefs { 84 // FIXME: more than one auto cols?? 85 if attr, ok := def.(*engine.AttributeDef); ok && attr.Attr.AutoIncrement { 86 return attr.Attr.Name 87 } 88 } 89 return "" 90 } 91 92 func getCurrentValue( 93 ctx context.Context, 94 tableID uint64, 95 col string) (uint64, error) { 96 return incrservice.GetAutoIncrementService(ctx).CurrentValue( 97 ctx, 98 tableID, 99 col) 100 }