github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/base_binder.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 plan 16 17 import ( 18 "context" 19 "encoding/hex" 20 "fmt" 21 "go/constant" 22 "strconv" 23 "strings" 24 25 "github.com/matrixorigin/matrixone/pkg/common/moerr" 26 "github.com/matrixorigin/matrixone/pkg/container/batch" 27 "github.com/matrixorigin/matrixone/pkg/container/types" 28 "github.com/matrixorigin/matrixone/pkg/defines" 29 "github.com/matrixorigin/matrixone/pkg/pb/plan" 30 "github.com/matrixorigin/matrixone/pkg/sql/parsers" 31 "github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect" 32 "github.com/matrixorigin/matrixone/pkg/sql/parsers/tree" 33 "github.com/matrixorigin/matrixone/pkg/sql/plan/function" 34 "github.com/matrixorigin/matrixone/pkg/sql/util" 35 "github.com/matrixorigin/matrixone/pkg/util/errutil" 36 "github.com/matrixorigin/matrixone/pkg/vm/process" 37 ) 38 39 func (b *baseBinder) baseBindExpr(astExpr tree.Expr, depth int32, isRoot bool) (expr *Expr, err error) { 40 switch exprImpl := astExpr.(type) { 41 case *tree.NumVal: 42 if d, ok := b.impl.(*DefaultBinder); ok { 43 expr, err = b.bindNumVal(exprImpl, d.typ) 44 } else { 45 expr, err = b.bindNumVal(exprImpl, plan.Type{}) 46 } 47 case *tree.ParenExpr: 48 expr, err = b.impl.BindExpr(exprImpl.Expr, depth, isRoot) 49 50 case *tree.OrExpr: 51 expr, err = b.bindFuncExprImplByAstExpr("or", []tree.Expr{exprImpl.Left, exprImpl.Right}, depth) 52 53 case *tree.NotExpr: 54 if subqueryAst, ok := exprImpl.Expr.(*tree.Subquery); ok { 55 expr, err = b.impl.BindSubquery(subqueryAst, isRoot) 56 if err != nil { 57 return 58 } 59 60 subquery := expr.GetSub() 61 if subquery.Typ == plan.SubqueryRef_EXISTS { 62 subquery.Typ = plan.SubqueryRef_NOT_EXISTS 63 } 64 } else { 65 expr, err = b.impl.BindExpr(exprImpl.Expr, depth, false) 66 if err != nil { 67 return 68 } 69 70 expr, err = BindFuncExprImplByPlanExpr(b.GetContext(), "not", []*plan.Expr{expr}) 71 } 72 73 case *tree.AndExpr: 74 expr, err = b.bindFuncExprImplByAstExpr("and", []tree.Expr{exprImpl.Left, exprImpl.Right}, depth) 75 76 case *tree.UnaryExpr: 77 expr, err = b.bindUnaryExpr(exprImpl, depth, isRoot) 78 79 case *tree.BinaryExpr: 80 expr, err = b.bindBinaryExpr(exprImpl, depth, isRoot) 81 82 case *tree.ComparisonExpr: 83 expr, err = b.bindComparisonExpr(exprImpl, depth, isRoot) 84 85 case *tree.FuncExpr: 86 expr, err = b.bindFuncExpr(exprImpl, depth, isRoot) 87 88 case *tree.RangeCond: 89 expr, err = b.bindRangeCond(exprImpl, depth, isRoot) 90 91 case *tree.UnresolvedName: 92 // check existence 93 if b.GetContext() != nil && b.GetContext().Value(defines.InSp{}) != nil && b.GetContext().Value(defines.InSp{}).(bool) { 94 tmpScope := b.GetContext().Value(defines.VarScopeKey{}).(*[]map[string]interface{}) 95 for i := len(*tmpScope) - 1; i >= 0; i-- { 96 curScope := (*tmpScope)[i] 97 if _, ok := curScope[strings.ToLower(exprImpl.Parts[0])]; ok { 98 typ := types.T_text.ToType() 99 expr = &Expr{ 100 Typ: makePlan2Type(&typ), 101 Expr: &plan.Expr_V{ 102 V: &plan.VarRef{ 103 Name: exprImpl.Parts[0], 104 System: false, 105 Global: false, 106 }, 107 }, 108 } 109 err = nil 110 return 111 } 112 } 113 } 114 expr, err = b.impl.BindColRef(exprImpl, depth, isRoot) 115 116 case *tree.SerialExtractExpr: 117 expr, err = b.bindFuncExprImplByAstExpr("serial_extract", []tree.Expr{astExpr}, depth) 118 119 case *tree.CastExpr: 120 expr, err = b.impl.BindExpr(exprImpl.Expr, depth, false) 121 if err != nil { 122 return 123 } 124 var typ Type 125 typ, err = getTypeFromAst(b.GetContext(), exprImpl.Type) 126 if err != nil { 127 return 128 } 129 expr, err = appendCastBeforeExpr(b.GetContext(), expr, typ) 130 131 case *tree.BitCastExpr: 132 expr, err = b.bindFuncExprImplByAstExpr("bit_cast", []tree.Expr{astExpr}, depth) 133 134 case *tree.IsNullExpr: 135 expr, err = b.bindFuncExprImplByAstExpr("isnull", []tree.Expr{exprImpl.Expr}, depth) 136 137 case *tree.IsNotNullExpr: 138 expr, err = b.bindFuncExprImplByAstExpr("isnotnull", []tree.Expr{exprImpl.Expr}, depth) 139 140 case *tree.IsUnknownExpr: 141 expr, err = b.bindFuncExprImplByAstExpr("isnull", []tree.Expr{exprImpl.Expr}, depth) 142 143 case *tree.IsNotUnknownExpr: 144 expr, err = b.bindFuncExprImplByAstExpr("isnotnull", []tree.Expr{exprImpl.Expr}, depth) 145 146 case *tree.IsTrueExpr: 147 expr, err = b.bindFuncExprImplByAstExpr("istrue", []tree.Expr{exprImpl.Expr}, depth) 148 149 case *tree.IsNotTrueExpr: 150 expr, err = b.bindFuncExprImplByAstExpr("isnottrue", []tree.Expr{exprImpl.Expr}, depth) 151 152 case *tree.IsFalseExpr: 153 expr, err = b.bindFuncExprImplByAstExpr("isfalse", []tree.Expr{exprImpl.Expr}, depth) 154 155 case *tree.IsNotFalseExpr: 156 expr, err = b.bindFuncExprImplByAstExpr("isnotfalse", []tree.Expr{exprImpl.Expr}, depth) 157 158 case *tree.Tuple: 159 exprs := make([]*Expr, 0, len(exprImpl.Exprs)) 160 var planItem *Expr 161 for _, astItem := range exprImpl.Exprs { 162 planItem, err = b.impl.BindExpr(astItem, depth, false) 163 if err != nil { 164 return 165 } 166 exprs = append(exprs, planItem) 167 } 168 expr = &Expr{ 169 Expr: &plan.Expr_List{ 170 List: &plan.ExprList{ 171 List: exprs, 172 }, 173 }, 174 Typ: plan.Type{ 175 Id: int32(types.T_tuple), 176 }, 177 } 178 179 case *tree.CaseExpr: 180 expr, err = b.bindCaseExpr(exprImpl, depth, isRoot) 181 182 case *tree.IntervalExpr: 183 err = moerr.NewNYI(b.GetContext(), "expr interval'%v'", exprImpl) 184 185 case *tree.XorExpr: 186 expr, err = b.bindFuncExprImplByAstExpr("xor", []tree.Expr{exprImpl.Left, exprImpl.Right}, depth) 187 188 case *tree.Subquery: 189 expr, err = b.impl.BindSubquery(exprImpl, isRoot) 190 191 case *tree.DefaultVal: 192 return &Expr{ 193 Typ: plan.Type{ 194 Id: int32(types.T_bool), 195 NotNullable: true, 196 }, 197 Expr: &plan.Expr_Lit{ 198 Lit: &Const{ 199 Isnull: false, 200 Value: &plan.Literal_Defaultval{ 201 Defaultval: true, 202 }, 203 }, 204 }, 205 }, nil 206 case *tree.UpdateVal: 207 return &Expr{ 208 Expr: &plan.Expr_Lit{ 209 Lit: &Const{ 210 Isnull: false, 211 Value: &plan.Literal_UpdateVal{ 212 UpdateVal: true, 213 }, 214 }, 215 }, 216 }, nil 217 case *tree.MaxValue: 218 return &Expr{ 219 Expr: &plan.Expr_Max{ 220 Max: &MaxValue{ 221 Value: "maxvalue", 222 }, 223 }, 224 }, nil 225 case *tree.VarExpr: 226 expr, err = b.baseBindVar(exprImpl, depth, isRoot) 227 228 case *tree.ParamExpr: 229 if !b.builder.isPrepareStatement { 230 err = moerr.NewInvalidInput(b.GetContext(), "only prepare statement can use ? expr") 231 } else { 232 expr, err = b.baseBindParam(exprImpl, depth, isRoot) 233 } 234 235 case *tree.StrVal: 236 err = moerr.NewNYI(b.GetContext(), "expr str'%v'", exprImpl) 237 238 case *tree.ExprList: 239 err = moerr.NewNYI(b.GetContext(), "expr plan.ExprList'%v'", exprImpl) 240 241 case tree.UnqualifiedStar: 242 // select * from table 243 // * should only appear in SELECT clause 244 err = moerr.NewInvalidInput(b.GetContext(), "SELECT clause contains unqualified star") 245 246 default: 247 err = moerr.NewNYI(b.GetContext(), "expr '%+v'", exprImpl) 248 } 249 250 return 251 } 252 253 func (b *baseBinder) baseBindParam(astExpr *tree.ParamExpr, depth int32, isRoot bool) (expr *plan.Expr, err error) { 254 typ := types.T_text.ToType() 255 return &Expr{ 256 Typ: makePlan2Type(&typ), 257 Expr: &plan.Expr_P{ 258 P: &plan.ParamRef{ 259 Pos: int32(astExpr.Offset), 260 }, 261 }, 262 }, nil 263 } 264 265 func (b *baseBinder) baseBindVar(astExpr *tree.VarExpr, depth int32, isRoot bool) (expr *plan.Expr, err error) { 266 typ := types.T_text.ToType() 267 return &Expr{ 268 Typ: makePlan2Type(&typ), 269 Expr: &plan.Expr_V{ 270 V: &plan.VarRef{ 271 Name: astExpr.Name, 272 System: astExpr.System, 273 Global: astExpr.Global, 274 }, 275 }, 276 }, nil 277 } 278 279 const ( 280 TimeWindowStart = "_wstart" 281 TimeWindowEnd = "_wend" 282 ) 283 284 func (b *baseBinder) baseBindColRef(astExpr *tree.UnresolvedName, depth int32, isRoot bool) (expr *plan.Expr, err error) { 285 if b.ctx == nil { 286 return nil, moerr.NewInvalidInput(b.GetContext(), "ambiguous column reference '%v'", astExpr.Parts[0]) 287 } 288 astStr := tree.String(astExpr, dialect.MYSQL) 289 290 col := astExpr.Parts[0] 291 table := astExpr.Parts[1] 292 name := tree.String(astExpr, dialect.MYSQL) 293 294 if b.ctx.timeTag > 0 && (col == TimeWindowStart || col == TimeWindowEnd) { 295 colPos := int32(len(b.ctx.times)) 296 expr = &plan.Expr{ 297 Typ: plan.Type{Id: int32(types.T_timestamp)}, 298 Expr: &plan.Expr_Col{ 299 Col: &plan.ColRef{ 300 RelPos: b.ctx.timeTag, 301 ColPos: colPos, 302 Name: col, 303 }, 304 }, 305 } 306 b.ctx.timeByAst[astStr] = colPos 307 b.ctx.times = append(b.ctx.times, expr) 308 return 309 } 310 311 relPos := NotFound 312 colPos := NotFound 313 var typ *plan.Type 314 localErrCtx := errutil.ContextWithNoReport(b.GetContext(), true) 315 316 if len(table) == 0 { 317 if binding, ok := b.ctx.bindingByCol[col]; ok { 318 if binding != nil { 319 relPos = binding.tag 320 colPos = binding.colIdByName[col] 321 typ = DeepCopyType(binding.types[colPos]) 322 table = binding.table 323 } else { 324 return nil, moerr.NewInvalidInput(b.GetContext(), "ambiguous column reference '%v'", name) 325 } 326 } else { 327 err = moerr.NewInvalidInput(localErrCtx, "column %s does not exist", name) 328 } 329 } else { 330 if binding, ok := b.ctx.bindingByTable[table]; ok { 331 colPos = binding.FindColumn(col) 332 if colPos == AmbiguousName { 333 return nil, moerr.NewInvalidInput(b.GetContext(), "ambiguous column reference '%v'", name) 334 } 335 if colPos != NotFound { 336 typ = DeepCopyType(binding.types[colPos]) 337 relPos = binding.tag 338 } else { 339 err = moerr.NewInvalidInput(localErrCtx, "column '%s' does not exist", name) 340 } 341 } else { 342 err = moerr.NewInvalidInput(localErrCtx, "missing FROM-clause entry for table '%v'", table) 343 } 344 } 345 346 if typ != nil && typ.Id == int32(types.T_enum) && len(typ.GetEnumvalues()) != 0 { 347 if err != nil { 348 errutil.ReportError(b.GetContext(), err) 349 return 350 } 351 astArgs := []tree.Expr{ 352 tree.NewNumValWithType(constant.MakeString(typ.Enumvalues), typ.Enumvalues, false, tree.P_char), 353 } 354 355 // bind ast function's args 356 args := make([]*Expr, len(astArgs)+1) 357 for idx, arg := range astArgs { 358 if idx == len(args)-1 { 359 continue 360 } 361 expr, err := b.impl.BindExpr(arg, depth, false) 362 if err != nil { 363 return nil, err 364 } 365 args[idx] = expr 366 } 367 args[len(args)-1] = &Expr{ 368 Typ: *typ, 369 Expr: &plan.Expr_Col{ 370 Col: &plan.ColRef{ 371 RelPos: relPos, 372 ColPos: colPos, 373 Name: col, 374 }, 375 }, 376 } 377 378 return BindFuncExprImplByPlanExpr(b.GetContext(), moEnumCastIndexToValueFun, args) 379 } 380 381 if colPos != NotFound { 382 b.boundCols = append(b.boundCols, table+"."+col) 383 384 expr = &plan.Expr{ 385 Typ: *typ, 386 } 387 388 if depth == 0 { 389 expr.Expr = &plan.Expr_Col{ 390 Col: &plan.ColRef{ 391 RelPos: relPos, 392 ColPos: colPos, 393 Name: col, 394 }, 395 } 396 } else { 397 expr.Expr = &plan.Expr_Corr{ 398 Corr: &plan.CorrColRef{ 399 RelPos: relPos, 400 ColPos: colPos, 401 Depth: depth, 402 }, 403 } 404 } 405 if err != nil { 406 errutil.ReportError(b.GetContext(), err) 407 } 408 return 409 } 410 411 parent := b.ctx.parent 412 for parent != nil && parent.binder == nil { 413 parent = parent.parent 414 } 415 416 if parent == nil { 417 if err != nil { 418 errutil.ReportError(b.GetContext(), err) 419 } 420 return 421 } 422 423 expr, err = parent.binder.BindColRef(astExpr, depth+1, isRoot) 424 425 if err == nil { 426 b.ctx.isCorrelated = true 427 } 428 429 return 430 } 431 432 func (b *baseBinder) baseBindSubquery(astExpr *tree.Subquery, isRoot bool) (*Expr, error) { 433 if b.ctx == nil { 434 return nil, moerr.NewInvalidInput(b.GetContext(), "field reference doesn't support SUBQUERY") 435 } 436 subCtx := NewBindContext(b.builder, b.ctx) 437 438 var nodeID int32 439 var err error 440 switch subquery := astExpr.Select.(type) { 441 case *tree.ParenSelect: 442 nodeID, err = b.builder.buildSelect(subquery.Select, subCtx, false) 443 if err != nil { 444 return nil, err 445 } 446 case *tree.Select: 447 nodeID, err = b.builder.buildSelect(subquery, subCtx, false) 448 if err != nil { 449 return nil, err 450 } 451 452 default: 453 return nil, moerr.NewNYI(b.GetContext(), "unsupported select statement: %s", tree.String(astExpr, dialect.MYSQL)) 454 } 455 456 rowSize := int32(len(subCtx.results)) 457 458 returnExpr := &plan.Expr{ 459 Typ: plan.Type{ 460 Id: int32(types.T_tuple), 461 }, 462 Expr: &plan.Expr_Sub{ 463 Sub: &plan.SubqueryRef{ 464 NodeId: nodeID, 465 RowSize: rowSize, 466 }, 467 }, 468 } 469 470 if astExpr.Exists { 471 returnExpr.Typ = plan.Type{ 472 Id: int32(types.T_bool), 473 NotNullable: true, 474 } 475 returnExpr.GetSub().Typ = plan.SubqueryRef_EXISTS 476 } else if rowSize == 1 { 477 returnExpr.Typ = subCtx.results[0].Typ 478 } 479 480 return returnExpr, nil 481 } 482 483 func (b *baseBinder) bindCaseExpr(astExpr *tree.CaseExpr, depth int32, isRoot bool) (*Expr, error) { 484 args := make([]tree.Expr, 0, len(astExpr.Whens)+1) 485 caseExist := astExpr.Expr != nil 486 487 for _, whenExpr := range astExpr.Whens { 488 if caseExist { 489 newCandExpr := tree.NewComparisonExpr(tree.EQUAL, astExpr.Expr, whenExpr.Cond) 490 args = append(args, newCandExpr) 491 } else { 492 args = append(args, whenExpr.Cond) 493 } 494 args = append(args, whenExpr.Val) 495 } 496 497 if astExpr.Else != nil { 498 args = append(args, astExpr.Else) 499 } else { 500 args = append(args, tree.NewNumValWithType(constant.MakeUnknown(), "", false, tree.P_null)) 501 } 502 503 return b.bindFuncExprImplByAstExpr("case", args, depth) 504 } 505 506 func (b *baseBinder) bindRangeCond(astExpr *tree.RangeCond, depth int32, isRoot bool) (*Expr, error) { 507 if astExpr.Not { 508 // rewrite 'col not between 1, 20' to 'col < 1 or col > 20' 509 newLeftExpr := tree.NewComparisonExpr(tree.LESS_THAN, astExpr.Left, astExpr.From) 510 newRightExpr := tree.NewComparisonExpr(tree.GREAT_THAN, astExpr.Left, astExpr.To) 511 return b.bindFuncExprImplByAstExpr("or", []tree.Expr{newLeftExpr, newRightExpr}, depth) 512 } else { 513 if _, ok := astExpr.Left.(*tree.Tuple); ok { 514 newLeftExpr := tree.NewComparisonExpr(tree.GREAT_THAN_EQUAL, astExpr.Left, astExpr.From) 515 newRightExpr := tree.NewComparisonExpr(tree.LESS_THAN_EQUAL, astExpr.Left, astExpr.To) 516 return b.bindFuncExprImplByAstExpr("and", []tree.Expr{newLeftExpr, newRightExpr}, depth) 517 } 518 519 return b.bindFuncExprImplByAstExpr("between", []tree.Expr{astExpr.Left, astExpr.From, astExpr.To}, depth) 520 } 521 } 522 523 func (b *baseBinder) bindUnaryExpr(astExpr *tree.UnaryExpr, depth int32, isRoot bool) (*Expr, error) { 524 switch astExpr.Op { 525 case tree.UNARY_MINUS: 526 return b.bindFuncExprImplByAstExpr("unary_minus", []tree.Expr{astExpr.Expr}, depth) 527 case tree.UNARY_PLUS: 528 return b.bindFuncExprImplByAstExpr("unary_plus", []tree.Expr{astExpr.Expr}, depth) 529 case tree.UNARY_TILDE: 530 return b.bindFuncExprImplByAstExpr("unary_tilde", []tree.Expr{astExpr.Expr}, depth) 531 case tree.UNARY_MARK: 532 return b.bindFuncExprImplByAstExpr("unary_mark", []tree.Expr{astExpr.Expr}, depth) 533 } 534 return nil, moerr.NewNYI(b.GetContext(), "'%v'", astExpr) 535 } 536 537 func (b *baseBinder) bindBinaryExpr(astExpr *tree.BinaryExpr, depth int32, isRoot bool) (*Expr, error) { 538 switch astExpr.Op { 539 case tree.PLUS: 540 return b.bindFuncExprImplByAstExpr("+", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 541 case tree.MINUS: 542 return b.bindFuncExprImplByAstExpr("-", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 543 case tree.MULTI: 544 return b.bindFuncExprImplByAstExpr("*", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 545 case tree.MOD: 546 return b.bindFuncExprImplByAstExpr("%", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 547 case tree.DIV: 548 return b.bindFuncExprImplByAstExpr("/", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 549 case tree.INTEGER_DIV: 550 return b.bindFuncExprImplByAstExpr("div", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 551 case tree.BIT_XOR: 552 return b.bindFuncExprImplByAstExpr("^", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 553 case tree.BIT_OR: 554 return b.bindFuncExprImplByAstExpr("|", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 555 case tree.BIT_AND: 556 return b.bindFuncExprImplByAstExpr("&", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 557 case tree.LEFT_SHIFT: 558 return b.bindFuncExprImplByAstExpr("<<", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 559 case tree.RIGHT_SHIFT: 560 return b.bindFuncExprImplByAstExpr(">>", []tree.Expr{astExpr.Left, astExpr.Right}, depth) 561 } 562 return nil, moerr.NewNYI(b.GetContext(), "'%v' operator", astExpr.Op.ToString()) 563 } 564 565 func (b *baseBinder) bindComparisonExpr(astExpr *tree.ComparisonExpr, depth int32, isRoot bool) (*Expr, error) { 566 var op string 567 568 switch astExpr.Op { 569 case tree.EQUAL: 570 op = "=" 571 switch leftexpr := astExpr.Left.(type) { 572 case *tree.Tuple: 573 switch rightexpr := astExpr.Right.(type) { 574 case *tree.Tuple: 575 if len(leftexpr.Exprs) == len(rightexpr.Exprs) { 576 var expr1, expr2 *plan.Expr 577 var err error 578 for i := 1; i < len(leftexpr.Exprs); i++ { 579 if i == 1 { 580 expr1, err = b.bindFuncExprImplByAstExpr("=", []tree.Expr{leftexpr.Exprs[0], rightexpr.Exprs[0]}, depth) 581 if err != nil { 582 return nil, err 583 } 584 } 585 expr2, err = b.bindFuncExprImplByAstExpr("=", []tree.Expr{leftexpr.Exprs[i], rightexpr.Exprs[i]}, depth) 586 if err != nil { 587 return nil, err 588 } 589 expr1, err = BindFuncExprImplByPlanExpr(b.GetContext(), "and", []*plan.Expr{expr1, expr2}) 590 if err != nil { 591 return nil, err 592 } 593 } 594 return expr1, nil 595 } else { 596 return nil, moerr.NewInvalidInput(b.GetContext(), "two tuples have different length(%v,%v)", len(leftexpr.Exprs), len(rightexpr.Exprs)) 597 } 598 } 599 } 600 601 case tree.LESS_THAN: 602 op = "<" 603 switch leftexpr := astExpr.Left.(type) { 604 case *tree.Tuple: 605 switch rightexpr := astExpr.Right.(type) { 606 case *tree.Tuple: 607 if len(leftexpr.Exprs) == len(rightexpr.Exprs) { 608 var expr1, expr2 *plan.Expr 609 var err error 610 for i := len(leftexpr.Exprs) - 2; i >= 0; i-- { 611 if i == len(leftexpr.Exprs)-2 { 612 expr1, err = b.bindFuncExprImplByAstExpr("<", []tree.Expr{leftexpr.Exprs[i+1], rightexpr.Exprs[i+1]}, depth) 613 if err != nil { 614 return nil, err 615 } 616 } 617 expr2, err = b.bindFuncExprImplByAstExpr("=", []tree.Expr{leftexpr.Exprs[i], rightexpr.Exprs[i]}, depth) 618 if err != nil { 619 return nil, err 620 } 621 expr1, err = BindFuncExprImplByPlanExpr(b.GetContext(), "and", []*plan.Expr{expr1, expr2}) 622 if err != nil { 623 return nil, err 624 } 625 expr2, err = b.bindFuncExprImplByAstExpr("<", []tree.Expr{leftexpr.Exprs[i], rightexpr.Exprs[i]}, depth) 626 if err != nil { 627 return nil, err 628 } 629 expr1, err = BindFuncExprImplByPlanExpr(b.GetContext(), "or", []*plan.Expr{expr1, expr2}) 630 if err != nil { 631 return nil, err 632 } 633 } 634 return expr1, nil 635 } else { 636 return nil, moerr.NewInvalidInput(b.GetContext(), "two tuples have different length(%v,%v)", len(leftexpr.Exprs), len(rightexpr.Exprs)) 637 } 638 } 639 } 640 641 case tree.LESS_THAN_EQUAL: 642 op = "<=" 643 switch leftexpr := astExpr.Left.(type) { 644 case *tree.Tuple: 645 switch rightexpr := astExpr.Right.(type) { 646 case *tree.Tuple: 647 if len(leftexpr.Exprs) == len(rightexpr.Exprs) { 648 var expr1, expr2 *plan.Expr 649 var err error 650 for i := len(leftexpr.Exprs) - 2; i >= 0; i-- { 651 if i == len(leftexpr.Exprs)-2 { 652 expr1, err = b.bindFuncExprImplByAstExpr("<=", []tree.Expr{leftexpr.Exprs[i+1], rightexpr.Exprs[i+1]}, depth) 653 if err != nil { 654 return nil, err 655 } 656 } 657 expr2, err = b.bindFuncExprImplByAstExpr("=", []tree.Expr{leftexpr.Exprs[i], rightexpr.Exprs[i]}, depth) 658 if err != nil { 659 return nil, err 660 } 661 expr1, err = BindFuncExprImplByPlanExpr(b.GetContext(), "and", []*plan.Expr{expr1, expr2}) 662 if err != nil { 663 return nil, err 664 } 665 expr2, err = b.bindFuncExprImplByAstExpr("<", []tree.Expr{leftexpr.Exprs[i], rightexpr.Exprs[i]}, depth) 666 if err != nil { 667 return nil, err 668 } 669 expr1, err = BindFuncExprImplByPlanExpr(b.GetContext(), "or", []*plan.Expr{expr1, expr2}) 670 if err != nil { 671 return nil, err 672 } 673 } 674 return expr1, nil 675 } else { 676 return nil, moerr.NewInvalidInput(b.GetContext(), "two tuples have different length(%v,%v)", len(leftexpr.Exprs), len(rightexpr.Exprs)) 677 } 678 } 679 } 680 681 case tree.GREAT_THAN: 682 op = ">" 683 switch leftexpr := astExpr.Left.(type) { 684 case *tree.Tuple: 685 switch rightexpr := astExpr.Right.(type) { 686 case *tree.Tuple: 687 if len(leftexpr.Exprs) == len(rightexpr.Exprs) { 688 var expr1, expr2 *plan.Expr 689 var err error 690 for i := len(leftexpr.Exprs) - 2; i >= 0; i-- { 691 if i == len(leftexpr.Exprs)-2 { 692 expr1, err = b.bindFuncExprImplByAstExpr(">", []tree.Expr{leftexpr.Exprs[i+1], rightexpr.Exprs[i+1]}, depth) 693 if err != nil { 694 return nil, err 695 } 696 } 697 expr2, err = b.bindFuncExprImplByAstExpr("=", []tree.Expr{leftexpr.Exprs[i], rightexpr.Exprs[i]}, depth) 698 if err != nil { 699 return nil, err 700 } 701 expr1, err = BindFuncExprImplByPlanExpr(b.GetContext(), "and", []*plan.Expr{expr1, expr2}) 702 if err != nil { 703 return nil, err 704 } 705 expr2, err = b.bindFuncExprImplByAstExpr(">", []tree.Expr{leftexpr.Exprs[i], rightexpr.Exprs[i]}, depth) 706 if err != nil { 707 return nil, err 708 } 709 expr1, err = BindFuncExprImplByPlanExpr(b.GetContext(), "or", []*plan.Expr{expr1, expr2}) 710 if err != nil { 711 return nil, err 712 } 713 } 714 return expr1, nil 715 } else { 716 return nil, moerr.NewInvalidInput(b.GetContext(), "two tuples have different length(%v,%v)", len(leftexpr.Exprs), len(rightexpr.Exprs)) 717 } 718 } 719 } 720 721 case tree.GREAT_THAN_EQUAL: 722 op = ">=" 723 switch leftexpr := astExpr.Left.(type) { 724 case *tree.Tuple: 725 switch rightexpr := astExpr.Right.(type) { 726 case *tree.Tuple: 727 if len(leftexpr.Exprs) == len(rightexpr.Exprs) { 728 var expr1, expr2 *plan.Expr 729 var err error 730 for i := len(leftexpr.Exprs) - 2; i >= 0; i-- { 731 if i == len(leftexpr.Exprs)-2 { 732 expr1, err = b.bindFuncExprImplByAstExpr(">=", []tree.Expr{leftexpr.Exprs[i+1], rightexpr.Exprs[i+1]}, depth) 733 if err != nil { 734 return nil, err 735 } 736 } 737 expr2, err = b.bindFuncExprImplByAstExpr("=", []tree.Expr{leftexpr.Exprs[i], rightexpr.Exprs[i]}, depth) 738 if err != nil { 739 return nil, err 740 } 741 expr1, err = BindFuncExprImplByPlanExpr(b.GetContext(), "and", []*plan.Expr{expr1, expr2}) 742 if err != nil { 743 return nil, err 744 } 745 expr2, err = b.bindFuncExprImplByAstExpr(">", []tree.Expr{leftexpr.Exprs[i], rightexpr.Exprs[i]}, depth) 746 if err != nil { 747 return nil, err 748 } 749 expr1, err = BindFuncExprImplByPlanExpr(b.GetContext(), "or", []*plan.Expr{expr1, expr2}) 750 if err != nil { 751 return nil, err 752 } 753 } 754 return expr1, nil 755 } else { 756 return nil, moerr.NewInvalidInput(b.GetContext(), "two tuples have different length(%v,%v)", len(leftexpr.Exprs), len(rightexpr.Exprs)) 757 } 758 } 759 } 760 761 case tree.NOT_EQUAL: 762 op = "<>" 763 switch leftexpr := astExpr.Left.(type) { 764 case *tree.Tuple: 765 switch rightexpr := astExpr.Right.(type) { 766 case *tree.Tuple: 767 if len(leftexpr.Exprs) == len(rightexpr.Exprs) { 768 var expr1, expr2 *plan.Expr 769 var err error 770 for i := 1; i < len(leftexpr.Exprs); i++ { 771 if i == 1 { 772 expr1, err = b.bindFuncExprImplByAstExpr("<>", []tree.Expr{leftexpr.Exprs[0], rightexpr.Exprs[0]}, depth) 773 if err != nil { 774 return nil, err 775 } 776 } 777 expr2, err = b.bindFuncExprImplByAstExpr("<>", []tree.Expr{leftexpr.Exprs[i], rightexpr.Exprs[i]}, depth) 778 if err != nil { 779 return nil, err 780 } 781 expr1, err = BindFuncExprImplByPlanExpr(b.GetContext(), "or", []*plan.Expr{expr1, expr2}) 782 if err != nil { 783 return nil, err 784 } 785 } 786 return expr1, nil 787 } else { 788 return nil, moerr.NewInvalidInput(b.GetContext(), "two tuples have different length(%v,%v)", len(leftexpr.Exprs), len(rightexpr.Exprs)) 789 } 790 } 791 } 792 793 case tree.LIKE: 794 op = "like" 795 796 case tree.NOT_LIKE: 797 newExpr := tree.NewComparisonExpr(tree.LIKE, astExpr.Left, astExpr.Right) 798 return b.bindFuncExprImplByAstExpr("not", []tree.Expr{newExpr}, depth) 799 800 case tree.ILIKE: 801 op = "ilike" 802 803 case tree.NOT_ILIKE: 804 newExpr := tree.NewComparisonExpr(tree.ILIKE, astExpr.Left, astExpr.Right) 805 return b.bindFuncExprImplByAstExpr("not", []tree.Expr{newExpr}, depth) 806 807 case tree.IN: 808 switch astExpr.Right.(type) { 809 case *tree.Tuple: 810 op = "in" 811 812 default: 813 leftArg, err := b.impl.BindExpr(astExpr.Left, depth, false) 814 if err != nil { 815 return nil, err 816 } 817 818 rightArg, err := b.impl.BindExpr(astExpr.Right, depth, false) 819 if err != nil { 820 return nil, err 821 } 822 823 if subquery := rightArg.GetSub(); subquery != nil { 824 if list := leftArg.GetList(); list != nil { 825 if len(list.List) != int(subquery.RowSize) { 826 return nil, moerr.NewNYI(b.GetContext(), "subquery should return %d columns", len(list.List)) 827 } 828 } else { 829 if subquery.RowSize > 1 { 830 return nil, moerr.NewInvalidInput(b.GetContext(), "subquery returns more than 1 column") 831 } 832 } 833 834 subquery.Typ = plan.SubqueryRef_IN 835 subquery.Child = leftArg 836 837 rightArg.Typ = plan.Type{ 838 Id: int32(types.T_bool), 839 NotNullable: leftArg.Typ.NotNullable && rightArg.Typ.NotNullable, 840 } 841 842 return rightArg, nil 843 } else { 844 return BindFuncExprImplByPlanExpr(b.GetContext(), "in", []*plan.Expr{leftArg, rightArg}) 845 } 846 } 847 848 case tree.NOT_IN: 849 switch astExpr.Right.(type) { 850 case *tree.Tuple: 851 op = "not_in" 852 853 default: 854 leftArg, err := b.impl.BindExpr(astExpr.Left, depth, false) 855 if err != nil { 856 return nil, err 857 } 858 859 rightArg, err := b.impl.BindExpr(astExpr.Right, depth, false) 860 if err != nil { 861 return nil, err 862 } 863 864 if subquery := rightArg.GetSub(); subquery != nil { 865 if list := leftArg.GetList(); list != nil { 866 if len(list.List) != int(subquery.RowSize) { 867 return nil, moerr.NewInvalidInput(b.GetContext(), "subquery should return %d columns", len(list.List)) 868 } 869 } else { 870 if subquery.RowSize > 1 { 871 return nil, moerr.NewInvalidInput(b.GetContext(), "subquery should return 1 column") 872 } 873 } 874 875 subquery.Typ = plan.SubqueryRef_NOT_IN 876 subquery.Child = leftArg 877 878 rightArg.Typ = plan.Type{ 879 Id: int32(types.T_bool), 880 NotNullable: leftArg.Typ.NotNullable && rightArg.Typ.NotNullable, 881 } 882 883 return rightArg, nil 884 } else { 885 expr, err := BindFuncExprImplByPlanExpr(b.GetContext(), "in", []*plan.Expr{leftArg, rightArg}) 886 if err != nil { 887 return nil, err 888 } 889 890 return BindFuncExprImplByPlanExpr(b.GetContext(), "not", []*plan.Expr{expr}) 891 } 892 } 893 case tree.REG_MATCH: 894 op = "reg_match" 895 case tree.NOT_REG_MATCH: 896 op = "not_reg_match" 897 default: 898 return nil, moerr.NewNYI(b.GetContext(), "'%v'", astExpr) 899 } 900 901 if astExpr.SubOp >= tree.ANY { 902 expr, err := b.impl.BindExpr(astExpr.Right, depth, false) 903 if err != nil { 904 return nil, err 905 } 906 907 child, err := b.impl.BindExpr(astExpr.Left, depth, false) 908 if err != nil { 909 return nil, err 910 } 911 912 if subquery := expr.GetSub(); subquery != nil { 913 if list := child.GetList(); list != nil { 914 if len(list.List) != int(subquery.RowSize) { 915 return nil, moerr.NewInvalidInput(b.GetContext(), "subquery should return %d columns", len(list.List)) 916 } 917 } else { 918 if subquery.RowSize > 1 { 919 return nil, moerr.NewInvalidInput(b.GetContext(), "subquery should return 1 column") 920 } 921 } 922 923 subquery.Op = op 924 subquery.Child = child 925 926 switch astExpr.SubOp { 927 case tree.ANY, tree.SOME: 928 subquery.Typ = plan.SubqueryRef_ANY 929 case tree.ALL: 930 subquery.Typ = plan.SubqueryRef_ALL 931 } 932 933 expr.Typ = plan.Type{ 934 Id: int32(types.T_bool), 935 NotNullable: expr.Typ.NotNullable && child.Typ.NotNullable, 936 } 937 938 return expr, nil 939 } else { 940 return nil, moerr.NewInvalidInput(b.GetContext(), "subquery '%s' is not a quantifying subquery", astExpr.SubOp.ToString()) 941 } 942 } 943 944 return b.bindFuncExprImplByAstExpr(op, []tree.Expr{astExpr.Left, astExpr.Right}, depth) 945 } 946 947 func (b *baseBinder) bindFuncExpr(astExpr *tree.FuncExpr, depth int32, isRoot bool) (*Expr, error) { 948 funcRef, ok := astExpr.Func.FunctionReference.(*tree.UnresolvedName) 949 if !ok { 950 return nil, moerr.NewNYI(b.GetContext(), "function expr '%v'", astExpr) 951 } 952 funcName := funcRef.Parts[0] 953 954 if function.GetFunctionIsAggregateByName(funcName) && astExpr.WindowSpec == nil { 955 if b.ctx.timeTag > 0 { 956 return b.impl.BindTimeWindowFunc(funcName, astExpr, depth, isRoot) 957 } 958 return b.impl.BindAggFunc(funcName, astExpr, depth, isRoot) 959 } else if function.GetFunctionIsWinFunByName(funcName) { 960 return b.impl.BindWinFunc(funcName, astExpr, depth, isRoot) 961 } 962 963 return b.bindFuncExprImplByAstExpr(funcName, astExpr.Exprs, depth) 964 } 965 966 func (b *baseBinder) bindFuncExprImplByAstExpr(name string, astArgs []tree.Expr, depth int32) (*plan.Expr, error) { 967 // rewrite some ast Exprs before binding 968 switch name { 969 case "nullif": 970 // rewrite 'nullif(expr1, expr2)' to 'case when expr1=expr2 then null else expr1' 971 if len(astArgs) != 2 { 972 return nil, moerr.NewInvalidArg(b.GetContext(), "nullif need two args", len(astArgs)) 973 } 974 elseExpr := astArgs[0] 975 thenExpr := tree.NewNumValWithType(constant.MakeUnknown(), "", false, tree.P_char) 976 whenExpr := tree.NewComparisonExpr(tree.EQUAL, astArgs[0], astArgs[1]) 977 astArgs = []tree.Expr{whenExpr, thenExpr, elseExpr} 978 name = "case" 979 980 case "ifnull": 981 // rewrite 'ifnull(expr1, expr2)' to 'case when isnull(expr1) then expr2 else null' 982 if len(astArgs) != 2 { 983 return nil, moerr.NewInvalidArg(b.GetContext(), "ifnull function need two args", len(astArgs)) 984 } 985 elseExpr := astArgs[0] 986 thenExpr := astArgs[1] 987 whenExpr := tree.NewIsNullExpr(astArgs[0]) 988 astArgs = []tree.Expr{whenExpr, thenExpr, elseExpr} 989 name = "case" 990 991 //case "extract": 992 // // "extract(year from col_name)" parser return year as UnresolvedName. 993 // // we must rewrite it to string。 because binder bind UnresolvedName as column name 994 // unit := astArgs[0].(*tree.UnresolvedName).Parts[0] 995 // astArgs[0] = tree.NewNumVal(constant.MakeString(unit), unit, false) 996 997 case "count": 998 if b.ctx == nil { 999 return nil, moerr.NewInvalidInput(b.GetContext(), "invalid field reference to COUNT") 1000 } 1001 // we will rewrite "count(*)" to "starcount(col)" 1002 // count(*) : astExprs[0].(type) is *tree.NumVal 1003 // count(col_name) : astExprs[0].(type) is *tree.UnresolvedName 1004 switch nval := astArgs[0].(type) { 1005 case *tree.NumVal: 1006 if nval.String() == "*" { 1007 if len(b.ctx.bindings) == 0 || len(b.ctx.bindings[0].cols) == 0 { 1008 // sql: 'select count(*)' without from clause. we do nothing 1009 } else { 1010 // sql: 'select count(*) from t1', 1011 // rewrite count(*) to starcount(col_name) 1012 name = "starcount" 1013 1014 astArgs = []tree.Expr{tree.NewNumValWithType(constant.MakeInt64(1), "1", false, tree.P_int64)} 1015 } 1016 } 1017 } 1018 1019 case "approx_count": 1020 if b.ctx == nil { 1021 return nil, moerr.NewInvalidInput(b.GetContext(), "invalid field reference to COUNT") 1022 } 1023 name = "count" 1024 1025 case "trim": 1026 astArgs = astArgs[1:] 1027 } 1028 1029 // bind ast function's args 1030 var args []*Expr 1031 if name == "bit_cast" { 1032 bitCastExpr := astArgs[0].(*tree.BitCastExpr) 1033 binExpr, err := b.impl.BindExpr(bitCastExpr.Expr, depth, false) 1034 if err != nil { 1035 return nil, err 1036 } 1037 1038 typ, err := getTypeFromAst(b.GetContext(), bitCastExpr.Type) 1039 if err != nil { 1040 return nil, err 1041 } 1042 typeExpr := &Expr{ 1043 Typ: typ, 1044 Expr: &plan.Expr_T{ 1045 T: &plan.TargetType{}, 1046 }, 1047 } 1048 1049 args = []*Expr{binExpr, typeExpr} 1050 } else if name == "serial_extract" { 1051 serialExtractExpr := astArgs[0].(*tree.SerialExtractExpr) 1052 1053 // 1. bind serial expr 1054 serialExpr, err := b.impl.BindExpr(serialExtractExpr.SerialExpr, depth, false) 1055 if err != nil { 1056 return nil, err 1057 } 1058 1059 // 2. bind index expr 1060 idxExpr, err := b.impl.BindExpr(serialExtractExpr.IndexExpr, depth, false) 1061 if err != nil { 1062 return nil, err 1063 } 1064 1065 // 3. bind type 1066 typ, err := getTypeFromAst(b.GetContext(), serialExtractExpr.ResultType) 1067 if err != nil { 1068 return nil, err 1069 } 1070 typeExpr := &Expr{ 1071 Typ: typ, 1072 Expr: &plan.Expr_T{ 1073 T: &plan.TargetType{}, 1074 }, 1075 } 1076 1077 // 4. return [serialExpr, idxExpr, typeExpr]. Used in list_builtIn.go 1078 args = []*Expr{serialExpr, idxExpr, typeExpr} 1079 } else { 1080 args = make([]*Expr, len(astArgs)) 1081 for idx, arg := range astArgs { 1082 expr, err := b.impl.BindExpr(arg, depth, false) 1083 if err != nil { 1084 return nil, err 1085 } 1086 1087 args[idx] = expr 1088 } 1089 } 1090 1091 if b.builder != nil { 1092 e, err := bindFuncExprAndConstFold(b.GetContext(), b.builder.compCtx.GetProcess(), name, args) 1093 if err == nil { 1094 return e, nil 1095 } 1096 if !strings.Contains(err.Error(), "not supported") { 1097 return nil, err 1098 } 1099 } else { 1100 // return bindFuncExprImplByPlanExpr(b.GetContext(), name, args) 1101 // first look for builtin func 1102 builtinExpr, err := BindFuncExprImplByPlanExpr(b.GetContext(), name, args) 1103 if err == nil { 1104 return builtinExpr, nil 1105 } 1106 if !strings.Contains(err.Error(), "not supported") { 1107 return nil, err 1108 } 1109 } 1110 1111 // not a builtin func, look to resolve udf 1112 cmpCtx := b.builder.compCtx 1113 udf, err := cmpCtx.ResolveUdf(name, args) 1114 if err != nil { 1115 return nil, err 1116 } 1117 1118 return bindFuncExprImplUdf(b, name, udf, astArgs, depth) 1119 } 1120 1121 func bindFuncExprImplUdf(b *baseBinder, name string, udf *function.Udf, args []tree.Expr, depth int32) (*plan.Expr, error) { 1122 if udf == nil { 1123 return nil, moerr.NewNotSupported(b.GetContext(), "function '%s'", name) 1124 } 1125 1126 switch udf.Language { 1127 case string(tree.SQL): 1128 sql := udf.Body 1129 // replace sql with actual arg value 1130 fmtctx := tree.NewFmtCtx(dialect.MYSQL, tree.WithQuoteString(true)) 1131 for i := 0; i < len(args); i++ { 1132 args[i].Format(fmtctx) 1133 sql = strings.Replace(sql, "$"+strconv.Itoa(i+1), fmtctx.String(), 1) 1134 fmtctx.Reset() 1135 } 1136 1137 // if does not contain SELECT, an expression. In order to pass the parser, 1138 // make it start with a 'SELECT'. 1139 1140 var expr *plan.Expr 1141 1142 if !strings.Contains(sql, "select") { 1143 sql = "select " + sql 1144 substmts, err := parsers.Parse(b.GetContext(), dialect.MYSQL, sql, 1, 0) 1145 if err != nil { 1146 return nil, err 1147 } 1148 expr, err = b.impl.BindExpr(substmts[0].(*tree.Select).Select.(*tree.SelectClause).Exprs[0].Expr, depth, false) 1149 if err != nil { 1150 return nil, err 1151 } 1152 } else { 1153 substmts, err := parsers.Parse(b.GetContext(), dialect.MYSQL, sql, 1, 0) 1154 if err != nil { 1155 return nil, err 1156 } 1157 subquery := tree.NewSubquery(substmts[0], false) 1158 expr, err = b.impl.BindSubquery(subquery, false) 1159 if err != nil { 1160 return nil, err 1161 } 1162 } 1163 return expr, nil 1164 case string(tree.PYTHON): 1165 expr, err := b.bindPythonUdf(udf, args, depth) 1166 if err != nil { 1167 return nil, err 1168 } 1169 return expr, nil 1170 default: 1171 return nil, moerr.NewInvalidArg(b.GetContext(), "function language", udf.Language) 1172 } 1173 } 1174 1175 func (b *baseBinder) bindPythonUdf(udf *function.Udf, astArgs []tree.Expr, depth int32) (*plan.Expr, error) { 1176 args := make([]*Expr, 2*len(astArgs)+2) 1177 1178 // python udf self info and query context 1179 args[0] = udf.GetPlanExpr() 1180 1181 // bind ast function's args 1182 for idx, arg := range astArgs { 1183 expr, err := b.impl.BindExpr(arg, depth, false) 1184 if err != nil { 1185 return nil, err 1186 } 1187 args[idx+1] = expr 1188 } 1189 1190 // function args 1191 fArgTypes := udf.GetArgsPlanType() 1192 for i, t := range fArgTypes { 1193 args[len(astArgs)+i+1] = &Expr{Typ: *t} 1194 } 1195 1196 // function ret 1197 fRetType := udf.GetRetPlanType() 1198 args[2*len(astArgs)+1] = &Expr{Typ: *fRetType} 1199 1200 return BindFuncExprImplByPlanExpr(b.GetContext(), "python_user_defined_function", args) 1201 } 1202 1203 func bindFuncExprAndConstFold(ctx context.Context, proc *process.Process, name string, args []*Expr) (*plan.Expr, error) { 1204 retExpr, err := BindFuncExprImplByPlanExpr(ctx, name, args) 1205 if err != nil { 1206 return nil, err 1207 } 1208 1209 switch retExpr.GetF().GetFunc().GetObjName() { 1210 case "+", "-", "*", "/", "unary_minus", "unary_plus", "unary_tilde", "in", "prefix_in", "serial", "serial_full": 1211 if proc != nil { 1212 tmpexpr, _ := ConstantFold(batch.EmptyForConstFoldBatch, DeepCopyExpr(retExpr), proc, false) 1213 if tmpexpr != nil { 1214 retExpr = tmpexpr 1215 } 1216 } 1217 1218 case "between": 1219 if proc == nil { 1220 goto between_fallback 1221 } 1222 1223 fnArgs := retExpr.GetF().Args 1224 arg1, err := ConstantFold(batch.EmptyForConstFoldBatch, fnArgs[1], proc, false) 1225 if err != nil { 1226 goto between_fallback 1227 } 1228 fnArgs[1] = arg1 1229 1230 lit0 := arg1.GetLit() 1231 if arg1.Typ.Id == int32(types.T_any) || lit0 == nil { 1232 goto between_fallback 1233 } 1234 1235 arg2, err := ConstantFold(batch.EmptyForConstFoldBatch, fnArgs[2], proc, false) 1236 if err != nil { 1237 goto between_fallback 1238 } 1239 fnArgs[2] = arg2 1240 1241 lit1 := arg1.GetLit() 1242 if arg1.Typ.Id == int32(types.T_any) || lit1 == nil { 1243 goto between_fallback 1244 } 1245 1246 rangeCheckFn, _ := BindFuncExprImplByPlanExpr(ctx, "<=", []*plan.Expr{arg1, arg2}) 1247 rangeCheckRes, _ := ConstantFold(batch.EmptyForConstFoldBatch, rangeCheckFn, proc, false) 1248 rangeCheckVal := rangeCheckRes.GetLit() 1249 if rangeCheckVal == nil || !rangeCheckVal.GetBval() { 1250 goto between_fallback 1251 } 1252 1253 retExpr, _ = ConstantFold(batch.EmptyForConstFoldBatch, retExpr, proc, false) 1254 } 1255 1256 return retExpr, nil 1257 1258 between_fallback: 1259 fnArgs := retExpr.GetF().Args 1260 leftFn, err := BindFuncExprImplByPlanExpr(ctx, ">=", []*plan.Expr{DeepCopyExpr(fnArgs[0]), fnArgs[1]}) 1261 if err != nil { 1262 return nil, err 1263 } 1264 rightFn, err := BindFuncExprImplByPlanExpr(ctx, "<=", []*plan.Expr{fnArgs[0], fnArgs[2]}) 1265 if err != nil { 1266 return nil, err 1267 } 1268 1269 retExpr, err = BindFuncExprImplByPlanExpr(ctx, "and", []*plan.Expr{leftFn, rightFn}) 1270 if err != nil { 1271 return nil, err 1272 } 1273 retExpr, err = ConstantFold(batch.EmptyForConstFoldBatch, retExpr, proc, false) 1274 if err != nil { 1275 return nil, err 1276 } 1277 1278 return retExpr, nil 1279 } 1280 1281 func BindFuncExprImplByPlanExpr(ctx context.Context, name string, args []*Expr) (*plan.Expr, error) { 1282 var err error 1283 1284 // deal with some special function 1285 switch name { 1286 case "interval": 1287 // rewrite interval function to ListExpr, and return directly 1288 return &plan.Expr{ 1289 Typ: plan.Type{ 1290 Id: int32(types.T_interval), 1291 }, 1292 Expr: &plan.Expr_List{ 1293 List: &plan.ExprList{ 1294 List: args, 1295 }, 1296 }, 1297 }, nil 1298 case "and", "or", "not", "xor": 1299 // why not append cast function? 1300 // for i := 0; i < len(args); i++ { 1301 // if args[i].Typ.Id != types.T_bool { 1302 // arg, err := appendCastBeforeExpr(args[i], &plan.Type{ 1303 // Id: types.T_bool, 1304 // }) 1305 // if err != nil { 1306 // return nil, err 1307 // } 1308 // args[i] = arg 1309 // } 1310 // } 1311 if err := convertValueIntoBool(name, args, true); err != nil { 1312 return nil, err 1313 } 1314 case "=", "<", "<=", ">", ">=", "<>": 1315 // why not append cast function? 1316 if err := convertValueIntoBool(name, args, false); err != nil { 1317 return nil, err 1318 } 1319 case "date_add", "date_sub": 1320 // rewrite date_add/date_sub function 1321 // date_add(col_name, "1 day"), will rewrite to date_add(col_name, number, unit) 1322 if len(args) != 2 { 1323 return nil, moerr.NewInvalidArg(ctx, "date_add/date_sub function need two args", len(args)) 1324 } 1325 args, err = resetDateFunction(ctx, args[0], args[1]) 1326 if err != nil { 1327 return nil, err 1328 } 1329 case "adddate", "subdate": 1330 if len(args) != 2 { 1331 return nil, moerr.NewInvalidArg(ctx, "adddate/subdate function need two args", len(args)) 1332 } 1333 args, err = resetDateFunction(ctx, args[0], args[1]) 1334 if err != nil { 1335 return nil, err 1336 } 1337 if name == "adddate" { 1338 name = "date_add" 1339 } else { 1340 name = "date_sub" 1341 } 1342 case "+": 1343 if len(args) != 2 { 1344 return nil, moerr.NewInvalidArg(ctx, "operator + need two args", len(args)) 1345 } 1346 if isNullExpr(args[0]) { 1347 return args[0], nil 1348 } 1349 if isNullExpr(args[1]) { 1350 return args[1], nil 1351 } 1352 if args[0].Typ.Id == int32(types.T_date) && args[1].Typ.Id == int32(types.T_interval) { 1353 name = "date_add" 1354 args, err = resetDateFunctionArgs(ctx, args[0], args[1]) 1355 } else if args[0].Typ.Id == int32(types.T_interval) && args[1].Typ.Id == int32(types.T_date) { 1356 name = "date_add" 1357 args, err = resetDateFunctionArgs(ctx, args[1], args[0]) 1358 } else if args[0].Typ.Id == int32(types.T_datetime) && args[1].Typ.Id == int32(types.T_interval) { 1359 name = "date_add" 1360 args, err = resetDateFunctionArgs(ctx, args[0], args[1]) 1361 } else if args[0].Typ.Id == int32(types.T_interval) && args[1].Typ.Id == int32(types.T_datetime) { 1362 name = "date_add" 1363 args, err = resetDateFunctionArgs(ctx, args[1], args[0]) 1364 } else if args[0].Typ.Id == int32(types.T_varchar) && args[1].Typ.Id == int32(types.T_interval) { 1365 name = "date_add" 1366 args, err = resetDateFunctionArgs(ctx, args[0], args[1]) 1367 } else if args[0].Typ.Id == int32(types.T_interval) && args[1].Typ.Id == int32(types.T_varchar) { 1368 name = "date_add" 1369 args, err = resetDateFunctionArgs(ctx, args[1], args[0]) 1370 } else if args[0].Typ.Id == int32(types.T_varchar) && args[1].Typ.Id == int32(types.T_varchar) { 1371 name = "concat" 1372 } 1373 if err != nil { 1374 return nil, err 1375 } 1376 case "-": 1377 if len(args) != 2 { 1378 return nil, moerr.NewInvalidArg(ctx, "operator - need two args", len(args)) 1379 } 1380 if isNullExpr(args[0]) { 1381 return args[0], nil 1382 } 1383 if isNullExpr(args[1]) { 1384 return args[1], nil 1385 } 1386 // rewrite "date '2001' - interval '1 day'" to date_sub(date '2001', 1, day(unit)) 1387 if args[0].Typ.Id == int32(types.T_date) && args[1].Typ.Id == int32(types.T_interval) { 1388 name = "date_sub" 1389 args, err = resetDateFunctionArgs(ctx, args[0], args[1]) 1390 } else if args[0].Typ.Id == int32(types.T_datetime) && args[1].Typ.Id == int32(types.T_interval) { 1391 name = "date_sub" 1392 args, err = resetDateFunctionArgs(ctx, args[0], args[1]) 1393 } else if args[0].Typ.Id == int32(types.T_varchar) && args[1].Typ.Id == int32(types.T_interval) { 1394 name = "date_sub" 1395 args, err = resetDateFunctionArgs(ctx, args[0], args[1]) 1396 } 1397 if err != nil { 1398 return nil, err 1399 } 1400 case "*", "/", "%": 1401 if len(args) != 2 { 1402 return nil, moerr.NewInvalidArg(ctx, fmt.Sprintf("operator %s need two args", name), len(args)) 1403 } 1404 if isNullExpr(args[0]) { 1405 return args[0], nil 1406 } 1407 if isNullExpr(args[1]) { 1408 return args[1], nil 1409 } 1410 case "unary_minus": 1411 if len(args) == 0 { 1412 return nil, moerr.NewInvalidArg(ctx, name+" function have invalid input args length", len(args)) 1413 } 1414 if args[0].Typ.Id == int32(types.T_uint64) { 1415 args[0], err = appendCastBeforeExpr(ctx, args[0], plan.Type{ 1416 Id: int32(types.T_decimal128), 1417 NotNullable: args[0].Typ.NotNullable, 1418 }) 1419 if err != nil { 1420 return nil, err 1421 } 1422 } 1423 case "oct", "bit_and", "bit_or", "bit_xor": 1424 if len(args) == 0 { 1425 return nil, moerr.NewInvalidArg(ctx, name+" function have invalid input args length", len(args)) 1426 } 1427 if args[0].Typ.Id == int32(types.T_decimal128) || args[0].Typ.Id == int32(types.T_decimal64) { 1428 args[0], err = appendCastBeforeExpr(ctx, args[0], plan.Type{ 1429 Id: int32(types.T_float64), 1430 NotNullable: args[0].Typ.NotNullable, 1431 }) 1432 if err != nil { 1433 return nil, err 1434 } 1435 } 1436 case "like": 1437 // sql 'select * from t where col like ?' the ? Expr's type will be T_any 1438 if len(args) != 2 { 1439 return nil, moerr.NewInvalidArg(ctx, name+" function have invalid input args length", len(args)) 1440 } 1441 if args[0].Typ.Id == int32(types.T_any) { 1442 args[0].Typ.Id = int32(types.T_varchar) 1443 } 1444 if args[1].Typ.Id == int32(types.T_any) { 1445 args[1].Typ.Id = int32(types.T_varchar) 1446 } 1447 if args[0].Typ.Id == int32(types.T_json) { 1448 targetTp := types.T_varchar.ToType() 1449 args[0], err = appendCastBeforeExpr(ctx, args[0], makePlan2Type(&targetTp), false) 1450 if err != nil { 1451 return nil, err 1452 } 1453 } 1454 if args[1].Typ.Id == int32(types.T_json) { 1455 targetTp := types.T_varchar.ToType() 1456 args[1], err = appendCastBeforeExpr(ctx, args[1], makePlan2Type(&targetTp), false) 1457 if err != nil { 1458 return nil, err 1459 } 1460 } 1461 case "timediff": 1462 if len(args) != 2 { 1463 return nil, moerr.NewInvalidArg(ctx, name+" function have invalid input args length", len(args)) 1464 } 1465 1466 case "str_to_date", "to_date": 1467 if len(args) != 2 { 1468 return nil, moerr.NewInvalidArg(ctx, name+" function have invalid input args length", len(args)) 1469 } 1470 1471 if args[1].Typ.Id == int32(types.T_varchar) || args[1].Typ.Id == int32(types.T_char) { 1472 var tp = types.T_date 1473 if exprC := args[1].GetLit(); exprC != nil { 1474 sval := exprC.Value.(*plan.Literal_Sval) 1475 tp, _ = ExtractToDateReturnType(sval.Sval) 1476 } 1477 args = append(args, makePlan2DateConstNullExpr(tp)) 1478 1479 } else if args[1].Typ.Id == int32(types.T_any) { 1480 args = append(args, makePlan2DateConstNullExpr(types.T_datetime)) 1481 } else { 1482 return nil, moerr.NewInvalidArg(ctx, name+" function have invalid input args length", len(args)) 1483 } 1484 case "unix_timestamp": 1485 if len(args) == 1 { 1486 if types.T(args[0].Typ.Id).IsMySQLString() { 1487 if exprC := args[0].GetLit(); exprC != nil { 1488 sval := exprC.Value.(*plan.Literal_Sval) 1489 tp := judgeUnixTimestampReturnType(sval.Sval) 1490 if tp == types.T_int64 { 1491 args = append(args, makePlan2Int64ConstExprWithType(0)) 1492 } else { 1493 args = append(args, makePlan2Decimal128ConstNullExpr()) 1494 } 1495 } else { 1496 args = append(args, makePlan2Decimal128ConstNullExpr()) 1497 } 1498 } 1499 } else if len(args) > 1 { 1500 return nil, moerr.NewInvalidArg(ctx, name+" function have invalid input args size", len(args)) 1501 } 1502 case "ascii": 1503 if len(args) != 1 { 1504 return nil, moerr.NewInvalidArg(ctx, name+" function have invalid input args length", len(args)) 1505 } 1506 tp := types.T(args[0].Typ.Id) 1507 switch { 1508 case tp.IsMySQLString(), tp.IsInteger(): 1509 default: 1510 targetTp := types.T_varchar.ToType() 1511 args[0], err = appendCastBeforeExpr(ctx, args[0], makePlan2Type(&targetTp), false) 1512 if err != nil { 1513 return nil, err 1514 } 1515 } 1516 } 1517 1518 // get args(exprs) & types 1519 argsLength := len(args) 1520 argsType := make([]types.Type, argsLength) 1521 for idx, expr := range args { 1522 argsType[idx] = makeTypeByPlan2Expr(expr) 1523 } 1524 1525 var funcID int64 1526 var returnType types.Type 1527 var argsCastType []types.Type 1528 1529 // get function definition 1530 fGet, err := function.GetFunctionByName(ctx, name, argsType) 1531 if err != nil { 1532 if name == "between" { 1533 leftFn, err := BindFuncExprImplByPlanExpr(ctx, ">=", []*plan.Expr{DeepCopyExpr(args[0]), args[1]}) 1534 if err != nil { 1535 return nil, err 1536 } 1537 1538 rightFn, err := BindFuncExprImplByPlanExpr(ctx, "<=", []*plan.Expr{args[0], args[2]}) 1539 if err != nil { 1540 return nil, err 1541 } 1542 1543 return BindFuncExprImplByPlanExpr(ctx, "and", []*plan.Expr{leftFn, rightFn}) 1544 } 1545 1546 return nil, err 1547 } 1548 1549 funcID = fGet.GetEncodedOverloadID() 1550 returnType = fGet.GetReturnType() 1551 argsCastType, _ = fGet.ShouldDoImplicitTypeCast() 1552 1553 if function.GetFunctionIsAggregateByName(name) { 1554 if constExpr := args[0].GetLit(); constExpr != nil && constExpr.Isnull { 1555 args[0].Typ = makePlan2Type(&returnType) 1556 } 1557 } 1558 1559 // rewrite some cast rule: expr: int32Col > 10, 1560 // old rule: cast(int32Col as int64) >10 , new rule: int32Col > (cast 10 as int32) 1561 switch name { 1562 case "=", "<", "<=", ">", ">=", "<>", "like": 1563 // if constant's type higher than column's type 1564 // and constant's value in range of column's type, then no cast was needed 1565 switch leftExpr := args[0].Expr.(type) { 1566 case *plan.Expr_Lit: 1567 if args[1].GetCol() != nil { 1568 if checkNoNeedCast(argsType[0], argsType[1], leftExpr.Lit) { 1569 argsCastType = []types.Type{argsType[1], argsType[1]} 1570 // need to update function id 1571 fGet, err = function.GetFunctionByName(ctx, name, argsCastType) 1572 if err != nil { 1573 return nil, err 1574 } 1575 funcID = fGet.GetEncodedOverloadID() 1576 } 1577 } 1578 case *plan.Expr_Col: 1579 if checkNoNeedCast(argsType[1], argsType[0], args[1].GetLit()) { 1580 argsCastType = []types.Type{argsType[0], argsType[0]} 1581 fGet, err = function.GetFunctionByName(ctx, name, argsCastType) 1582 if err != nil { 1583 return nil, err 1584 } 1585 funcID = fGet.GetEncodedOverloadID() 1586 } 1587 } 1588 1589 case "between": 1590 if checkNoNeedCast(argsType[1], argsType[0], args[1].GetLit()) && checkNoNeedCast(argsType[2], argsType[0], args[2].GetLit()) { 1591 argsCastType = []types.Type{argsType[0], argsType[0], argsType[0]} 1592 fGet, err = function.GetFunctionByName(ctx, name, argsCastType) 1593 if err != nil { 1594 return nil, err 1595 } 1596 funcID = fGet.GetEncodedOverloadID() 1597 } 1598 1599 case "in", "not_in": 1600 //if all the expr in the in list can safely cast to left type, we call it safe 1601 if rightList := args[1].GetList(); rightList != nil { 1602 typLeft := makeTypeByPlan2Expr(args[0]) 1603 var inExprList, orExprList []*plan.Expr 1604 1605 for _, rightVal := range rightList.List { 1606 if checkNoNeedCast(makeTypeByPlan2Expr(rightVal), typLeft, rightVal.GetLit()) { 1607 inExpr, err := appendCastBeforeExpr(ctx, rightVal, args[0].Typ) 1608 if err != nil { 1609 return nil, err 1610 } 1611 inExprList = append(inExprList, inExpr) 1612 } else { 1613 orExprList = append(orExprList, rightVal) 1614 } 1615 } 1616 1617 var newExpr *plan.Expr 1618 1619 if len(inExprList) > 1 { 1620 rightList.List = inExprList 1621 typ := makePlan2Type(&returnType) 1622 typ.NotNullable = function.DeduceNotNullable(funcID, args) 1623 newExpr = &Expr{ 1624 Expr: &plan.Expr_F{ 1625 F: &plan.Function{ 1626 Func: getFunctionObjRef(funcID, name), 1627 Args: args, 1628 }, 1629 }, 1630 Typ: typ, 1631 } 1632 } else if len(inExprList) > 0 { 1633 orExprList = append(inExprList, orExprList...) 1634 } 1635 1636 //expand the in list to col=a or col=b or ...... 1637 if name == "in" { 1638 for _, expr := range orExprList { 1639 tmpExpr, _ := BindFuncExprImplByPlanExpr(ctx, "=", []*Expr{DeepCopyExpr(args[0]), expr}) 1640 if newExpr == nil { 1641 newExpr = tmpExpr 1642 } else { 1643 newExpr, _ = BindFuncExprImplByPlanExpr(ctx, "or", []*Expr{newExpr, tmpExpr}) 1644 } 1645 } 1646 } else { 1647 for _, expr := range orExprList { 1648 tmpExpr, _ := BindFuncExprImplByPlanExpr(ctx, "!=", []*Expr{DeepCopyExpr(args[0]), expr}) 1649 if newExpr == nil { 1650 newExpr = tmpExpr 1651 } else { 1652 newExpr, _ = BindFuncExprImplByPlanExpr(ctx, "and", []*Expr{newExpr, tmpExpr}) 1653 } 1654 } 1655 } 1656 1657 return newExpr, nil 1658 } 1659 1660 case "timediff": 1661 if len(argsType) == len(argsCastType) { 1662 for i := range argsType { 1663 if int(argsType[i].Oid) == int(types.T_time) && int(argsCastType[i].Oid) == int(types.T_datetime) { 1664 return nil, moerr.NewInvalidInput(ctx, name+" function have invalid input args type") 1665 } 1666 } 1667 } 1668 1669 case "python_user_defined_function": 1670 size := (argsLength - 2) / 2 1671 args = args[:size+1] 1672 argsLength = len(args) 1673 argsType = argsType[:size+1] 1674 if len(argsCastType) > 0 { 1675 argsCastType = argsCastType[:size+1] 1676 } 1677 } 1678 1679 if len(argsCastType) != 0 { 1680 if len(argsCastType) != argsLength { 1681 return nil, moerr.NewInvalidArg(ctx, "cast types length not match args length", "") 1682 } 1683 for idx, castType := range argsCastType { 1684 if !argsType[idx].Eq(castType) && castType.Oid != types.T_any { 1685 if argsType[idx].Oid == castType.Oid && castType.Oid.IsDecimal() && argsType[idx].Scale == castType.Scale { 1686 continue 1687 } 1688 typ := makePlan2Type(&castType) 1689 args[idx], err = appendCastBeforeExpr(ctx, args[idx], typ) 1690 if err != nil { 1691 return nil, err 1692 } 1693 } 1694 } 1695 } 1696 1697 // return new expr 1698 Typ := makePlan2Type(&returnType) 1699 Typ.NotNullable = function.DeduceNotNullable(funcID, args) 1700 return &Expr{ 1701 Expr: &plan.Expr_F{ 1702 F: &plan.Function{ 1703 Func: getFunctionObjRef(funcID, name), 1704 Args: args, 1705 }, 1706 }, 1707 Typ: Typ, 1708 }, nil 1709 } 1710 1711 func (b *baseBinder) bindNumVal(astExpr *tree.NumVal, typ Type) (*Expr, error) { 1712 // over_int64_err := moerr.NewInternalError(b.GetContext(), "", "Constants over int64 will support in future version.") 1713 // rewrite the hexnum process logic 1714 // for float64, if the number is over 1<<53-1,it will lost, so if typ is float64, 1715 // don't cast 0xXXXX as float64, use the uint64 1716 returnDecimalExpr := func(val string) (*Expr, error) { 1717 if !typ.IsEmpty() { 1718 return appendCastBeforeExpr(b.GetContext(), makePlan2StringConstExprWithType(val), typ) 1719 } 1720 return makePlan2DecimalExprWithType(b.GetContext(), val) 1721 } 1722 1723 returnHexNumExpr := func(val string, isBin ...bool) (*Expr, error) { 1724 if !typ.IsEmpty() { 1725 isFloat := typ.Id == int32(types.T_float32) || typ.Id == int32(types.T_float64) 1726 return appendCastBeforeExpr(b.GetContext(), makePlan2StringConstExprWithType(val, isBin[0]), typ, isBin[0], isFloat) 1727 } 1728 return makePlan2StringConstExprWithType(val, isBin...), nil 1729 } 1730 1731 switch astExpr.ValType { 1732 case tree.P_null: 1733 return makePlan2NullConstExprWithType(), nil 1734 case tree.P_bool: 1735 val := constant.BoolVal(astExpr.Value) 1736 return makePlan2BoolConstExprWithType(val), nil 1737 case tree.P_int64: 1738 val, ok := constant.Int64Val(astExpr.Value) 1739 if !ok { 1740 return nil, moerr.NewInvalidInput(b.GetContext(), "invalid int value '%s'", astExpr.Value.String()) 1741 } 1742 expr := makePlan2Int64ConstExprWithType(val) 1743 if !typ.IsEmpty() && typ.Id == int32(types.T_varchar) { 1744 return appendCastBeforeExpr(b.GetContext(), expr, typ) 1745 } 1746 return expr, nil 1747 case tree.P_uint64: 1748 val, ok := constant.Uint64Val(astExpr.Value) 1749 if !ok { 1750 return nil, moerr.NewInvalidInput(b.GetContext(), "invalid int value '%s'", astExpr.Value.String()) 1751 } 1752 return makePlan2Uint64ConstExprWithType(val), nil 1753 case tree.P_decimal: 1754 if !typ.IsEmpty() { 1755 if typ.Id == int32(types.T_decimal64) { 1756 d64, err := types.ParseDecimal64(astExpr.String(), typ.Width, typ.Scale) 1757 if err != nil { 1758 return nil, err 1759 } 1760 return &Expr{ 1761 Expr: &plan.Expr_Lit{ 1762 Lit: &Const{ 1763 Isnull: false, 1764 Value: &plan.Literal_Decimal64Val{ 1765 Decimal64Val: &plan.Decimal64{A: int64(d64)}, 1766 }, 1767 }, 1768 }, 1769 Typ: typ, 1770 }, nil 1771 } 1772 if typ.Id == int32(types.T_decimal128) { 1773 d128, err := types.ParseDecimal128(astExpr.String(), typ.Width, typ.Scale) 1774 if err != nil { 1775 return nil, err 1776 } 1777 a := int64(d128.B0_63) 1778 b := int64(d128.B64_127) 1779 return &Expr{ 1780 Expr: &plan.Expr_Lit{ 1781 Lit: &Const{ 1782 Isnull: false, 1783 Value: &plan.Literal_Decimal128Val{ 1784 Decimal128Val: &plan.Decimal128{A: a, B: b}, 1785 }, 1786 }, 1787 }, 1788 Typ: typ, 1789 }, nil 1790 } 1791 return appendCastBeforeExpr(b.GetContext(), makePlan2StringConstExprWithType(astExpr.String()), typ) 1792 } 1793 d128, scale, err := types.Parse128(astExpr.String()) 1794 if err != nil { 1795 return nil, err 1796 } 1797 a := int64(d128.B0_63) 1798 b := int64(d128.B64_127) 1799 return &Expr{ 1800 Expr: &plan.Expr_Lit{ 1801 Lit: &Const{ 1802 Isnull: false, 1803 Value: &plan.Literal_Decimal128Val{ 1804 Decimal128Val: &plan.Decimal128{A: a, B: b}, 1805 }, 1806 }, 1807 }, 1808 Typ: plan.Type{ 1809 Id: int32(types.T_decimal128), 1810 Width: 38, 1811 Scale: scale, 1812 NotNullable: true, 1813 }, 1814 }, nil 1815 case tree.P_float64: 1816 originString := astExpr.String() 1817 if !typ.IsEmpty() && (typ.Id == int32(types.T_decimal64) || typ.Id == int32(types.T_decimal128)) { 1818 return returnDecimalExpr(originString) 1819 } 1820 if !strings.Contains(originString, "e") { 1821 expr, err := returnDecimalExpr(originString) 1822 if err == nil { 1823 return expr, nil 1824 } 1825 } 1826 floatValue, ok := constant.Float64Val(astExpr.Value) 1827 if !ok { 1828 return returnDecimalExpr(originString) 1829 } 1830 return makePlan2Float64ConstExprWithType(floatValue), nil 1831 case tree.P_hexnum: 1832 s := astExpr.String()[2:] 1833 if len(s)%2 != 0 { 1834 s = string('0') + s 1835 } 1836 bytes, _ := hex.DecodeString(s) 1837 return returnHexNumExpr(string(bytes), true) 1838 case tree.P_ScoreBinary: 1839 return returnHexNumExpr(astExpr.String(), true) 1840 case tree.P_bit: 1841 s := astExpr.String()[2:] 1842 bytes, _ := util.DecodeBinaryString(s) 1843 return returnHexNumExpr(string(bytes), true) 1844 case tree.P_char: 1845 expr := makePlan2StringConstExprWithType(astExpr.String()) 1846 return expr, nil 1847 case tree.P_nulltext: 1848 expr := MakePlan2NullTextConstExprWithType(astExpr.String()) 1849 return expr, nil 1850 default: 1851 return nil, moerr.NewInvalidInput(b.GetContext(), "unsupport value '%s'", astExpr.String()) 1852 } 1853 } 1854 1855 func (b *baseBinder) GetContext() context.Context { return b.sysCtx } 1856 1857 // --- util functions ---- 1858 1859 func appendCastBeforeExpr(ctx context.Context, expr *Expr, toType Type, isBin ...bool) (*Expr, error) { 1860 toType.NotNullable = expr.Typ.NotNullable 1861 argsType := []types.Type{ 1862 makeTypeByPlan2Expr(expr), 1863 makeTypeByPlan2Type(toType), 1864 } 1865 fGet, err := function.GetFunctionByName(ctx, "cast", argsType) 1866 if err != nil { 1867 return nil, err 1868 } 1869 // for 0xXXXX, if the value is over 1<<53-1, when covert it into float64,it will lost, so just change it into uint64 1870 typ := toType 1871 if len(isBin) == 2 && isBin[0] && isBin[1] { 1872 typ.Id = int32(types.T_uint64) 1873 } 1874 return &Expr{ 1875 Expr: &plan.Expr_F{ 1876 F: &plan.Function{ 1877 Func: getFunctionObjRef(fGet.GetEncodedOverloadID(), "cast"), 1878 Args: []*Expr{ 1879 expr, 1880 { 1881 Typ: typ, 1882 Expr: &plan.Expr_T{ 1883 T: &plan.TargetType{}, 1884 }, 1885 }, 1886 }, 1887 }, 1888 }, 1889 Typ: typ, 1890 }, nil 1891 } 1892 1893 func resetDateFunctionArgs(ctx context.Context, dateExpr *Expr, intervalExpr *Expr) ([]*Expr, error) { 1894 firstExpr := intervalExpr.GetList().List[0] 1895 secondExpr := intervalExpr.GetList().List[1] 1896 1897 intervalTypeStr := secondExpr.GetLit().GetSval() 1898 intervalType, err := types.IntervalTypeOf(intervalTypeStr) 1899 if err != nil { 1900 return nil, err 1901 } 1902 1903 intervalTypeInFunction := &plan.Type{ 1904 Id: int32(types.T_int64), 1905 } 1906 1907 if firstExpr.Typ.Id == int32(types.T_varchar) || firstExpr.Typ.Id == int32(types.T_char) { 1908 s := firstExpr.GetLit().GetSval() 1909 returnNum, returnType, err := types.NormalizeInterval(s, intervalType) 1910 1911 if err != nil { 1912 return nil, err 1913 } 1914 // "date '2020-10-10' - interval 1 Hour" will return datetime 1915 // so we rewrite "date '2020-10-10' - interval 1 Hour" to "date_add(datetime, 1, hour)" 1916 if dateExpr.Typ.Id == int32(types.T_date) { 1917 switch returnType { 1918 case types.Day, types.Week, types.Month, types.Quarter, types.Year: 1919 default: 1920 dateExpr, err = appendCastBeforeExpr(ctx, dateExpr, plan.Type{ 1921 Id: int32(types.T_datetime), 1922 }) 1923 1924 if err != nil { 1925 return nil, err 1926 } 1927 } 1928 } 1929 return []*Expr{ 1930 dateExpr, 1931 makePlan2Int64ConstExprWithType(returnNum), 1932 makePlan2Int64ConstExprWithType(int64(returnType)), 1933 }, nil 1934 } 1935 1936 // "date '2020-10-10' - interval 1 Hour" will return datetime 1937 // so we rewrite "date '2020-10-10' - interval 1 Hour" to "date_add(datetime, 1, hour)" 1938 if dateExpr.Typ.Id == int32(types.T_date) { 1939 switch intervalType { 1940 case types.Day, types.Week, types.Month, types.Quarter, types.Year: 1941 default: 1942 dateExpr, err = appendCastBeforeExpr(ctx, dateExpr, plan.Type{ 1943 Id: int32(types.T_datetime), 1944 }) 1945 1946 if err != nil { 1947 return nil, err 1948 } 1949 } 1950 } 1951 1952 numberExpr, err := appendCastBeforeExpr(ctx, firstExpr, *intervalTypeInFunction) 1953 if err != nil { 1954 return nil, err 1955 } 1956 1957 return []*Expr{ 1958 dateExpr, 1959 numberExpr, 1960 makePlan2Int64ConstExprWithType(int64(intervalType)), 1961 }, nil 1962 } 1963 1964 func resetDateFunction(ctx context.Context, dateExpr *Expr, intervalExpr *Expr) ([]*Expr, error) { 1965 switch intervalExpr.Expr.(type) { 1966 case *plan.Expr_List: 1967 return resetDateFunctionArgs(ctx, dateExpr, intervalExpr) 1968 } 1969 list := &plan.ExprList{ 1970 List: make([]*Expr, 2), 1971 } 1972 list.List[0] = intervalExpr 1973 strType := &plan.Type{ 1974 Id: int32(types.T_char), 1975 } 1976 strExpr := &Expr{ 1977 Expr: &plan.Expr_Lit{ 1978 Lit: &Const{ 1979 Value: &plan.Literal_Sval{ 1980 Sval: "day", 1981 }, 1982 }, 1983 }, 1984 Typ: *strType, 1985 } 1986 list.List[1] = strExpr 1987 expr := &plan.Expr_List{ 1988 List: list, 1989 } 1990 listExpr := &Expr{ 1991 Expr: expr, 1992 } 1993 return resetDateFunctionArgs(ctx, dateExpr, listExpr) 1994 }