github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/make.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 plan 16 17 import ( 18 "context" 19 "go/constant" 20 "unicode/utf8" 21 22 "github.com/matrixorigin/matrixone/pkg/catalog" 23 "github.com/matrixorigin/matrixone/pkg/common/mpool" 24 "github.com/matrixorigin/matrixone/pkg/container/types" 25 "github.com/matrixorigin/matrixone/pkg/container/vector" 26 "github.com/matrixorigin/matrixone/pkg/pb/plan" 27 "github.com/matrixorigin/matrixone/pkg/sql/parsers/tree" 28 "github.com/matrixorigin/matrixone/pkg/sql/plan/function" 29 ) 30 31 func MakePlan2Decimal64ExprWithType(v types.Decimal64, typ *Type) *plan.Expr { 32 rawA := int64(v) 33 return &plan.Expr{ 34 Typ: *typ, 35 Expr: &plan.Expr_Lit{ 36 Lit: &Const{ 37 Isnull: false, 38 Value: &plan.Literal_Decimal64Val{ 39 Decimal64Val: &plan.Decimal64{ 40 A: rawA, 41 }, 42 }, 43 }, 44 }, 45 } 46 } 47 48 func MakePlan2Decimal128ExprWithType(v types.Decimal128, typ *Type) *plan.Expr { 49 rawA := v.B0_63 50 rawB := v.B64_127 51 return &plan.Expr{ 52 Typ: *typ, 53 Expr: &plan.Expr_Lit{ 54 Lit: &Const{ 55 Isnull: false, 56 Value: &plan.Literal_Decimal128Val{ 57 Decimal128Val: &plan.Decimal128{ 58 A: int64(rawA), 59 B: int64(rawB), 60 }, 61 }, 62 }, 63 }, 64 } 65 } 66 67 func makePlan2DecimalExprWithType(ctx context.Context, v string, isBin ...bool) (*plan.Expr, error) { 68 _, scale, err := types.Parse128(v) 69 if err != nil { 70 return nil, err 71 } 72 var typ plan.Type 73 if scale < 18 && len(v) < 18 { 74 typ = plan.Type{ 75 Id: int32(types.T_decimal64), 76 Width: 18, 77 Scale: scale, 78 NotNullable: true, 79 } 80 } else { 81 typ = plan.Type{ 82 Id: int32(types.T_decimal128), 83 Width: 38, 84 Scale: scale, 85 NotNullable: true, 86 } 87 } 88 return appendCastBeforeExpr(ctx, makePlan2StringConstExprWithType(v, isBin...), typ) 89 } 90 91 func makePlan2DateConstNullExpr(t types.T) *plan.Expr { 92 return &plan.Expr{ 93 Expr: &plan.Expr_Lit{ 94 Lit: &Const{ 95 Isnull: true, 96 }, 97 }, 98 Typ: plan.Type{ 99 Id: int32(t), 100 NotNullable: false, 101 }, 102 } 103 } 104 105 func makePlan2Decimal128ConstNullExpr() *plan.Expr { 106 return &plan.Expr{ 107 Expr: &plan.Expr_Lit{ 108 Lit: &Const{ 109 Isnull: true, 110 }, 111 }, 112 Typ: plan.Type{ 113 Id: int32(types.T_decimal128), 114 Width: 38, 115 Scale: 0, 116 NotNullable: false, 117 }, 118 } 119 } 120 121 func makePlan2NullConstExprWithType() *plan.Expr { 122 return &plan.Expr{ 123 Expr: &plan.Expr_Lit{ 124 Lit: &Const{ 125 Isnull: true, 126 }, 127 }, 128 Typ: plan.Type{ 129 Id: int32(types.T_any), 130 NotNullable: false, 131 }, 132 } 133 } 134 135 func makePlan2BoolConstExpr(v bool) *plan.Expr_Lit { 136 return &plan.Expr_Lit{Lit: &plan.Literal{ 137 Isnull: false, 138 Value: &plan.Literal_Bval{ 139 Bval: v, 140 }, 141 }} 142 } 143 144 func makePlan2BoolConstExprWithType(v bool) *plan.Expr { 145 return &plan.Expr{ 146 Expr: makePlan2BoolConstExpr(v), 147 Typ: plan.Type{ 148 Id: int32(types.T_bool), 149 NotNullable: true, 150 }, 151 } 152 } 153 154 func makePlan2Int64ConstExpr(v int64) *plan.Expr_Lit { 155 return &plan.Expr_Lit{Lit: &plan.Literal{ 156 Isnull: false, 157 Value: &plan.Literal_I64Val{ 158 I64Val: v, 159 }, 160 }} 161 } 162 163 var MakePlan2Int64ConstExprWithType = makePlan2Int64ConstExprWithType 164 165 func makePlan2Int64ConstExprWithType(v int64) *plan.Expr { 166 return &plan.Expr{ 167 Expr: makePlan2Int64ConstExpr(v), 168 Typ: plan.Type{ 169 Id: int32(types.T_int64), 170 NotNullable: true, 171 }, 172 } 173 } 174 175 var MakePlan2Vecf32ConstExprWithType = makePlan2Vecf32ConstExprWithType 176 177 // makePlan2Vecf32ConstExprWithType makes a vecf32 const expr. 178 // usage: makePlan2Vecf32ConstExprWithType("[1,2,3]", 3) 179 func makePlan2Vecf32ConstExprWithType(v string, l int32) *plan.Expr { 180 return &plan.Expr{ 181 Expr: makePlan2Vecf32ConstExpr(v), 182 Typ: plan.Type{ 183 Id: int32(types.T_array_float32), 184 Width: l, 185 NotNullable: true, 186 }, 187 } 188 } 189 190 func makePlan2Vecf32ConstExpr(v string) *plan.Expr_Lit { 191 return &plan.Expr_Lit{Lit: &plan.Literal{ 192 Isnull: false, 193 Value: &plan.Literal_Sval{ 194 Sval: v, 195 }, 196 }} 197 } 198 199 var MakePlan2StringVecExprWithType = makePlan2StringVecExprWithType 200 201 func makePlan2StringVecExprWithType(mp *mpool.MPool, vals ...string) *plan.Expr { 202 vec := vector.NewVec(types.T_char.ToType()) 203 for _, val := range vals { 204 vector.AppendBytes(vec, []byte(val), false, mp) 205 } 206 data, _ := vec.MarshalBinary() 207 vec.Free(mp) 208 return &plan.Expr{ 209 Typ: plan.Type{ 210 Id: int32(types.T_tuple), 211 }, 212 Expr: &plan.Expr_Vec{ 213 Vec: &plan.LiteralVec{ 214 Len: int32(len(vals)), 215 Data: data, 216 }, 217 }, 218 } 219 } 220 221 var MakePlan2Int64VecExprWithType = makePlan2Int64VecExprWithType 222 223 func makePlan2Int64VecExprWithType(mp *mpool.MPool, vals ...int64) *plan.Expr { 224 vec := vector.NewVec(types.T_int64.ToType()) 225 for _, val := range vals { 226 vector.AppendFixed(vec, val, false, mp) 227 } 228 data, _ := vec.MarshalBinary() 229 vec.Free(mp) 230 return &plan.Expr{ 231 Typ: plan.Type{ 232 Id: int32(types.T_tuple), 233 }, 234 Expr: &plan.Expr_Vec{ 235 Vec: &plan.LiteralVec{ 236 Len: int32(len(vals)), 237 Data: data, 238 }, 239 }, 240 } 241 } 242 243 func makePlan2Uint64ConstExpr(v uint64) *plan.Expr_Lit { 244 return &plan.Expr_Lit{Lit: &plan.Literal{ 245 Isnull: false, 246 Value: &plan.Literal_U64Val{ 247 U64Val: v, 248 }, 249 }} 250 } 251 252 func makePlan2Uint64ConstExprWithType(v uint64) *plan.Expr { 253 return &plan.Expr{ 254 Expr: makePlan2Uint64ConstExpr(v), 255 Typ: plan.Type{ 256 Id: int32(types.T_uint64), 257 NotNullable: true, 258 }, 259 } 260 } 261 262 func makePlan2Float64ConstExpr(v float64) *plan.Expr_Lit { 263 return &plan.Expr_Lit{Lit: &plan.Literal{ 264 Isnull: false, 265 Value: &plan.Literal_Dval{ 266 Dval: v, 267 }, 268 }} 269 } 270 271 var MakePlan2Float64ConstExprWithType = makePlan2Float64ConstExprWithType 272 273 func makePlan2Float64ConstExprWithType(v float64) *plan.Expr { 274 return &plan.Expr{ 275 Expr: makePlan2Float64ConstExpr(v), 276 Typ: plan.Type{ 277 Id: int32(types.T_float64), 278 NotNullable: true, 279 }, 280 } 281 } 282 283 func makePlan2StringConstExpr(v string, isBin ...bool) *plan.Expr_Lit { 284 c := &plan.Expr_Lit{Lit: &plan.Literal{ 285 Isnull: false, 286 Value: &plan.Literal_Sval{ 287 Sval: v, 288 }, 289 }} 290 if len(isBin) > 0 { 291 c.Lit.IsBin = isBin[0] 292 } 293 return c 294 } 295 296 var MakePlan2StringConstExprWithType = makePlan2StringConstExprWithType 297 298 func makePlan2StringConstExprWithType(v string, isBin ...bool) *plan.Expr { 299 width := int32(utf8.RuneCountInString(v)) 300 id := int32(types.T_varchar) 301 if width == 0 { 302 id = int32(types.T_char) 303 } 304 return &plan.Expr{ 305 Expr: makePlan2StringConstExpr(v, isBin...), 306 Typ: plan.Type{ 307 Id: id, 308 NotNullable: true, 309 Width: width, 310 }, 311 } 312 } 313 314 func makePlan2NullTextConstExpr(v string) *plan.Expr_Lit { 315 c := &plan.Expr_Lit{Lit: &plan.Literal{ 316 Isnull: true, 317 }} 318 return c 319 } 320 321 func MakePlan2NullTextConstExprWithType(v string) *plan.Expr { 322 return &plan.Expr{ 323 Expr: makePlan2NullTextConstExpr(v), 324 Typ: plan.Type{ 325 Id: int32(types.T_text), 326 NotNullable: false, 327 Width: int32(utf8.RuneCountInString(v)), 328 }, 329 } 330 } 331 332 func makePlan2CastExpr(ctx context.Context, expr *Expr, targetType Type) (*Expr, error) { 333 var err error 334 if isSameColumnType(expr.Typ, targetType) { 335 return expr, nil 336 } 337 targetType.NotNullable = expr.Typ.NotNullable 338 if types.T(expr.Typ.Id) == types.T_any { 339 expr.Typ = targetType 340 return expr, nil 341 } 342 343 if targetType.Id == int32(types.T_enum) { 344 expr, err = funcCastForEnumType(ctx, expr, targetType) 345 if err != nil { 346 return nil, err 347 } 348 } 349 350 t1, t2 := makeTypeByPlan2Expr(expr), makeTypeByPlan2Type(targetType) 351 fGet, err := function.GetFunctionByName(ctx, "cast", []types.Type{t1, t2}) 352 if err != nil { 353 return nil, err 354 } 355 t := &plan.Expr{ 356 Typ: targetType, 357 Expr: &plan.Expr_T{ 358 T: &plan.TargetType{}, 359 }, 360 } 361 return &plan.Expr{ 362 Expr: &plan.Expr_F{ 363 F: &plan.Function{ 364 Func: &ObjectRef{Obj: fGet.GetEncodedOverloadID(), ObjName: "cast"}, 365 Args: []*Expr{expr, t}, 366 }, 367 }, 368 Typ: targetType, 369 }, nil 370 } 371 372 func funcCastForEnumType(ctx context.Context, expr *Expr, targetType Type) (*Expr, error) { 373 var err error 374 if targetType.Id != int32(types.T_enum) { 375 return expr, nil 376 } 377 378 astArgs := []tree.Expr{ 379 tree.NewNumValWithType(constant.MakeString(targetType.Enumvalues), targetType.Enumvalues, false, tree.P_char), 380 } 381 382 // bind ast function's args 383 args := make([]*Expr, len(astArgs)+1) 384 binder := NewDefaultBinder(ctx, nil, nil, targetType, nil) 385 for idx, arg := range astArgs { 386 if idx == len(args)-1 { 387 continue 388 } 389 expr, err := binder.BindExpr(arg, 0, false) 390 if err != nil { 391 return nil, err 392 } 393 args[idx] = expr 394 } 395 args[len(args)-1] = expr 396 if 20 <= expr.Typ.Id && expr.Typ.Id <= 29 { 397 expr, err = BindFuncExprImplByPlanExpr(ctx, moEnumCastIndexValueToIndexFun, args) 398 if err != nil { 399 return nil, err 400 } 401 } else { 402 expr, err = BindFuncExprImplByPlanExpr(ctx, moEnumCastValueToIndexFun, args) 403 if err != nil { 404 return nil, err 405 } 406 } 407 return expr, nil 408 } 409 410 // if typ is decimal128 and decimal64 without scalar and width 411 // set a default value for it. 412 func rewriteDecimalTypeIfNecessary(typ *plan.Type) *plan.Type { 413 if typ.Id == int32(types.T_decimal128) && typ.Scale == 0 && typ.Width == 0 { 414 typ.Scale = 10 415 typ.Width = 38 // width 416 } 417 if typ.Id == int32(types.T_decimal64) && typ.Scale == 0 && typ.Width == 0 { 418 typ.Scale = 2 419 typ.Width = 6 // width 420 } 421 return typ 422 } 423 424 var MakePlan2Type = makePlan2Type 425 426 func makePlan2Type(typ *types.Type) plan.Type { 427 return plan.Type{ 428 Id: int32(typ.Oid), 429 Width: typ.Width, 430 Scale: typ.Scale, 431 } 432 } 433 func makePlan2TypeValue(typ *types.Type) plan.Type { 434 return plan.Type{ 435 Id: int32(typ.Oid), 436 Width: typ.Width, 437 Scale: typ.Scale, 438 } 439 } 440 441 var MakeTypeByPlan2Type = makeTypeByPlan2Type 442 443 func makeTypeByPlan2Type(typ plan.Type) types.Type { 444 oid := types.T(typ.Id) 445 return types.New(oid, typ.Width, typ.Scale) 446 } 447 448 var MakeTypeByPlan2Expr = makeTypeByPlan2Expr 449 450 func makeTypeByPlan2Expr(expr *plan.Expr) types.Type { 451 oid := types.T(expr.Typ.Id) 452 return types.New(oid, expr.Typ.Width, expr.Typ.Scale) 453 } 454 455 func makeHiddenColTyp() Type { 456 return Type{ 457 Id: int32(types.T_varchar), 458 Width: types.MaxVarcharLen, 459 } 460 } 461 462 // used for Compound primary key column name && clusterby column name 463 func MakeHiddenColDefByName(name string) *ColDef { 464 return &ColDef{ 465 Name: name, 466 Hidden: true, 467 Typ: makeHiddenColTyp(), 468 Default: &plan.Default{ 469 NullAbility: false, 470 Expr: nil, 471 OriginString: "", 472 }, 473 } 474 } 475 476 func MakeRowIdColDef() *ColDef { 477 return &ColDef{ 478 Name: catalog.Row_ID, 479 Hidden: true, 480 Typ: Type{ 481 Id: int32(types.T_Rowid), 482 }, 483 Default: &plan.Default{ 484 NullAbility: false, 485 Expr: nil, 486 OriginString: "", 487 }, 488 } 489 } 490 491 func isSameColumnType(t1 Type, t2 Type) bool { 492 if t1.Id != t2.Id { 493 return false 494 } 495 if t1.Width == t2.Width && t1.Scale == t2.Scale { 496 return true 497 } 498 return true 499 } 500 501 // GetColDefFromTable Find the target column definition from the predefined 502 // table columns and return its deep copy 503 func GetColDefFromTable(Cols []*ColDef, hidenColName string) *ColDef { 504 for _, coldef := range Cols { 505 if coldef.Name == hidenColName { 506 return DeepCopyColDef(coldef) 507 } 508 } 509 panic("Unable to find target column from predefined table columns") 510 }