github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/show_visible_bin.go (about) 1 // Copyright 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 binary 16 17 import ( 18 "fmt" 19 "github.com/matrixorigin/matrixone/pkg/common/moerr" 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 22 "github.com/matrixorigin/matrixone/pkg/pb/plan" 23 "github.com/matrixorigin/matrixone/pkg/vm/process" 24 ) 25 26 const ( 27 onUpdateExpr = iota 28 defaultExpr 29 typNormal 30 typWithLen 31 ) 32 33 func ShowVisibleBin(vec []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 34 if !vec[1].IsScalar() { 35 return nil, moerr.NewInvalidInput(proc.Ctx, "show visible bin, the second argument must be a scalar") 36 } 37 var err error 38 tpSlice := vector.MustTCols[uint8](vec[1]) 39 srcSlice := vector.MustBytesCols(vec[0]) 40 tp := tpSlice[0] 41 resultVec := vector.New(types.T_varchar.ToType()) 42 defer func() { 43 if err != nil { 44 resultVec.Free(proc.Mp()) 45 } 46 }() 47 var ret []string 48 switch tp { 49 case onUpdateExpr: 50 ret, err = showExpr(srcSlice, byOnUpdate) 51 case defaultExpr: 52 ret, err = showExpr(srcSlice, byDefault) 53 case typNormal: 54 ret, err = showType(srcSlice, false) 55 case typWithLen: 56 ret, err = showType(srcSlice, true) 57 default: 58 return nil, moerr.NewNotSupported(proc.Ctx, fmt.Sprintf("show visible bin, the second argument must be in [0, 3], but got %d", tp)) 59 } 60 if err != nil { 61 return nil, err 62 } 63 for i := range ret { 64 err = resultVec.Append([]byte(ret[i]), len(ret[i]) == 0, proc.Mp()) 65 if err != nil { 66 return nil, err 67 } 68 } 69 return resultVec, nil 70 } 71 72 func showType(s [][]byte, showLen bool) ([]string, error) { 73 ret := make([]string, len(s)) 74 for i := range s { 75 tp := new(types.Type) 76 err := types.Decode(s[i], tp) 77 if err != nil { 78 return nil, err 79 } 80 if showLen { 81 ret[i] = fmt.Sprintf("%s(%d)", tp.String(), tp.Width) 82 } else { 83 ret[i] = tp.String() 84 } 85 } 86 return ret, nil 87 } 88 89 func byOnUpdate(s []byte) (string, error) { 90 if len(s) == 0 { 91 return "", nil 92 } 93 update := new(plan.OnUpdate) 94 err := types.Decode(s, update) 95 if err != nil { 96 return "", err 97 } 98 return update.OriginString, nil 99 } 100 func byDefault(s []byte) (string, error) { 101 if len(s) == 0 { 102 return "", nil 103 } 104 def := new(plan.Default) 105 err := types.Decode(s, def) 106 if err != nil { 107 return "", err 108 } 109 return def.OriginString, nil 110 } 111 112 func showExpr(s [][]byte, fn func(ss []byte) (string, error)) ([]string, error) { 113 ret := make([]string, len(s)) 114 var err error 115 for i := range s { 116 ret[i], err = fn(s[i]) 117 if err != nil { 118 return nil, err 119 } 120 } 121 return ret, nil 122 }