github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/sqlparse/tidbparser/ast/ast.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 is the abstract syntax tree parsed from a SQL statement by parser.
    15  // It can be analysed and transformed by optimizer.
    16  package ast
    17  
    18  import (
    19  	"context"
    20  	"io"
    21  
    22  	"github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/model"
    23  	"github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/types"
    24  	"github.com/bingoohuang/gg/pkg/sqlparse/tidbparser/dependency/util/chunk"
    25  )
    26  
    27  // Node is the basic element of the AST.
    28  // Interfaces embed Node should have 'Node' name suffix.
    29  type Node interface {
    30  	// Accept accepts Visitor to visit itself.
    31  	// The returned node should replace original node.
    32  	// ok returns false to stop visiting.
    33  	//
    34  	// Implementation of this method should first call visitor.Enter,
    35  	// assign the returned node to its method receiver, if skipChildren returns true,
    36  	// children should be skipped. Otherwise, call its children in particular order that
    37  	// later elements depends on former elements. Finally, return visitor.Leave.
    38  	Accept(v Visitor) (node Node, ok bool)
    39  	// Text returns the original text of the element.
    40  	Text() string
    41  	// SetText sets original text to the Node.
    42  	SetText(text string)
    43  }
    44  
    45  // Flags indicates whether an expression contains certain types of expression.
    46  const (
    47  	FlagConstant       uint64 = 0
    48  	FlagHasParamMarker uint64 = 1 << iota
    49  	FlagHasFunc
    50  	FlagHasReference
    51  	FlagHasAggregateFunc
    52  	FlagHasSubquery
    53  	FlagHasVariable
    54  	FlagHasDefault
    55  	FlagPreEvaluated
    56  )
    57  
    58  // ExprNode is a node that can be evaluated.
    59  // Name of implementations should have 'Expr' suffix.
    60  type ExprNode interface {
    61  	// Node is embedded in ExprNode.
    62  	Node
    63  	// SetType sets evaluation type to the expression.
    64  	SetType(tp *types.FieldType)
    65  	// GetType gets the evaluation type of the expression.
    66  	GetType() *types.FieldType
    67  	// SetValue sets value to the expression.
    68  	SetValue(val interface{})
    69  	// GetValue gets value of the expression.
    70  	GetValue() interface{}
    71  	// SetDatum sets datum to the expression.
    72  	SetDatum(datum types.Datum)
    73  	// GetDatum gets datum of the expression.
    74  	GetDatum() *types.Datum
    75  	// SetFlag sets flag to the expression.
    76  	// Flag indicates whether the expression contains
    77  	// parameter marker, reference, aggregate function...
    78  	SetFlag(flag uint64)
    79  	// GetFlag returns the flag of the expression.
    80  	GetFlag() uint64
    81  
    82  	// Format formats the AST into a writer.
    83  	Format(w io.Writer)
    84  }
    85  
    86  // FuncNode represents function call expression node.
    87  type FuncNode interface {
    88  	ExprNode
    89  	functionExpression()
    90  }
    91  
    92  // StmtNode represents statement node.
    93  // Name of implementations should have 'Stmt' suffix.
    94  type StmtNode interface {
    95  	Node
    96  	statement()
    97  }
    98  
    99  // DDLNode represents DDL statement node.
   100  type DDLNode interface {
   101  	StmtNode
   102  	ddlStatement()
   103  }
   104  
   105  // DMLNode represents DML statement node.
   106  type DMLNode interface {
   107  	StmtNode
   108  	dmlStatement()
   109  }
   110  
   111  // ResultField represents a result field which can be a column from a table,
   112  // or an expression in select field. It is a generated property during
   113  // binding process. ResultField is the key element to evaluate a ColumnNameExpr.
   114  // After resolving process, every ColumnNameExpr will be resolved to a ResultField.
   115  // During execution, every row retrieved from table will set the row value to
   116  // ResultFields of that table, so ColumnNameExpr resolved to that ResultField can be
   117  // easily evaluated.
   118  type ResultField struct {
   119  	Column       *model.ColumnInfo
   120  	ColumnAsName model.CIStr
   121  	Table        *model.TableInfo
   122  	TableAsName  model.CIStr
   123  	DBName       model.CIStr
   124  
   125  	// Expr represents the expression for the result field. If it is generated from a select field, it would
   126  	// be the expression of that select field, otherwise the type would be ValueExpr and value
   127  	// will be set for every retrieved row.
   128  	Expr      ExprNode
   129  	TableName *TableName
   130  	// Referenced indicates the result field has been referenced or not.
   131  	// If not, we don't need to get the values.
   132  	Referenced bool
   133  }
   134  
   135  // RecordSet is an abstract result set interface to help get data from Plan.
   136  type RecordSet interface {
   137  	// Fields gets result fields.
   138  	Fields() []*ResultField
   139  
   140  	// Next returns the next row, nil row means there is no more to return.
   141  	Next(ctx context.Context) (row types.Row, err error)
   142  
   143  	// NextChunk reads records into chunk.
   144  	NextChunk(ctx context.Context, chk *chunk.Chunk) error
   145  
   146  	// NewChunk creates a new chunk with initial capacity.
   147  	NewChunk() *chunk.Chunk
   148  
   149  	// Close closes the underlying iterator, call Next after Close will
   150  	// restart the iteration.
   151  	Close() error
   152  }
   153  
   154  // RowToDatums converts row to datum slice.
   155  func RowToDatums(row types.Row, fields []*ResultField) []types.Datum {
   156  	datums := make([]types.Datum, len(fields))
   157  	for i, f := range fields {
   158  		datums[i] = row.GetDatum(i, &f.Column.FieldType)
   159  	}
   160  	return datums
   161  }
   162  
   163  // ResultSetNode interface has a ResultFields property, represents a Node that returns result set.
   164  // Implementations include SelectStmt, SubqueryExpr, TableSource, TableName and Join.
   165  type ResultSetNode interface {
   166  	Node
   167  }
   168  
   169  // SensitiveStmtNode overloads StmtNode and provides a SecureText method.
   170  type SensitiveStmtNode interface {
   171  	StmtNode
   172  	// SecureText is different from Text that it hide password information.
   173  	SecureText() string
   174  }
   175  
   176  // Statement is an interface for SQL execution.
   177  // NOTE: all Statement implementations must be safe for
   178  // concurrent using by multiple goroutines.
   179  // If the Exec method requires any Execution domain local data,
   180  // they must be held out of the implementing instance.
   181  type Statement interface {
   182  	// OriginText gets the origin SQL text.
   183  	OriginText() string
   184  
   185  	// Exec executes SQL and gets a Recordset.
   186  	Exec(ctx context.Context) (RecordSet, error)
   187  
   188  	// IsPrepared returns whether this statement is prepared statement.
   189  	IsPrepared() bool
   190  
   191  	// IsReadOnly returns if the statement is read only. For example: SelectStmt without lock.
   192  	IsReadOnly() bool
   193  
   194  	// RebuildPlan rebuilds the plan of the statement.
   195  	RebuildPlan() error
   196  }
   197  
   198  // Visitor visits a Node.
   199  type Visitor interface {
   200  	// Enter is called before children nodes are visited.
   201  	// The returned node must be the same type as the input node n.
   202  	// skipChildren returns true means children nodes should be skipped,
   203  	// this is useful when work is done in Enter and there is no need to visit children.
   204  	Enter(n Node) (node Node, skipChildren bool)
   205  	// Leave is called after children nodes have been visited.
   206  	// The returned node's type can be different from the input node if it is a ExprNode,
   207  	// Non-expression node must be the same type as the input node n.
   208  	// ok returns false to stop visiting.
   209  	Leave(n Node) (node Node, ok bool)
   210  }