github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/parser/ast/expr.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     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 ast
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/pingcap/errors"
    22  	"github.com/vescale/zgraph/datum"
    23  	"github.com/vescale/zgraph/parser/format"
    24  	"github.com/vescale/zgraph/parser/model"
    25  	"github.com/vescale/zgraph/parser/opcode"
    26  	"github.com/vescale/zgraph/types"
    27  )
    28  
    29  var (
    30  	_ ExprNode = &ValueExpr{}
    31  	_ ExprNode = &VariableReference{}
    32  	_ ExprNode = &PropertyAccess{}
    33  	_ ExprNode = &BindVariable{}
    34  	_ ExprNode = &UnaryExpr{}
    35  	_ ExprNode = &BinaryExpr{}
    36  	_ ExprNode = &ParenthesesExpr{}
    37  	_ ExprNode = &FuncCallExpr{}
    38  	_ ExprNode = &SubstrFuncExpr{}
    39  	_ ExprNode = &AggregateFuncExpr{}
    40  	_ ExprNode = &ExtractFuncExpr{}
    41  	_ ExprNode = &IsNullExpr{}
    42  	_ ExprNode = &CastFuncExpr{}
    43  	_ ExprNode = &CaseExpr{}
    44  	_ ExprNode = &PatternInExpr{}
    45  	_ ExprNode = &SubqueryExpr{}
    46  	_ ExprNode = &ExistsSubqueryExpr{}
    47  
    48  	_ Node = &WhenClause{}
    49  )
    50  
    51  // ValueExpr is the simple value expression.
    52  type ValueExpr struct {
    53  	exprNode
    54  
    55  	datum.Datum
    56  }
    57  
    58  // NewValueExpr creates a ValueExpr with value, and sets default field type.
    59  func NewValueExpr(lit interface{}) *ValueExpr {
    60  	if d, ok := lit.(datum.Datum); ok {
    61  		return &ValueExpr{
    62  			Datum: d,
    63  		}
    64  	}
    65  	var d datum.Datum
    66  	switch v := lit.(type) {
    67  	case int:
    68  		d = datum.NewInt(int64(v))
    69  	case int64:
    70  		d = datum.NewInt(v)
    71  	case uint64:
    72  		d = datum.NewInt(int64(v))
    73  	case float64:
    74  		d = datum.NewFloat(v)
    75  	case bool:
    76  		d = datum.NewBool(v)
    77  	case string:
    78  		d = datum.NewString(v)
    79  	default:
    80  		panic(fmt.Sprintf("unknown literal type %T", v))
    81  	}
    82  	return &ValueExpr{
    83  		Datum: d,
    84  	}
    85  }
    86  
    87  // Restore implements Node interface.
    88  func (n *ValueExpr) Restore(ctx *format.RestoreCtx) error {
    89  	str := n.Datum.String()
    90  	switch n.Type() {
    91  	case types.Bool, types.Int, types.Float, types.Decimal:
    92  		ctx.WritePlain(str)
    93  	case types.String:
    94  		ctx.WriteString(str)
    95  	case types.Bytes:
    96  		ctx.WritePlain(fmt.Sprintf("X'%X'", str))
    97  	case types.Date:
    98  		ctx.WriteKeyWord("DATE ")
    99  		ctx.WriteString(str)
   100  	case types.Time, types.TimeTZ:
   101  		ctx.WriteKeyWord("TIME ")
   102  		ctx.WriteString(str)
   103  	case types.Timestamp, types.TimestampTZ:
   104  		ctx.WriteKeyWord("TIMESTAMP ")
   105  		ctx.WriteString(str)
   106  	case types.Interval:
   107  		ctx.WriteKeyWord("INTERVAL ")
   108  		ctx.WritePlain(str)
   109  	default:
   110  		return fmt.Errorf("unexpected datum type %s in ValueExpr", n.Type())
   111  	}
   112  	return nil
   113  }
   114  
   115  // Accept implements Node interface.
   116  func (n *ValueExpr) Accept(v Visitor) (Node, bool) {
   117  	newNode, skipChildren := v.Enter(n)
   118  	if skipChildren {
   119  		return v.Leave(newNode)
   120  	}
   121  	n = newNode.(*ValueExpr)
   122  	return v.Leave(n)
   123  }
   124  
   125  type VariableReference struct {
   126  	exprNode
   127  
   128  	VariableName model.CIStr
   129  }
   130  
   131  func (n *VariableReference) Restore(ctx *format.RestoreCtx) error {
   132  	ctx.WriteName(n.VariableName.O)
   133  	return nil
   134  }
   135  
   136  func (n *VariableReference) Accept(v Visitor) (node Node, ok bool) {
   137  	newNode, _ := v.Enter(n)
   138  	return v.Leave(newNode)
   139  }
   140  
   141  type PropertyAccess struct {
   142  	exprNode
   143  
   144  	VariableName model.CIStr
   145  	PropertyName model.CIStr
   146  }
   147  
   148  func (n *PropertyAccess) Restore(ctx *format.RestoreCtx) error {
   149  	ctx.WriteName(n.VariableName.O)
   150  	ctx.WritePlain(".")
   151  	ctx.WriteName(n.PropertyName.O)
   152  	return nil
   153  }
   154  
   155  func (n *PropertyAccess) Accept(v Visitor) (node Node, ok bool) {
   156  	newNode, _ := v.Enter(n)
   157  	return v.Leave(newNode)
   158  }
   159  
   160  type BindVariable struct {
   161  	exprNode
   162  }
   163  
   164  func (n *BindVariable) Restore(ctx *format.RestoreCtx) error {
   165  	ctx.WritePlain("?")
   166  	return nil
   167  }
   168  
   169  func (n *BindVariable) Accept(v Visitor) (node Node, ok bool) {
   170  	newNode, _ := v.Enter(n)
   171  	return v.Leave(newNode)
   172  }
   173  
   174  // UnaryExpr is the expression for unary operator.
   175  type UnaryExpr struct {
   176  	exprNode
   177  	// Op is the operator opcode.
   178  	Op opcode.Op
   179  	// V is the unary expression.
   180  	V ExprNode
   181  }
   182  
   183  // Restore implements Node interface.
   184  func (n *UnaryExpr) Restore(ctx *format.RestoreCtx) error {
   185  	if err := n.Op.Restore(ctx); err != nil {
   186  		return errors.Trace(err)
   187  	}
   188  	if err := n.V.Restore(ctx); err != nil {
   189  		return errors.Trace(err)
   190  	}
   191  	return nil
   192  }
   193  
   194  // Accept implements Node Accept interface.
   195  func (n *UnaryExpr) Accept(v Visitor) (Node, bool) {
   196  	newNode, skipChildren := v.Enter(n)
   197  	if skipChildren {
   198  		return v.Leave(newNode)
   199  	}
   200  	n = newNode.(*UnaryExpr)
   201  	node, ok := n.V.Accept(v)
   202  	if !ok {
   203  		return n, false
   204  	}
   205  	n.V = node.(ExprNode)
   206  	return v.Leave(n)
   207  }
   208  
   209  // BinaryExpr is for binary operation like `1 + 1`, `1 - 1`, etc.
   210  type BinaryExpr struct {
   211  	exprNode
   212  	// Op is the operator code for BinaryOperation.
   213  	Op opcode.Op
   214  	// L is the left expression in BinaryOperation.
   215  	L ExprNode
   216  	// R is the right expression in BinaryOperation.
   217  	R ExprNode
   218  }
   219  
   220  // Restore implements Node interface.
   221  func (n *BinaryExpr) Restore(ctx *format.RestoreCtx) error {
   222  	if ctx.Flags.HasRestoreBracketAroundBinaryOperation() {
   223  		ctx.WritePlain("(")
   224  	}
   225  	if err := n.L.Restore(ctx); err != nil {
   226  		return errors.Annotate(err, "An error occurred when restore BinaryExpr.L")
   227  	}
   228  	if err := restoreBinaryOpWithSpacesAround(ctx, n.Op); err != nil {
   229  		return errors.Annotate(err, "An error occurred when restore BinaryExpr.Op")
   230  	}
   231  	if err := n.R.Restore(ctx); err != nil {
   232  		return errors.Annotate(err, "An error occurred when restore BinaryExpr.R")
   233  	}
   234  	if ctx.Flags.HasRestoreBracketAroundBinaryOperation() {
   235  		ctx.WritePlain(")")
   236  	}
   237  	return nil
   238  }
   239  
   240  func restoreBinaryOpWithSpacesAround(ctx *format.RestoreCtx, op opcode.Op) error {
   241  	shouldInsertSpace := ctx.Flags.HasSpacesAroundBinaryOperationFlag() || op.IsKeyword()
   242  	if shouldInsertSpace {
   243  		ctx.WritePlain(" ")
   244  	}
   245  	if err := op.Restore(ctx); err != nil {
   246  		return err // no need to annotate, the caller will annotate.
   247  	}
   248  	if shouldInsertSpace {
   249  		ctx.WritePlain(" ")
   250  	}
   251  	return nil
   252  }
   253  
   254  // Accept implements Node interface.
   255  func (n *BinaryExpr) Accept(v Visitor) (Node, bool) {
   256  	newNode, skipChildren := v.Enter(n)
   257  	if skipChildren {
   258  		return v.Leave(newNode)
   259  	}
   260  
   261  	n = newNode.(*BinaryExpr)
   262  	node, ok := n.L.Accept(v)
   263  	if !ok {
   264  		return n, false
   265  	}
   266  	n.L = node.(ExprNode)
   267  
   268  	node, ok = n.R.Accept(v)
   269  	if !ok {
   270  		return n, false
   271  	}
   272  	n.R = node.(ExprNode)
   273  
   274  	return v.Leave(n)
   275  }
   276  
   277  // ParenthesesExpr is the parentheses expression.
   278  type ParenthesesExpr struct {
   279  	exprNode
   280  	// Expr is the expression in parentheses.
   281  	Expr ExprNode
   282  }
   283  
   284  // Restore implements Node interface.
   285  func (n *ParenthesesExpr) Restore(ctx *format.RestoreCtx) error {
   286  	ctx.WritePlain("(")
   287  	if err := n.Expr.Restore(ctx); err != nil {
   288  		return errors.Annotate(err, "An error occurred when restore ParenthesesExpr.Expr")
   289  	}
   290  	ctx.WritePlain(")")
   291  	return nil
   292  }
   293  
   294  // Accept implements Node Accept interface.
   295  func (n *ParenthesesExpr) Accept(v Visitor) (Node, bool) {
   296  	newNode, skipChildren := v.Enter(n)
   297  	if skipChildren {
   298  		return v.Leave(newNode)
   299  	}
   300  	n = newNode.(*ParenthesesExpr)
   301  	if n.Expr != nil {
   302  		node, ok := n.Expr.Accept(v)
   303  		if !ok {
   304  			return n, false
   305  		}
   306  		n.Expr = node.(ExprNode)
   307  	}
   308  	return v.Leave(n)
   309  }
   310  
   311  // FuncCallExpr is for function expression.
   312  type FuncCallExpr struct {
   313  	exprNode
   314  
   315  	// FnName is the function name.
   316  	FnName model.CIStr
   317  	// Args is the function args.
   318  	Args []ExprNode
   319  }
   320  
   321  // Restore implements Node interface.
   322  func (n *FuncCallExpr) Restore(ctx *format.RestoreCtx) error {
   323  	ctx.WriteKeyWord(n.FnName.O)
   324  	ctx.WritePlain("(")
   325  	for i, argv := range n.Args {
   326  		if i != 0 {
   327  			ctx.WritePlain(", ")
   328  		}
   329  		if err := argv.Restore(ctx); err != nil {
   330  			return errors.Annotatef(err, "An error occurred while restore FuncCallExpr.Args %d", i)
   331  		}
   332  	}
   333  	ctx.WritePlain(")")
   334  	return nil
   335  }
   336  
   337  // Accept implements Node interface.
   338  func (n *FuncCallExpr) Accept(v Visitor) (Node, bool) {
   339  	newNode, skipChildren := v.Enter(n)
   340  	if skipChildren {
   341  		return v.Leave(newNode)
   342  	}
   343  	n = newNode.(*FuncCallExpr)
   344  	for i, val := range n.Args {
   345  		node, ok := val.Accept(v)
   346  		if !ok {
   347  			return n, false
   348  		}
   349  		n.Args[i] = node.(ExprNode)
   350  	}
   351  	return v.Leave(n)
   352  }
   353  
   354  // SubstrFuncExpr is for function expression.
   355  type SubstrFuncExpr struct {
   356  	exprNode
   357  
   358  	Expr  ExprNode
   359  	Start ExprNode
   360  	For   ExprNode
   361  }
   362  
   363  func (s *SubstrFuncExpr) Restore(ctx *format.RestoreCtx) error {
   364  	//TODO implement me
   365  	panic("implement me")
   366  }
   367  
   368  func (s *SubstrFuncExpr) Accept(v Visitor) (node Node, ok bool) {
   369  	//TODO implement me
   370  	panic("implement me")
   371  }
   372  
   373  // AggregateFuncExpr represents aggregate function expression.
   374  type AggregateFuncExpr struct {
   375  	exprNode
   376  	// F is the function name.
   377  	F string
   378  	// Args is the function args.
   379  	Args []ExprNode
   380  	// Distinct is true, function hence only aggregate distinct values.
   381  	// For example, column c1 values are "1", "2", "2",  "sum(c1)" is "5",
   382  	// but "sum(distinct c1)" is "3".
   383  	Distinct bool
   384  }
   385  
   386  // Restore implements Node interface.
   387  func (n *AggregateFuncExpr) Restore(ctx *format.RestoreCtx) error {
   388  	ctx.WriteKeyWord(n.F)
   389  	ctx.WritePlain("(")
   390  	if n.Distinct {
   391  		ctx.WriteKeyWord("DISTINCT ")
   392  	}
   393  	switch strings.ToLower(n.F) {
   394  	case "listagg":
   395  		for i := 0; i < len(n.Args)-1; i++ {
   396  			if i != 0 {
   397  				ctx.WritePlain(", ")
   398  			}
   399  			if err := n.Args[i].Restore(ctx); err != nil {
   400  				return errors.Annotatef(err, "An error occurred while restore AggregateFuncExpr.Args[%d]", i)
   401  			}
   402  		}
   403  		ctx.WritePlain(", ")
   404  		if err := n.Args[len(n.Args)-1].Restore(ctx); err != nil {
   405  			return errors.Annotate(err, "An error occurred while restore AggregateFuncExpr.Args SEPARATOR")
   406  		}
   407  	default:
   408  		for i, argv := range n.Args {
   409  			if i != 0 {
   410  				ctx.WritePlain(", ")
   411  			}
   412  			if err := argv.Restore(ctx); err != nil {
   413  				return errors.Annotatef(err, "An error occurred while restore AggregateFuncExpr.Args[%d]", i)
   414  			}
   415  		}
   416  	}
   417  	ctx.WritePlain(")")
   418  	return nil
   419  }
   420  
   421  // Accept implements Node Accept interface.
   422  func (n *AggregateFuncExpr) Accept(v Visitor) (Node, bool) {
   423  	newNode, skipChildren := v.Enter(n)
   424  	if skipChildren {
   425  		return v.Leave(newNode)
   426  	}
   427  	n = newNode.(*AggregateFuncExpr)
   428  	for i, val := range n.Args {
   429  		node, ok := val.Accept(v)
   430  		if !ok {
   431  			return n, false
   432  		}
   433  		n.Args[i] = node.(ExprNode)
   434  	}
   435  
   436  	return v.Leave(n)
   437  }
   438  
   439  type ExtractField byte
   440  
   441  const (
   442  	ExtractFieldYear ExtractField = iota
   443  	ExtractFieldMonth
   444  	ExtractFieldDay
   445  	ExtractFieldHour
   446  	ExtractFieldMinute
   447  	ExtractFieldSecond
   448  	ExtractFieldTimezoneHour
   449  	ExtractFieldTimezoneMinute
   450  )
   451  
   452  // ExtractFuncExpr is for function expression.
   453  type ExtractFuncExpr struct {
   454  	exprNode
   455  
   456  	ExtractField ExtractField
   457  	Expr         ExprNode
   458  }
   459  
   460  func (e *ExtractFuncExpr) Restore(ctx *format.RestoreCtx) error {
   461  	//TODO implement me
   462  	panic("implement me")
   463  }
   464  
   465  func (e *ExtractFuncExpr) Accept(v Visitor) (node Node, ok bool) {
   466  	//TODO implement me
   467  	panic("implement me")
   468  }
   469  
   470  // IsNullExpr is the expression for null check.
   471  type IsNullExpr struct {
   472  	exprNode
   473  	// Expr is the expression to be checked.
   474  	Expr ExprNode
   475  	// Not is true, the expression is "is not null".
   476  	Not bool
   477  }
   478  
   479  // Restore implements Node interface.
   480  func (n *IsNullExpr) Restore(ctx *format.RestoreCtx) error {
   481  	if err := n.Expr.Restore(ctx); err != nil {
   482  		return errors.Trace(err)
   483  	}
   484  	if n.Not {
   485  		ctx.WriteKeyWord(" IS NOT NULL")
   486  	} else {
   487  		ctx.WriteKeyWord(" IS NULL")
   488  	}
   489  	return nil
   490  }
   491  
   492  // Accept implements Node Accept interface.
   493  func (n *IsNullExpr) Accept(v Visitor) (Node, bool) {
   494  	newNode, skipChildren := v.Enter(n)
   495  	if skipChildren {
   496  		return v.Leave(newNode)
   497  	}
   498  	n = newNode.(*IsNullExpr)
   499  	node, ok := n.Expr.Accept(v)
   500  	if !ok {
   501  		return n, false
   502  	}
   503  	n.Expr = node.(ExprNode)
   504  	return v.Leave(n)
   505  }
   506  
   507  type DataType uint8
   508  
   509  const (
   510  	DataTypeString DataType = iota
   511  	DataTypeBoolean
   512  	DataTypeInteger
   513  	DataTypeFloat
   514  	DataTypeDouble
   515  	DataTypeDecimal
   516  	DataTypeDate
   517  	DataTypeTime
   518  	DataTypeTimeWithTimeZone
   519  	DataTypeTimestamp
   520  	DataTypeTimestampWithTimeZone
   521  )
   522  
   523  func (f DataType) String() string {
   524  	switch f {
   525  	case DataTypeString:
   526  		return "STRING"
   527  	case DataTypeBoolean:
   528  		return "BOOLEAN"
   529  	case DataTypeInteger:
   530  		return "INTEGER"
   531  	case DataTypeFloat:
   532  		return "FLOAT"
   533  	case DataTypeDouble:
   534  		return "DOUBLE"
   535  	case DataTypeDecimal:
   536  		return "DECIMAL"
   537  	case DataTypeDate:
   538  		return "DATE"
   539  	case DataTypeTime:
   540  		return "TIME"
   541  	case DataTypeTimeWithTimeZone:
   542  		return "TIME WITH TIME ZONE"
   543  	case DataTypeTimestamp:
   544  		return "TIMESTAMP"
   545  	case DataTypeTimestampWithTimeZone:
   546  		return "TIMESTAMP WITH TIME ZONE"
   547  	default:
   548  		return fmt.Sprintf("UNKNOWN<%d>", f)
   549  	}
   550  }
   551  
   552  type CastFuncExpr struct {
   553  	exprNode
   554  	// Expr is the expression to be converted.
   555  	Expr ExprNode
   556  	// DataType is the conversion type.
   557  	DataType DataType
   558  }
   559  
   560  // Restore implements Node interface.
   561  func (n *CastFuncExpr) Restore(ctx *format.RestoreCtx) error {
   562  	ctx.WriteKeyWord("CAST")
   563  	ctx.WritePlain("(")
   564  	if err := n.Expr.Restore(ctx); err != nil {
   565  		return errors.Annotatef(err, "An error occurred while restore CastFuncExpr.Expr")
   566  	}
   567  	ctx.WriteKeyWord(" AS ")
   568  	ctx.WriteKeyWord(n.DataType.String())
   569  	ctx.WritePlain(")")
   570  	return nil
   571  }
   572  
   573  // Accept implements Node Accept interface.
   574  func (n *CastFuncExpr) Accept(v Visitor) (Node, bool) {
   575  	newNode, skipChildren := v.Enter(n)
   576  	if skipChildren {
   577  		return v.Leave(newNode)
   578  	}
   579  	n = newNode.(*CastFuncExpr)
   580  	node, ok := n.Expr.Accept(v)
   581  	if !ok {
   582  		return n, false
   583  	}
   584  	n.Expr = node.(ExprNode)
   585  	return v.Leave(n)
   586  }
   587  
   588  // CaseExpr is the case expression.
   589  type CaseExpr struct {
   590  	exprNode
   591  	// Value is the compare value expression.
   592  	Value ExprNode
   593  	// WhenClauses is the condition check expression.
   594  	WhenClauses []*WhenClause
   595  	// ElseClause is the else result expression.
   596  	ElseClause ExprNode
   597  }
   598  
   599  // Restore implements Node interface.
   600  func (n *CaseExpr) Restore(ctx *format.RestoreCtx) error {
   601  	ctx.WriteKeyWord("CASE")
   602  	if n.Value != nil {
   603  		ctx.WritePlain(" ")
   604  		if err := n.Value.Restore(ctx); err != nil {
   605  			return errors.Annotate(err, "An error occurred while restore CaseExpr.Value")
   606  		}
   607  	}
   608  	for _, clause := range n.WhenClauses {
   609  		ctx.WritePlain(" ")
   610  		if err := clause.Restore(ctx); err != nil {
   611  			return errors.Annotate(err, "An error occurred while restore CaseExpr.WhenClauses")
   612  		}
   613  	}
   614  	if n.ElseClause != nil {
   615  		ctx.WriteKeyWord(" ELSE ")
   616  		if err := n.ElseClause.Restore(ctx); err != nil {
   617  			return errors.Annotate(err, "An error occurred while restore CaseExpr.ElseClause")
   618  		}
   619  	}
   620  	ctx.WriteKeyWord(" END")
   621  
   622  	return nil
   623  }
   624  
   625  // Accept implements Node Accept interface.
   626  func (n *CaseExpr) Accept(v Visitor) (Node, bool) {
   627  	newNode, skipChildren := v.Enter(n)
   628  	if skipChildren {
   629  		return v.Leave(newNode)
   630  	}
   631  
   632  	n = newNode.(*CaseExpr)
   633  	if n.Value != nil {
   634  		node, ok := n.Value.Accept(v)
   635  		if !ok {
   636  			return n, false
   637  		}
   638  		n.Value = node.(ExprNode)
   639  	}
   640  	for i, val := range n.WhenClauses {
   641  		node, ok := val.Accept(v)
   642  		if !ok {
   643  			return n, false
   644  		}
   645  		n.WhenClauses[i] = node.(*WhenClause)
   646  	}
   647  	if n.ElseClause != nil {
   648  		node, ok := n.ElseClause.Accept(v)
   649  		if !ok {
   650  			return n, false
   651  		}
   652  		n.ElseClause = node.(ExprNode)
   653  	}
   654  	return v.Leave(n)
   655  }
   656  
   657  // WhenClause is the when clause in Case expression for "when condition then result".
   658  type WhenClause struct {
   659  	node
   660  	// Expr is the condition expression in WhenClause.
   661  	Expr ExprNode
   662  	// Result is the result expression in WhenClause.
   663  	Result ExprNode
   664  }
   665  
   666  // Restore implements Node interface.
   667  func (n *WhenClause) Restore(ctx *format.RestoreCtx) error {
   668  	ctx.WriteKeyWord("WHEN ")
   669  	if err := n.Expr.Restore(ctx); err != nil {
   670  		return errors.Annotate(err, "An error occurred while restore WhenClauses.Expr")
   671  	}
   672  	ctx.WriteKeyWord(" THEN ")
   673  	if err := n.Result.Restore(ctx); err != nil {
   674  		return errors.Annotate(err, "An error occurred while restore WhenClauses.Result")
   675  	}
   676  	return nil
   677  }
   678  
   679  // Accept implements Node Accept interface.
   680  func (n *WhenClause) Accept(v Visitor) (Node, bool) {
   681  	newNode, skipChildren := v.Enter(n)
   682  	if skipChildren {
   683  		return v.Leave(newNode)
   684  	}
   685  
   686  	n = newNode.(*WhenClause)
   687  	node, ok := n.Expr.Accept(v)
   688  	if !ok {
   689  		return n, false
   690  	}
   691  	n.Expr = node.(ExprNode)
   692  
   693  	node, ok = n.Result.Accept(v)
   694  	if !ok {
   695  		return n, false
   696  	}
   697  	n.Result = node.(ExprNode)
   698  	return v.Leave(n)
   699  }
   700  
   701  // PatternInExpr is the expression for in operator, like "expr in (1, 2, 3)" or "expr in (select c from t)".
   702  type PatternInExpr struct {
   703  	exprNode
   704  	// Expr is the value expression to be compared.
   705  	Expr ExprNode
   706  	// List is the list expression in compare list.
   707  	List []ExprNode
   708  	// Not is true, the expression is "not in".
   709  	Not bool
   710  }
   711  
   712  // Restore implements Node interface.
   713  func (n *PatternInExpr) Restore(ctx *format.RestoreCtx) error {
   714  	if err := n.Expr.Restore(ctx); err != nil {
   715  		return errors.Annotate(err, "An error occurred while restore PatternInExpr.Expr")
   716  	}
   717  	if n.Not {
   718  		ctx.WriteKeyWord(" NOT IN ")
   719  	} else {
   720  		ctx.WriteKeyWord(" IN ")
   721  	}
   722  
   723  	ctx.WritePlain("(")
   724  	for i, expr := range n.List {
   725  		if i != 0 {
   726  			ctx.WritePlain(",")
   727  		}
   728  		if err := expr.Restore(ctx); err != nil {
   729  			return errors.Annotatef(err, "An error occurred while restore PatternInExpr.List[%d]", i)
   730  		}
   731  	}
   732  	ctx.WritePlain(")")
   733  	return nil
   734  }
   735  
   736  // Accept implements Node Accept interface.
   737  func (n *PatternInExpr) Accept(v Visitor) (Node, bool) {
   738  	newNode, skipChildren := v.Enter(n)
   739  	if skipChildren {
   740  		return v.Leave(newNode)
   741  	}
   742  	n = newNode.(*PatternInExpr)
   743  	node, ok := n.Expr.Accept(v)
   744  	if !ok {
   745  		return n, false
   746  	}
   747  	n.Expr = node.(ExprNode)
   748  	for i, val := range n.List {
   749  		node, ok = val.Accept(v)
   750  		if !ok {
   751  			return n, false
   752  		}
   753  		n.List[i] = node.(ExprNode)
   754  	}
   755  	return v.Leave(n)
   756  }
   757  
   758  // SubqueryExpr represents a subquery.
   759  type SubqueryExpr struct {
   760  	exprNode
   761  	// Query is the query SelectNode.
   762  	Query      *SelectStmt
   763  	Evaluated  bool
   764  	Correlated bool
   765  	MultiRows  bool
   766  	Exists     bool
   767  }
   768  
   769  func (*SubqueryExpr) resultSet() {}
   770  
   771  // Restore implements Node interface.
   772  func (n *SubqueryExpr) Restore(ctx *format.RestoreCtx) error {
   773  	ctx.WritePlain("(")
   774  	if err := n.Query.Restore(ctx); err != nil {
   775  		return errors.Annotate(err, "An error occurred while restore SubqueryExpr.Query")
   776  	}
   777  	ctx.WritePlain(")")
   778  	return nil
   779  }
   780  
   781  // Accept implements Node Accept interface.
   782  func (n *SubqueryExpr) Accept(v Visitor) (Node, bool) {
   783  	newNode, skipChildren := v.Enter(n)
   784  	if skipChildren {
   785  		return v.Leave(newNode)
   786  	}
   787  	n = newNode.(*SubqueryExpr)
   788  	//node, ok := n.Query.Accept(v)
   789  	//if !ok {
   790  	//	return n, false
   791  	//}
   792  	//n.Query = node.(ResultSetNode)
   793  	return v.Leave(n)
   794  }
   795  
   796  // ExistsSubqueryExpr is the expression for "exists (select ...)".
   797  // See https://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
   798  type ExistsSubqueryExpr struct {
   799  	exprNode
   800  	// Sel is the subquery, may be rewritten to other type of expression.
   801  	Sel ExprNode
   802  	// Not is true, the expression is "not exists".
   803  	Not bool
   804  }
   805  
   806  // Restore implements Node interface.
   807  func (n *ExistsSubqueryExpr) Restore(ctx *format.RestoreCtx) error {
   808  	if n.Not {
   809  		ctx.WriteKeyWord("NOT EXISTS ")
   810  	} else {
   811  		ctx.WriteKeyWord("EXISTS ")
   812  	}
   813  	if err := n.Sel.Restore(ctx); err != nil {
   814  		return errors.Annotate(err, "An error occurred while restore ExistsSubqueryExpr.Sel")
   815  	}
   816  	return nil
   817  }
   818  
   819  // Accept implements Node Accept interface.
   820  func (n *ExistsSubqueryExpr) Accept(v Visitor) (Node, bool) {
   821  	newNode, skipChildren := v.Enter(n)
   822  	if skipChildren {
   823  		return v.Leave(newNode)
   824  	}
   825  	n = newNode.(*ExistsSubqueryExpr)
   826  	node, ok := n.Sel.Accept(v)
   827  	if !ok {
   828  		return n, false
   829  	}
   830  	n.Sel = node.(ExprNode)
   831  	return v.Leave(n)
   832  }