github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/sqlparse/tidbparser/ast/dml.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  	"github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/model"
    18  	"github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/mysql"
    19  	"github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/util/auth"
    20  )
    21  
    22  var (
    23  	_ DMLNode = &DeleteStmt{}
    24  	_ DMLNode = &InsertStmt{}
    25  	_ DMLNode = &UnionStmt{}
    26  	_ DMLNode = &UpdateStmt{}
    27  	_ DMLNode = &SelectStmt{}
    28  	_ DMLNode = &ShowStmt{}
    29  	_ DMLNode = &LoadDataStmt{}
    30  
    31  	_ Node = &Assignment{}
    32  	_ Node = &ByItem{}
    33  	_ Node = &FieldList{}
    34  	_ Node = &GroupByClause{}
    35  	_ Node = &HavingClause{}
    36  	_ Node = &Join{}
    37  	_ Node = &Limit{}
    38  	_ Node = &OnCondition{}
    39  	_ Node = &OrderByClause{}
    40  	_ Node = &SelectField{}
    41  	_ Node = &TableName{}
    42  	_ Node = &TableRefsClause{}
    43  	_ Node = &TableSource{}
    44  	_ Node = &UnionSelectList{}
    45  	_ Node = &WildCardField{}
    46  )
    47  
    48  // JoinType is join type, including cross/left/right/full.
    49  type JoinType int
    50  
    51  const (
    52  	// CrossJoin is cross join type.
    53  	CrossJoin JoinType = iota + 1
    54  	// LeftJoin is left Join type.
    55  	LeftJoin
    56  	// RightJoin is right Join type.
    57  	RightJoin
    58  )
    59  
    60  // Join represents table join.
    61  type Join struct {
    62  	node
    63  	resultSetNode
    64  
    65  	// Left table can be TableSource or JoinNode.
    66  	Left ResultSetNode
    67  	// Right table can be TableSource or JoinNode or nil.
    68  	Right ResultSetNode
    69  	// Tp represents join type.
    70  	Tp JoinType
    71  	// On represents join on condition.
    72  	On *OnCondition
    73  	// Using represents join using clause.
    74  	Using []*ColumnName
    75  	// NaturalJoin represents join is natural join
    76  	NaturalJoin bool
    77  }
    78  
    79  // Accept implements Node Accept interface.
    80  func (n *Join) Accept(v Visitor) (Node, bool) {
    81  	newNode, skipChildren := v.Enter(n)
    82  	if skipChildren {
    83  		return v.Leave(newNode)
    84  	}
    85  	n = newNode.(*Join)
    86  	node, ok := n.Left.Accept(v)
    87  	if !ok {
    88  		return n, false
    89  	}
    90  	n.Left = node.(ResultSetNode)
    91  	if n.Right != nil {
    92  		node, ok = n.Right.Accept(v)
    93  		if !ok {
    94  			return n, false
    95  		}
    96  		n.Right = node.(ResultSetNode)
    97  	}
    98  	if n.On != nil {
    99  		node, ok = n.On.Accept(v)
   100  		if !ok {
   101  			return n, false
   102  		}
   103  		n.On = node.(*OnCondition)
   104  	}
   105  	return v.Leave(n)
   106  }
   107  
   108  // TableName represents a table name.
   109  type TableName struct {
   110  	node
   111  	resultSetNode
   112  
   113  	Schema model.CIStr
   114  	Name   model.CIStr
   115  
   116  	DBInfo    *model.DBInfo
   117  	TableInfo *model.TableInfo
   118  
   119  	IndexHints []*IndexHint
   120  }
   121  
   122  // IndexHintType is the type for index hint use, ignore or force.
   123  type IndexHintType int
   124  
   125  // IndexHintUseType values.
   126  const (
   127  	HintUse    IndexHintType = 1
   128  	HintIgnore IndexHintType = 2
   129  	HintForce  IndexHintType = 3
   130  )
   131  
   132  // IndexHintScope is the type for index hint for join, order by or group by.
   133  type IndexHintScope int
   134  
   135  // Index hint scopes.
   136  const (
   137  	HintForScan    IndexHintScope = 1
   138  	HintForJoin    IndexHintScope = 2
   139  	HintForOrderBy IndexHintScope = 3
   140  	HintForGroupBy IndexHintScope = 4
   141  )
   142  
   143  // IndexHint represents a hint for optimizer to use/ignore/force for join/order by/group by.
   144  type IndexHint struct {
   145  	IndexNames []model.CIStr
   146  	HintType   IndexHintType
   147  	HintScope  IndexHintScope
   148  }
   149  
   150  // Accept implements Node Accept interface.
   151  func (n *TableName) Accept(v Visitor) (Node, bool) {
   152  	newNode, skipChildren := v.Enter(n)
   153  	if skipChildren {
   154  		return v.Leave(newNode)
   155  	}
   156  	n = newNode.(*TableName)
   157  	return v.Leave(n)
   158  }
   159  
   160  // DeleteTableList is the tablelist used in delete statement multi-table mode.
   161  type DeleteTableList struct {
   162  	node
   163  	Tables []*TableName
   164  }
   165  
   166  // Accept implements Node Accept interface.
   167  func (n *DeleteTableList) Accept(v Visitor) (Node, bool) {
   168  	newNode, skipChildren := v.Enter(n)
   169  	if skipChildren {
   170  		return v.Leave(newNode)
   171  	}
   172  	n = newNode.(*DeleteTableList)
   173  	if n != nil {
   174  		for i, t := range n.Tables {
   175  			node, ok := t.Accept(v)
   176  			if !ok {
   177  				return n, false
   178  			}
   179  			n.Tables[i] = node.(*TableName)
   180  		}
   181  	}
   182  	return v.Leave(n)
   183  }
   184  
   185  // OnCondition represetns JOIN on condition.
   186  type OnCondition struct {
   187  	node
   188  
   189  	Expr ExprNode
   190  }
   191  
   192  // Accept implements Node Accept interface.
   193  func (n *OnCondition) Accept(v Visitor) (Node, bool) {
   194  	newNode, skipChildren := v.Enter(n)
   195  	if skipChildren {
   196  		return v.Leave(newNode)
   197  	}
   198  	n = newNode.(*OnCondition)
   199  	node, ok := n.Expr.Accept(v)
   200  	if !ok {
   201  		return n, false
   202  	}
   203  	n.Expr = node.(ExprNode)
   204  	return v.Leave(n)
   205  }
   206  
   207  // TableSource represents table source with a name.
   208  type TableSource struct {
   209  	node
   210  
   211  	// Source is the source of the data, can be a TableName,
   212  	// a SelectStmt, a UnionStmt, or a JoinNode.
   213  	Source ResultSetNode
   214  
   215  	// AsName is the alias name of the table source.
   216  	AsName model.CIStr
   217  }
   218  
   219  // Accept implements Node Accept interface.
   220  func (n *TableSource) Accept(v Visitor) (Node, bool) {
   221  	newNode, skipChildren := v.Enter(n)
   222  	if skipChildren {
   223  		return v.Leave(newNode)
   224  	}
   225  	n = newNode.(*TableSource)
   226  	node, ok := n.Source.Accept(v)
   227  	if !ok {
   228  		return n, false
   229  	}
   230  	n.Source = node.(ResultSetNode)
   231  	return v.Leave(n)
   232  }
   233  
   234  // SelectLockType is the lock type for SelectStmt.
   235  type SelectLockType int
   236  
   237  // Select lock types.
   238  const (
   239  	SelectLockNone SelectLockType = iota
   240  	SelectLockForUpdate
   241  	SelectLockInShareMode
   242  )
   243  
   244  // String implements fmt.Stringer.
   245  func (slt SelectLockType) String() string {
   246  	switch slt {
   247  	case SelectLockNone:
   248  		return "none"
   249  	case SelectLockForUpdate:
   250  		return "for update"
   251  	case SelectLockInShareMode:
   252  		return "in share mode"
   253  	}
   254  	return "unsupported select lock type"
   255  }
   256  
   257  // WildCardField is a special type of select field content.
   258  type WildCardField struct {
   259  	node
   260  
   261  	Table  model.CIStr
   262  	Schema model.CIStr
   263  }
   264  
   265  // Accept implements Node Accept interface.
   266  func (n *WildCardField) Accept(v Visitor) (Node, bool) {
   267  	newNode, skipChildren := v.Enter(n)
   268  	if skipChildren {
   269  		return v.Leave(newNode)
   270  	}
   271  	n = newNode.(*WildCardField)
   272  	return v.Leave(n)
   273  }
   274  
   275  // SelectField represents fields in select statement.
   276  // There are two type of select field: wildcard
   277  // and expression with optional alias name.
   278  type SelectField struct {
   279  	node
   280  
   281  	// Offset is used to get original text.
   282  	Offset int
   283  	// WildCard is not nil, Expr will be nil.
   284  	WildCard *WildCardField
   285  	// Expr is not nil, WildCard will be nil.
   286  	Expr ExprNode
   287  	// AsName is alias name for Expr.
   288  	AsName model.CIStr
   289  	// Auxiliary stands for if this field is auxiliary.
   290  	// When we add a Field into SelectField list which is used for having/orderby clause but the field is not in select clause,
   291  	// we should set its Auxiliary to true. Then the TrimExec will trim the field.
   292  	Auxiliary bool
   293  }
   294  
   295  // Accept implements Node Accept interface.
   296  func (n *SelectField) Accept(v Visitor) (Node, bool) {
   297  	newNode, skipChildren := v.Enter(n)
   298  	if skipChildren {
   299  		return v.Leave(newNode)
   300  	}
   301  	n = newNode.(*SelectField)
   302  	if n.Expr != nil {
   303  		node, ok := n.Expr.Accept(v)
   304  		if !ok {
   305  			return n, false
   306  		}
   307  		n.Expr = node.(ExprNode)
   308  	}
   309  	return v.Leave(n)
   310  }
   311  
   312  // FieldList represents field list in select statement.
   313  type FieldList struct {
   314  	node
   315  
   316  	Fields []*SelectField
   317  }
   318  
   319  // Accept implements Node Accept interface.
   320  func (n *FieldList) Accept(v Visitor) (Node, bool) {
   321  	newNode, skipChildren := v.Enter(n)
   322  	if skipChildren {
   323  		return v.Leave(newNode)
   324  	}
   325  	n = newNode.(*FieldList)
   326  	for i, val := range n.Fields {
   327  		node, ok := val.Accept(v)
   328  		if !ok {
   329  			return n, false
   330  		}
   331  		n.Fields[i] = node.(*SelectField)
   332  	}
   333  	return v.Leave(n)
   334  }
   335  
   336  // TableRefsClause represents table references clause in dml statement.
   337  type TableRefsClause struct {
   338  	node
   339  
   340  	TableRefs *Join
   341  }
   342  
   343  // Accept implements Node Accept interface.
   344  func (n *TableRefsClause) Accept(v Visitor) (Node, bool) {
   345  	newNode, skipChildren := v.Enter(n)
   346  	if skipChildren {
   347  		return v.Leave(newNode)
   348  	}
   349  	n = newNode.(*TableRefsClause)
   350  	node, ok := n.TableRefs.Accept(v)
   351  	if !ok {
   352  		return n, false
   353  	}
   354  	n.TableRefs = node.(*Join)
   355  	return v.Leave(n)
   356  }
   357  
   358  // ByItem represents an item in order by or group by.
   359  type ByItem struct {
   360  	node
   361  
   362  	Expr ExprNode
   363  	Desc bool
   364  }
   365  
   366  // Accept implements Node Accept interface.
   367  func (n *ByItem) Accept(v Visitor) (Node, bool) {
   368  	newNode, skipChildren := v.Enter(n)
   369  	if skipChildren {
   370  		return v.Leave(newNode)
   371  	}
   372  	n = newNode.(*ByItem)
   373  	node, ok := n.Expr.Accept(v)
   374  	if !ok {
   375  		return n, false
   376  	}
   377  	n.Expr = node.(ExprNode)
   378  	return v.Leave(n)
   379  }
   380  
   381  // GroupByClause represents group by clause.
   382  type GroupByClause struct {
   383  	node
   384  	Items []*ByItem
   385  }
   386  
   387  // Accept implements Node Accept interface.
   388  func (n *GroupByClause) Accept(v Visitor) (Node, bool) {
   389  	newNode, skipChildren := v.Enter(n)
   390  	if skipChildren {
   391  		return v.Leave(newNode)
   392  	}
   393  	n = newNode.(*GroupByClause)
   394  	for i, val := range n.Items {
   395  		node, ok := val.Accept(v)
   396  		if !ok {
   397  			return n, false
   398  		}
   399  		n.Items[i] = node.(*ByItem)
   400  	}
   401  	return v.Leave(n)
   402  }
   403  
   404  // HavingClause represents having clause.
   405  type HavingClause struct {
   406  	node
   407  	Expr ExprNode
   408  }
   409  
   410  // Accept implements Node Accept interface.
   411  func (n *HavingClause) Accept(v Visitor) (Node, bool) {
   412  	newNode, skipChildren := v.Enter(n)
   413  	if skipChildren {
   414  		return v.Leave(newNode)
   415  	}
   416  	n = newNode.(*HavingClause)
   417  	node, ok := n.Expr.Accept(v)
   418  	if !ok {
   419  		return n, false
   420  	}
   421  	n.Expr = node.(ExprNode)
   422  	return v.Leave(n)
   423  }
   424  
   425  // OrderByClause represents order by clause.
   426  type OrderByClause struct {
   427  	node
   428  	Items    []*ByItem
   429  	ForUnion bool
   430  }
   431  
   432  // Accept implements Node Accept interface.
   433  func (n *OrderByClause) Accept(v Visitor) (Node, bool) {
   434  	newNode, skipChildren := v.Enter(n)
   435  	if skipChildren {
   436  		return v.Leave(newNode)
   437  	}
   438  	n = newNode.(*OrderByClause)
   439  	for i, val := range n.Items {
   440  		node, ok := val.Accept(v)
   441  		if !ok {
   442  			return n, false
   443  		}
   444  		n.Items[i] = node.(*ByItem)
   445  	}
   446  	return v.Leave(n)
   447  }
   448  
   449  // SelectStmt represents the select query node.
   450  // See https://dev.mysql.com/doc/refman/5.7/en/select.html
   451  type SelectStmt struct {
   452  	dmlNode
   453  	resultSetNode
   454  
   455  	// SelectStmtOpts wraps around select hints and switches.
   456  	*SelectStmtOpts
   457  	// Distinct represents whether the select has distinct option.
   458  	Distinct bool
   459  	// From is the from clause of the query.
   460  	From *TableRefsClause
   461  	// Where is the where clause in select statement.
   462  	Where ExprNode
   463  	// Fields is the select expression list.
   464  	Fields *FieldList
   465  	// GroupBy is the group by expression list.
   466  	GroupBy *GroupByClause
   467  	// Having is the having condition.
   468  	Having *HavingClause
   469  	// OrderBy is the ordering expression list.
   470  	OrderBy *OrderByClause
   471  	// Limit is the limit clause.
   472  	Limit *Limit
   473  	// LockTp is the lock type
   474  	LockTp SelectLockType
   475  	// TableHints represents the level Optimizer Hint
   476  	TableHints []*TableOptimizerHint
   477  }
   478  
   479  // Accept implements Node Accept interface.
   480  func (n *SelectStmt) Accept(v Visitor) (Node, bool) {
   481  	newNode, skipChildren := v.Enter(n)
   482  	if skipChildren {
   483  		return v.Leave(newNode)
   484  	}
   485  
   486  	n = newNode.(*SelectStmt)
   487  	if n.TableHints != nil && len(n.TableHints) != 0 {
   488  		newHints := make([]*TableOptimizerHint, len(n.TableHints))
   489  		for i, hint := range n.TableHints {
   490  			node, ok := hint.Accept(v)
   491  			if !ok {
   492  				return n, false
   493  			}
   494  			newHints[i] = node.(*TableOptimizerHint)
   495  		}
   496  		n.TableHints = newHints
   497  	}
   498  
   499  	if n.From != nil {
   500  		node, ok := n.From.Accept(v)
   501  		if !ok {
   502  			return n, false
   503  		}
   504  		n.From = node.(*TableRefsClause)
   505  	}
   506  
   507  	if n.Where != nil {
   508  		node, ok := n.Where.Accept(v)
   509  		if !ok {
   510  			return n, false
   511  		}
   512  		n.Where = node.(ExprNode)
   513  	}
   514  
   515  	if n.Fields != nil {
   516  		node, ok := n.Fields.Accept(v)
   517  		if !ok {
   518  			return n, false
   519  		}
   520  		n.Fields = node.(*FieldList)
   521  	}
   522  
   523  	if n.GroupBy != nil {
   524  		node, ok := n.GroupBy.Accept(v)
   525  		if !ok {
   526  			return n, false
   527  		}
   528  		n.GroupBy = node.(*GroupByClause)
   529  	}
   530  
   531  	if n.Having != nil {
   532  		node, ok := n.Having.Accept(v)
   533  		if !ok {
   534  			return n, false
   535  		}
   536  		n.Having = node.(*HavingClause)
   537  	}
   538  
   539  	if n.OrderBy != nil {
   540  		node, ok := n.OrderBy.Accept(v)
   541  		if !ok {
   542  			return n, false
   543  		}
   544  		n.OrderBy = node.(*OrderByClause)
   545  	}
   546  
   547  	if n.Limit != nil {
   548  		node, ok := n.Limit.Accept(v)
   549  		if !ok {
   550  			return n, false
   551  		}
   552  		n.Limit = node.(*Limit)
   553  	}
   554  
   555  	return v.Leave(n)
   556  }
   557  
   558  // UnionSelectList represents the select list in a union statement.
   559  type UnionSelectList struct {
   560  	node
   561  
   562  	Selects []*SelectStmt
   563  }
   564  
   565  // Accept implements Node Accept interface.
   566  func (n *UnionSelectList) Accept(v Visitor) (Node, bool) {
   567  	newNode, skipChildren := v.Enter(n)
   568  	if skipChildren {
   569  		return v.Leave(newNode)
   570  	}
   571  	n = newNode.(*UnionSelectList)
   572  	for i, sel := range n.Selects {
   573  		node, ok := sel.Accept(v)
   574  		if !ok {
   575  			return n, false
   576  		}
   577  		n.Selects[i] = node.(*SelectStmt)
   578  	}
   579  	return v.Leave(n)
   580  }
   581  
   582  // UnionStmt represents "union statement"
   583  // See https://dev.mysql.com/doc/refman/5.7/en/union.html
   584  type UnionStmt struct {
   585  	dmlNode
   586  	resultSetNode
   587  
   588  	Distinct   bool
   589  	SelectList *UnionSelectList
   590  	OrderBy    *OrderByClause
   591  	Limit      *Limit
   592  }
   593  
   594  // Accept implements Node Accept interface.
   595  func (n *UnionStmt) Accept(v Visitor) (Node, bool) {
   596  	newNode, skipChildren := v.Enter(n)
   597  	if skipChildren {
   598  		return v.Leave(newNode)
   599  	}
   600  	n = newNode.(*UnionStmt)
   601  	if n.SelectList != nil {
   602  		node, ok := n.SelectList.Accept(v)
   603  		if !ok {
   604  			return n, false
   605  		}
   606  		n.SelectList = node.(*UnionSelectList)
   607  	}
   608  	if n.OrderBy != nil {
   609  		node, ok := n.OrderBy.Accept(v)
   610  		if !ok {
   611  			return n, false
   612  		}
   613  		n.OrderBy = node.(*OrderByClause)
   614  	}
   615  	if n.Limit != nil {
   616  		node, ok := n.Limit.Accept(v)
   617  		if !ok {
   618  			return n, false
   619  		}
   620  		n.Limit = node.(*Limit)
   621  	}
   622  	return v.Leave(n)
   623  }
   624  
   625  // Assignment is the expression for assignment, like a = 1.
   626  type Assignment struct {
   627  	node
   628  	// Column is the column name to be assigned.
   629  	Column *ColumnName
   630  	// Expr is the expression assigning to ColName.
   631  	Expr ExprNode
   632  }
   633  
   634  // Accept implements Node Accept interface.
   635  func (n *Assignment) Accept(v Visitor) (Node, bool) {
   636  	newNode, skipChildren := v.Enter(n)
   637  	if skipChildren {
   638  		return v.Leave(newNode)
   639  	}
   640  	n = newNode.(*Assignment)
   641  	node, ok := n.Column.Accept(v)
   642  	if !ok {
   643  		return n, false
   644  	}
   645  	n.Column = node.(*ColumnName)
   646  	node, ok = n.Expr.Accept(v)
   647  	if !ok {
   648  		return n, false
   649  	}
   650  	n.Expr = node.(ExprNode)
   651  	return v.Leave(n)
   652  }
   653  
   654  // LoadDataStmt is a statement to load data from a specified file, then insert this rows into an existing table.
   655  // See https://dev.mysql.com/doc/refman/5.7/en/load-data.html
   656  type LoadDataStmt struct {
   657  	dmlNode
   658  
   659  	IsLocal    bool
   660  	Path       string
   661  	Table      *TableName
   662  	Columns    []*ColumnName
   663  	FieldsInfo *FieldsClause
   664  	LinesInfo  *LinesClause
   665  }
   666  
   667  // Accept implements Node Accept interface.
   668  func (n *LoadDataStmt) Accept(v Visitor) (Node, bool) {
   669  	newNode, skipChildren := v.Enter(n)
   670  	if skipChildren {
   671  		return v.Leave(newNode)
   672  	}
   673  	n = newNode.(*LoadDataStmt)
   674  	if n.Table != nil {
   675  		node, ok := n.Table.Accept(v)
   676  		if !ok {
   677  			return n, false
   678  		}
   679  		n.Table = node.(*TableName)
   680  	}
   681  	for i, val := range n.Columns {
   682  		node, ok := val.Accept(v)
   683  		if !ok {
   684  			return n, false
   685  		}
   686  		n.Columns[i] = node.(*ColumnName)
   687  	}
   688  	return v.Leave(n)
   689  }
   690  
   691  // FieldsClause represents fields references clause in load data statement.
   692  type FieldsClause struct {
   693  	Terminated string
   694  	Enclosed   byte
   695  	Escaped    byte
   696  }
   697  
   698  // LinesClause represents lines references clause in load data statement.
   699  type LinesClause struct {
   700  	Starting   string
   701  	Terminated string
   702  }
   703  
   704  // InsertStmt is a statement to insert new rows into an existing table.
   705  // See https://dev.mysql.com/doc/refman/5.7/en/insert.html
   706  type InsertStmt struct {
   707  	dmlNode
   708  
   709  	IsReplace   bool
   710  	IgnoreErr   bool
   711  	Table       *TableRefsClause
   712  	Columns     []*ColumnName
   713  	Lists       [][]ExprNode
   714  	Setlist     []*Assignment
   715  	Priority    mysql.PriorityEnum
   716  	OnDuplicate []*Assignment
   717  	Select      ResultSetNode
   718  }
   719  
   720  // Accept implements Node Accept interface.
   721  func (n *InsertStmt) Accept(v Visitor) (Node, bool) {
   722  	newNode, skipChildren := v.Enter(n)
   723  	if skipChildren {
   724  		return v.Leave(newNode)
   725  	}
   726  
   727  	n = newNode.(*InsertStmt)
   728  	if n.Select != nil {
   729  		node, ok := n.Select.Accept(v)
   730  		if !ok {
   731  			return n, false
   732  		}
   733  		n.Select = node.(ResultSetNode)
   734  	}
   735  
   736  	node, ok := n.Table.Accept(v)
   737  	if !ok {
   738  		return n, false
   739  	}
   740  	n.Table = node.(*TableRefsClause)
   741  
   742  	for i, val := range n.Columns {
   743  		node, ok := val.Accept(v)
   744  		if !ok {
   745  			return n, false
   746  		}
   747  		n.Columns[i] = node.(*ColumnName)
   748  	}
   749  	for i, list := range n.Lists {
   750  		for j, val := range list {
   751  			node, ok := val.Accept(v)
   752  			if !ok {
   753  				return n, false
   754  			}
   755  			n.Lists[i][j] = node.(ExprNode)
   756  		}
   757  	}
   758  	for i, val := range n.Setlist {
   759  		node, ok := val.Accept(v)
   760  		if !ok {
   761  			return n, false
   762  		}
   763  		n.Setlist[i] = node.(*Assignment)
   764  	}
   765  	for i, val := range n.OnDuplicate {
   766  		node, ok := val.Accept(v)
   767  		if !ok {
   768  			return n, false
   769  		}
   770  		n.OnDuplicate[i] = node.(*Assignment)
   771  	}
   772  	return v.Leave(n)
   773  }
   774  
   775  // DeleteStmt is a statement to delete rows from table.
   776  // See https://dev.mysql.com/doc/refman/5.7/en/delete.html
   777  type DeleteStmt struct {
   778  	dmlNode
   779  
   780  	// TableRefs is used in both single table and multiple table delete statement.
   781  	TableRefs *TableRefsClause
   782  	// Tables is only used in multiple table delete statement.
   783  	Tables       *DeleteTableList
   784  	Where        ExprNode
   785  	Order        *OrderByClause
   786  	Limit        *Limit
   787  	LowPriority  bool
   788  	IgnoreErr    bool
   789  	Quick        bool
   790  	IsMultiTable bool
   791  	BeforeFrom   bool
   792  }
   793  
   794  // Accept implements Node Accept interface.
   795  func (n *DeleteStmt) Accept(v Visitor) (Node, bool) {
   796  	newNode, skipChildren := v.Enter(n)
   797  	if skipChildren {
   798  		return v.Leave(newNode)
   799  	}
   800  
   801  	n = newNode.(*DeleteStmt)
   802  	node, ok := n.TableRefs.Accept(v)
   803  	if !ok {
   804  		return n, false
   805  	}
   806  	n.TableRefs = node.(*TableRefsClause)
   807  
   808  	node, ok = n.Tables.Accept(v)
   809  	if !ok {
   810  		return n, false
   811  	}
   812  	n.Tables = node.(*DeleteTableList)
   813  
   814  	if n.Where != nil {
   815  		node, ok = n.Where.Accept(v)
   816  		if !ok {
   817  			return n, false
   818  		}
   819  		n.Where = node.(ExprNode)
   820  	}
   821  	if n.Order != nil {
   822  		node, ok = n.Order.Accept(v)
   823  		if !ok {
   824  			return n, false
   825  		}
   826  		n.Order = node.(*OrderByClause)
   827  	}
   828  	if n.Limit != nil {
   829  		node, ok = n.Limit.Accept(v)
   830  		if !ok {
   831  			return n, false
   832  		}
   833  		n.Limit = node.(*Limit)
   834  	}
   835  	return v.Leave(n)
   836  }
   837  
   838  // UpdateStmt is a statement to update columns of existing rows in tables with new values.
   839  // See https://dev.mysql.com/doc/refman/5.7/en/update.html
   840  type UpdateStmt struct {
   841  	dmlNode
   842  
   843  	TableRefs     *TableRefsClause
   844  	List          []*Assignment
   845  	Where         ExprNode
   846  	Order         *OrderByClause
   847  	Limit         *Limit
   848  	LowPriority   bool
   849  	IgnoreErr     bool
   850  	MultipleTable bool
   851  }
   852  
   853  // Accept implements Node Accept interface.
   854  func (n *UpdateStmt) Accept(v Visitor) (Node, bool) {
   855  	newNode, skipChildren := v.Enter(n)
   856  	if skipChildren {
   857  		return v.Leave(newNode)
   858  	}
   859  	n = newNode.(*UpdateStmt)
   860  	node, ok := n.TableRefs.Accept(v)
   861  	if !ok {
   862  		return n, false
   863  	}
   864  	n.TableRefs = node.(*TableRefsClause)
   865  	for i, val := range n.List {
   866  		node, ok = val.Accept(v)
   867  		if !ok {
   868  			return n, false
   869  		}
   870  		n.List[i] = node.(*Assignment)
   871  	}
   872  	if n.Where != nil {
   873  		node, ok = n.Where.Accept(v)
   874  		if !ok {
   875  			return n, false
   876  		}
   877  		n.Where = node.(ExprNode)
   878  	}
   879  	if n.Order != nil {
   880  		node, ok = n.Order.Accept(v)
   881  		if !ok {
   882  			return n, false
   883  		}
   884  		n.Order = node.(*OrderByClause)
   885  	}
   886  	if n.Limit != nil {
   887  		node, ok = n.Limit.Accept(v)
   888  		if !ok {
   889  			return n, false
   890  		}
   891  		n.Limit = node.(*Limit)
   892  	}
   893  	return v.Leave(n)
   894  }
   895  
   896  // Limit is the limit clause.
   897  type Limit struct {
   898  	node
   899  
   900  	Count  ExprNode
   901  	Offset ExprNode
   902  }
   903  
   904  // Accept implements Node Accept interface.
   905  func (n *Limit) Accept(v Visitor) (Node, bool) {
   906  	newNode, skipChildren := v.Enter(n)
   907  	if skipChildren {
   908  		return v.Leave(newNode)
   909  	}
   910  	if n.Count != nil {
   911  		node, ok := n.Count.Accept(v)
   912  		if !ok {
   913  			return n, false
   914  		}
   915  		n.Count = node.(ExprNode)
   916  	}
   917  	if n.Offset != nil {
   918  		node, ok := n.Offset.Accept(v)
   919  		if !ok {
   920  			return n, false
   921  		}
   922  		n.Offset = node.(ExprNode)
   923  	}
   924  
   925  	n = newNode.(*Limit)
   926  	return v.Leave(n)
   927  }
   928  
   929  // ShowStmtType is the type for SHOW statement.
   930  type ShowStmtType int
   931  
   932  // Show statement types.
   933  const (
   934  	ShowNone = iota
   935  	ShowEngines
   936  	ShowDatabases
   937  	ShowTables
   938  	ShowTableStatus
   939  	ShowColumns
   940  	ShowWarnings
   941  	ShowCharset
   942  	ShowVariables
   943  	ShowStatus
   944  	ShowCollation
   945  	ShowCreateTable
   946  	ShowGrants
   947  	ShowTriggers
   948  	ShowProcedureStatus
   949  	ShowIndex
   950  	ShowProcessList
   951  	ShowCreateDatabase
   952  	ShowEvents
   953  	ShowStatsMeta
   954  	ShowStatsHistograms
   955  	ShowStatsBuckets
   956  	ShowStatsHealthy
   957  	ShowPlugins
   958  	ShowProfiles
   959  )
   960  
   961  // ShowStmt is a statement to provide information about databases, tables, columns and so on.
   962  // See https://dev.mysql.com/doc/refman/5.7/en/show.html
   963  type ShowStmt struct {
   964  	dmlNode
   965  	resultSetNode
   966  
   967  	Tp     ShowStmtType // Databases/Tables/Columns/....
   968  	DBName string
   969  	Table  *TableName  // Used for showing columns.
   970  	Column *ColumnName // Used for `desc table column`.
   971  	Flag   int         // Some flag parsed from sql, such as FULL.
   972  	Full   bool
   973  	User   *auth.UserIdentity // Used for show grants.
   974  
   975  	// GlobalScope is used by show variables
   976  	GlobalScope bool
   977  	Pattern     *PatternLikeExpr
   978  	Where       ExprNode
   979  }
   980  
   981  // Accept implements Node Accept interface.
   982  func (n *ShowStmt) Accept(v Visitor) (Node, bool) {
   983  	newNode, skipChildren := v.Enter(n)
   984  	if skipChildren {
   985  		return v.Leave(newNode)
   986  	}
   987  	n = newNode.(*ShowStmt)
   988  	if n.Table != nil {
   989  		node, ok := n.Table.Accept(v)
   990  		if !ok {
   991  			return n, false
   992  		}
   993  		n.Table = node.(*TableName)
   994  	}
   995  	if n.Column != nil {
   996  		node, ok := n.Column.Accept(v)
   997  		if !ok {
   998  			return n, false
   999  		}
  1000  		n.Column = node.(*ColumnName)
  1001  	}
  1002  	if n.Pattern != nil {
  1003  		node, ok := n.Pattern.Accept(v)
  1004  		if !ok {
  1005  			return n, false
  1006  		}
  1007  		n.Pattern = node.(*PatternLikeExpr)
  1008  	}
  1009  
  1010  	switch n.Tp {
  1011  	case ShowTriggers, ShowProcedureStatus, ShowProcessList, ShowEvents:
  1012  		// We don't have any data to return for those types,
  1013  		// but visiting Where may cause resolving error, so return here to avoid error.
  1014  		return v.Leave(n)
  1015  	}
  1016  
  1017  	if n.Where != nil {
  1018  		node, ok := n.Where.Accept(v)
  1019  		if !ok {
  1020  			return n, false
  1021  		}
  1022  		n.Where = node.(ExprNode)
  1023  	}
  1024  	return v.Leave(n)
  1025  }