Documentation ¶
Overview ¶
Package ast is an interface for Abstract Syntax Tree parser
Index ¶
- Constants
- Variables
- func AlterAffectTable(stmt sqlparser.Statement) string
- func Compress(sql string) string
- func FindAllCols(node sqlparser.SQLNode, targets ...Expression) []*common.Column
- func FindAllCondition(node sqlparser.SQLNode) []interface{}
- func FindColumn(node sqlparser.SQLNode) []*common.Column
- func FindEQColsInJoinCond(node sqlparser.SQLNode) []*common.Column
- func FindEQColsInWhere(node sqlparser.SQLNode) []*common.Column
- func FindGroupByCols(node sqlparser.SQLNode) []*common.Column
- func FindINEQColsInJoinCond(node sqlparser.SQLNode) []*common.Column
- func FindINEQColsInWhere(node sqlparser.SQLNode) []*common.Column
- func FindJoinCols(node sqlparser.SQLNode) [][]*common.Column
- func FindJoinTable(node sqlparser.SQLNode, meta common.Meta) common.Meta
- func FindOrderByCols(node sqlparser.SQLNode) []*common.Column
- func FindSubquery(depth int, node sqlparser.SQLNode, queries ...string) []string
- func FindWhereEQ(node sqlparser.SQLNode) []*common.Column
- func FindWhereINEQ(node sqlparser.SQLNode) []*common.Column
- func GetMeta(stmt sqlparser.Statement, meta common.Meta) common.Meta
- func GetSubqueryDepth(node sqlparser.SQLNode) int
- func GetTableFromExprs(exprs sqlparser.TableExprs, metas ...common.Meta) common.Meta
- func IsMysqlKeyword(name string) bool
- func LeftNewLines(buf []byte) int
- func ListRewriteRules(rules []Rule)
- func MergeAlterTables(sqls ...string) map[string]string
- func NewLines(buf []byte) int
- func Pretty(sql string, method string) (output string)
- func PrintPrettyStmtNode(sql, charset, collation string)
- func PrintPrettyVitessStmtNode(sql string)
- func QueryType(sql string) string
- func RewriteRuleMatch(name string) bool
- func SchemaMetaInfo(sql string, defaultDatabase string) []string
- func SplitStatement(buf []byte, delimiter []byte) (string, string, []byte)
- func StmtNode2JSON(sql, charset, collation string) string
- func TiParse(sql, charset, collation string) ([]ast.StmtNode, error)
- func VitessStmtNode2JSON(sql string) string
- type Expression
- type NodeItem
- type NodeList
- type Rewrite
- func (rw *Rewrite) Rewrite() *Rewrite
- func (rw *Rewrite) RewriteAddOrderByNull() *Rewrite
- func (rw *Rewrite) RewriteAlwaysTrue() (reWriter *Rewrite)
- func (rw *Rewrite) RewriteAutoIncrement() *Rewrite
- func (rw *Rewrite) RewriteCountStar() *Rewrite
- func (rw *Rewrite) RewriteDML2Select() *Rewrite
- func (rw *Rewrite) RewriteDelimiter() *Rewrite
- func (rw *Rewrite) RewriteDistinctStar() *Rewrite
- func (rw *Rewrite) RewriteGroupByConst() *Rewrite
- func (rw *Rewrite) RewriteHaving() *Rewrite
- func (rw *Rewrite) RewriteInNull() *Rewrite
- func (rw *Rewrite) RewriteInnoDB() *Rewrite
- func (rw *Rewrite) RewriteInsertColumns() *Rewrite
- func (rw *Rewrite) RewriteIntWidth() *Rewrite
- func (rw *Rewrite) RewriteJoin2SubQuery() *Rewrite
- func (rw *Rewrite) RewriteOr2In() *Rewrite
- func (rw *Rewrite) RewriteOr2Union() *Rewrite
- func (rw *Rewrite) RewriteRemoveDMLOrderBy() *Rewrite
- func (rw *Rewrite) RewriteRmParenthesis() *Rewrite
- func (rw *Rewrite) RewriteStandard() *Rewrite
- func (rw *Rewrite) RewriteStar2Columns() *Rewrite
- func (rw *Rewrite) RewriteSubQuery2Join() *Rewrite
- func (rw *Rewrite) RewriteTruncate() *Rewrite
- func (rw *Rewrite) RewriteUnionAll() *Rewrite
- type Rule
- type Token
Constants ¶
const ( TokenTypeWhitespace = 0 TokenTypeWord = 1 TokenTypeQuote = 2 TokenTypeBacktickQuote = 3 TokenTypeReserved = 4 TokenTypeReservedToplevel = 5 TokenTypeReservedNewline = 6 TokenTypeBoundary = 7 TokenTypeComment = 8 TokenTypeBlockComment = 9 TokenTypeNumber = 10 TokenTypeError = 11 TokenTypeVariable = 12 )
TokenType
Variables ¶
var RewriteRules []Rule
RewriteRules SQL重写规则,注意这个规则是有序的,先后顺序不能乱
var TokenString = map[int]string{}/* 214 elements not displayed */
TokenString sqlparser tokens
Functions ¶
func AlterAffectTable ¶
AlterAffectTable 获取ALTER影响的库表名,返回:`db`.`table`
func FindAllCols ¶
func FindAllCols(node sqlparser.SQLNode, targets ...Expression) []*common.Column
FindAllCols 获取 AST 中某个节点下所有的 columns
func FindAllCondition ¶
FindAllCondition 获取 AST 中所有的 condition 条件
func FindColumn ¶
FindColumn 从传入的 node 中获取所有可能加索引的的 column 信息
func FindEQColsInJoinCond ¶
FindEQColsInJoinCond 获取 join condition 中应转为whereEQ条件的列
func FindEQColsInWhere ¶
FindEQColsInWhere 获取等值条件信息 将所有值得加索引的condition条件信息进行过滤
func FindGroupByCols ¶
FindGroupByCols 获取groupBy中可能需要加索引的列信息
func FindINEQColsInJoinCond ¶
FindINEQColsInJoinCond 获取 join condition 中应转为whereINEQ条件的列
func FindINEQColsInWhere ¶
FindINEQColsInWhere 获取非等值条件中可能需要加索引的列 将所有值得加索引的condition条件信息进行过滤 TODO: 将 where 条件中隐含的 join 条件合并到 join condition中
func FindJoinCols ¶
FindJoinCols 获取 join condition 中使用到的列(必须是 `列 operator 列` 的情况。 如果列对应的值或是 function,则应该移到where condition中) 某些 where 条件隐含在 join 条件中(INNER JOIN)
func FindJoinTable ¶
FindJoinTable 获取 Join 中需要添加索引的表 join 优化添加索引分为三种类型:1. inner join, 2. left join, 3.right join 针对三种优化类型,需要三种不同的索引添加方案: 1. inner join 需要对 join 左右的表添加索引 2. left join 由于左表为全表扫描,需要对右表的关联列添加索引。 3. right join 与 left join 相反,需要对左表的关联列添加索引。 以上添加索引的策略前提为join的表为实体表而非临时表。
func FindOrderByCols ¶
FindOrderByCols 为索引优化获取orderBy中可能添加索引的列信息
func FindSubquery ¶
FindSubquery 拆分subquery,获取最深层的subquery 为索引优化获取subquery中包含的列信息
func FindWhereEQ ¶
FindWhereEQ 找到Where中的等值条件
func FindWhereINEQ ¶
FindWhereINEQ 找到Where条件中的非等值条件
func GetMeta ¶
GetMeta 获取元数据信息,构建到db->table层级。 从 SQL 或 Statement 中获取表信息,并返回。当 meta 不为 nil 时,返回值会将新老 meta 合并去重
func GetSubqueryDepth ¶
GetSubqueryDepth 获取一条SQL的嵌套深度
func GetTableFromExprs ¶
GetTableFromExprs 从sqlparser.Exprs中获取所有的库表
func MergeAlterTables ¶
MergeAlterTables mergealter: 将同一张表的多条 ALTER 语句合成一条 ALTER 语句 @input: sql, alter string @output: [[db.]table]sql, 如果找不到 DB,key 为表名;如果找得到 DB,key 为 db.table
func PrintPrettyStmtNode ¶
func PrintPrettyStmtNode(sql, charset, collation string)
PrintPrettyStmtNode 打印TiParse语法树
func PrintPrettyVitessStmtNode ¶ added in v0.11.1
func PrintPrettyVitessStmtNode(sql string)
PrintPrettyVitessStmtNode print vitess AST struct data
func QueryType ¶ added in v0.11.1
QueryType get query type such as SELECT/INSERT/DELETE/CREATE/ALTER
func SchemaMetaInfo ¶ added in v0.11.1
SchemaMetaInfo get used database, table name from SQL
func SplitStatement ¶
SplitStatement SQL切分 return original sql, remove comment sql, left over buf
func StmtNode2JSON ¶ added in v0.11.1
StmtNode2JSON TiParse AST tree into json format
func VitessStmtNode2JSON ¶ added in v0.11.1
VitessStmtNode2JSON vitess AST tree into json format
Types ¶
type Expression ¶ added in v0.11.1
type Expression string
Expression describe sql expression type
const ( // WhereExpression 用于标记 where WhereExpression Expression = "where" // JoinExpression 用于标记 join JoinExpression Expression = "join" // GroupByExpression 用于标记 group by GroupByExpression Expression = "group by" // OrderByExpression 用于标记 order by OrderByExpression Expression = "order by" )
type NodeItem ¶
type NodeItem struct { ID int // NodeItem 在 List 中的编号,与顺序有关 Prev *NodeItem // 前一个节点 Self sqlparser.SQLNode // 自身指向的AST Node Next *NodeItem // 后一个节点 Array *NodeList // 指针指向所在的链表,用于快速跳转node }
NodeItem 链表节点
type NodeList ¶
NodeList 链表结构体
func NewNodeList ¶
NewNodeList 从抽象语法树中构造一个链表
type Rewrite ¶
type Rewrite struct { SQL string NewSQL string Stmt sqlparser.Statement Columns common.TableColumns }
Rewrite 用于重写SQL
func NewRewrite ¶
NewRewrite 返回一个*Rewrite对象,如果SQL无法被正常解析,将错误输出到日志中,返回一个nil
func (*Rewrite) RewriteAddOrderByNull ¶
RewriteAddOrderByNull orderbynull: 对应 CLA.008,GROUP BY 无排序要求时添加 ORDER BY NULL
func (*Rewrite) RewriteAlwaysTrue ¶
RewriteAlwaysTrue always true: 删除恒真条件
func (*Rewrite) RewriteAutoIncrement ¶
RewriteAutoIncrement autoincrement: 将auto_increment设置为1
func (*Rewrite) RewriteCountStar ¶
RewriteCountStar countstar: 将COUNT(col)改写为COUNT(*) COUNT(DISTINCT col)不能替换为COUNT(*)
func (*Rewrite) RewriteDML2Select ¶
RewriteDML2Select dml2select: DML 转成 SELECT,兼容低版本的 EXPLAIN
func (*Rewrite) RewriteDelimiter ¶
RewriteDelimiter delimiter: 补分号,可以指定不同的DELIMITER
func (*Rewrite) RewriteDistinctStar ¶
RewriteDistinctStar distinctstar: 对应DIS.003,将多余的`DISTINCT *`删除
func (*Rewrite) RewriteGroupByConst ¶
RewriteGroupByConst 对应CLA.004,将GROUP BY CONST替换为列名 TODO:
func (*Rewrite) RewriteHaving ¶
RewriteHaving having: 对应CLA.013,使用 WHERE 过滤条件替代 HAVING
func (*Rewrite) RewriteInNull ¶
RewriteInNull innull: TODO: 对应ARG.004
func (*Rewrite) RewriteInnoDB ¶
RewriteInnoDB InnoDB: 为未指定 Engine 的表默认添加 InnoDB 引擎,将其他存储引擎转为 InnoDB
func (*Rewrite) RewriteInsertColumns ¶
RewriteInsertColumns insertcolumns: 对应COL.002,INSERT补全列名
func (*Rewrite) RewriteIntWidth ¶
RewriteIntWidth intwidth: int 类型转为 int(10),bigint 类型转为 bigint(20)
func (*Rewrite) RewriteJoin2SubQuery ¶
RewriteJoin2SubQuery join2sub: TODO: https://mariadb.com/kb/en/library/subqueries-and-joins/
func (*Rewrite) RewriteOr2In ¶
RewriteOr2In or2in: 同一列的 OR 过滤条件使用 IN() 替代,如果值有相等的会进行合并
func (*Rewrite) RewriteOr2Union ¶
RewriteOr2Union or2union: 将 OR 查询转写为 UNION ALL TODO: 暂无对应 HeuristicRules TODO: https://sqlperformance.com/2014/09/sql-plan/rewriting-queries-improve-performance
func (*Rewrite) RewriteRemoveDMLOrderBy ¶
RewriteRemoveDMLOrderBy dmlorderby: 对应 RES.004,删除无 LIMIT 条件时 UPDATE, DELETE 中包含的 ORDER BY
func (*Rewrite) RewriteRmParenthesis ¶
RewriteRmParenthesis rmparenthesis: 去除无意义的括号
func (*Rewrite) RewriteStandard ¶
RewriteStandard standard: 使用 vitess 提供的 String 功能将抽象语法树转写回 SQL,注意:这可能转写失败。
func (*Rewrite) RewriteStar2Columns ¶
RewriteStar2Columns star2columns: 对应COL.001,SELECT补全*指代的列名
func (*Rewrite) RewriteSubQuery2Join ¶
RewriteSubQuery2Join 将 subquery 转写成 join
func (*Rewrite) RewriteTruncate ¶
RewriteTruncate truncate: DELETE 全表修改为 TRUNCATE TABLE
func (*Rewrite) RewriteUnionAll ¶
RewriteUnionAll unionall: 不介意重复数据的情况下使用 union all 替换 union