github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/rpc/utils.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 rpc 16 17 import ( 18 "strings" 19 20 pkgcatalog "github.com/matrixorigin/matrixone/pkg/catalog" 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 "github.com/matrixorigin/matrixone/pkg/pb/plan" 23 "github.com/matrixorigin/matrixone/pkg/vm/engine" 24 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog" 25 ) 26 27 func DefsToSchema(name string, defs []engine.TableDef) (schema *catalog.Schema, err error) { 28 schema = catalog.NewEmptySchema(name) 29 schema.CatalogVersion = pkgcatalog.CatalogVersion_Curr 30 var pkeyColName string 31 for _, def := range defs { 32 switch defVal := def.(type) { 33 case *engine.ConstraintDef: 34 primaryKeyDef := defVal.GetPrimaryKeyDef() 35 if primaryKeyDef != nil { 36 pkeyColName = primaryKeyDef.Pkey.PkeyColName 37 break 38 } 39 } 40 } 41 for _, def := range defs { 42 switch defVal := def.(type) { 43 case *engine.AttributeDef: 44 if pkeyColName == defVal.Attr.Name { 45 if err = schema.AppendSortColWithAttribute(defVal.Attr, 0, true); err != nil { 46 return 47 } 48 } else if defVal.Attr.ClusterBy { 49 if err = schema.AppendSortColWithAttribute(defVal.Attr, 0, false); err != nil { 50 return 51 } 52 } else { 53 if err = schema.AppendColWithAttribute(defVal.Attr); err != nil { 54 return 55 } 56 } 57 58 case *engine.PropertiesDef: 59 for _, property := range defVal.Properties { 60 switch strings.ToLower(property.Key) { 61 case pkgcatalog.SystemRelAttr_Comment: 62 schema.Comment = property.Value 63 case pkgcatalog.SystemRelAttr_Kind: 64 schema.Relkind = property.Value 65 case pkgcatalog.SystemRelAttr_CreateSQL: 66 schema.Createsql = property.Value 67 default: 68 } 69 } 70 71 case *engine.PartitionDef: 72 schema.Partitioned = defVal.Partitioned 73 schema.Partition = defVal.Partition 74 case *engine.ViewDef: 75 schema.View = defVal.View 76 case *engine.CommentDef: 77 schema.Comment = defVal.Comment 78 case *engine.ConstraintDef: 79 schema.Constraint, err = defVal.MarshalBinary() 80 if err != nil { 81 return nil, err 82 } 83 default: 84 // We will not deal with other cases for the time being 85 } 86 } 87 if err = schema.Finalize(false); err != nil { 88 return 89 } 90 return 91 } 92 93 func SchemaToDefs(schema *catalog.Schema) (defs []engine.TableDef, err error) { 94 if schema.Comment != "" { 95 commentDef := new(engine.CommentDef) 96 commentDef.Comment = schema.Comment 97 defs = append(defs, commentDef) 98 } 99 100 if schema.Partitioned > 0 || schema.Partition != "" { 101 partitionDef := new(engine.PartitionDef) 102 partitionDef.Partitioned = schema.Partitioned 103 partitionDef.Partition = schema.Partition 104 defs = append(defs, partitionDef) 105 } 106 107 if schema.View != "" { 108 viewDef := new(engine.ViewDef) 109 viewDef.View = schema.View 110 defs = append(defs, viewDef) 111 } 112 113 if len(schema.Constraint) > 0 { 114 c := new(engine.ConstraintDef) 115 if err := c.UnmarshalBinary(schema.Constraint); err != nil { 116 return nil, err 117 } 118 defs = append(defs, c) 119 } 120 121 for _, col := range schema.ColDefs { 122 if col.IsPhyAddr() { 123 continue 124 } 125 attr, err := AttrFromColDef(col) 126 if err != nil { 127 return nil, err 128 } 129 defs = append(defs, &engine.AttributeDef{Attr: *attr}) 130 } 131 pro := new(engine.PropertiesDef) 132 pro.Properties = append(pro.Properties, engine.Property{ 133 Key: pkgcatalog.SystemRelAttr_Kind, 134 Value: string(schema.Relkind), 135 }) 136 if schema.Createsql != "" { 137 pro.Properties = append(pro.Properties, engine.Property{ 138 Key: pkgcatalog.SystemRelAttr_CreateSQL, 139 Value: schema.Createsql, 140 }) 141 } 142 defs = append(defs, pro) 143 144 return 145 } 146 147 func AttrFromColDef(col *catalog.ColDef) (attrs *engine.Attribute, err error) { 148 var defaultVal *plan.Default 149 if len(col.Default) > 0 { 150 defaultVal = &plan.Default{} 151 if err := types.Decode(col.Default, defaultVal); err != nil { 152 return nil, err 153 } 154 } 155 156 var onUpdate *plan.OnUpdate 157 if len(col.OnUpdate) > 0 { 158 onUpdate = new(plan.OnUpdate) 159 if err := types.Decode(col.OnUpdate, onUpdate); err != nil { 160 return nil, err 161 } 162 } 163 164 attr := &engine.Attribute{ 165 Name: col.Name, 166 Type: col.Type, 167 Primary: col.IsPrimary(), 168 IsHidden: col.IsHidden(), 169 IsRowId: col.IsPhyAddr(), 170 Comment: col.Comment, 171 Default: defaultVal, 172 OnUpdate: onUpdate, 173 AutoIncrement: col.IsAutoIncrement(), 174 ClusterBy: col.IsClusterBy(), 175 } 176 return attr, nil 177 } 178 179 type mItem struct { 180 objcnt int 181 did, tid uint64 182 } 183 184 type itemSet []mItem 185 186 func (is itemSet) Len() int { return len(is) } 187 188 func (is itemSet) Less(i, j int) bool { 189 return is[i].objcnt < is[j].objcnt 190 } 191 192 func (is itemSet) Swap(i, j int) { 193 is[i], is[j] = is[j], is[i] 194 } 195 196 func (is *itemSet) Push(x any) { 197 item := x.(mItem) 198 *is = append(*is, item) 199 } 200 201 func (is *itemSet) Pop() any { 202 old := *is 203 n := len(old) 204 item := old[n-1] 205 // old[n-1] = nil // avoid memory leak 206 *is = old[0 : n-1] 207 return item 208 } 209 210 func (is *itemSet) Clear() { 211 old := *is 212 *is = old[:0] 213 }