github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/util/composite_clusterby_util.go (about) 1 // Copyright 2021 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 util 16 17 import ( 18 "strconv" 19 20 "github.com/matrixorigin/matrixone/pkg/sql/plan/function/builtin/multi" 21 22 "github.com/fagongzi/util/format" 23 "github.com/matrixorigin/matrixone/pkg/catalog" 24 "github.com/matrixorigin/matrixone/pkg/container/batch" 25 "github.com/matrixorigin/matrixone/pkg/container/vector" 26 "github.com/matrixorigin/matrixone/pkg/vm/process" 27 ) 28 29 func JudgeIsCompositeClusterByColumn(s string) bool { 30 if len(s) < len(catalog.PrefixCBColName) { 31 return false 32 } 33 return s[0:len(catalog.PrefixCBColName)] == catalog.PrefixCBColName 34 } 35 36 func BuildCompositeClusterByColumnName(s []string) string { 37 var name string 38 name = catalog.PrefixCBColName 39 for _, single := range s { 40 lenNum := format.Int64ToString(int64(len(single))) 41 for num := 0; num < 3-len(lenNum); num++ { 42 name += string('0') 43 } 44 name += lenNum 45 name += single 46 } 47 return name 48 } 49 50 func SplitCompositeClusterByColumnName(s string) []string { 51 var names []string 52 for next := len(catalog.PrefixCBColName); next < len(s); { 53 strLen, _ := strconv.Atoi(s[next : next+3]) 54 names = append(names, s[next+3:next+3+strLen]) 55 next += strLen + 3 56 } 57 58 return names 59 } 60 61 func GetClusterByColumnOrder(cbName, colName string) int { 62 if len(cbName) == 0 || len(colName) == 0 { 63 return -1 64 } 65 if cbName == colName { 66 return 0 67 } 68 if !JudgeIsCompositeClusterByColumn(cbName) { 69 return -1 70 } 71 idx := 0 72 for next := len(catalog.PrefixCBColName); next < len(cbName); { 73 strLen, _ := strconv.Atoi(cbName[next : next+3]) 74 if cbName[next+3:next+3+strLen] == colName { 75 return idx 76 } 77 next += strLen + 3 78 idx++ 79 } 80 return -1 81 } 82 83 func FillCompositeClusterByBatch(bat *batch.Batch, cbName string, proc *process.Process) { 84 names := SplitCompositeClusterByColumnName(cbName) 85 cCBVecMap := make(map[string]*vector.Vector) 86 for num, attrName := range bat.Attrs { 87 for _, name := range names { 88 if attrName == name { 89 cCBVecMap[name] = bat.Vecs[num] 90 } 91 } 92 } 93 vs := make([]*vector.Vector, 0) 94 for _, name := range names { 95 v := cCBVecMap[name] 96 vs = append(vs, v) 97 } 98 vec, _ := multi.Serial(vs, proc) 99 bat.Attrs = append(bat.Attrs, cbName) 100 bat.Vecs = append(bat.Vecs, vec) 101 }