github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/parsers/tree/expr.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 tree 16 17 import ( 18 "fmt" 19 ) 20 21 // AST for the expression 22 type Expr interface { 23 fmt.Stringer 24 NodeFormatter 25 } 26 27 type exprImpl struct { 28 Expr 29 } 30 31 func (node *exprImpl) String() string { 32 return "" 33 } 34 35 // Binary Operator 36 type BinaryOp int 37 38 const ( 39 PLUS BinaryOp = iota 40 MINUS 41 MULTI 42 DIV // / 43 INTEGER_DIV // 44 BIT_OR // | 45 BIT_AND // & 46 BIT_XOR // ^ 47 LEFT_SHIFT // << 48 RIGHT_SHIFT // >> 49 MOD // % 50 ) 51 52 func (op BinaryOp) ToString() string { 53 switch op { 54 case PLUS: 55 return "+" 56 case MINUS: 57 return "-" 58 case MULTI: 59 return "*" 60 case DIV: 61 return "/" 62 case INTEGER_DIV: 63 return "div" 64 case BIT_OR: 65 return "|" 66 case BIT_AND: 67 return "&" 68 case BIT_XOR: 69 return "^" 70 case LEFT_SHIFT: 71 return "<<" 72 case RIGHT_SHIFT: 73 return ">>" 74 case MOD: 75 return "%" 76 default: 77 return "Unknown BinaryExprOperator" 78 } 79 } 80 81 // binary expression 82 type BinaryExpr struct { 83 exprImpl 84 85 //operator 86 Op BinaryOp 87 88 //left expression 89 Left Expr 90 91 //right expression 92 Right Expr 93 } 94 95 func (node *BinaryExpr) Format(ctx *FmtCtx) { 96 ctx.PrintExpr(node, node.Left, true) 97 ctx.WriteByte(' ') 98 ctx.WriteString(node.Op.ToString()) 99 ctx.WriteByte(' ') 100 ctx.PrintExpr(node, node.Right, false) 101 } 102 103 func NewBinaryExpr(op BinaryOp, left Expr, right Expr) *BinaryExpr { 104 return &BinaryExpr{ 105 Op: op, 106 Left: left, 107 Right: right, 108 } 109 } 110 111 // unary expression 112 type UnaryOp int 113 114 const ( 115 //- 116 UNARY_MINUS UnaryOp = iota 117 //+ 118 UNARY_PLUS 119 //~ 120 UNARY_TILDE 121 //! 122 UNARY_MARK 123 ) 124 125 func (op UnaryOp) ToString() string { 126 switch op { 127 case UNARY_MINUS: 128 return "-" 129 case UNARY_PLUS: 130 return "+" 131 case UNARY_TILDE: 132 return "~" 133 case UNARY_MARK: 134 return "!" 135 default: 136 return "Unknown UnaryExprOperator" 137 } 138 } 139 140 // unary expression 141 type UnaryExpr struct { 142 exprImpl 143 144 //operator 145 Op UnaryOp 146 147 //expression 148 Expr Expr 149 } 150 151 func (e *UnaryExpr) Format(ctx *FmtCtx) { 152 if _, unary := e.Expr.(*UnaryExpr); unary { 153 ctx.WriteString(e.Op.ToString()) 154 ctx.WriteByte(' ') 155 ctx.PrintExpr(e, e.Expr, true) 156 return 157 } 158 ctx.WriteString(e.Op.ToString()) 159 ctx.PrintExpr(e, e.Expr, true) 160 } 161 162 func (e *UnaryExpr) String() string { 163 return unaryOpName[e.Op] + e.Expr.String() 164 } 165 166 func NewUnaryExpr(op UnaryOp, expr Expr) *UnaryExpr { 167 return &UnaryExpr{ 168 Op: op, 169 Expr: expr, 170 } 171 } 172 173 var unaryOpName = []string{ 174 "-", 175 "+", 176 "~", 177 "!", 178 } 179 180 // comparion operation 181 type ComparisonOp int 182 183 const ( 184 EQUAL ComparisonOp = iota // = 185 LESS_THAN // < 186 LESS_THAN_EQUAL // <= 187 GREAT_THAN // > 188 GREAT_THAN_EQUAL // >= 189 NOT_EQUAL // <>, != 190 IN // IN 191 NOT_IN // NOT IN 192 LIKE // LIKE 193 NOT_LIKE // NOT LIKE 194 ILIKE 195 NOT_ILIKE 196 REG_MATCH // REG_MATCH 197 NOT_REG_MATCH // NOT REG_MATCH 198 IS_DISTINCT_FROM 199 IS_NOT_DISTINCT_FROM 200 NULL_SAFE_EQUAL // <=> 201 //reference: https://dev.mysql.com/doc/refman/8.0/en/all-subqueries.html 202 //subquery with ANY,SOME,ALL 203 //operand comparison_operator [ANY | SOME | ALL] (subquery) 204 ANY 205 SOME 206 ALL 207 ) 208 209 func (op ComparisonOp) ToString() string { 210 switch op { 211 case EQUAL: 212 return "=" 213 case LESS_THAN: 214 return "<" 215 case LESS_THAN_EQUAL: 216 return "<=" 217 case GREAT_THAN: 218 return ">" 219 case GREAT_THAN_EQUAL: 220 return ">=" 221 case NOT_EQUAL: 222 return "!=" 223 case IN: 224 return "in" 225 case NOT_IN: 226 return "not in" 227 case LIKE: 228 return "like" 229 case NOT_LIKE: 230 return "not like" 231 case REG_MATCH: 232 return "reg_match" 233 case NOT_REG_MATCH: 234 return "not reg_match" 235 case IS_DISTINCT_FROM: 236 return "is distinct from" 237 case IS_NOT_DISTINCT_FROM: 238 return "is not distinct from" 239 case NULL_SAFE_EQUAL: 240 return "<=>" 241 case ANY: 242 return "any" 243 case SOME: 244 return "some" 245 case ALL: 246 return "all" 247 case ILIKE: 248 return "ilike" 249 case NOT_ILIKE: 250 return "not ilike" 251 default: 252 return "Unknown ComparisonExprOperator" 253 } 254 } 255 256 type ComparisonExpr struct { 257 exprImpl 258 Op ComparisonOp 259 260 //ANY SOME ALL with subquery 261 SubOp ComparisonOp 262 Left Expr 263 Right Expr 264 Escape Expr 265 } 266 267 func (node *ComparisonExpr) Format(ctx *FmtCtx) { 268 if node.Left != nil { 269 ctx.PrintExpr(node, node.Left, true) 270 ctx.WriteByte(' ') 271 } 272 ctx.WriteString(node.Op.ToString()) 273 ctx.WriteByte(' ') 274 275 if node.SubOp != ComparisonOp(0) { 276 ctx.WriteString(node.SubOp.ToString()) 277 ctx.WriteByte(' ') 278 } 279 280 ctx.PrintExpr(node, node.Right, false) 281 if node.Escape != nil { 282 ctx.WriteString(" escape ") 283 ctx.PrintExpr(node, node.Escape, true) 284 } 285 } 286 287 func NewComparisonExpr(op ComparisonOp, l, r Expr) *ComparisonExpr { 288 return &ComparisonExpr{ 289 Op: op, 290 SubOp: ComparisonOp(0), 291 Left: l, 292 Right: r, 293 } 294 } 295 296 func NewSubqueryComparisonExpr(op ComparisonOp, subOp ComparisonOp, l, r Expr) *ComparisonExpr { 297 return &ComparisonExpr{ 298 Op: op, 299 SubOp: subOp, 300 Left: l, 301 Right: r, 302 } 303 } 304 305 func NewComparisonExprWithSubop(op, subop ComparisonOp, l, r Expr) *ComparisonExpr { 306 return &ComparisonExpr{ 307 Op: op, 308 SubOp: subop, 309 Left: l, 310 Right: r, 311 } 312 } 313 314 func NewComparisonExprWithEscape(op ComparisonOp, l, r, e Expr) *ComparisonExpr { 315 return &ComparisonExpr{ 316 Op: op, 317 Left: l, 318 Right: r, 319 Escape: e, 320 } 321 } 322 323 // and expression 324 type AndExpr struct { 325 exprImpl 326 Left, Right Expr 327 } 328 329 func (node *AndExpr) Format(ctx *FmtCtx) { 330 ctx.PrintExpr(node, node.Left, true) 331 ctx.WriteString(" and ") 332 ctx.PrintExpr(node, node.Right, false) 333 } 334 335 func NewAndExpr(l, r Expr) *AndExpr { 336 return &AndExpr{ 337 Left: l, 338 Right: r, 339 } 340 } 341 342 // xor expression 343 type XorExpr struct { 344 exprImpl 345 Left, Right Expr 346 } 347 348 func (node *XorExpr) Format(ctx *FmtCtx) { 349 ctx.PrintExpr(node, node.Left, true) 350 ctx.WriteString(" xor ") 351 ctx.PrintExpr(node, node.Right, false) 352 } 353 354 func NewXorExpr(l, r Expr) *XorExpr { 355 return &XorExpr{ 356 Left: l, 357 Right: r, 358 } 359 } 360 361 // or expression 362 type OrExpr struct { 363 exprImpl 364 Left, Right Expr 365 } 366 367 func (node *OrExpr) Format(ctx *FmtCtx) { 368 ctx.PrintExpr(node, node.Left, true) 369 ctx.WriteString(" or ") 370 ctx.PrintExpr(node, node.Right, false) 371 } 372 373 func NewOrExpr(l, r Expr) *OrExpr { 374 return &OrExpr{ 375 Left: l, 376 Right: r, 377 } 378 } 379 380 // not expression 381 type NotExpr struct { 382 exprImpl 383 Expr Expr 384 } 385 386 func (node *NotExpr) Format(ctx *FmtCtx) { 387 ctx.WriteString("not ") 388 ctx.PrintExpr(node, node.Expr, true) 389 } 390 391 func NewNotExpr(e Expr) *NotExpr { 392 return &NotExpr{ 393 Expr: e, 394 } 395 } 396 397 // is null expression 398 type IsNullExpr struct { 399 exprImpl 400 Expr Expr 401 } 402 403 func (node *IsNullExpr) Format(ctx *FmtCtx) { 404 ctx.PrintExpr(node, node.Expr, true) 405 ctx.WriteString(" is null") 406 } 407 408 func NewIsNullExpr(e Expr) *IsNullExpr { 409 return &IsNullExpr{ 410 Expr: e, 411 } 412 } 413 414 // is not null expression 415 type IsNotNullExpr struct { 416 exprImpl 417 Expr Expr 418 } 419 420 func (node *IsNotNullExpr) Format(ctx *FmtCtx) { 421 ctx.PrintExpr(node, node.Expr, true) 422 ctx.WriteString(" is not null") 423 } 424 425 func NewIsNotNullExpr(e Expr) *IsNotNullExpr { 426 return &IsNotNullExpr{ 427 Expr: e, 428 } 429 } 430 431 // is unknown expression 432 type IsUnknownExpr struct { 433 exprImpl 434 Expr Expr 435 } 436 437 func (node *IsUnknownExpr) Format(ctx *FmtCtx) { 438 ctx.PrintExpr(node, node.Expr, true) 439 ctx.WriteString(" is unknown") 440 } 441 442 func NewIsUnknownExpr(e Expr) *IsUnknownExpr { 443 return &IsUnknownExpr{ 444 Expr: e, 445 } 446 } 447 448 // is not unknown expression 449 type IsNotUnknownExpr struct { 450 exprImpl 451 Expr Expr 452 } 453 454 func (node *IsNotUnknownExpr) Format(ctx *FmtCtx) { 455 ctx.PrintExpr(node, node.Expr, true) 456 ctx.WriteString(" is not unknown") 457 } 458 459 func NewIsNotUnknownExpr(e Expr) *IsNotUnknownExpr { 460 return &IsNotUnknownExpr{ 461 Expr: e, 462 } 463 } 464 465 // is true expression 466 type IsTrueExpr struct { 467 exprImpl 468 Expr Expr 469 } 470 471 func (node *IsTrueExpr) Format(ctx *FmtCtx) { 472 ctx.PrintExpr(node, node.Expr, true) 473 ctx.WriteString(" is true") 474 } 475 476 func NewIsTrueExpr(e Expr) *IsTrueExpr { 477 return &IsTrueExpr{ 478 Expr: e, 479 } 480 } 481 482 // is not true expression 483 type IsNotTrueExpr struct { 484 exprImpl 485 Expr Expr 486 } 487 488 func (node *IsNotTrueExpr) Format(ctx *FmtCtx) { 489 ctx.PrintExpr(node, node.Expr, true) 490 ctx.WriteString(" is not true") 491 } 492 493 func NewIsNotTrueExpr(e Expr) *IsNotTrueExpr { 494 return &IsNotTrueExpr{ 495 Expr: e, 496 } 497 } 498 499 // is false expression 500 type IsFalseExpr struct { 501 exprImpl 502 Expr Expr 503 } 504 505 func (node *IsFalseExpr) Format(ctx *FmtCtx) { 506 ctx.PrintExpr(node, node.Expr, true) 507 ctx.WriteString(" is false") 508 } 509 510 func NewIsFalseExpr(e Expr) *IsFalseExpr { 511 return &IsFalseExpr{ 512 Expr: e, 513 } 514 } 515 516 // is not false expression 517 type IsNotFalseExpr struct { 518 exprImpl 519 Expr Expr 520 } 521 522 func (node *IsNotFalseExpr) Format(ctx *FmtCtx) { 523 ctx.PrintExpr(node, node.Expr, true) 524 ctx.WriteString(" is not false") 525 } 526 527 func NewIsNotFalseExpr(e Expr) *IsNotFalseExpr { 528 return &IsNotFalseExpr{ 529 Expr: e, 530 } 531 } 532 533 // subquery interface 534 type SubqueryExpr interface { 535 Expr 536 } 537 538 // subquery 539 type Subquery struct { 540 SubqueryExpr 541 542 Select SelectStatement 543 Exists bool 544 } 545 546 func (node *Subquery) Format(ctx *FmtCtx) { 547 if node.Exists { 548 ctx.WriteString("exists ") 549 } 550 node.Select.Format(ctx) 551 } 552 553 func NewSubquery(s SelectStatement, e bool) *Subquery { 554 return &Subquery{ 555 Select: s, 556 Exists: e, 557 } 558 } 559 560 // a list of expression. 561 type Exprs []Expr 562 563 func (node Exprs) Format(ctx *FmtCtx) { 564 prefix := "" 565 for _, n := range node { 566 ctx.WriteString(prefix) 567 n.Format(ctx) 568 prefix = ", " 569 } 570 } 571 572 // ast fir the list of expression 573 type ExprList struct { 574 exprImpl 575 Exprs Exprs 576 } 577 578 // the parenthesized expression. 579 type ParenExpr struct { 580 exprImpl 581 Expr Expr 582 } 583 584 func (node *ParenExpr) Format(ctx *FmtCtx) { 585 ctx.WriteByte('(') 586 node.Expr.Format(ctx) 587 ctx.WriteByte(')') 588 } 589 590 func NewParenExpr(e Expr) *ParenExpr { 591 return &ParenExpr{ 592 Expr: e, 593 } 594 } 595 596 type FuncType int 597 598 func (node *FuncType) ToString() string { 599 switch *node { 600 case FUNC_TYPE_DISTINCT: 601 return "distinct" 602 case FUNC_TYPE_ALL: 603 return "all" 604 case FUNC_TYPE_TABLE: 605 return "table function" 606 default: 607 return "Unknown FuncType" 608 } 609 } 610 611 const ( 612 FUNC_TYPE_DEFAULT FuncType = iota 613 FUNC_TYPE_DISTINCT 614 FUNC_TYPE_ALL 615 FUNC_TYPE_TABLE 616 ) 617 618 // AggType specifies the type of aggregation. 619 type AggType int 620 621 const ( 622 _ AggType = iota 623 AGG_TYPE_GENERAL 624 ) 625 626 // the common interface to UnresolvedName and QualifiedFunctionName. 627 type FunctionReference interface { 628 fmt.Stringer 629 NodeFormatter 630 } 631 632 var _ FunctionReference = &UnresolvedName{} 633 634 // function reference 635 type ResolvableFunctionReference struct { 636 FunctionReference 637 } 638 639 func (node *ResolvableFunctionReference) Format(ctx *FmtCtx) { 640 node.FunctionReference.(*UnresolvedName).Format(ctx) 641 } 642 643 func FuncName2ResolvableFunctionReference(funcName *UnresolvedName) ResolvableFunctionReference { 644 return ResolvableFunctionReference{FunctionReference: funcName} 645 } 646 647 // function call expression 648 type FuncExpr struct { 649 exprImpl 650 Func ResolvableFunctionReference 651 Type FuncType 652 Exprs Exprs 653 654 //specify the type of aggregation. 655 AggType AggType 656 657 WindowSpec *WindowSpec 658 } 659 660 func (node *FuncExpr) Format(ctx *FmtCtx) { 661 node.Func.Format(ctx) 662 663 ctx.WriteString("(") 664 if node.Type != FUNC_TYPE_DEFAULT && node.Type != FUNC_TYPE_TABLE { 665 ctx.WriteString(node.Type.ToString()) 666 ctx.WriteByte(' ') 667 } 668 if node.Func.FunctionReference.(*UnresolvedName).Parts[0] == "trim" { 669 trimExprsFormat(ctx, node.Exprs) 670 } else { 671 node.Exprs.Format(ctx) 672 } 673 ctx.WriteByte(')') 674 675 if node.WindowSpec != nil { 676 ctx.WriteString(" over (") 677 678 if len(node.WindowSpec.PartitionBy) > 0 { 679 ctx.WriteString("partition by ") 680 node.WindowSpec.PartitionBy.Format(ctx) 681 } 682 683 if len(node.WindowSpec.OrderBy) > 0 { 684 if len(node.WindowSpec.PartitionBy) > 0 { 685 ctx.WriteByte(' ') 686 } 687 node.WindowSpec.OrderBy.Format(ctx) 688 } 689 690 if node.WindowSpec.WindowFrame != nil { 691 frame := node.WindowSpec.WindowFrame 692 693 switch frame.Unit { 694 case WIN_FRAME_UNIT_ROWS: 695 ctx.WriteString(" rows ") 696 697 case WIN_FRAME_UNIT_RANGE: 698 ctx.WriteString(" range ") 699 700 case WIN_FRAME_UNIT_GROUPS: 701 ctx.WriteString(" groups ") 702 } 703 704 if frame.EndBound == nil { 705 frame.StartBound.Format(ctx) 706 } else { 707 ctx.WriteString("between ") 708 frame.StartBound.Format(ctx) 709 ctx.WriteString(" and ") 710 frame.EndBound.Format(ctx) 711 } 712 } 713 714 ctx.WriteByte(')') 715 } 716 } 717 718 func trimExprsFormat(ctx *FmtCtx, exprs Exprs) { 719 tp := exprs[0].(*NumVal).String() 720 switch tp { 721 case "0": 722 exprs[3].Format(ctx) 723 case "1": 724 exprs[2].Format(ctx) 725 ctx.WriteString(" from ") 726 exprs[3].Format(ctx) 727 case "2": 728 exprs[1].Format(ctx) 729 ctx.WriteString(" from ") 730 exprs[3].Format(ctx) 731 case "3": 732 exprs[1].Format(ctx) 733 ctx.WriteString(" ") 734 exprs[2].Format(ctx) 735 ctx.WriteString(" from ") 736 exprs[3].Format(ctx) 737 default: 738 panic("unknown trim type") 739 } 740 } 741 742 func NewFuncExpr(ft FuncType, name *UnresolvedName, e Exprs, order OrderBy) *FuncExpr { 743 return &FuncExpr{ 744 Func: FuncName2ResolvableFunctionReference(name), 745 Type: ft, 746 Exprs: e, 747 AggType: AGG_TYPE_GENERAL, 748 } 749 } 750 751 type WindowSpec struct { 752 PartitionBy Exprs 753 OrderBy OrderBy 754 WindowFrame *WindowFrame 755 } 756 757 type WindowFrame struct { 758 Unit WindowFrameUnits 759 StartBound WindowFrameBound 760 EndBound WindowFrameBound 761 } 762 763 type WindowFrameUnits int 764 765 const ( 766 WIN_FRAME_UNIT_ROWS WindowFrameUnits = iota 767 WIN_FRAME_UNIT_RANGE 768 WIN_FRAME_UNIT_GROUPS //MySQL don't support 769 ) 770 771 type WindowFrameBound interface { 772 Format(ctx *FmtCtx) 773 } 774 775 type WindowFrameBoundCurrentRow struct { 776 WindowFrameBound 777 } 778 779 func (currentRow *WindowFrameBoundCurrentRow) Format(ctx *FmtCtx) { 780 ctx.WriteString("current row") 781 } 782 783 type WindowFrameBoundPreceding struct { 784 WindowFrameBound 785 Expr Expr 786 } 787 788 func (preceding *WindowFrameBoundPreceding) Format(ctx *FmtCtx) { 789 if preceding.Expr == nil { 790 ctx.WriteString("unbounded preceding") 791 } else { 792 preceding.Expr.Format(ctx) 793 ctx.WriteString(" preceding") 794 } 795 } 796 797 type WindowFrameBoundFollowing struct { 798 WindowFrameBound 799 Expr Expr 800 } 801 802 func (following *WindowFrameBoundFollowing) Format(ctx *FmtCtx) { 803 if following.Expr == nil { 804 ctx.WriteString("unbounded following") 805 } else { 806 following.Expr.Format(ctx) 807 ctx.WriteString(" following") 808 } 809 } 810 811 // type reference 812 type ResolvableTypeReference interface { 813 } 814 815 var _ ResolvableTypeReference = &UnresolvedObjectName{} 816 var _ ResolvableTypeReference = &T{} 817 818 // the Cast expression 819 type CastExpr struct { 820 exprImpl 821 Expr Expr 822 Type ResolvableTypeReference 823 } 824 825 func (node *CastExpr) Format(ctx *FmtCtx) { 826 ctx.WriteString("cast(") 827 node.Expr.Format(ctx) 828 ctx.WriteString(" as ") 829 node.Type.(*T).InternalType.Format(ctx) 830 ctx.WriteByte(')') 831 } 832 833 func NewCastExpr(e Expr, t ResolvableTypeReference) *CastExpr { 834 return &CastExpr{ 835 Expr: e, 836 Type: t, 837 } 838 } 839 840 // the parenthesized list of expressions. 841 type Tuple struct { 842 exprImpl 843 Exprs Exprs 844 } 845 846 func (node *Tuple) Format(ctx *FmtCtx) { 847 if node.Exprs != nil { 848 ctx.WriteByte('(') 849 node.Exprs.Format(ctx) 850 ctx.WriteByte(')') 851 } 852 } 853 854 func NewTuple(e Exprs) *Tuple { 855 return &Tuple{Exprs: e} 856 } 857 858 // the BETWEEN or a NOT BETWEEN expression 859 type RangeCond struct { 860 exprImpl 861 Not bool 862 Left Expr 863 From, To Expr 864 } 865 866 func (node *RangeCond) Format(ctx *FmtCtx) { 867 ctx.PrintExpr(node, node.Left, true) 868 if node.Not { 869 ctx.WriteString(" not") 870 } 871 ctx.WriteString(" between ") 872 ctx.PrintExpr(node, node.From, true) 873 ctx.WriteString(" and ") 874 ctx.PrintExpr(node, node.To, false) 875 } 876 877 func NewRangeCond(n bool, l, f, t Expr) *RangeCond { 878 return &RangeCond{ 879 Not: n, 880 Left: l, 881 From: f, 882 To: t, 883 } 884 } 885 886 // Case-When expression. 887 type CaseExpr struct { 888 exprImpl 889 Expr Expr 890 Whens []*When 891 Else Expr 892 } 893 894 func (node *CaseExpr) Format(ctx *FmtCtx) { 895 ctx.WriteString("case") 896 if node.Expr != nil { 897 ctx.WriteByte(' ') 898 node.Expr.Format(ctx) 899 } 900 ctx.WriteByte(' ') 901 prefix := "" 902 for _, w := range node.Whens { 903 ctx.WriteString(prefix) 904 w.Format(ctx) 905 prefix = " " 906 } 907 if node.Else != nil { 908 ctx.WriteString(" else ") 909 node.Else.Format(ctx) 910 } 911 ctx.WriteString(" end") 912 } 913 914 func NewCaseExpr(e Expr, w []*When, el Expr) *CaseExpr { 915 return &CaseExpr{ 916 Expr: e, 917 Whens: w, 918 Else: el, 919 } 920 } 921 922 // When sub-expression. 923 type When struct { 924 Cond Expr 925 Val Expr 926 } 927 928 func (node *When) Format(ctx *FmtCtx) { 929 ctx.WriteString("when ") 930 node.Cond.Format(ctx) 931 ctx.WriteString(" then ") 932 node.Val.Format(ctx) 933 } 934 935 func NewWhen(c, v Expr) *When { 936 return &When{ 937 Cond: c, 938 Val: v, 939 } 940 } 941 942 // IntervalType is the type for time and timestamp units. 943 type IntervalType int 944 945 func (node *IntervalType) ToString() string { 946 switch *node { 947 case INTERVAL_TYPE_SECOND: 948 return "second" 949 default: 950 return "Unknown IntervalType" 951 } 952 } 953 954 const ( 955 //an invalid time or timestamp unit 956 INTERVAL_TYPE_INVALID IntervalType = iota 957 //the time or timestamp unit MICROSECOND. 958 INTERVAL_TYPE_MICROSECOND 959 //the time or timestamp unit SECOND. 960 INTERVAL_TYPE_SECOND 961 //the time or timestamp unit MINUTE. 962 INTERVAL_TYPE_MINUTE 963 //the time or timestamp unit HOUR. 964 INTERVAL_TYPE_HOUR 965 //the time or timestamp unit DAY. 966 INTERVAL_TYPE_DAY 967 //the time or timestamp unit WEEK. 968 INTERVAL_TYPE_WEEK 969 //the time or timestamp unit MONTH. 970 INTERVAL_TYPE_MONTH 971 //the time or timestamp unit QUARTER. 972 INTERVAL_TYPE_QUARTER 973 //the time or timestamp unit YEAR. 974 INTERVAL_TYPE_YEAR 975 //the time unit SECOND_MICROSECOND. 976 INTERVAL_TYPE_SECOND_MICROSECOND 977 //the time unit MINUTE_MICROSECOND. 978 INTERVAL_TYPE_MINUTE_MICROSECOND 979 //the time unit MINUTE_SECOND. 980 INTERVAL_TYPE_MINUTE_SECOND 981 //the time unit HOUR_MICROSECOND. 982 INTERVAL_TYPE_HOUR_MICROSECOND 983 //the time unit HOUR_SECOND. 984 INTERVAL_TYPE_HOUR_SECOND 985 //the time unit HOUR_MINUTE. 986 INTERVAL_TYPE_HOUR_MINUTE 987 //the time unit DAY_MICROSECOND. 988 INTERVAL_TYPE_DAY_MICROSECOND 989 //the time unit DAY_SECOND. 990 INTERVAL_TYPE_DAY_SECOND 991 //the time unit DAY_MINUTE. 992 INTERVAL_TYPE_DAYMINUTE 993 //the time unit DAY_HOUR. 994 INTERVAL_TYPE_DAYHOUR 995 //the time unit YEAR_MONTH. 996 INTERVAL_TYPE_YEARMONTH 997 ) 998 999 // INTERVAL / time unit 1000 type IntervalExpr struct { 1001 exprImpl 1002 Expr Expr 1003 Type IntervalType 1004 } 1005 1006 func (node *IntervalExpr) Format(ctx *FmtCtx) { 1007 ctx.WriteString("interval") 1008 if node.Expr != nil { 1009 ctx.WriteByte(' ') 1010 node.Expr.Format(ctx) 1011 } 1012 if node.Type != INTERVAL_TYPE_INVALID { 1013 ctx.WriteByte(' ') 1014 ctx.WriteString(node.Type.ToString()) 1015 } 1016 } 1017 1018 func NewIntervalExpr(t IntervalType) *IntervalExpr { 1019 return &IntervalExpr{ 1020 Type: t, 1021 } 1022 } 1023 1024 // the DEFAULT expression. 1025 type DefaultVal struct { 1026 exprImpl 1027 Expr Expr 1028 } 1029 1030 func (node *DefaultVal) Format(ctx *FmtCtx) { 1031 ctx.WriteString("default") 1032 if node.Expr != nil { 1033 node.Expr.Format(ctx) 1034 } 1035 } 1036 1037 func NewDefaultVal(e Expr) *DefaultVal { 1038 return &DefaultVal{ 1039 Expr: e, 1040 } 1041 } 1042 1043 type UpdateVal struct { 1044 exprImpl 1045 } 1046 1047 func (node *UpdateVal) Format(ctx *FmtCtx) {} 1048 1049 type TypeExpr interface { 1050 Expr 1051 } 1052 1053 /* 1054 Variable Expression Used in Set Statement, 1055 Load Data statement, Show statement,etc. 1056 Variable types: 1057 User-Defined Variable 1058 Local-Variable: DECLARE statement 1059 System Variable: Global System Variable, Session System Variable 1060 */ 1061 1062 type VarExpr struct { 1063 exprImpl 1064 Name string 1065 System bool 1066 Global bool 1067 Expr Expr 1068 } 1069 1070 // incomplete 1071 func (node *VarExpr) Format(ctx *FmtCtx) { 1072 if node.Name != "" { 1073 ctx.WriteByte('@') 1074 if node.System { 1075 ctx.WriteByte('@') 1076 } 1077 ctx.WriteString(node.Name) 1078 } 1079 } 1080 1081 func NewVarExpr(n string, s bool, g bool, e Expr) *VarExpr { 1082 return &VarExpr{ 1083 Name: n, 1084 System: s, 1085 Global: g, 1086 Expr: e, 1087 } 1088 } 1089 1090 // select a from t1 where a > ? 1091 type ParamExpr struct { 1092 exprImpl 1093 Offset int 1094 } 1095 1096 func (node *ParamExpr) Format(ctx *FmtCtx) { 1097 ctx.WriteByte('?') 1098 } 1099 1100 func NewParamExpr(offset int) *ParamExpr { 1101 return &ParamExpr{ 1102 Offset: offset, 1103 } 1104 } 1105 1106 type MaxValue struct { 1107 exprImpl 1108 } 1109 1110 func (node *MaxValue) Format(ctx *FmtCtx) { 1111 ctx.WriteString("MAXVALUE") 1112 } 1113 1114 func NewMaxValue() *MaxValue { 1115 return &MaxValue{} 1116 }