github.com/XiaoMi/Gaea@v1.2.5/parser/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  	"fmt"
    18  	"io"
    19  	"regexp"
    20  	"strings"
    21  
    22  	"github.com/pingcap/errors"
    23  
    24  	"github.com/XiaoMi/Gaea/parser/format"
    25  	"github.com/XiaoMi/Gaea/parser/model"
    26  	"github.com/XiaoMi/Gaea/parser/opcode"
    27  )
    28  
    29  var (
    30  	_ ExprNode = &BetweenExpr{}
    31  	_ ExprNode = &BinaryOperationExpr{}
    32  	_ ExprNode = &CaseExpr{}
    33  	_ ExprNode = &ColumnNameExpr{}
    34  	_ ExprNode = &CompareSubqueryExpr{}
    35  	_ ExprNode = &DefaultExpr{}
    36  	_ ExprNode = &ExistsSubqueryExpr{}
    37  	_ ExprNode = &IsNullExpr{}
    38  	_ ExprNode = &IsTruthExpr{}
    39  	_ ExprNode = &ParenthesesExpr{}
    40  	_ ExprNode = &PatternInExpr{}
    41  	_ ExprNode = &PatternLikeExpr{}
    42  	_ ExprNode = &PatternRegexpExpr{}
    43  	_ ExprNode = &PositionExpr{}
    44  	_ ExprNode = &RowExpr{}
    45  	_ ExprNode = &SubqueryExpr{}
    46  	_ ExprNode = &UnaryOperationExpr{}
    47  	_ ExprNode = &ValuesExpr{}
    48  	_ ExprNode = &VariableExpr{}
    49  
    50  	_ Node = &ColumnName{}
    51  	_ Node = &WhenClause{}
    52  )
    53  
    54  // ValueExpr define a interface for ValueExpr.
    55  type ValueExpr interface {
    56  	ExprNode
    57  	SetValue(val interface{})
    58  	GetValue() interface{}
    59  	GetDatumString() string
    60  	GetString() string
    61  	GetProjectionOffset() int
    62  	SetProjectionOffset(offset int)
    63  }
    64  
    65  // NewValueExpr creates a ValueExpr with value, and sets default field type.
    66  var NewValueExpr func(interface{}) ValueExpr
    67  
    68  // NewParamMarkerExpr creates a ParamMarkerExpr.
    69  var NewParamMarkerExpr func(offset int) ParamMarkerExpr
    70  
    71  // BetweenExpr is for "between and" or "not between and" expression.
    72  type BetweenExpr struct {
    73  	exprNode
    74  	// Expr is the expression to be checked.
    75  	Expr ExprNode
    76  	// Left is the expression for minimal value in the range.
    77  	Left ExprNode
    78  	// Right is the expression for maximum value in the range.
    79  	Right ExprNode
    80  	// Not is true, the expression is "not between and".
    81  	Not bool
    82  }
    83  
    84  // Restore implements Node interface.
    85  func (n *BetweenExpr) Restore(ctx *format.RestoreCtx) error {
    86  	if err := n.Expr.Restore(ctx); err != nil {
    87  		return errors.Annotate(err, "An error occurred while restore BetweenExpr.Expr")
    88  	}
    89  	if n.Not {
    90  		ctx.WriteKeyWord(" NOT BETWEEN ")
    91  	} else {
    92  		ctx.WriteKeyWord(" BETWEEN ")
    93  	}
    94  	if err := n.Left.Restore(ctx); err != nil {
    95  		return errors.Annotate(err, "An error occurred while restore BetweenExpr.Left")
    96  	}
    97  	ctx.WriteKeyWord(" AND ")
    98  	if err := n.Right.Restore(ctx); err != nil {
    99  		return errors.Annotate(err, "An error occurred while restore BetweenExpr.Right ")
   100  	}
   101  	return nil
   102  }
   103  
   104  // Format the ExprNode into a Writer.
   105  func (n *BetweenExpr) Format(w io.Writer) {
   106  	n.Expr.Format(w)
   107  	if n.Not {
   108  		fmt.Fprint(w, " NOT BETWEEN ")
   109  	} else {
   110  		fmt.Fprint(w, " BETWEEN ")
   111  	}
   112  	n.Left.Format(w)
   113  	fmt.Fprint(w, " AND ")
   114  	n.Right.Format(w)
   115  }
   116  
   117  // Accept implements Node interface.
   118  func (n *BetweenExpr) Accept(v Visitor) (Node, bool) {
   119  	newNode, skipChildren := v.Enter(n)
   120  	if skipChildren {
   121  		return v.Leave(newNode)
   122  	}
   123  
   124  	n = newNode.(*BetweenExpr)
   125  	node, ok := n.Expr.Accept(v)
   126  	if !ok {
   127  		return n, false
   128  	}
   129  	n.Expr = node.(ExprNode)
   130  
   131  	node, ok = n.Left.Accept(v)
   132  	if !ok {
   133  		return n, false
   134  	}
   135  	n.Left = node.(ExprNode)
   136  
   137  	node, ok = n.Right.Accept(v)
   138  	if !ok {
   139  		return n, false
   140  	}
   141  	n.Right = node.(ExprNode)
   142  
   143  	return v.Leave(n)
   144  }
   145  
   146  // BinaryOperationExpr is for binary operation like `1 + 1`, `1 - 1`, etc.
   147  type BinaryOperationExpr struct {
   148  	exprNode
   149  	// Op is the operator code for BinaryOperation.
   150  	Op opcode.Op
   151  	// L is the left expression in BinaryOperation.
   152  	L ExprNode
   153  	// R is the right expression in BinaryOperation.
   154  	R ExprNode
   155  }
   156  
   157  // Restore implements Node interface.
   158  func (n *BinaryOperationExpr) Restore(ctx *format.RestoreCtx) error {
   159  	if err := n.L.Restore(ctx); err != nil {
   160  		return errors.Annotate(err, "An error occurred when restore BinaryOperationExpr.L")
   161  	}
   162  	if err := n.Op.Restore(ctx); err != nil {
   163  		return errors.Annotate(err, "An error occurred when restore BinaryOperationExpr.Op")
   164  	}
   165  	if err := n.R.Restore(ctx); err != nil {
   166  		return errors.Annotate(err, "An error occurred when restore BinaryOperationExpr.R")
   167  	}
   168  
   169  	return nil
   170  }
   171  
   172  // Format the ExprNode into a Writer.
   173  func (n *BinaryOperationExpr) Format(w io.Writer) {
   174  	n.L.Format(w)
   175  	fmt.Fprint(w, " ")
   176  	n.Op.Format(w)
   177  	fmt.Fprint(w, " ")
   178  	n.R.Format(w)
   179  }
   180  
   181  // Accept implements Node interface.
   182  func (n *BinaryOperationExpr) Accept(v Visitor) (Node, bool) {
   183  	newNode, skipChildren := v.Enter(n)
   184  	if skipChildren {
   185  		return v.Leave(newNode)
   186  	}
   187  
   188  	n = newNode.(*BinaryOperationExpr)
   189  	node, ok := n.L.Accept(v)
   190  	if !ok {
   191  		return n, false
   192  	}
   193  	n.L = node.(ExprNode)
   194  
   195  	node, ok = n.R.Accept(v)
   196  	if !ok {
   197  		return n, false
   198  	}
   199  	n.R = node.(ExprNode)
   200  
   201  	return v.Leave(n)
   202  }
   203  
   204  // WhenClause is the when clause in Case expression for "when condition then result".
   205  type WhenClause struct {
   206  	node
   207  	// Expr is the condition expression in WhenClause.
   208  	Expr ExprNode
   209  	// Result is the result expression in WhenClause.
   210  	Result ExprNode
   211  }
   212  
   213  // Restore implements Node interface.
   214  func (n *WhenClause) Restore(ctx *format.RestoreCtx) error {
   215  	ctx.WriteKeyWord("WHEN ")
   216  	if err := n.Expr.Restore(ctx); err != nil {
   217  		return errors.Annotate(err, "An error occurred while restore WhenClauses.Expr")
   218  	}
   219  	ctx.WriteKeyWord(" THEN ")
   220  	if err := n.Result.Restore(ctx); err != nil {
   221  		return errors.Annotate(err, "An error occurred while restore WhenClauses.Result")
   222  	}
   223  	return nil
   224  }
   225  
   226  // Accept implements Node Accept interface.
   227  func (n *WhenClause) Accept(v Visitor) (Node, bool) {
   228  	newNode, skipChildren := v.Enter(n)
   229  	if skipChildren {
   230  		return v.Leave(newNode)
   231  	}
   232  
   233  	n = newNode.(*WhenClause)
   234  	node, ok := n.Expr.Accept(v)
   235  	if !ok {
   236  		return n, false
   237  	}
   238  	n.Expr = node.(ExprNode)
   239  
   240  	node, ok = n.Result.Accept(v)
   241  	if !ok {
   242  		return n, false
   243  	}
   244  	n.Result = node.(ExprNode)
   245  	return v.Leave(n)
   246  }
   247  
   248  // CaseExpr is the case expression.
   249  type CaseExpr struct {
   250  	exprNode
   251  	// Value is the compare value expression.
   252  	Value ExprNode
   253  	// WhenClauses is the condition check expression.
   254  	WhenClauses []*WhenClause
   255  	// ElseClause is the else result expression.
   256  	ElseClause ExprNode
   257  }
   258  
   259  // Restore implements Node interface.
   260  func (n *CaseExpr) Restore(ctx *format.RestoreCtx) error {
   261  	ctx.WriteKeyWord("CASE")
   262  	if n.Value != nil {
   263  		ctx.WritePlain(" ")
   264  		if err := n.Value.Restore(ctx); err != nil {
   265  			return errors.Annotate(err, "An error occurred while restore CaseExpr.Value")
   266  		}
   267  	}
   268  	for _, clause := range n.WhenClauses {
   269  		ctx.WritePlain(" ")
   270  		if err := clause.Restore(ctx); err != nil {
   271  			return errors.Annotate(err, "An error occurred while restore CaseExpr.WhenClauses")
   272  		}
   273  	}
   274  	if n.ElseClause != nil {
   275  		ctx.WriteKeyWord(" ELSE ")
   276  		if err := n.ElseClause.Restore(ctx); err != nil {
   277  			return errors.Annotate(err, "An error occurred while restore CaseExpr.ElseClause")
   278  		}
   279  	}
   280  	ctx.WriteKeyWord(" END")
   281  
   282  	return nil
   283  }
   284  
   285  // Format the ExprNode into a Writer.
   286  func (n *CaseExpr) Format(w io.Writer) {
   287  	fmt.Fprint(w, "CASE ")
   288  	// Because the presence of `case when` syntax, `Value` could be nil and we need check this.
   289  	if n.Value != nil {
   290  		n.Value.Format(w)
   291  		fmt.Fprint(w, " ")
   292  	}
   293  	for _, clause := range n.WhenClauses {
   294  		fmt.Fprint(w, "WHEN ")
   295  		clause.Expr.Format(w)
   296  		fmt.Fprint(w, " THEN ")
   297  		clause.Result.Format(w)
   298  	}
   299  	if n.ElseClause != nil {
   300  		fmt.Fprint(w, " ELSE ")
   301  		n.ElseClause.Format(w)
   302  	}
   303  	fmt.Fprint(w, " END")
   304  }
   305  
   306  // Accept implements Node Accept interface.
   307  func (n *CaseExpr) Accept(v Visitor) (Node, bool) {
   308  	newNode, skipChildren := v.Enter(n)
   309  	if skipChildren {
   310  		return v.Leave(newNode)
   311  	}
   312  
   313  	n = newNode.(*CaseExpr)
   314  	if n.Value != nil {
   315  		node, ok := n.Value.Accept(v)
   316  		if !ok {
   317  			return n, false
   318  		}
   319  		n.Value = node.(ExprNode)
   320  	}
   321  	for i, val := range n.WhenClauses {
   322  		node, ok := val.Accept(v)
   323  		if !ok {
   324  			return n, false
   325  		}
   326  		n.WhenClauses[i] = node.(*WhenClause)
   327  	}
   328  	if n.ElseClause != nil {
   329  		node, ok := n.ElseClause.Accept(v)
   330  		if !ok {
   331  			return n, false
   332  		}
   333  		n.ElseClause = node.(ExprNode)
   334  	}
   335  	return v.Leave(n)
   336  }
   337  
   338  // SubqueryExpr represents a subquery.
   339  type SubqueryExpr struct {
   340  	exprNode
   341  	// Query is the query SelectNode.
   342  	Query      ResultSetNode
   343  	Evaluated  bool
   344  	Correlated bool
   345  	MultiRows  bool
   346  	Exists     bool
   347  }
   348  
   349  // Restore implements Node interface.
   350  func (n *SubqueryExpr) Restore(ctx *format.RestoreCtx) error {
   351  	ctx.WritePlain("(")
   352  	if err := n.Query.Restore(ctx); err != nil {
   353  		return errors.Annotate(err, "An error occurred while restore SubqueryExpr.Query")
   354  	}
   355  	ctx.WritePlain(")")
   356  	return nil
   357  }
   358  
   359  // Format the ExprNode into a Writer.
   360  func (n *SubqueryExpr) Format(w io.Writer) {
   361  	panic("Not implemented")
   362  }
   363  
   364  // Accept implements Node Accept interface.
   365  func (n *SubqueryExpr) Accept(v Visitor) (Node, bool) {
   366  	newNode, skipChildren := v.Enter(n)
   367  	if skipChildren {
   368  		return v.Leave(newNode)
   369  	}
   370  	n = newNode.(*SubqueryExpr)
   371  	node, ok := n.Query.Accept(v)
   372  	if !ok {
   373  		return n, false
   374  	}
   375  	n.Query = node.(ResultSetNode)
   376  	return v.Leave(n)
   377  }
   378  
   379  // CompareSubqueryExpr is the expression for "expr cmp (select ...)".
   380  // See https://dev.mysql.com/doc/refman/5.7/en/comparisons-using-subqueries.html
   381  // See https://dev.mysql.com/doc/refman/5.7/en/any-in-some-subqueries.html
   382  // See https://dev.mysql.com/doc/refman/5.7/en/all-subqueries.html
   383  type CompareSubqueryExpr struct {
   384  	exprNode
   385  	// L is the left expression
   386  	L ExprNode
   387  	// Op is the comparison opcode.
   388  	Op opcode.Op
   389  	// R is the subquery for right expression, may be rewritten to other type of expression.
   390  	R ExprNode
   391  	// All is true, we should compare all records in subquery.
   392  	All bool
   393  }
   394  
   395  // Restore implements Node interface.
   396  func (n *CompareSubqueryExpr) Restore(ctx *format.RestoreCtx) error {
   397  	if err := n.L.Restore(ctx); err != nil {
   398  		return errors.Annotate(err, "An error occurred while restore CompareSubqueryExpr.L")
   399  	}
   400  	if err := n.Op.Restore(ctx); err != nil {
   401  		return errors.Annotate(err, "An error occurred while restore CompareSubqueryExpr.Op")
   402  	}
   403  	if n.All {
   404  		ctx.WriteKeyWord("ALL ")
   405  	} else {
   406  		ctx.WriteKeyWord("ANY ")
   407  	}
   408  	if err := n.R.Restore(ctx); err != nil {
   409  		return errors.Annotate(err, "An error occurred while restore CompareSubqueryExpr.R")
   410  	}
   411  	return nil
   412  }
   413  
   414  // Format the ExprNode into a Writer.
   415  func (n *CompareSubqueryExpr) Format(w io.Writer) {
   416  	panic("Not implemented")
   417  }
   418  
   419  // Accept implements Node Accept interface.
   420  func (n *CompareSubqueryExpr) Accept(v Visitor) (Node, bool) {
   421  	newNode, skipChildren := v.Enter(n)
   422  	if skipChildren {
   423  		return v.Leave(newNode)
   424  	}
   425  	n = newNode.(*CompareSubqueryExpr)
   426  	node, ok := n.L.Accept(v)
   427  	if !ok {
   428  		return n, false
   429  	}
   430  	n.L = node.(ExprNode)
   431  	node, ok = n.R.Accept(v)
   432  	if !ok {
   433  		return n, false
   434  	}
   435  	n.R = node.(ExprNode)
   436  	return v.Leave(n)
   437  }
   438  
   439  // ColumnName represents column name.
   440  type ColumnName struct {
   441  	node
   442  	Schema model.CIStr
   443  	Table  model.CIStr
   444  	Name   model.CIStr
   445  }
   446  
   447  // Restore implements Node interface.
   448  func (n *ColumnName) Restore(ctx *format.RestoreCtx) error {
   449  	if n.Schema.O != "" {
   450  		ctx.WriteName(n.Schema.O)
   451  		ctx.WritePlain(".")
   452  	}
   453  	if n.Table.O != "" {
   454  		ctx.WriteName(n.Table.O)
   455  		ctx.WritePlain(".")
   456  	}
   457  	ctx.WriteName(n.Name.O)
   458  	return nil
   459  }
   460  
   461  // Accept implements Node Accept interface.
   462  func (n *ColumnName) Accept(v Visitor) (Node, bool) {
   463  	newNode, skipChildren := v.Enter(n)
   464  	if skipChildren {
   465  		return v.Leave(newNode)
   466  	}
   467  	n = newNode.(*ColumnName)
   468  	return v.Leave(n)
   469  }
   470  
   471  // String implements Stringer interface.
   472  func (n *ColumnName) String() string {
   473  	result := n.Name.L
   474  	if n.Table.L != "" {
   475  		result = n.Table.L + "." + result
   476  	}
   477  	if n.Schema.L != "" {
   478  		result = n.Schema.L + "." + result
   479  	}
   480  	return result
   481  }
   482  
   483  // OrigColName returns the full original column name.
   484  func (n *ColumnName) OrigColName() (ret string) {
   485  	ret = n.Name.O
   486  	if n.Table.O == "" {
   487  		return
   488  	}
   489  	ret = n.Table.O + "." + ret
   490  	if n.Schema.O == "" {
   491  		return
   492  	}
   493  	ret = n.Schema.O + "." + ret
   494  	return
   495  }
   496  
   497  // ColumnNameExpr represents a column name expression.
   498  type ColumnNameExpr struct {
   499  	exprNode
   500  
   501  	// Name is the referenced column name.
   502  	Name *ColumnName
   503  
   504  	// Refer is the result field the column name refers to.
   505  	// The value of Refer.Expr is used as the value of the expression.
   506  	Refer *ResultField
   507  }
   508  
   509  // Restore implements Node interface.
   510  func (n *ColumnNameExpr) Restore(ctx *format.RestoreCtx) error {
   511  	if err := n.Name.Restore(ctx); err != nil {
   512  		return errors.Trace(err)
   513  	}
   514  	return nil
   515  }
   516  
   517  // Format the ExprNode into a Writer.
   518  func (n *ColumnNameExpr) Format(w io.Writer) {
   519  	name := strings.Replace(n.Name.String(), ".", "`.`", -1)
   520  	fmt.Fprintf(w, "`%s`", name)
   521  }
   522  
   523  // Accept implements Node Accept interface.
   524  func (n *ColumnNameExpr) Accept(v Visitor) (Node, bool) {
   525  	newNode, skipChildren := v.Enter(n)
   526  	if skipChildren {
   527  		return v.Leave(newNode)
   528  	}
   529  	n = newNode.(*ColumnNameExpr)
   530  	node, ok := n.Name.Accept(v)
   531  	if !ok {
   532  		return n, false
   533  	}
   534  	n.Name = node.(*ColumnName)
   535  	return v.Leave(n)
   536  }
   537  
   538  // DefaultExpr is the default expression using default value for a column.
   539  type DefaultExpr struct {
   540  	exprNode
   541  	// Name is the column name.
   542  	Name *ColumnName
   543  }
   544  
   545  // Restore implements Node interface.
   546  func (n *DefaultExpr) Restore(ctx *format.RestoreCtx) error {
   547  	ctx.WriteKeyWord("DEFAULT")
   548  	if n.Name != nil {
   549  		ctx.WritePlain("(")
   550  		if err := n.Name.Restore(ctx); err != nil {
   551  			return errors.Annotate(err, "An error occurred while restore DefaultExpr.Name")
   552  		}
   553  		ctx.WritePlain(")")
   554  	}
   555  	return nil
   556  }
   557  
   558  // Format the ExprNode into a Writer.
   559  func (n *DefaultExpr) Format(w io.Writer) {
   560  	panic("Not implemented")
   561  }
   562  
   563  // Accept implements Node Accept interface.
   564  func (n *DefaultExpr) Accept(v Visitor) (Node, bool) {
   565  	newNode, skipChildren := v.Enter(n)
   566  	if skipChildren {
   567  		return v.Leave(newNode)
   568  	}
   569  	n = newNode.(*DefaultExpr)
   570  	if n.Name != nil {
   571  		node, ok := n.Name.Accept(v)
   572  		if !ok {
   573  			return n, false
   574  		}
   575  		n.Name = node.(*ColumnName)
   576  	}
   577  	return v.Leave(n)
   578  }
   579  
   580  // ExistsSubqueryExpr is the expression for "exists (select ...)".
   581  // See https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
   582  type ExistsSubqueryExpr struct {
   583  	exprNode
   584  	// Sel is the subquery, may be rewritten to other type of expression.
   585  	Sel ExprNode
   586  	// Not is true, the expression is "not exists".
   587  	Not bool
   588  }
   589  
   590  // Restore implements Node interface.
   591  func (n *ExistsSubqueryExpr) Restore(ctx *format.RestoreCtx) error {
   592  	if n.Not {
   593  		ctx.WriteKeyWord("NOT EXISTS ")
   594  	} else {
   595  		ctx.WriteKeyWord("EXISTS ")
   596  	}
   597  	if err := n.Sel.Restore(ctx); err != nil {
   598  		return errors.Annotate(err, "An error occurred while restore ExistsSubqueryExpr.Sel")
   599  	}
   600  	return nil
   601  }
   602  
   603  // Format the ExprNode into a Writer.
   604  func (n *ExistsSubqueryExpr) Format(w io.Writer) {
   605  	panic("Not implemented")
   606  }
   607  
   608  // Accept implements Node Accept interface.
   609  func (n *ExistsSubqueryExpr) Accept(v Visitor) (Node, bool) {
   610  	newNode, skipChildren := v.Enter(n)
   611  	if skipChildren {
   612  		return v.Leave(newNode)
   613  	}
   614  	n = newNode.(*ExistsSubqueryExpr)
   615  	node, ok := n.Sel.Accept(v)
   616  	if !ok {
   617  		return n, false
   618  	}
   619  	n.Sel = node.(ExprNode)
   620  	return v.Leave(n)
   621  }
   622  
   623  // PatternInExpr is the expression for in operator, like "expr in (1, 2, 3)" or "expr in (select c from t)".
   624  type PatternInExpr struct {
   625  	exprNode
   626  	// Expr is the value expression to be compared.
   627  	Expr ExprNode
   628  	// List is the list expression in compare list.
   629  	List []ExprNode
   630  	// Not is true, the expression is "not in".
   631  	Not bool
   632  	// Sel is the subquery, may be rewritten to other type of expression.
   633  	Sel ExprNode
   634  }
   635  
   636  // Restore implements Node interface.
   637  func (n *PatternInExpr) Restore(ctx *format.RestoreCtx) error {
   638  	if err := n.Expr.Restore(ctx); err != nil {
   639  		return errors.Annotate(err, "An error occurred while restore PatternInExpr.Expr")
   640  	}
   641  	if n.Not {
   642  		ctx.WriteKeyWord(" NOT IN ")
   643  	} else {
   644  		ctx.WriteKeyWord(" IN ")
   645  	}
   646  	if n.Sel != nil {
   647  		if err := n.Sel.Restore(ctx); err != nil {
   648  			return errors.Annotate(err, "An error occurred while restore PatternInExpr.Sel")
   649  		}
   650  	} else {
   651  		ctx.WritePlain("(")
   652  		for i, expr := range n.List {
   653  			if i != 0 {
   654  				ctx.WritePlain(",")
   655  			}
   656  			if err := expr.Restore(ctx); err != nil {
   657  				return errors.Annotatef(err, "An error occurred while restore PatternInExpr.List[%d]", i)
   658  			}
   659  		}
   660  		ctx.WritePlain(")")
   661  	}
   662  	return nil
   663  }
   664  
   665  // Format the ExprNode into a Writer.
   666  func (n *PatternInExpr) Format(w io.Writer) {
   667  	n.Expr.Format(w)
   668  	if n.Not {
   669  		fmt.Fprint(w, " NOT IN (")
   670  	} else {
   671  		fmt.Fprint(w, " IN (")
   672  	}
   673  	for i, expr := range n.List {
   674  		if i != 0 {
   675  			fmt.Fprint(w, ",")
   676  		}
   677  		expr.Format(w)
   678  	}
   679  	fmt.Fprint(w, ")")
   680  }
   681  
   682  // Accept implements Node Accept interface.
   683  func (n *PatternInExpr) Accept(v Visitor) (Node, bool) {
   684  	newNode, skipChildren := v.Enter(n)
   685  	if skipChildren {
   686  		return v.Leave(newNode)
   687  	}
   688  	n = newNode.(*PatternInExpr)
   689  	node, ok := n.Expr.Accept(v)
   690  	if !ok {
   691  		return n, false
   692  	}
   693  	n.Expr = node.(ExprNode)
   694  	for i, val := range n.List {
   695  		node, ok = val.Accept(v)
   696  		if !ok {
   697  			return n, false
   698  		}
   699  		n.List[i] = node.(ExprNode)
   700  	}
   701  	if n.Sel != nil {
   702  		node, ok = n.Sel.Accept(v)
   703  		if !ok {
   704  			return n, false
   705  		}
   706  		n.Sel = node.(ExprNode)
   707  	}
   708  	return v.Leave(n)
   709  }
   710  
   711  // IsNullExpr is the expression for null check.
   712  type IsNullExpr struct {
   713  	exprNode
   714  	// Expr is the expression to be checked.
   715  	Expr ExprNode
   716  	// Not is true, the expression is "is not null".
   717  	Not bool
   718  }
   719  
   720  // Restore implements Node interface.
   721  func (n *IsNullExpr) Restore(ctx *format.RestoreCtx) error {
   722  	if err := n.Expr.Restore(ctx); err != nil {
   723  		return errors.Trace(err)
   724  	}
   725  	if n.Not {
   726  		ctx.WriteKeyWord(" IS NOT NULL")
   727  	} else {
   728  		ctx.WriteKeyWord(" IS NULL")
   729  	}
   730  	return nil
   731  }
   732  
   733  // Format the ExprNode into a Writer.
   734  func (n *IsNullExpr) Format(w io.Writer) {
   735  	n.Expr.Format(w)
   736  	if n.Not {
   737  		fmt.Fprint(w, " IS NOT NULL")
   738  		return
   739  	}
   740  	fmt.Fprint(w, " IS NULL")
   741  }
   742  
   743  // Accept implements Node Accept interface.
   744  func (n *IsNullExpr) Accept(v Visitor) (Node, bool) {
   745  	newNode, skipChildren := v.Enter(n)
   746  	if skipChildren {
   747  		return v.Leave(newNode)
   748  	}
   749  	n = newNode.(*IsNullExpr)
   750  	node, ok := n.Expr.Accept(v)
   751  	if !ok {
   752  		return n, false
   753  	}
   754  	n.Expr = node.(ExprNode)
   755  	return v.Leave(n)
   756  }
   757  
   758  // IsTruthExpr is the expression for true/false check.
   759  type IsTruthExpr struct {
   760  	exprNode
   761  	// Expr is the expression to be checked.
   762  	Expr ExprNode
   763  	// Not is true, the expression is "is not true/false".
   764  	Not bool
   765  	// True indicates checking true or false.
   766  	True int64
   767  }
   768  
   769  // Restore implements Node interface.
   770  func (n *IsTruthExpr) Restore(ctx *format.RestoreCtx) error {
   771  	if err := n.Expr.Restore(ctx); err != nil {
   772  		return errors.Trace(err)
   773  	}
   774  	if n.Not {
   775  		ctx.WriteKeyWord(" IS NOT")
   776  	} else {
   777  		ctx.WriteKeyWord(" IS")
   778  	}
   779  	if n.True > 0 {
   780  		ctx.WriteKeyWord(" TRUE")
   781  	} else {
   782  		ctx.WriteKeyWord(" FALSE")
   783  	}
   784  	return nil
   785  }
   786  
   787  // Format the ExprNode into a Writer.
   788  func (n *IsTruthExpr) Format(w io.Writer) {
   789  	n.Expr.Format(w)
   790  	if n.Not {
   791  		fmt.Fprint(w, " IS NOT")
   792  	} else {
   793  		fmt.Fprint(w, " IS")
   794  	}
   795  	if n.True > 0 {
   796  		fmt.Fprint(w, " TRUE")
   797  	} else {
   798  		fmt.Fprint(w, " FALSE")
   799  	}
   800  }
   801  
   802  // Accept implements Node Accept interface.
   803  func (n *IsTruthExpr) Accept(v Visitor) (Node, bool) {
   804  	newNode, skipChildren := v.Enter(n)
   805  	if skipChildren {
   806  		return v.Leave(newNode)
   807  	}
   808  	n = newNode.(*IsTruthExpr)
   809  	node, ok := n.Expr.Accept(v)
   810  	if !ok {
   811  		return n, false
   812  	}
   813  	n.Expr = node.(ExprNode)
   814  	return v.Leave(n)
   815  }
   816  
   817  // PatternLikeExpr is the expression for like operator, e.g, expr like "%123%"
   818  type PatternLikeExpr struct {
   819  	exprNode
   820  	// Expr is the expression to be checked.
   821  	Expr ExprNode
   822  	// Pattern is the like expression.
   823  	Pattern ExprNode
   824  	// Not is true, the expression is "not like".
   825  	Not bool
   826  
   827  	Escape byte
   828  
   829  	PatChars []byte
   830  	PatTypes []byte
   831  }
   832  
   833  // Restore implements Node interface.
   834  func (n *PatternLikeExpr) Restore(ctx *format.RestoreCtx) error {
   835  	if err := n.Expr.Restore(ctx); err != nil {
   836  		return errors.Annotate(err, "An error occurred while restore PatternLikeExpr.Expr")
   837  	}
   838  
   839  	if n.Not {
   840  		ctx.WriteKeyWord(" NOT LIKE ")
   841  	} else {
   842  		ctx.WriteKeyWord(" LIKE ")
   843  	}
   844  
   845  	if err := n.Pattern.Restore(ctx); err != nil {
   846  		return errors.Annotate(err, "An error occurred while restore PatternLikeExpr.Pattern")
   847  	}
   848  
   849  	escape := string(n.Escape)
   850  	if escape != "\\" {
   851  		ctx.WriteKeyWord(" ESCAPE ")
   852  		ctx.WriteString(escape)
   853  
   854  	}
   855  	return nil
   856  }
   857  
   858  // Format the ExprNode into a Writer.
   859  func (n *PatternLikeExpr) Format(w io.Writer) {
   860  	n.Expr.Format(w)
   861  	if n.Not {
   862  		fmt.Fprint(w, " NOT LIKE ")
   863  	} else {
   864  		fmt.Fprint(w, " LIKE ")
   865  	}
   866  	n.Pattern.Format(w)
   867  	if n.Escape != '\\' {
   868  		fmt.Fprint(w, " ESCAPE ")
   869  		fmt.Fprintf(w, "'%c'", n.Escape)
   870  	}
   871  }
   872  
   873  // Accept implements Node Accept interface.
   874  func (n *PatternLikeExpr) Accept(v Visitor) (Node, bool) {
   875  	newNode, skipChildren := v.Enter(n)
   876  	if skipChildren {
   877  		return v.Leave(newNode)
   878  	}
   879  	n = newNode.(*PatternLikeExpr)
   880  	if n.Expr != nil {
   881  		node, ok := n.Expr.Accept(v)
   882  		if !ok {
   883  			return n, false
   884  		}
   885  		n.Expr = node.(ExprNode)
   886  	}
   887  	if n.Pattern != nil {
   888  		node, ok := n.Pattern.Accept(v)
   889  		if !ok {
   890  			return n, false
   891  		}
   892  		n.Pattern = node.(ExprNode)
   893  	}
   894  	return v.Leave(n)
   895  }
   896  
   897  // ParamMarkerExpr expression holds a place for another expression.
   898  // Used in parsing prepare statement.
   899  type ParamMarkerExpr interface {
   900  	ValueExpr
   901  	SetOrder(int)
   902  }
   903  
   904  // ParenthesesExpr is the parentheses expression.
   905  type ParenthesesExpr struct {
   906  	exprNode
   907  	// Expr is the expression in parentheses.
   908  	Expr ExprNode
   909  }
   910  
   911  // Restore implements Node interface.
   912  func (n *ParenthesesExpr) Restore(ctx *format.RestoreCtx) error {
   913  	ctx.WritePlain("(")
   914  	if err := n.Expr.Restore(ctx); err != nil {
   915  		return errors.Annotate(err, "An error occurred when restore ParenthesesExpr.Expr")
   916  	}
   917  	ctx.WritePlain(")")
   918  	return nil
   919  }
   920  
   921  // Format the ExprNode into a Writer.
   922  func (n *ParenthesesExpr) Format(w io.Writer) {
   923  	fmt.Fprint(w, "(")
   924  	n.Expr.Format(w)
   925  	fmt.Fprint(w, ")")
   926  }
   927  
   928  // Accept implements Node Accept interface.
   929  func (n *ParenthesesExpr) Accept(v Visitor) (Node, bool) {
   930  	newNode, skipChildren := v.Enter(n)
   931  	if skipChildren {
   932  		return v.Leave(newNode)
   933  	}
   934  	n = newNode.(*ParenthesesExpr)
   935  	if n.Expr != nil {
   936  		node, ok := n.Expr.Accept(v)
   937  		if !ok {
   938  			return n, false
   939  		}
   940  		n.Expr = node.(ExprNode)
   941  	}
   942  	return v.Leave(n)
   943  }
   944  
   945  // PositionExpr is the expression for order by and group by position.
   946  // MySQL use position expression started from 1, it looks a little confused inner.
   947  // maybe later we will use 0 at first.
   948  type PositionExpr struct {
   949  	exprNode
   950  	// N is the position, started from 1 now.
   951  	N int
   952  	// P is the parameterized position.
   953  	P ExprNode
   954  	// Refer is the result field the position refers to.
   955  	Refer *ResultField
   956  }
   957  
   958  // Restore implements Node interface.
   959  func (n *PositionExpr) Restore(ctx *format.RestoreCtx) error {
   960  	ctx.WritePlainf("%d", n.N)
   961  	return nil
   962  }
   963  
   964  // Format the ExprNode into a Writer.
   965  func (n *PositionExpr) Format(w io.Writer) {
   966  	panic("Not implemented")
   967  }
   968  
   969  // Accept implements Node Accept interface.
   970  func (n *PositionExpr) Accept(v Visitor) (Node, bool) {
   971  	newNode, skipChildren := v.Enter(n)
   972  	if skipChildren {
   973  		return v.Leave(newNode)
   974  	}
   975  	n = newNode.(*PositionExpr)
   976  	if n.P != nil {
   977  		node, ok := n.P.Accept(v)
   978  		if !ok {
   979  			return n, false
   980  		}
   981  		n.P = node.(ExprNode)
   982  	}
   983  	return v.Leave(n)
   984  }
   985  
   986  // PatternRegexpExpr is the pattern expression for pattern match.
   987  type PatternRegexpExpr struct {
   988  	exprNode
   989  	// Expr is the expression to be checked.
   990  	Expr ExprNode
   991  	// Pattern is the expression for pattern.
   992  	Pattern ExprNode
   993  	// Not is true, the expression is "not rlike",
   994  	Not bool
   995  
   996  	// Re is the compiled regexp.
   997  	Re *regexp.Regexp
   998  	// Sexpr is the string for Expr expression.
   999  	Sexpr *string
  1000  }
  1001  
  1002  // Restore implements Node interface.
  1003  func (n *PatternRegexpExpr) Restore(ctx *format.RestoreCtx) error {
  1004  	if err := n.Expr.Restore(ctx); err != nil {
  1005  		return errors.Annotate(err, "An error occurred while restore PatternRegexpExpr.Expr")
  1006  	}
  1007  
  1008  	if n.Not {
  1009  		ctx.WriteKeyWord(" NOT REGEXP ")
  1010  	} else {
  1011  		ctx.WriteKeyWord(" REGEXP ")
  1012  	}
  1013  
  1014  	if err := n.Pattern.Restore(ctx); err != nil {
  1015  		return errors.Annotate(err, "An error occurred while restore PatternRegexpExpr.Pattern")
  1016  	}
  1017  
  1018  	return nil
  1019  }
  1020  
  1021  // Format the ExprNode into a Writer.
  1022  func (n *PatternRegexpExpr) Format(w io.Writer) {
  1023  	n.Expr.Format(w)
  1024  	if n.Not {
  1025  		fmt.Fprint(w, " NOT REGEXP ")
  1026  	} else {
  1027  		fmt.Fprint(w, " REGEXP ")
  1028  	}
  1029  	n.Pattern.Format(w)
  1030  }
  1031  
  1032  // Accept implements Node Accept interface.
  1033  func (n *PatternRegexpExpr) Accept(v Visitor) (Node, bool) {
  1034  	newNode, skipChildren := v.Enter(n)
  1035  	if skipChildren {
  1036  		return v.Leave(newNode)
  1037  	}
  1038  	n = newNode.(*PatternRegexpExpr)
  1039  	node, ok := n.Expr.Accept(v)
  1040  	if !ok {
  1041  		return n, false
  1042  	}
  1043  	n.Expr = node.(ExprNode)
  1044  	node, ok = n.Pattern.Accept(v)
  1045  	if !ok {
  1046  		return n, false
  1047  	}
  1048  	n.Pattern = node.(ExprNode)
  1049  	return v.Leave(n)
  1050  }
  1051  
  1052  // RowExpr is the expression for row constructor.
  1053  // See https://dev.mysql.com/doc/refman/5.7/en/row-subqueries.html
  1054  type RowExpr struct {
  1055  	exprNode
  1056  
  1057  	Values []ExprNode
  1058  }
  1059  
  1060  // Restore implements Node interface.
  1061  func (n *RowExpr) Restore(ctx *format.RestoreCtx) error {
  1062  	ctx.WriteKeyWord("ROW")
  1063  	ctx.WritePlain("(")
  1064  	for i, v := range n.Values {
  1065  		if i != 0 {
  1066  			ctx.WritePlain(",")
  1067  		}
  1068  		if err := v.Restore(ctx); err != nil {
  1069  			return errors.Annotatef(err, "An error occurred when restore RowExpr.Values[%v]", i)
  1070  		}
  1071  	}
  1072  	ctx.WritePlain(")")
  1073  	return nil
  1074  }
  1075  
  1076  // Format the ExprNode into a Writer.
  1077  func (n *RowExpr) Format(w io.Writer) {
  1078  	panic("Not implemented")
  1079  }
  1080  
  1081  // Accept implements Node Accept interface.
  1082  func (n *RowExpr) Accept(v Visitor) (Node, bool) {
  1083  	newNode, skipChildren := v.Enter(n)
  1084  	if skipChildren {
  1085  		return v.Leave(newNode)
  1086  	}
  1087  	n = newNode.(*RowExpr)
  1088  	for i, val := range n.Values {
  1089  		node, ok := val.Accept(v)
  1090  		if !ok {
  1091  			return n, false
  1092  		}
  1093  		n.Values[i] = node.(ExprNode)
  1094  	}
  1095  	return v.Leave(n)
  1096  }
  1097  
  1098  // UnaryOperationExpr is the expression for unary operator.
  1099  type UnaryOperationExpr struct {
  1100  	exprNode
  1101  	// Op is the operator opcode.
  1102  	Op opcode.Op
  1103  	// V is the unary expression.
  1104  	V ExprNode
  1105  }
  1106  
  1107  // Restore implements Node interface.
  1108  func (n *UnaryOperationExpr) Restore(ctx *format.RestoreCtx) error {
  1109  	if err := n.Op.Restore(ctx); err != nil {
  1110  		return errors.Trace(err)
  1111  	}
  1112  	if err := n.V.Restore(ctx); err != nil {
  1113  		return errors.Trace(err)
  1114  	}
  1115  	return nil
  1116  }
  1117  
  1118  // Format the ExprNode into a Writer.
  1119  func (n *UnaryOperationExpr) Format(w io.Writer) {
  1120  	n.Op.Format(w)
  1121  	n.V.Format(w)
  1122  }
  1123  
  1124  // Accept implements Node Accept interface.
  1125  func (n *UnaryOperationExpr) Accept(v Visitor) (Node, bool) {
  1126  	newNode, skipChildren := v.Enter(n)
  1127  	if skipChildren {
  1128  		return v.Leave(newNode)
  1129  	}
  1130  	n = newNode.(*UnaryOperationExpr)
  1131  	node, ok := n.V.Accept(v)
  1132  	if !ok {
  1133  		return n, false
  1134  	}
  1135  	n.V = node.(ExprNode)
  1136  	return v.Leave(n)
  1137  }
  1138  
  1139  // ValuesExpr is the expression used in INSERT VALUES.
  1140  type ValuesExpr struct {
  1141  	exprNode
  1142  	// Column is column name.
  1143  	Column *ColumnNameExpr
  1144  }
  1145  
  1146  // Restore implements Node interface.
  1147  func (n *ValuesExpr) Restore(ctx *format.RestoreCtx) error {
  1148  	ctx.WriteKeyWord("VALUES")
  1149  	ctx.WritePlain("(")
  1150  	if err := n.Column.Restore(ctx); err != nil {
  1151  		return errors.Annotate(err, "An error occurred while restore ValuesExpr.Column")
  1152  	}
  1153  	ctx.WritePlain(")")
  1154  
  1155  	return nil
  1156  }
  1157  
  1158  // Format the ExprNode into a Writer.
  1159  func (n *ValuesExpr) Format(w io.Writer) {
  1160  	panic("Not implemented")
  1161  }
  1162  
  1163  // Accept implements Node Accept interface.
  1164  func (n *ValuesExpr) Accept(v Visitor) (Node, bool) {
  1165  	newNode, skipChildren := v.Enter(n)
  1166  	if skipChildren {
  1167  		return v.Leave(newNode)
  1168  	}
  1169  	n = newNode.(*ValuesExpr)
  1170  	node, ok := n.Column.Accept(v)
  1171  	if !ok {
  1172  		return n, false
  1173  	}
  1174  	// `node` may be *ast.ValueExpr, to avoid panic, we write `ok` but do not use
  1175  	// it.
  1176  	n.Column, ok = node.(*ColumnNameExpr)
  1177  	return v.Leave(n)
  1178  }
  1179  
  1180  // VariableExpr is the expression for variable.
  1181  type VariableExpr struct {
  1182  	exprNode
  1183  	// Name is the variable name.
  1184  	Name string
  1185  	// IsGlobal indicates whether this variable is global.
  1186  	IsGlobal bool
  1187  	// IsSystem indicates whether this variable is a system variable in current session.
  1188  	IsSystem bool
  1189  	// ExplicitScope indicates whether this variable scope is set explicitly.
  1190  	ExplicitScope bool
  1191  	// Value is the variable value.
  1192  	Value ExprNode
  1193  }
  1194  
  1195  // Restore implements Node interface.
  1196  func (n *VariableExpr) Restore(ctx *format.RestoreCtx) error {
  1197  	if n.IsSystem {
  1198  		ctx.WritePlain("@@")
  1199  		if n.ExplicitScope {
  1200  			if n.IsGlobal {
  1201  				ctx.WriteKeyWord("GLOBAL")
  1202  			} else {
  1203  				ctx.WriteKeyWord("SESSION")
  1204  			}
  1205  			ctx.WritePlain(".")
  1206  		}
  1207  	} else {
  1208  		ctx.WritePlain("@")
  1209  	}
  1210  	ctx.WriteName(n.Name)
  1211  
  1212  	if n.Value != nil {
  1213  		ctx.WritePlain(":=")
  1214  		if err := n.Value.Restore(ctx); err != nil {
  1215  			return errors.Annotate(err, "An error occurred while restore VariableExpr.Value")
  1216  		}
  1217  	}
  1218  
  1219  	return nil
  1220  }
  1221  
  1222  // Format the ExprNode into a Writer.
  1223  func (n *VariableExpr) Format(w io.Writer) {
  1224  	panic("Not implemented")
  1225  }
  1226  
  1227  // Accept implements Node Accept interface.
  1228  func (n *VariableExpr) Accept(v Visitor) (Node, bool) {
  1229  	newNode, skipChildren := v.Enter(n)
  1230  	if skipChildren {
  1231  		return v.Leave(newNode)
  1232  	}
  1233  	n = newNode.(*VariableExpr)
  1234  	if n.Value == nil {
  1235  		return v.Leave(n)
  1236  	}
  1237  
  1238  	node, ok := n.Value.Accept(v)
  1239  	if !ok {
  1240  		return n, false
  1241  	}
  1242  	n.Value = node.(ExprNode)
  1243  	return v.Leave(n)
  1244  }
  1245  
  1246  // MaxValueExpr is the expression for "maxvalue" used in partition.
  1247  type MaxValueExpr struct {
  1248  	exprNode
  1249  }
  1250  
  1251  // Restore implements Node interface.
  1252  func (n *MaxValueExpr) Restore(ctx *format.RestoreCtx) error {
  1253  	ctx.WriteKeyWord("MAXVALUE")
  1254  	return nil
  1255  }
  1256  
  1257  // Format the ExprNode into a Writer.
  1258  func (n *MaxValueExpr) Format(w io.Writer) {
  1259  	fmt.Fprint(w, "MAXVALUE")
  1260  }
  1261  
  1262  // Accept implements Node Accept interface.
  1263  func (n *MaxValueExpr) Accept(v Visitor) (Node, bool) {
  1264  	newNode, skipChildren := v.Enter(n)
  1265  	if skipChildren {
  1266  		return v.Leave(newNode)
  1267  	}
  1268  	return v.Leave(n)
  1269  }