github.com/matrixorigin/matrixone@v1.2.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 "math" 20 "strconv" 21 "strings" 22 23 "github.com/matrixorigin/matrixone/pkg/common/moerr" 24 ) 25 26 // AST for the expression 27 type Expr interface { 28 fmt.Stringer 29 NodeFormatter 30 NodeChecker 31 } 32 33 type exprImpl struct { 34 Expr 35 } 36 37 func (node *exprImpl) String() string { 38 return "" 39 } 40 41 // Binary Operator 42 type BinaryOp int 43 44 const ( 45 PLUS BinaryOp = iota 46 MINUS 47 MULTI 48 DIV // / 49 INTEGER_DIV // 50 BIT_OR // | 51 BIT_AND // & 52 BIT_XOR // ^ 53 LEFT_SHIFT // << 54 RIGHT_SHIFT // >> 55 MOD // % 56 ) 57 58 func (op BinaryOp) ToString() string { 59 switch op { 60 case PLUS: 61 return "+" 62 case MINUS: 63 return "-" 64 case MULTI: 65 return "*" 66 case DIV: 67 return "/" 68 case INTEGER_DIV: 69 return "div" 70 case BIT_OR: 71 return "|" 72 case BIT_AND: 73 return "&" 74 case BIT_XOR: 75 return "^" 76 case LEFT_SHIFT: 77 return "<<" 78 case RIGHT_SHIFT: 79 return ">>" 80 case MOD: 81 return "%" 82 default: 83 return "Unknown BinaryExprOperator" 84 } 85 } 86 87 // binary expression 88 type BinaryExpr struct { 89 exprImpl 90 91 //operator 92 Op BinaryOp 93 94 //left expression 95 Left Expr 96 97 //right expression 98 Right Expr 99 } 100 101 func (node *BinaryExpr) Format(ctx *FmtCtx) { 102 ctx.PrintExpr(node, node.Left, true) 103 ctx.WriteByte(' ') 104 ctx.WriteString(node.Op.ToString()) 105 ctx.WriteByte(' ') 106 ctx.PrintExpr(node, node.Right, false) 107 } 108 109 // Accept implements NodeChecker interface 110 func (node *BinaryExpr) Accept(v Visitor) (Expr, bool) { 111 newNode, skipChildren := v.Enter(node) 112 if skipChildren { 113 return v.Exit(newNode) 114 } 115 116 node = newNode.(*BinaryExpr) 117 tmpNode, ok := node.Left.Accept(v) 118 if !ok { 119 return node, false 120 } 121 node.Left = tmpNode 122 123 tmpNode, ok = node.Right.Accept(v) 124 if !ok { 125 return node, false 126 } 127 node.Right = tmpNode 128 return v.Exit(node) 129 } 130 131 func NewBinaryExpr(op BinaryOp, left Expr, right Expr) *BinaryExpr { 132 return &BinaryExpr{ 133 Op: op, 134 Left: left, 135 Right: right, 136 } 137 } 138 139 // unary expression 140 type UnaryOp int 141 142 const ( 143 //- 144 UNARY_MINUS UnaryOp = iota 145 //+ 146 UNARY_PLUS 147 //~ 148 UNARY_TILDE 149 //! 150 UNARY_MARK 151 ) 152 153 func (op UnaryOp) ToString() string { 154 switch op { 155 case UNARY_MINUS: 156 return "-" 157 case UNARY_PLUS: 158 return "+" 159 case UNARY_TILDE: 160 return "~" 161 case UNARY_MARK: 162 return "!" 163 default: 164 return "Unknown UnaryExprOperator" 165 } 166 } 167 168 // unary expression 169 type UnaryExpr struct { 170 exprImpl 171 172 //operator 173 Op UnaryOp 174 175 //expression 176 Expr Expr 177 } 178 179 func (e *UnaryExpr) Format(ctx *FmtCtx) { 180 if _, unary := e.Expr.(*UnaryExpr); unary { 181 ctx.WriteString(e.Op.ToString()) 182 ctx.WriteByte(' ') 183 ctx.PrintExpr(e, e.Expr, true) 184 return 185 } 186 ctx.WriteString(e.Op.ToString()) 187 ctx.PrintExpr(e, e.Expr, true) 188 } 189 190 func (e *UnaryExpr) Accept(v Visitor) (Expr, bool) { 191 newNode, skipChildren := v.Enter(e) 192 if skipChildren { 193 return v.Exit(newNode) 194 } 195 e = newNode.(*UnaryExpr) 196 node, ok := e.Expr.Accept(v) 197 if !ok { 198 return e, false 199 } 200 e.Expr = node 201 return v.Exit(e) 202 } 203 204 func (e *UnaryExpr) String() string { 205 return unaryOpName[e.Op] + e.Expr.String() 206 } 207 208 func NewUnaryExpr(op UnaryOp, expr Expr) *UnaryExpr { 209 return &UnaryExpr{ 210 Op: op, 211 Expr: expr, 212 } 213 } 214 215 var unaryOpName = []string{ 216 "-", 217 "+", 218 "~", 219 "!", 220 } 221 222 // comparion operation 223 type ComparisonOp int 224 225 const ( 226 EQUAL ComparisonOp = iota // = 227 LESS_THAN // < 228 LESS_THAN_EQUAL // <= 229 GREAT_THAN // > 230 GREAT_THAN_EQUAL // >= 231 NOT_EQUAL // <>, != 232 IN // IN 233 NOT_IN // NOT IN 234 LIKE // LIKE 235 NOT_LIKE // NOT LIKE 236 ILIKE 237 NOT_ILIKE 238 REG_MATCH // REG_MATCH 239 NOT_REG_MATCH // NOT REG_MATCH 240 IS_DISTINCT_FROM 241 IS_NOT_DISTINCT_FROM 242 NULL_SAFE_EQUAL // <=> 243 //reference: https://dev.mysql.com/doc/refman/8.0/en/all-subqueries.html 244 //subquery with ANY,SOME,ALL 245 //operand comparison_operator [ANY | SOME | ALL] (subquery) 246 ANY 247 SOME 248 ALL 249 ) 250 251 func (op ComparisonOp) ToString() string { 252 switch op { 253 case EQUAL: 254 return "=" 255 case LESS_THAN: 256 return "<" 257 case LESS_THAN_EQUAL: 258 return "<=" 259 case GREAT_THAN: 260 return ">" 261 case GREAT_THAN_EQUAL: 262 return ">=" 263 case NOT_EQUAL: 264 return "!=" 265 case IN: 266 return "in" 267 case NOT_IN: 268 return "not in" 269 case LIKE: 270 return "like" 271 case NOT_LIKE: 272 return "not like" 273 case REG_MATCH: 274 return "reg_match" 275 case NOT_REG_MATCH: 276 return "not reg_match" 277 case IS_DISTINCT_FROM: 278 return "is distinct from" 279 case IS_NOT_DISTINCT_FROM: 280 return "is not distinct from" 281 case NULL_SAFE_EQUAL: 282 return "<=>" 283 case ANY: 284 return "any" 285 case SOME: 286 return "some" 287 case ALL: 288 return "all" 289 case ILIKE: 290 return "ilike" 291 case NOT_ILIKE: 292 return "not ilike" 293 default: 294 return "Unknown ComparisonExprOperator" 295 } 296 } 297 298 type ComparisonExpr struct { 299 exprImpl 300 Op ComparisonOp 301 302 //ANY SOME ALL with subquery 303 SubOp ComparisonOp 304 Left Expr 305 Right Expr 306 Escape Expr 307 } 308 309 func (node *ComparisonExpr) Format(ctx *FmtCtx) { 310 if node.Left != nil { 311 ctx.PrintExpr(node, node.Left, true) 312 ctx.WriteByte(' ') 313 } 314 ctx.WriteString(node.Op.ToString()) 315 ctx.WriteByte(' ') 316 317 if node.SubOp != ComparisonOp(0) { 318 ctx.WriteString(node.SubOp.ToString()) 319 ctx.WriteByte(' ') 320 } 321 322 ctx.PrintExpr(node, node.Right, false) 323 if node.Escape != nil { 324 ctx.WriteString(" escape ") 325 ctx.PrintExpr(node, node.Escape, true) 326 } 327 } 328 329 // Accept implements NodeChecker interface 330 func (node *ComparisonExpr) Accept(v Visitor) (Expr, bool) { 331 newNode, skipChildren := v.Enter(node) 332 if skipChildren { 333 return v.Exit(newNode) 334 } 335 336 node = newNode.(*ComparisonExpr) 337 tmpNode, ok := node.Left.Accept(v) 338 if !ok { 339 return node, false 340 } 341 node.Left = tmpNode 342 343 tmpNode, ok = node.Right.Accept(v) 344 if !ok { 345 return node, false 346 } 347 node.Right = tmpNode 348 return v.Exit(node) 349 } 350 351 func NewComparisonExpr(op ComparisonOp, l, r Expr) *ComparisonExpr { 352 return &ComparisonExpr{ 353 Op: op, 354 SubOp: ComparisonOp(0), 355 Left: l, 356 Right: r, 357 } 358 } 359 360 func NewSubqueryComparisonExpr(op ComparisonOp, subOp ComparisonOp, l, r Expr) *ComparisonExpr { 361 return &ComparisonExpr{ 362 Op: op, 363 SubOp: subOp, 364 Left: l, 365 Right: r, 366 } 367 } 368 369 func NewComparisonExprWithSubop(op, subop ComparisonOp, l, r Expr) *ComparisonExpr { 370 return &ComparisonExpr{ 371 Op: op, 372 SubOp: subop, 373 Left: l, 374 Right: r, 375 } 376 } 377 378 func NewComparisonExprWithEscape(op ComparisonOp, l, r, e Expr) *ComparisonExpr { 379 return &ComparisonExpr{ 380 Op: op, 381 Left: l, 382 Right: r, 383 Escape: e, 384 } 385 } 386 387 // and expression 388 type AndExpr struct { 389 exprImpl 390 Left, Right Expr 391 } 392 393 func (node *AndExpr) Format(ctx *FmtCtx) { 394 ctx.PrintExpr(node, node.Left, true) 395 ctx.WriteString(" and ") 396 ctx.PrintExpr(node, node.Right, false) 397 } 398 399 func (node *AndExpr) Accept(v Visitor) (Expr, bool) { 400 newNode, skipChildren := v.Enter(node) 401 if skipChildren { 402 return v.Exit(newNode) 403 } 404 405 node = newNode.(*AndExpr) 406 tmpNode, ok := node.Left.Accept(v) 407 if !ok { 408 return node, false 409 } 410 node.Left = tmpNode 411 412 tmpNode, ok = node.Right.Accept(v) 413 if !ok { 414 return node, false 415 } 416 node.Right = tmpNode 417 418 return v.Exit(node) 419 } 420 421 func NewAndExpr(l, r Expr) *AndExpr { 422 return &AndExpr{ 423 Left: l, 424 Right: r, 425 } 426 } 427 428 // xor expression 429 type XorExpr struct { 430 exprImpl 431 Left, Right Expr 432 } 433 434 func (node *XorExpr) Format(ctx *FmtCtx) { 435 ctx.PrintExpr(node, node.Left, true) 436 ctx.WriteString(" xor ") 437 ctx.PrintExpr(node, node.Right, false) 438 } 439 440 func (node *XorExpr) Accept(v Visitor) (Expr, bool) { 441 newNode, skipChildren := v.Enter(node) 442 if skipChildren { 443 return v.Exit(newNode) 444 } 445 446 node = newNode.(*XorExpr) 447 tmpNode, ok := node.Left.Accept(v) 448 if !ok { 449 return node, false 450 } 451 node.Left = tmpNode 452 453 tmpNode, ok = node.Right.Accept(v) 454 if !ok { 455 return node, false 456 } 457 node.Right = tmpNode 458 459 return v.Exit(node) 460 } 461 462 func NewXorExpr(l, r Expr) *XorExpr { 463 return &XorExpr{ 464 Left: l, 465 Right: r, 466 } 467 } 468 469 // or expression 470 type OrExpr struct { 471 exprImpl 472 Left, Right Expr 473 } 474 475 func (node *OrExpr) Format(ctx *FmtCtx) { 476 ctx.PrintExpr(node, node.Left, true) 477 ctx.WriteString(" or ") 478 ctx.PrintExpr(node, node.Right, false) 479 } 480 481 func (node *OrExpr) Accept(v Visitor) (Expr, bool) { 482 newNode, skipChildren := v.Enter(node) 483 if skipChildren { 484 return v.Exit(newNode) 485 } 486 487 node = newNode.(*OrExpr) 488 tmpNode, ok := node.Left.Accept(v) 489 if !ok { 490 return node, false 491 } 492 node.Left = tmpNode 493 494 tmpNode, ok = node.Right.Accept(v) 495 if !ok { 496 return node, false 497 } 498 node.Right = tmpNode 499 500 return v.Exit(node) 501 } 502 503 func NewOrExpr(l, r Expr) *OrExpr { 504 return &OrExpr{ 505 Left: l, 506 Right: r, 507 } 508 } 509 510 // not expression 511 type NotExpr struct { 512 exprImpl 513 Expr Expr 514 } 515 516 func (node *NotExpr) Format(ctx *FmtCtx) { 517 ctx.WriteString("not ") 518 ctx.PrintExpr(node, node.Expr, true) 519 } 520 521 func (node *NotExpr) Accept(v Visitor) (Expr, bool) { 522 newNode, skipChildren := v.Enter(node) 523 if skipChildren { 524 return v.Exit(newNode) 525 } 526 527 node = newNode.(*NotExpr) 528 tmpNode, ok := node.Expr.Accept(v) 529 if !ok { 530 return node, false 531 } 532 node.Expr = tmpNode 533 return v.Exit(node) 534 } 535 536 func NewNotExpr(e Expr) *NotExpr { 537 return &NotExpr{ 538 Expr: e, 539 } 540 } 541 542 // is null expression 543 type IsNullExpr struct { 544 exprImpl 545 Expr Expr 546 } 547 548 func (node *IsNullExpr) Format(ctx *FmtCtx) { 549 ctx.PrintExpr(node, node.Expr, true) 550 ctx.WriteString(" is null") 551 } 552 553 // Accept implements NodeChecker interface 554 func (node *IsNullExpr) Accept(v Visitor) (Expr, bool) { 555 newNode, skipChildren := v.Enter(node) 556 if skipChildren { 557 return v.Exit(newNode) 558 } 559 node = newNode.(*IsNullExpr) 560 tmpNode, ok := node.Expr.Accept(v) 561 if !ok { 562 return node, false 563 } 564 node.Expr = tmpNode 565 return v.Exit(node) 566 } 567 568 func NewIsNullExpr(e Expr) *IsNullExpr { 569 return &IsNullExpr{ 570 Expr: e, 571 } 572 } 573 574 // is not null expression 575 type IsNotNullExpr struct { 576 exprImpl 577 Expr Expr 578 } 579 580 func (node *IsNotNullExpr) Format(ctx *FmtCtx) { 581 ctx.PrintExpr(node, node.Expr, true) 582 ctx.WriteString(" is not null") 583 } 584 585 // Accept implements NodeChecker interface 586 func (node *IsNotNullExpr) Accept(v Visitor) (Expr, bool) { 587 newNode, skipChildren := v.Enter(node) 588 if skipChildren { 589 return v.Exit(newNode) 590 } 591 node = newNode.(*IsNotNullExpr) 592 tmpNode, ok := node.Expr.Accept(v) 593 if !ok { 594 return node, false 595 } 596 node.Expr = tmpNode 597 return v.Exit(node) 598 } 599 600 func NewIsNotNullExpr(e Expr) *IsNotNullExpr { 601 return &IsNotNullExpr{ 602 Expr: e, 603 } 604 } 605 606 // is unknown expression 607 type IsUnknownExpr struct { 608 exprImpl 609 Expr Expr 610 } 611 612 func (node *IsUnknownExpr) Format(ctx *FmtCtx) { 613 ctx.PrintExpr(node, node.Expr, true) 614 ctx.WriteString(" is unknown") 615 } 616 617 // Accept implements NodeChecker interface 618 func (node *IsUnknownExpr) Accept(v Visitor) (Expr, bool) { 619 newNode, skipChildren := v.Enter(node) 620 if skipChildren { 621 return v.Exit(newNode) 622 } 623 node = newNode.(*IsUnknownExpr) 624 tmpNode, ok := node.Expr.Accept(v) 625 if !ok { 626 return node, false 627 } 628 node.Expr = tmpNode 629 return v.Exit(node) 630 } 631 632 func NewIsUnknownExpr(e Expr) *IsUnknownExpr { 633 return &IsUnknownExpr{ 634 Expr: e, 635 } 636 } 637 638 // is not unknown expression 639 type IsNotUnknownExpr struct { 640 exprImpl 641 Expr Expr 642 } 643 644 func (node *IsNotUnknownExpr) Format(ctx *FmtCtx) { 645 ctx.PrintExpr(node, node.Expr, true) 646 ctx.WriteString(" is not unknown") 647 } 648 649 // Accept implements NodeChecker interface 650 func (node *IsNotUnknownExpr) Accept(v Visitor) (Expr, bool) { 651 newNode, skipChildren := v.Enter(node) 652 if skipChildren { 653 return v.Exit(newNode) 654 } 655 node = newNode.(*IsNotUnknownExpr) 656 tmpNode, ok := node.Expr.Accept(v) 657 if !ok { 658 return node, false 659 } 660 node.Expr = tmpNode 661 return v.Exit(node) 662 } 663 664 func NewIsNotUnknownExpr(e Expr) *IsNotUnknownExpr { 665 return &IsNotUnknownExpr{ 666 Expr: e, 667 } 668 } 669 670 // is true expression 671 type IsTrueExpr struct { 672 exprImpl 673 Expr Expr 674 } 675 676 // Accept implements NodeChecker interface 677 func (node *IsTrueExpr) Accept(v Visitor) (Expr, bool) { 678 newNode, skipChildren := v.Enter(node) 679 if skipChildren { 680 return v.Exit(newNode) 681 } 682 node = newNode.(*IsTrueExpr) 683 tmpNode, ok := node.Expr.Accept(v) 684 if !ok { 685 return node, false 686 } 687 node.Expr = tmpNode 688 return v.Exit(node) 689 } 690 691 func (node *IsTrueExpr) Format(ctx *FmtCtx) { 692 ctx.PrintExpr(node, node.Expr, true) 693 ctx.WriteString(" is true") 694 } 695 696 func NewIsTrueExpr(e Expr) *IsTrueExpr { 697 return &IsTrueExpr{ 698 Expr: e, 699 } 700 } 701 702 // is not true expression 703 type IsNotTrueExpr struct { 704 exprImpl 705 Expr Expr 706 } 707 708 func (node *IsNotTrueExpr) Format(ctx *FmtCtx) { 709 ctx.PrintExpr(node, node.Expr, true) 710 ctx.WriteString(" is not true") 711 } 712 713 // Accept implements NodeChecker interface 714 func (node *IsNotTrueExpr) Accept(v Visitor) (Expr, bool) { 715 newNode, skipChildren := v.Enter(node) 716 if skipChildren { 717 return v.Exit(newNode) 718 } 719 node = newNode.(*IsNotTrueExpr) 720 tmpNode, ok := node.Expr.Accept(v) 721 if !ok { 722 return node, false 723 } 724 node.Expr = tmpNode 725 return v.Exit(node) 726 } 727 728 func NewIsNotTrueExpr(e Expr) *IsNotTrueExpr { 729 return &IsNotTrueExpr{ 730 Expr: e, 731 } 732 } 733 734 // is false expression 735 type IsFalseExpr struct { 736 exprImpl 737 Expr Expr 738 } 739 740 func (node *IsFalseExpr) Format(ctx *FmtCtx) { 741 ctx.PrintExpr(node, node.Expr, true) 742 ctx.WriteString(" is false") 743 } 744 745 // Accept implements NodeChecker interface 746 func (node *IsFalseExpr) Accept(v Visitor) (Expr, bool) { 747 newNode, skipChildren := v.Enter(node) 748 if skipChildren { 749 return v.Exit(newNode) 750 } 751 node = newNode.(*IsFalseExpr) 752 tmpNode, ok := node.Expr.Accept(v) 753 if !ok { 754 return node, false 755 } 756 node.Expr = tmpNode 757 return v.Exit(node) 758 } 759 760 func NewIsFalseExpr(e Expr) *IsFalseExpr { 761 return &IsFalseExpr{ 762 Expr: e, 763 } 764 } 765 766 // is not false expression 767 type IsNotFalseExpr struct { 768 exprImpl 769 Expr Expr 770 } 771 772 func (node *IsNotFalseExpr) Format(ctx *FmtCtx) { 773 ctx.PrintExpr(node, node.Expr, true) 774 ctx.WriteString(" is not false") 775 } 776 777 // Accept implements NodeChecker interface 778 func (node *IsNotFalseExpr) Accept(v Visitor) (Expr, bool) { 779 newNode, skipChildren := v.Enter(node) 780 if skipChildren { 781 return v.Exit(newNode) 782 } 783 node = newNode.(*IsNotFalseExpr) 784 tmpNode, ok := node.Expr.Accept(v) 785 if !ok { 786 return node, false 787 } 788 node.Expr = tmpNode 789 return v.Exit(node) 790 } 791 792 func NewIsNotFalseExpr(e Expr) *IsNotFalseExpr { 793 return &IsNotFalseExpr{ 794 Expr: e, 795 } 796 } 797 798 // subquery interface 799 type SubqueryExpr interface { 800 Expr 801 } 802 803 // subquery 804 type Subquery struct { 805 SubqueryExpr 806 807 Select SelectStatement 808 Exists bool 809 } 810 811 func (node *Subquery) Format(ctx *FmtCtx) { 812 if node.Exists { 813 ctx.WriteString("exists ") 814 } 815 node.Select.Format(ctx) 816 } 817 818 func (node *Subquery) Accept(v Visitor) (Expr, bool) { 819 panic("unimplement Subquery Accept") 820 } 821 822 func (node *Subquery) String() string { 823 return "subquery" 824 } 825 826 func NewSubquery(s SelectStatement, e bool) *Subquery { 827 return &Subquery{ 828 Select: s, 829 Exists: e, 830 } 831 } 832 833 // a list of expression. 834 type Exprs []Expr 835 836 func (node Exprs) Format(ctx *FmtCtx) { 837 prefix := "" 838 for _, n := range node { 839 ctx.WriteString(prefix) 840 n.Format(ctx) 841 prefix = ", " 842 } 843 } 844 845 // ast fir the list of expression 846 type ExprList struct { 847 exprImpl 848 Exprs Exprs 849 } 850 851 func (n *ExprList) Accept(v Visitor) (Expr, bool) { 852 panic("unimplement ExprList Accept") 853 } 854 855 // the parenthesized expression. 856 type ParenExpr struct { 857 exprImpl 858 Expr Expr 859 } 860 861 func (node *ParenExpr) Format(ctx *FmtCtx) { 862 ctx.WriteByte('(') 863 node.Expr.Format(ctx) 864 ctx.WriteByte(')') 865 } 866 867 func (node *ParenExpr) Accept(v Visitor) (Expr, bool) { 868 newNode, skipChildren := v.Enter(node) 869 if skipChildren { 870 return v.Exit(newNode) 871 } 872 node = newNode.(*ParenExpr) 873 if node.Expr != nil { 874 tmpNode, ok := node.Expr.Accept(v) 875 if !ok { 876 return node, false 877 } 878 node.Expr = tmpNode 879 } 880 return v.Exit(node) 881 } 882 883 func NewParentExpr(e Expr) *ParenExpr { 884 return &ParenExpr{ 885 Expr: e, 886 } 887 } 888 889 type FuncType int 890 891 func (node *FuncType) ToString() string { 892 switch *node { 893 case FUNC_TYPE_DISTINCT: 894 return "distinct" 895 case FUNC_TYPE_ALL: 896 return "all" 897 case FUNC_TYPE_TABLE: 898 return "table function" 899 default: 900 return "Unknown FuncType" 901 } 902 } 903 904 const ( 905 FUNC_TYPE_DEFAULT FuncType = iota 906 FUNC_TYPE_DISTINCT 907 FUNC_TYPE_ALL 908 FUNC_TYPE_TABLE 909 ) 910 911 // AggType specifies the type of aggregation. 912 type AggType int 913 914 const ( 915 _ AggType = iota 916 AGG_TYPE_GENERAL 917 ) 918 919 // the common interface to UnresolvedName and QualifiedFunctionName. 920 type FunctionReference interface { 921 fmt.Stringer 922 NodeFormatter 923 } 924 925 var _ FunctionReference = &UnresolvedName{} 926 927 // function reference 928 type ResolvableFunctionReference struct { 929 FunctionReference 930 } 931 932 func (node *ResolvableFunctionReference) Format(ctx *FmtCtx) { 933 node.FunctionReference.(*UnresolvedName).Format(ctx) 934 } 935 936 func FuncName2ResolvableFunctionReference(funcName *UnresolvedName) ResolvableFunctionReference { 937 return ResolvableFunctionReference{FunctionReference: funcName} 938 } 939 940 // function call expression 941 type FuncExpr struct { 942 exprImpl 943 Func ResolvableFunctionReference 944 FuncName *CStr 945 Type FuncType 946 Exprs Exprs 947 948 //specify the type of aggregation. 949 AggType AggType 950 951 WindowSpec *WindowSpec 952 953 OrderBy OrderBy 954 } 955 956 func (node *FuncExpr) Format(ctx *FmtCtx) { 957 if node.FuncName != nil { 958 ctx.WriteString(node.FuncName.Compare()) 959 } else { 960 node.Func.Format(ctx) 961 } 962 963 ctx.WriteString("(") 964 if node.Type != FUNC_TYPE_DEFAULT && node.Type != FUNC_TYPE_TABLE { 965 ctx.WriteString(node.Type.ToString()) 966 ctx.WriteByte(' ') 967 } 968 if node.Func.FunctionReference.(*UnresolvedName).Parts[0] == "trim" { 969 trimExprsFormat(ctx, node.Exprs) 970 } else { 971 node.Exprs.Format(ctx) 972 } 973 974 if node.OrderBy != nil { 975 node.OrderBy.Format(ctx) 976 } 977 978 ctx.WriteByte(')') 979 980 if node.WindowSpec != nil { 981 ctx.WriteString(" ") 982 node.WindowSpec.Format(ctx) 983 } 984 } 985 986 // Accept implements NodeChecker interface 987 func (node *FuncExpr) Accept(v Visitor) (Expr, bool) { 988 newNode, skipChildren := v.Enter(node) 989 if skipChildren { 990 return v.Exit(newNode) 991 } 992 node = newNode.(*FuncExpr) 993 for i, val := range node.Exprs { 994 tmpNode, ok := val.Accept(v) 995 if !ok { 996 return node, false 997 } 998 node.Exprs[i] = tmpNode 999 } 1000 return v.Exit(node) 1001 } 1002 1003 func trimExprsFormat(ctx *FmtCtx, exprs Exprs) { 1004 tp := exprs[0].(*NumVal).String() 1005 switch tp { 1006 case "0": 1007 exprs[3].Format(ctx) 1008 case "1": 1009 exprs[2].Format(ctx) 1010 ctx.WriteString(" from ") 1011 exprs[3].Format(ctx) 1012 case "2": 1013 exprs[1].Format(ctx) 1014 ctx.WriteString(" from ") 1015 exprs[3].Format(ctx) 1016 case "3": 1017 exprs[1].Format(ctx) 1018 ctx.WriteString(" ") 1019 exprs[2].Format(ctx) 1020 ctx.WriteString(" from ") 1021 exprs[3].Format(ctx) 1022 default: 1023 panic("unknown trim type") 1024 } 1025 } 1026 1027 type WindowSpec struct { 1028 PartitionBy Exprs 1029 OrderBy OrderBy 1030 HasFrame bool 1031 Frame *FrameClause 1032 } 1033 1034 func (node *WindowSpec) Format(ctx *FmtCtx) { 1035 ctx.WriteString("over (") 1036 flag := false 1037 if len(node.PartitionBy) > 0 { 1038 ctx.WriteString("partition by ") 1039 node.PartitionBy.Format(ctx) 1040 flag = true 1041 } 1042 1043 if len(node.OrderBy) > 0 { 1044 if flag { 1045 ctx.WriteString(" ") 1046 } 1047 node.OrderBy.Format(ctx) 1048 flag = true 1049 } 1050 1051 if node.Frame != nil && node.HasFrame { 1052 if flag { 1053 ctx.WriteString(" ") 1054 } 1055 node.Frame.Format(ctx) 1056 } 1057 1058 ctx.WriteByte(')') 1059 } 1060 1061 type FrameType int 1062 1063 const ( 1064 Rows FrameType = iota 1065 Range 1066 Groups 1067 ) 1068 1069 type FrameClause struct { 1070 Type FrameType 1071 HasEnd bool 1072 Start *FrameBound 1073 End *FrameBound 1074 } 1075 1076 func (node *FrameClause) Format(ctx *FmtCtx) { 1077 switch node.Type { 1078 case Rows: 1079 ctx.WriteString("rows") 1080 case Range: 1081 ctx.WriteString("range") 1082 case Groups: 1083 ctx.WriteString("groups") 1084 } 1085 ctx.WriteString(" ") 1086 if !node.HasEnd { 1087 node.Start.Format(ctx) 1088 return 1089 } 1090 ctx.WriteString("between ") 1091 node.Start.Format(ctx) 1092 ctx.WriteString(" and ") 1093 node.End.Format(ctx) 1094 } 1095 1096 type BoundType int 1097 1098 const ( 1099 Following BoundType = iota 1100 Preceding 1101 CurrentRow 1102 ) 1103 1104 type FrameBound struct { 1105 Type BoundType 1106 UnBounded bool 1107 Expr Expr 1108 } 1109 1110 func (node *FrameBound) Format(ctx *FmtCtx) { 1111 if node.UnBounded { 1112 ctx.WriteString("unbounded") 1113 } 1114 if node.Type == CurrentRow { 1115 ctx.WriteString("current row") 1116 } else { 1117 if node.Expr != nil { 1118 node.Expr.Format(ctx) 1119 } 1120 if node.Type == Preceding { 1121 ctx.WriteString(" preceding") 1122 } else { 1123 ctx.WriteString(" following") 1124 } 1125 } 1126 } 1127 1128 // type reference 1129 type ResolvableTypeReference interface { 1130 } 1131 1132 var _ ResolvableTypeReference = &UnresolvedObjectName{} 1133 var _ ResolvableTypeReference = &T{} 1134 1135 type SerialExtractExpr struct { 1136 exprImpl 1137 SerialExpr Expr 1138 IndexExpr Expr 1139 ResultType ResolvableTypeReference 1140 } 1141 1142 func (node *SerialExtractExpr) Format(ctx *FmtCtx) { 1143 ctx.WriteString("serial_extract(") 1144 node.SerialExpr.Format(ctx) 1145 ctx.WriteString(", ") 1146 node.IndexExpr.Format(ctx) 1147 ctx.WriteString(" as ") 1148 node.ResultType.(*T).InternalType.Format(ctx) 1149 ctx.WriteByte(')') 1150 } 1151 1152 // Accept implements NodeChecker interface 1153 func (node *SerialExtractExpr) Accept(v Visitor) (Expr, bool) { 1154 //TODO: need validation from @iamlinjunhong 1155 1156 newNode, skipChildren := v.Enter(node) 1157 if skipChildren { 1158 return v.Exit(newNode) 1159 } 1160 node = newNode.(*SerialExtractExpr) 1161 1162 tmpNode, ok := node.SerialExpr.Accept(v) 1163 if !ok { 1164 return node, false 1165 } 1166 node.SerialExpr = tmpNode 1167 1168 tmpNode, ok = node.IndexExpr.Accept(v) 1169 if !ok { 1170 return node, false 1171 } 1172 node.IndexExpr = tmpNode 1173 1174 return v.Exit(node) 1175 } 1176 1177 func NewSerialExtractExpr(serialExpr Expr, indexExpr Expr, typ ResolvableTypeReference) *SerialExtractExpr { 1178 return &SerialExtractExpr{ 1179 SerialExpr: serialExpr, 1180 IndexExpr: indexExpr, 1181 ResultType: typ, 1182 } 1183 } 1184 1185 // the Cast expression 1186 type CastExpr struct { 1187 exprImpl 1188 Expr Expr 1189 Type ResolvableTypeReference 1190 } 1191 1192 func (node *CastExpr) Format(ctx *FmtCtx) { 1193 ctx.WriteString("cast(") 1194 node.Expr.Format(ctx) 1195 ctx.WriteString(" as ") 1196 node.Type.(*T).InternalType.Format(ctx) 1197 ctx.WriteByte(')') 1198 } 1199 1200 // Accept implements NodeChecker interface 1201 func (node *CastExpr) Accept(v Visitor) (Expr, bool) { 1202 newNode, skipChildren := v.Enter(node) 1203 if skipChildren { 1204 return v.Exit(newNode) 1205 } 1206 node = newNode.(*CastExpr) 1207 tmpNode, ok := node.Expr.Accept(v) 1208 if !ok { 1209 return node, false 1210 } 1211 node.Expr = tmpNode 1212 return v.Exit(node) 1213 } 1214 1215 func NewCastExpr(e Expr, t ResolvableTypeReference) *CastExpr { 1216 return &CastExpr{ 1217 Expr: e, 1218 Type: t, 1219 } 1220 } 1221 1222 type BitCastExpr struct { 1223 exprImpl 1224 Expr Expr 1225 Type ResolvableTypeReference 1226 } 1227 1228 func (node *BitCastExpr) Format(ctx *FmtCtx) { 1229 ctx.WriteString("bit_cast(") 1230 node.Expr.Format(ctx) 1231 ctx.WriteString(" as ") 1232 node.Type.(*T).InternalType.Format(ctx) 1233 ctx.WriteByte(')') 1234 } 1235 1236 // Accept implements NodeChecker interface 1237 func (node *BitCastExpr) Accept(v Visitor) (Expr, bool) { 1238 newNode, skipChildren := v.Enter(node) 1239 if skipChildren { 1240 return v.Exit(newNode) 1241 } 1242 node = newNode.(*BitCastExpr) 1243 tmpNode, ok := node.Expr.Accept(v) 1244 if !ok { 1245 return node, false 1246 } 1247 node.Expr = tmpNode 1248 return v.Exit(node) 1249 } 1250 1251 func NewBitCastExpr(e Expr, t ResolvableTypeReference) *BitCastExpr { 1252 return &BitCastExpr{ 1253 Expr: e, 1254 Type: t, 1255 } 1256 } 1257 1258 // the parenthesized list of expressions. 1259 type Tuple struct { 1260 exprImpl 1261 Exprs Exprs 1262 } 1263 1264 func (node *Tuple) Format(ctx *FmtCtx) { 1265 if node.Exprs != nil { 1266 ctx.WriteByte('(') 1267 node.Exprs.Format(ctx) 1268 ctx.WriteByte(')') 1269 } 1270 } 1271 1272 // Accept implements NodeChecker interface 1273 func (node *Tuple) Accept(v Visitor) (Expr, bool) { 1274 newNode, skipChildren := v.Enter(node) 1275 if skipChildren { 1276 return v.Exit(newNode) 1277 } 1278 node = newNode.(*Tuple) 1279 for i, val := range node.Exprs { 1280 tmpNode, ok := val.Accept(v) 1281 if !ok { 1282 return node, false 1283 } 1284 node.Exprs[i] = tmpNode 1285 } 1286 return v.Exit(node) 1287 } 1288 1289 func NewTuple(e Exprs) *Tuple { 1290 return &Tuple{Exprs: e} 1291 } 1292 1293 // the BETWEEN or a NOT BETWEEN expression 1294 type RangeCond struct { 1295 exprImpl 1296 Not bool 1297 Left Expr 1298 From, To Expr 1299 } 1300 1301 func (node *RangeCond) Format(ctx *FmtCtx) { 1302 ctx.PrintExpr(node, node.Left, true) 1303 if node.Not { 1304 ctx.WriteString(" not") 1305 } 1306 ctx.WriteString(" between ") 1307 ctx.PrintExpr(node, node.From, true) 1308 ctx.WriteString(" and ") 1309 ctx.PrintExpr(node, node.To, false) 1310 } 1311 1312 // Accept implements NodeChecker interface 1313 func (node *RangeCond) Accept(v Visitor) (Expr, bool) { 1314 newNode, skipChildren := v.Enter(node) 1315 if skipChildren { 1316 return v.Exit(newNode) 1317 } 1318 1319 node = newNode.(*RangeCond) 1320 tmpNode, ok := node.Left.Accept(v) 1321 if !ok { 1322 return node, false 1323 } 1324 node.Left = tmpNode 1325 1326 tmpNode, ok = node.From.Accept(v) 1327 if !ok { 1328 return node, false 1329 } 1330 node.From = tmpNode 1331 1332 tmpNode, ok = node.To.Accept(v) 1333 if !ok { 1334 return node, false 1335 } 1336 node.To = tmpNode 1337 1338 return v.Exit(node) 1339 } 1340 1341 func NewRangeCond(n bool, l, f, t Expr) *RangeCond { 1342 return &RangeCond{ 1343 Not: n, 1344 Left: l, 1345 From: f, 1346 To: t, 1347 } 1348 } 1349 1350 // Case-When expression. 1351 type CaseExpr struct { 1352 exprImpl 1353 Expr Expr 1354 Whens []*When 1355 Else Expr 1356 } 1357 1358 func (node *CaseExpr) Format(ctx *FmtCtx) { 1359 ctx.WriteString("case") 1360 if node.Expr != nil { 1361 ctx.WriteByte(' ') 1362 node.Expr.Format(ctx) 1363 } 1364 ctx.WriteByte(' ') 1365 prefix := "" 1366 for _, w := range node.Whens { 1367 ctx.WriteString(prefix) 1368 w.Format(ctx) 1369 prefix = " " 1370 } 1371 if node.Else != nil { 1372 ctx.WriteString(" else ") 1373 node.Else.Format(ctx) 1374 } 1375 ctx.WriteString(" end") 1376 } 1377 1378 // Accept implements NodeChecker interface 1379 func (node *CaseExpr) Accept(v Visitor) (Expr, bool) { 1380 newNode, skipChildren := v.Enter(node) 1381 if skipChildren { 1382 return v.Exit(newNode) 1383 } 1384 node = newNode.(*CaseExpr) 1385 1386 tmpNode, ok := node.Expr.Accept(v) 1387 if !ok { 1388 return node, false 1389 } 1390 node.Expr = tmpNode 1391 1392 for _, when := range node.Whens { 1393 tmpNode, ok = when.Cond.Accept(v) 1394 if !ok { 1395 return node, false 1396 } 1397 when.Cond = tmpNode 1398 1399 tmpNode, ok = when.Val.Accept(v) 1400 if !ok { 1401 return node, false 1402 } 1403 when.Val = tmpNode 1404 } 1405 1406 tmpNode, ok = node.Else.Accept(v) 1407 if !ok { 1408 return node, false 1409 } 1410 node.Else = tmpNode 1411 1412 return v.Exit(node) 1413 } 1414 1415 func NewCaseExpr(e Expr, w []*When, el Expr) *CaseExpr { 1416 return &CaseExpr{ 1417 Expr: e, 1418 Whens: w, 1419 Else: el, 1420 } 1421 } 1422 1423 // When sub-expression. 1424 type When struct { 1425 Cond Expr 1426 Val Expr 1427 } 1428 1429 func (node *When) Format(ctx *FmtCtx) { 1430 ctx.WriteString("when ") 1431 node.Cond.Format(ctx) 1432 ctx.WriteString(" then ") 1433 node.Val.Format(ctx) 1434 } 1435 1436 func NewWhen(c, v Expr) *When { 1437 return &When{ 1438 Cond: c, 1439 Val: v, 1440 } 1441 } 1442 1443 // IntervalType is the type for time and timestamp units. 1444 type IntervalType int 1445 1446 func (node *IntervalType) ToString() string { 1447 switch *node { 1448 case INTERVAL_TYPE_SECOND: 1449 return "second" 1450 default: 1451 return "Unknown IntervalType" 1452 } 1453 } 1454 1455 const ( 1456 //an invalid time or timestamp unit 1457 INTERVAL_TYPE_INVALID IntervalType = iota 1458 //the time or timestamp unit MICROSECOND. 1459 INTERVAL_TYPE_MICROSECOND 1460 //the time or timestamp unit SECOND. 1461 INTERVAL_TYPE_SECOND 1462 //the time or timestamp unit MINUTE. 1463 INTERVAL_TYPE_MINUTE 1464 //the time or timestamp unit HOUR. 1465 INTERVAL_TYPE_HOUR 1466 //the time or timestamp unit DAY. 1467 INTERVAL_TYPE_DAY 1468 //the time or timestamp unit WEEK. 1469 INTERVAL_TYPE_WEEK 1470 //the time or timestamp unit MONTH. 1471 INTERVAL_TYPE_MONTH 1472 //the time or timestamp unit QUARTER. 1473 INTERVAL_TYPE_QUARTER 1474 //the time or timestamp unit YEAR. 1475 INTERVAL_TYPE_YEAR 1476 //the time unit SECOND_MICROSECOND. 1477 INTERVAL_TYPE_SECOND_MICROSECOND 1478 //the time unit MINUTE_MICROSECOND. 1479 INTERVAL_TYPE_MINUTE_MICROSECOND 1480 //the time unit MINUTE_SECOND. 1481 INTERVAL_TYPE_MINUTE_SECOND 1482 //the time unit HOUR_MICROSECOND. 1483 INTERVAL_TYPE_HOUR_MICROSECOND 1484 //the time unit HOUR_SECOND. 1485 INTERVAL_TYPE_HOUR_SECOND 1486 //the time unit HOUR_MINUTE. 1487 INTERVAL_TYPE_HOUR_MINUTE 1488 //the time unit DAY_MICROSECOND. 1489 INTERVAL_TYPE_DAY_MICROSECOND 1490 //the time unit DAY_SECOND. 1491 INTERVAL_TYPE_DAY_SECOND 1492 //the time unit DAY_MINUTE. 1493 INTERVAL_TYPE_DAYMINUTE 1494 //the time unit DAY_HOUR. 1495 INTERVAL_TYPE_DAYHOUR 1496 //the time unit YEAR_MONTH. 1497 INTERVAL_TYPE_YEARMONTH 1498 ) 1499 1500 // INTERVAL / time unit 1501 type IntervalExpr struct { 1502 exprImpl 1503 Expr Expr 1504 Type IntervalType 1505 } 1506 1507 func (node *IntervalExpr) Format(ctx *FmtCtx) { 1508 ctx.WriteString("interval") 1509 if node.Expr != nil { 1510 ctx.WriteByte(' ') 1511 node.Expr.Format(ctx) 1512 } 1513 if node.Type != INTERVAL_TYPE_INVALID { 1514 ctx.WriteByte(' ') 1515 ctx.WriteString(node.Type.ToString()) 1516 } 1517 } 1518 1519 // Accept implements NodeChecker Accept interface. 1520 func (node *IntervalExpr) Accept(v Visitor) (Expr, bool) { 1521 // TODO: 1522 panic("unimplement interval expr Accept") 1523 } 1524 1525 func NewIntervalExpr(t IntervalType) *IntervalExpr { 1526 return &IntervalExpr{ 1527 Type: t, 1528 } 1529 } 1530 1531 // the DEFAULT expression. 1532 type DefaultVal struct { 1533 exprImpl 1534 Expr Expr 1535 } 1536 1537 func (node *DefaultVal) Format(ctx *FmtCtx) { 1538 ctx.WriteString("default") 1539 if node.Expr != nil { 1540 node.Expr.Format(ctx) 1541 } 1542 } 1543 1544 // Accept implements NodeChecker interface 1545 func (node *DefaultVal) Accept(v Visitor) (Expr, bool) { 1546 newNode, skipChildren := v.Enter(node) 1547 if skipChildren { 1548 return v.Exit(newNode) 1549 } 1550 node = newNode.(*DefaultVal) 1551 tmpNode, ok := node.Expr.Accept(v) 1552 if !ok { 1553 return node, false 1554 } 1555 node.Expr = tmpNode 1556 return v.Exit(node) 1557 } 1558 1559 func NewDefaultVal(e Expr) *DefaultVal { 1560 return &DefaultVal{ 1561 Expr: e, 1562 } 1563 } 1564 1565 type UpdateVal struct { 1566 exprImpl 1567 } 1568 1569 func (node *UpdateVal) Format(ctx *FmtCtx) {} 1570 1571 // Accept implements NodeChecker interface 1572 func (node *UpdateVal) Accept(v Visitor) (Expr, bool) { 1573 newNode, skipChildren := v.Enter(node) 1574 if skipChildren { 1575 return v.Exit(newNode) 1576 } 1577 return v.Exit(node) 1578 } 1579 1580 type TypeExpr interface { 1581 Expr 1582 } 1583 1584 /* 1585 Variable Expression Used in Set Statement, 1586 Load Data statement, Show statement,etc. 1587 Variable types: 1588 User-Defined Variable 1589 Local-Variable: DECLARE statement 1590 System Variable: Global System Variable, Session System Variable 1591 */ 1592 1593 type VarExpr struct { 1594 exprImpl 1595 Name string 1596 System bool 1597 Global bool 1598 Expr Expr 1599 } 1600 1601 // incomplete 1602 func (node *VarExpr) Format(ctx *FmtCtx) { 1603 if node.Name != "" { 1604 ctx.WriteByte('@') 1605 if node.System { 1606 ctx.WriteByte('@') 1607 } 1608 ctx.WriteString(node.Name) 1609 } 1610 } 1611 1612 // Accept implements NodeChecker Accept interface. 1613 func (node *VarExpr) Accept(v Visitor) (Expr, bool) { 1614 panic("unimplement VarExpr Accept") 1615 } 1616 1617 func NewVarExpr(n string, s bool, g bool, e Expr) *VarExpr { 1618 return &VarExpr{ 1619 Name: n, 1620 System: s, 1621 Global: g, 1622 Expr: e, 1623 } 1624 } 1625 1626 // select a from t1 where a > ? 1627 type ParamExpr struct { 1628 exprImpl 1629 Offset int 1630 } 1631 1632 func (node *ParamExpr) Format(ctx *FmtCtx) { 1633 ctx.WriteByte('?') 1634 } 1635 1636 // Accept implements NodeChecker Accept interface. 1637 func (node *ParamExpr) Accept(v Visitor) (Expr, bool) { 1638 panic("unimplement ParamExpr Accept") 1639 } 1640 1641 func NewParamExpr(offset int) *ParamExpr { 1642 return &ParamExpr{ 1643 Offset: offset, 1644 } 1645 } 1646 1647 type MaxValue struct { 1648 exprImpl 1649 } 1650 1651 func (node *MaxValue) Format(ctx *FmtCtx) { 1652 ctx.WriteString("MAXVALUE") 1653 } 1654 1655 func NewMaxValue() *MaxValue { 1656 return &MaxValue{} 1657 } 1658 1659 // Accept implements NodeChecker interface 1660 func (node *MaxValue) Accept(v Visitor) (Expr, bool) { 1661 newNode, skipChildren := v.Enter(node) 1662 if skipChildren { 1663 return v.Exit(newNode) 1664 } 1665 return v.Exit(node) 1666 } 1667 1668 // SampleExpr for sample(exprList, N rows / R percent) 1669 type SampleExpr struct { 1670 // rows or percent. 1671 typ sampleType 1672 // sample level. 1673 level sampleLevel 1674 1675 // N or K 1676 n int 1677 k float64 1678 1679 // sample by '*' 1680 isStar bool 1681 // sample by columns. 1682 columns Exprs 1683 } 1684 1685 func (s SampleExpr) String() string { 1686 return "sample" 1687 } 1688 1689 func (s SampleExpr) Format(ctx *FmtCtx) { 1690 if s.typ == SampleRows { 1691 ctx.WriteString(fmt.Sprintf("sample %d rows", s.n)) 1692 } else { 1693 ctx.WriteString(fmt.Sprintf("sample %.1f percent", s.k)) 1694 } 1695 } 1696 1697 func (s SampleExpr) Accept(v Visitor) (node Expr, ok bool) { 1698 newNode, skipChildren := v.Enter(node) 1699 if skipChildren { 1700 return v.Exit(newNode) 1701 } 1702 return v.Exit(node) 1703 } 1704 1705 func (s SampleExpr) Valid() error { 1706 if s.typ == SampleRows { 1707 if s.n < 1 || s.n > 11_000 { 1708 return moerr.NewSyntaxErrorNoCtx("sample(expr list, N rows) requires N between 1 and 11000.") 1709 } 1710 return nil 1711 } else { 1712 if s.k < 0 || s.k > 100 { 1713 return moerr.NewSyntaxErrorNoCtx("sample(expr list, K percent) requires K between 0.00 and 100.00") 1714 } 1715 return nil 1716 } 1717 } 1718 1719 func (s SampleExpr) GetColumns() (columns Exprs, isStar bool) { 1720 return s.columns, s.isStar 1721 } 1722 1723 func (s SampleExpr) GetSampleDetail() (isSampleRows bool, usingRow bool, n int32, k float64) { 1724 return s.typ == SampleRows, s.level == SampleUsingRow, int32(s.n), s.k 1725 } 1726 1727 type sampleType int 1728 type sampleLevel int 1729 1730 const ( 1731 SampleRows sampleType = 0 1732 SamplePercent sampleType = 1 1733 1734 SampleUsingBlock sampleLevel = 0 1735 SampleUsingRow sampleLevel = 1 1736 ) 1737 1738 func NewSampleRowsFuncExpression(number int, isStar bool, columns Exprs, sampleUnit string) (*SampleExpr, error) { 1739 e := &SampleExpr{ 1740 typ: SampleRows, 1741 n: number, 1742 k: 0, 1743 isStar: isStar, 1744 columns: columns, 1745 } 1746 if len(sampleUnit) == 5 && strings.ToLower(sampleUnit) == "block" { 1747 e.level = SampleUsingBlock 1748 return e, nil 1749 } 1750 if len(sampleUnit) == 3 && strings.ToLower(sampleUnit) == "row" { 1751 e.level = SampleUsingRow 1752 return e, nil 1753 } 1754 return e, moerr.NewInternalErrorNoCtx("sample(expr, N rows, unit) only support unit 'block' or 'row'") 1755 } 1756 1757 func NewSamplePercentFuncExpression1(percent int64, isStar bool, columns Exprs) (*SampleExpr, error) { 1758 return &SampleExpr{ 1759 typ: SamplePercent, 1760 n: 0, 1761 k: float64(percent), 1762 isStar: isStar, 1763 columns: columns, 1764 }, nil 1765 } 1766 1767 func NewSamplePercentFuncExpression2(percent float64, isStar bool, columns Exprs) (*SampleExpr, error) { 1768 if nan := math.IsNaN(percent); nan { 1769 return nil, moerr.NewSyntaxErrorNoCtx("sample(expr, K percent) requires K between 0.00 and 100.00") 1770 } 1771 k, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", percent), 64) 1772 1773 return &SampleExpr{ 1774 typ: SamplePercent, 1775 n: 0, 1776 k: k, 1777 isStar: isStar, 1778 columns: columns, 1779 }, nil 1780 }