github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/concat.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 multi 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/container/nulls" 19 "github.com/matrixorigin/matrixone/pkg/container/types" 20 "github.com/matrixorigin/matrixone/pkg/container/vector" 21 "github.com/matrixorigin/matrixone/pkg/vm/process" 22 ) 23 24 func Concat(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 25 resultType := types.Type{Oid: types.T_varchar, Size: 24, Width: types.MaxVarcharLen} 26 isAllConst := true 27 28 for i := range vectors { 29 if vectors[i].IsScalarNull() { 30 return proc.AllocScalarNullVector(resultType), nil 31 } 32 if !vectors[i].IsScalar() { 33 isAllConst = false 34 } 35 } 36 if isAllConst { 37 return concatWithAllConst(vectors, proc) 38 } 39 return concatWithSomeCols(vectors, proc) 40 } 41 42 func concatWithAllConst(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 43 length := vector.Length(vectors[0]) 44 vct := types.T_varchar.ToType() 45 res := "" 46 for i := range vectors { 47 res += vectors[i].GetString(0) 48 } 49 return vector.NewConstString(vct, length, res, proc.Mp()), nil 50 } 51 52 func concatWithSomeCols(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 53 length := vector.Length(vectors[0]) 54 vct := types.T_varchar.ToType() 55 nsp := new(nulls.Nulls) 56 val := make([]string, length) 57 for i := 0; i < length; i++ { 58 for j := range vectors { 59 if nulls.Contains(vectors[j].Nsp, uint64(i)) { 60 nulls.Add(nsp, uint64(i)) 61 break 62 } 63 if vectors[j].IsScalar() { 64 val[i] += vectors[j].GetString(int64(0)) 65 } else { 66 val[i] += vectors[j].GetString(int64(i)) 67 } 68 } 69 } 70 return vector.NewWithStrings(vct, val, nsp, proc.Mp()), nil 71 }