github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/ast/expressions.go (about) 1 // Copyright 2015 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package ast 15 16 import ( 17 "regexp" 18 19 "github.com/insionng/yougam/libraries/pingcap/tidb/context" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/model" 21 "github.com/insionng/yougam/libraries/pingcap/tidb/mysql" 22 "github.com/insionng/yougam/libraries/pingcap/tidb/parser/opcode" 23 "github.com/insionng/yougam/libraries/pingcap/tidb/util/types" 24 ) 25 26 var ( 27 _ ExprNode = &BetweenExpr{} 28 _ ExprNode = &BinaryOperationExpr{} 29 _ ExprNode = &CaseExpr{} 30 _ ExprNode = &ColumnNameExpr{} 31 _ ExprNode = &CompareSubqueryExpr{} 32 _ ExprNode = &DefaultExpr{} 33 _ ExprNode = &ExistsSubqueryExpr{} 34 _ ExprNode = &IsNullExpr{} 35 _ ExprNode = &IsTruthExpr{} 36 _ ExprNode = &ParamMarkerExpr{} 37 _ ExprNode = &ParenthesesExpr{} 38 _ ExprNode = &PatternInExpr{} 39 _ ExprNode = &PatternLikeExpr{} 40 _ ExprNode = &PatternRegexpExpr{} 41 _ ExprNode = &PositionExpr{} 42 _ ExprNode = &RowExpr{} 43 _ ExprNode = &SubqueryExpr{} 44 _ ExprNode = &UnaryOperationExpr{} 45 _ ExprNode = &ValueExpr{} 46 _ ExprNode = &ValuesExpr{} 47 _ ExprNode = &VariableExpr{} 48 49 _ Node = &ColumnName{} 50 _ Node = &WhenClause{} 51 ) 52 53 // ValueExpr is the simple value expression. 54 type ValueExpr struct { 55 exprNode 56 } 57 58 // NewValueExpr creates a ValueExpr with value, and sets default field type. 59 func NewValueExpr(value interface{}) *ValueExpr { 60 if ve, ok := value.(*ValueExpr); ok { 61 return ve 62 } 63 ve := &ValueExpr{} 64 ve.SetValue(value) 65 if _, ok := value.(UnquoteString); ok { 66 ve.Type = types.NewFieldType(mysql.TypeVarchar) 67 ve.Type.Charset = mysql.DefaultCharset 68 ve.Type.Collate = mysql.DefaultCollationName 69 return ve 70 } 71 ve.Type = types.DefaultTypeForValue(value) 72 return ve 73 } 74 75 // Accept implements Node interface. 76 func (n *ValueExpr) Accept(v Visitor) (Node, bool) { 77 newNode, skipChildren := v.Enter(n) 78 if skipChildren { 79 return v.Leave(newNode) 80 } 81 n = newNode.(*ValueExpr) 82 return v.Leave(n) 83 } 84 85 // BetweenExpr is for "between and" or "not between and" expression. 86 type BetweenExpr struct { 87 exprNode 88 // Expr is the expression to be checked. 89 Expr ExprNode 90 // Left is the expression for minimal value in the range. 91 Left ExprNode 92 // Right is the expression for maximum value in the range. 93 Right ExprNode 94 // Not is true, the expression is "not between and". 95 Not bool 96 } 97 98 // Accept implements Node interface. 99 func (n *BetweenExpr) Accept(v Visitor) (Node, bool) { 100 newNode, skipChildren := v.Enter(n) 101 if skipChildren { 102 return v.Leave(newNode) 103 } 104 105 n = newNode.(*BetweenExpr) 106 node, ok := n.Expr.Accept(v) 107 if !ok { 108 return n, false 109 } 110 n.Expr = node.(ExprNode) 111 112 node, ok = n.Left.Accept(v) 113 if !ok { 114 return n, false 115 } 116 n.Left = node.(ExprNode) 117 118 node, ok = n.Right.Accept(v) 119 if !ok { 120 return n, false 121 } 122 n.Right = node.(ExprNode) 123 124 return v.Leave(n) 125 } 126 127 // BinaryOperationExpr is for binary operation like `1 + 1`, `1 - 1`, etc. 128 type BinaryOperationExpr struct { 129 exprNode 130 // Op is the operator code for BinaryOperation. 131 Op opcode.Op 132 // L is the left expression in BinaryOperation. 133 L ExprNode 134 // R is the right expression in BinaryOperation. 135 R ExprNode 136 } 137 138 // Accept implements Node interface. 139 func (n *BinaryOperationExpr) Accept(v Visitor) (Node, bool) { 140 newNode, skipChildren := v.Enter(n) 141 if skipChildren { 142 return v.Leave(newNode) 143 } 144 145 n = newNode.(*BinaryOperationExpr) 146 node, ok := n.L.Accept(v) 147 if !ok { 148 return n, false 149 } 150 n.L = node.(ExprNode) 151 152 node, ok = n.R.Accept(v) 153 if !ok { 154 return n, false 155 } 156 n.R = node.(ExprNode) 157 158 return v.Leave(n) 159 } 160 161 // WhenClause is the when clause in Case expression for "when condition then result". 162 type WhenClause struct { 163 node 164 // Expr is the condition expression in WhenClause. 165 Expr ExprNode 166 // Result is the result expression in WhenClause. 167 Result ExprNode 168 } 169 170 // Accept implements Node Accept interface. 171 func (n *WhenClause) Accept(v Visitor) (Node, bool) { 172 newNode, skipChildren := v.Enter(n) 173 if skipChildren { 174 return v.Leave(newNode) 175 } 176 177 n = newNode.(*WhenClause) 178 node, ok := n.Expr.Accept(v) 179 if !ok { 180 return n, false 181 } 182 n.Expr = node.(ExprNode) 183 184 node, ok = n.Result.Accept(v) 185 if !ok { 186 return n, false 187 } 188 n.Result = node.(ExprNode) 189 return v.Leave(n) 190 } 191 192 // CaseExpr is the case expression. 193 type CaseExpr struct { 194 exprNode 195 // Value is the compare value expression. 196 Value ExprNode 197 // WhenClauses is the condition check expression. 198 WhenClauses []*WhenClause 199 // ElseClause is the else result expression. 200 ElseClause ExprNode 201 } 202 203 // Accept implements Node Accept interface. 204 func (n *CaseExpr) Accept(v Visitor) (Node, bool) { 205 newNode, skipChildren := v.Enter(n) 206 if skipChildren { 207 return v.Leave(newNode) 208 } 209 210 n = newNode.(*CaseExpr) 211 if n.Value != nil { 212 node, ok := n.Value.Accept(v) 213 if !ok { 214 return n, false 215 } 216 n.Value = node.(ExprNode) 217 } 218 for i, val := range n.WhenClauses { 219 node, ok := val.Accept(v) 220 if !ok { 221 return n, false 222 } 223 n.WhenClauses[i] = node.(*WhenClause) 224 } 225 if n.ElseClause != nil { 226 node, ok := n.ElseClause.Accept(v) 227 if !ok { 228 return n, false 229 } 230 n.ElseClause = node.(ExprNode) 231 } 232 return v.Leave(n) 233 } 234 235 // SubqueryExec represents a subquery executor interface. 236 // This interface is implemented in executor and used in plan/evaluator. 237 // It will execute the subselect and get the result. 238 type SubqueryExec interface { 239 // EvalRows executes the subquery and returns the multi rows with rowCount. 240 // rowCount < 0 means no limit. 241 // If the ColumnCount is 1, we will return a column result like {1, 2, 3}, 242 // otherwise, we will return a table result like {{1, 1}, {2, 2}}. 243 EvalRows(ctx context.Context, rowCount int) ([]types.Datum, error) 244 245 // ColumnCount returns column count for the sub query. 246 ColumnCount() (int, error) 247 } 248 249 // SubqueryExpr represents a subquery. 250 type SubqueryExpr struct { 251 exprNode 252 // Query is the query SelectNode. 253 Query ResultSetNode 254 SubqueryExec SubqueryExec 255 Evaluated bool 256 Correlated bool 257 MultiRows bool 258 Exists bool 259 } 260 261 // Accept implements Node Accept interface. 262 func (n *SubqueryExpr) Accept(v Visitor) (Node, bool) { 263 newNode, skipChildren := v.Enter(n) 264 if skipChildren { 265 return v.Leave(newNode) 266 } 267 n = newNode.(*SubqueryExpr) 268 if n.SubqueryExec != nil { 269 return v.Leave(n) 270 } 271 node, ok := n.Query.Accept(v) 272 if !ok { 273 return n, false 274 } 275 n.Query = node.(ResultSetNode) 276 return v.Leave(n) 277 } 278 279 // SetResultFields implements ResultSetNode interface. 280 func (n *SubqueryExpr) SetResultFields(rfs []*ResultField) { 281 n.Query.SetResultFields(rfs) 282 } 283 284 // GetResultFields implements ResultSetNode interface. 285 func (n *SubqueryExpr) GetResultFields() []*ResultField { 286 return n.Query.GetResultFields() 287 } 288 289 // CompareSubqueryExpr is the expression for "expr cmp (select ...)". 290 // See: https://dev.mysql.com/doc/refman/5.7/en/comparisons-using-subqueries.html 291 // See: https://dev.mysql.com/doc/refman/5.7/en/any-in-some-subqueries.html 292 // See: https://dev.mysql.com/doc/refman/5.7/en/all-subqueries.html 293 type CompareSubqueryExpr struct { 294 exprNode 295 // L is the left expression 296 L ExprNode 297 // Op is the comparison opcode. 298 Op opcode.Op 299 // R is the subquery for right expression, may be rewritten to other type of expression. 300 R ExprNode 301 // All is true, we should compare all records in subquery. 302 All bool 303 } 304 305 // Accept implements Node Accept interface. 306 func (n *CompareSubqueryExpr) Accept(v Visitor) (Node, bool) { 307 newNode, skipChildren := v.Enter(n) 308 if skipChildren { 309 return v.Leave(newNode) 310 } 311 n = newNode.(*CompareSubqueryExpr) 312 node, ok := n.L.Accept(v) 313 if !ok { 314 return n, false 315 } 316 n.L = node.(ExprNode) 317 node, ok = n.R.Accept(v) 318 if !ok { 319 return n, false 320 } 321 n.R = node.(ExprNode) 322 return v.Leave(n) 323 } 324 325 // ColumnName represents column name. 326 type ColumnName struct { 327 node 328 Schema model.CIStr 329 Table model.CIStr 330 Name model.CIStr 331 } 332 333 // Accept implements Node Accept interface. 334 func (n *ColumnName) Accept(v Visitor) (Node, bool) { 335 newNode, skipChildren := v.Enter(n) 336 if skipChildren { 337 return v.Leave(newNode) 338 } 339 n = newNode.(*ColumnName) 340 return v.Leave(n) 341 } 342 343 // ColumnNameExpr represents a column name expression. 344 type ColumnNameExpr struct { 345 exprNode 346 347 // Name is the referenced column name. 348 Name *ColumnName 349 350 // Refer is the result field the column name refers to. 351 // The value of Refer.Expr is used as the value of the expression. 352 Refer *ResultField 353 } 354 355 // Accept implements Node Accept interface. 356 func (n *ColumnNameExpr) Accept(v Visitor) (Node, bool) { 357 newNode, skipChildren := v.Enter(n) 358 if skipChildren { 359 return v.Leave(newNode) 360 } 361 n = newNode.(*ColumnNameExpr) 362 node, ok := n.Name.Accept(v) 363 if !ok { 364 return n, false 365 } 366 n.Name = node.(*ColumnName) 367 return v.Leave(n) 368 } 369 370 // DefaultExpr is the default expression using default value for a column. 371 type DefaultExpr struct { 372 exprNode 373 // Name is the column name. 374 Name *ColumnName 375 } 376 377 // Accept implements Node Accept interface. 378 func (n *DefaultExpr) Accept(v Visitor) (Node, bool) { 379 newNode, skipChildren := v.Enter(n) 380 if skipChildren { 381 return v.Leave(newNode) 382 } 383 n = newNode.(*DefaultExpr) 384 if n.Name != nil { 385 node, ok := n.Name.Accept(v) 386 if !ok { 387 return n, false 388 } 389 n.Name = node.(*ColumnName) 390 } 391 return v.Leave(n) 392 } 393 394 // ExistsSubqueryExpr is the expression for "exists (select ...)". 395 // https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html 396 type ExistsSubqueryExpr struct { 397 exprNode 398 // Sel is the subquery, may be rewritten to other type of expression. 399 Sel ExprNode 400 } 401 402 // Accept implements Node Accept interface. 403 func (n *ExistsSubqueryExpr) Accept(v Visitor) (Node, bool) { 404 newNode, skipChildren := v.Enter(n) 405 if skipChildren { 406 return v.Leave(newNode) 407 } 408 n = newNode.(*ExistsSubqueryExpr) 409 node, ok := n.Sel.Accept(v) 410 if !ok { 411 return n, false 412 } 413 n.Sel = node.(ExprNode) 414 return v.Leave(n) 415 } 416 417 // PatternInExpr is the expression for in operator, like "expr in (1, 2, 3)" or "expr in (select c from t)". 418 type PatternInExpr struct { 419 exprNode 420 // Expr is the value expression to be compared. 421 Expr ExprNode 422 // List is the list expression in compare list. 423 List []ExprNode 424 // Not is true, the expression is "not in". 425 Not bool 426 // Sel is the subquery, may be rewritten to other type of expression. 427 Sel ExprNode 428 } 429 430 // Accept implements Node Accept interface. 431 func (n *PatternInExpr) Accept(v Visitor) (Node, bool) { 432 newNode, skipChildren := v.Enter(n) 433 if skipChildren { 434 return v.Leave(newNode) 435 } 436 n = newNode.(*PatternInExpr) 437 node, ok := n.Expr.Accept(v) 438 if !ok { 439 return n, false 440 } 441 n.Expr = node.(ExprNode) 442 for i, val := range n.List { 443 node, ok = val.Accept(v) 444 if !ok { 445 return n, false 446 } 447 n.List[i] = node.(ExprNode) 448 } 449 if n.Sel != nil { 450 node, ok = n.Sel.Accept(v) 451 if !ok { 452 return n, false 453 } 454 n.Sel = node.(ExprNode) 455 } 456 return v.Leave(n) 457 } 458 459 // IsNullExpr is the expression for null check. 460 type IsNullExpr struct { 461 exprNode 462 // Expr is the expression to be checked. 463 Expr ExprNode 464 // Not is true, the expression is "is not null". 465 Not bool 466 } 467 468 // Accept implements Node Accept interface. 469 func (n *IsNullExpr) Accept(v Visitor) (Node, bool) { 470 newNode, skipChildren := v.Enter(n) 471 if skipChildren { 472 return v.Leave(newNode) 473 } 474 n = newNode.(*IsNullExpr) 475 node, ok := n.Expr.Accept(v) 476 if !ok { 477 return n, false 478 } 479 n.Expr = node.(ExprNode) 480 return v.Leave(n) 481 } 482 483 // IsTruthExpr is the expression for true/false check. 484 type IsTruthExpr struct { 485 exprNode 486 // Expr is the expression to be checked. 487 Expr ExprNode 488 // Not is true, the expression is "is not true/false". 489 Not bool 490 // True indicates checking true or false. 491 True int64 492 } 493 494 // Accept implements Node Accept interface. 495 func (n *IsTruthExpr) Accept(v Visitor) (Node, bool) { 496 newNode, skipChildren := v.Enter(n) 497 if skipChildren { 498 return v.Leave(newNode) 499 } 500 n = newNode.(*IsTruthExpr) 501 node, ok := n.Expr.Accept(v) 502 if !ok { 503 return n, false 504 } 505 n.Expr = node.(ExprNode) 506 return v.Leave(n) 507 } 508 509 // PatternLikeExpr is the expression for like operator, e.g, expr like "%123%" 510 type PatternLikeExpr struct { 511 exprNode 512 // Expr is the expression to be checked. 513 Expr ExprNode 514 // Pattern is the like expression. 515 Pattern ExprNode 516 // Not is true, the expression is "not like". 517 Not bool 518 519 Escape byte 520 521 PatChars []byte 522 PatTypes []byte 523 } 524 525 // Accept implements Node Accept interface. 526 func (n *PatternLikeExpr) Accept(v Visitor) (Node, bool) { 527 newNode, skipChildren := v.Enter(n) 528 if skipChildren { 529 return v.Leave(newNode) 530 } 531 n = newNode.(*PatternLikeExpr) 532 if n.Expr != nil { 533 node, ok := n.Expr.Accept(v) 534 if !ok { 535 return n, false 536 } 537 n.Expr = node.(ExprNode) 538 } 539 if n.Pattern != nil { 540 node, ok := n.Pattern.Accept(v) 541 if !ok { 542 return n, false 543 } 544 n.Pattern = node.(ExprNode) 545 } 546 return v.Leave(n) 547 } 548 549 // ParamMarkerExpr expression holds a place for another expression. 550 // Used in parsing prepare statement. 551 type ParamMarkerExpr struct { 552 exprNode 553 Offset int 554 } 555 556 // Accept implements Node Accept interface. 557 func (n *ParamMarkerExpr) Accept(v Visitor) (Node, bool) { 558 newNode, skipChildren := v.Enter(n) 559 if skipChildren { 560 return v.Leave(newNode) 561 } 562 n = newNode.(*ParamMarkerExpr) 563 return v.Leave(n) 564 } 565 566 // ParenthesesExpr is the parentheses expression. 567 type ParenthesesExpr struct { 568 exprNode 569 // Expr is the expression in parentheses. 570 Expr ExprNode 571 } 572 573 // Accept implements Node Accept interface. 574 func (n *ParenthesesExpr) Accept(v Visitor) (Node, bool) { 575 newNode, skipChildren := v.Enter(n) 576 if skipChildren { 577 return v.Leave(newNode) 578 } 579 n = newNode.(*ParenthesesExpr) 580 if n.Expr != nil { 581 node, ok := n.Expr.Accept(v) 582 if !ok { 583 return n, false 584 } 585 n.Expr = node.(ExprNode) 586 } 587 return v.Leave(n) 588 } 589 590 // PositionExpr is the expression for order by and group by position. 591 // MySQL use position expression started from 1, it looks a little confused inner. 592 // maybe later we will use 0 at first. 593 type PositionExpr struct { 594 exprNode 595 // N is the position, started from 1 now. 596 N int 597 // Refer is the result field the position refers to. 598 Refer *ResultField 599 } 600 601 // Accept implements Node Accept interface. 602 func (n *PositionExpr) Accept(v Visitor) (Node, bool) { 603 newNode, skipChildren := v.Enter(n) 604 if skipChildren { 605 return v.Leave(newNode) 606 } 607 n = newNode.(*PositionExpr) 608 return v.Leave(n) 609 } 610 611 // PatternRegexpExpr is the pattern expression for pattern match. 612 type PatternRegexpExpr struct { 613 exprNode 614 // Expr is the expression to be checked. 615 Expr ExprNode 616 // Pattern is the expression for pattern. 617 Pattern ExprNode 618 // Not is true, the expression is "not rlike", 619 Not bool 620 621 // Re is the compiled regexp. 622 Re *regexp.Regexp 623 // Sexpr is the string for Expr expression. 624 Sexpr *string 625 } 626 627 // Accept implements Node Accept interface. 628 func (n *PatternRegexpExpr) Accept(v Visitor) (Node, bool) { 629 newNode, skipChildren := v.Enter(n) 630 if skipChildren { 631 return v.Leave(newNode) 632 } 633 n = newNode.(*PatternRegexpExpr) 634 node, ok := n.Expr.Accept(v) 635 if !ok { 636 return n, false 637 } 638 n.Expr = node.(ExprNode) 639 node, ok = n.Pattern.Accept(v) 640 if !ok { 641 return n, false 642 } 643 n.Pattern = node.(ExprNode) 644 return v.Leave(n) 645 } 646 647 // RowExpr is the expression for row constructor. 648 // See https://dev.mysql.com/doc/refman/5.7/en/row-subqueries.html 649 type RowExpr struct { 650 exprNode 651 652 Values []ExprNode 653 } 654 655 // Accept implements Node Accept interface. 656 func (n *RowExpr) Accept(v Visitor) (Node, bool) { 657 newNode, skipChildren := v.Enter(n) 658 if skipChildren { 659 return v.Leave(newNode) 660 } 661 n = newNode.(*RowExpr) 662 for i, val := range n.Values { 663 node, ok := val.Accept(v) 664 if !ok { 665 return n, false 666 } 667 n.Values[i] = node.(ExprNode) 668 } 669 return v.Leave(n) 670 } 671 672 // UnaryOperationExpr is the expression for unary operator. 673 type UnaryOperationExpr struct { 674 exprNode 675 // Op is the operator opcode. 676 Op opcode.Op 677 // V is the unary expression. 678 V ExprNode 679 } 680 681 // Accept implements Node Accept interface. 682 func (n *UnaryOperationExpr) Accept(v Visitor) (Node, bool) { 683 newNode, skipChildren := v.Enter(n) 684 if skipChildren { 685 return v.Leave(newNode) 686 } 687 n = newNode.(*UnaryOperationExpr) 688 node, ok := n.V.Accept(v) 689 if !ok { 690 return n, false 691 } 692 n.V = node.(ExprNode) 693 return v.Leave(n) 694 } 695 696 // ValuesExpr is the expression used in INSERT VALUES 697 type ValuesExpr struct { 698 exprNode 699 // model.CIStr is column name. 700 Column *ColumnNameExpr 701 } 702 703 // Accept implements Node Accept interface. 704 func (n *ValuesExpr) Accept(v Visitor) (Node, bool) { 705 newNode, skipChildren := v.Enter(n) 706 if skipChildren { 707 return v.Leave(newNode) 708 } 709 n = newNode.(*ValuesExpr) 710 node, ok := n.Column.Accept(v) 711 if !ok { 712 return n, false 713 } 714 n.Column = node.(*ColumnNameExpr) 715 return v.Leave(n) 716 } 717 718 // VariableExpr is the expression for variable. 719 type VariableExpr struct { 720 exprNode 721 // Name is the variable name. 722 Name string 723 // IsGlobal indicates whether this variable is global. 724 IsGlobal bool 725 // IsSystem indicates whether this variable is a system variable in current session. 726 IsSystem bool 727 // Value is the variable value. 728 Value ExprNode 729 } 730 731 // Accept implements Node Accept interface. 732 func (n *VariableExpr) Accept(v Visitor) (Node, bool) { 733 newNode, skipChildren := v.Enter(n) 734 if skipChildren { 735 return v.Leave(newNode) 736 } 737 n = newNode.(*VariableExpr) 738 if n.Value == nil { 739 return v.Leave(n) 740 } 741 742 node, ok := n.Value.Accept(v) 743 if !ok { 744 return n, false 745 } 746 n.Value = node.(ExprNode) 747 return v.Leave(n) 748 }