ast

package
v0.11.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package ast is an interface for Abstract Syntax Tree parser

Index

Constants

View Source
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

View Source
var RewriteRules []Rule

RewriteRules SQL重写规则,注意这个规则是有序的,先后顺序不能乱

View Source
var TokenString = map[int]string{}/* 214 elements not displayed */

TokenString sqlparser tokens

Functions

func AlterAffectTable

func AlterAffectTable(stmt sqlparser.Statement) string

AlterAffectTable 获取ALTER影响的库表名,返回:`db`.`table`

func Compress

func Compress(sql string) string

Compress compress sql this method is inspired by eversql.com

func FindAllCols

func FindAllCols(node sqlparser.SQLNode, targets ...Expression) []*common.Column

FindAllCols 获取 AST 中某个节点下所有的 columns

func FindAllCondition

func FindAllCondition(node sqlparser.SQLNode) []interface{}

FindAllCondition 获取 AST 中所有的 condition 条件

func FindColumn

func FindColumn(node sqlparser.SQLNode) []*common.Column

FindColumn 从传入的 node 中获取所有可能加索引的的 column 信息

func FindEQColsInJoinCond

func FindEQColsInJoinCond(node sqlparser.SQLNode) []*common.Column

FindEQColsInJoinCond 获取 join condition 中应转为whereEQ条件的列

func FindEQColsInWhere

func FindEQColsInWhere(node sqlparser.SQLNode) []*common.Column

FindEQColsInWhere 获取等值条件信息 将所有值得加索引的condition条件信息进行过滤

func FindGroupByCols

func FindGroupByCols(node sqlparser.SQLNode) []*common.Column

FindGroupByCols 获取groupBy中可能需要加索引的列信息

func FindINEQColsInJoinCond

func FindINEQColsInJoinCond(node sqlparser.SQLNode) []*common.Column

FindINEQColsInJoinCond 获取 join condition 中应转为whereINEQ条件的列

func FindINEQColsInWhere

func FindINEQColsInWhere(node sqlparser.SQLNode) []*common.Column

FindINEQColsInWhere 获取非等值条件中可能需要加索引的列 将所有值得加索引的condition条件信息进行过滤 TODO: 将 where 条件中隐含的 join 条件合并到 join condition中

func FindJoinCols

func FindJoinCols(node sqlparser.SQLNode) [][]*common.Column

FindJoinCols 获取 join condition 中使用到的列(必须是 `列 operator 列` 的情况。 如果列对应的值或是 function,则应该移到where condition中) 某些 where 条件隐含在 join 条件中(INNER JOIN)

func FindJoinTable

func FindJoinTable(node sqlparser.SQLNode, meta common.Meta) common.Meta

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

func FindOrderByCols(node sqlparser.SQLNode) []*common.Column

FindOrderByCols 为索引优化获取orderBy中可能添加索引的列信息

func FindSubquery

func FindSubquery(depth int, node sqlparser.SQLNode, queries ...string) []string

FindSubquery 拆分subquery,获取最深层的subquery 为索引优化获取subquery中包含的列信息

func FindWhereEQ

func FindWhereEQ(node sqlparser.SQLNode) []*common.Column

FindWhereEQ 找到Where中的等值条件

func FindWhereINEQ

func FindWhereINEQ(node sqlparser.SQLNode) []*common.Column

FindWhereINEQ 找到Where条件中的非等值条件

func GetMeta

func GetMeta(stmt sqlparser.Statement, meta common.Meta) common.Meta

GetMeta 获取元数据信息,构建到db->table层级。 从 SQL 或 Statement 中获取表信息,并返回。当 meta 不为 nil 时,返回值会将新老 meta 合并去重

func GetSubqueryDepth

func GetSubqueryDepth(node sqlparser.SQLNode) int

GetSubqueryDepth 获取一条SQL的嵌套深度

func GetTableFromExprs

func GetTableFromExprs(exprs sqlparser.TableExprs, metas ...common.Meta) common.Meta

GetTableFromExprs 从sqlparser.Exprs中获取所有的库表

func IsMysqlKeyword

func IsMysqlKeyword(name string) bool

IsMysqlKeyword 判断是否是关键字

func LeftNewLines

func LeftNewLines(buf []byte) int

LeftNewLines cal left new lines in space

func ListRewriteRules

func ListRewriteRules(rules []Rule)

ListRewriteRules 打印SQL重写规则

func MergeAlterTables

func MergeAlterTables(sqls ...string) map[string]string

MergeAlterTables mergealter: 将同一张表的多条 ALTER 语句合成一条 ALTER 语句 @input: sql, alter string @output: [[db.]table]sql, 如果找不到 DB,key 为表名;如果找得到 DB,key 为 db.table

func NewLines

func NewLines(buf []byte) int

NewLines cal all new lines

func Pretty

func Pretty(sql string, method string) (output string)

Pretty 格式化输出SQL

func PrintPrettyStmtNode

func PrintPrettyStmtNode(sql, charset, collation string)

PrintPrettyStmtNode 打印TiParse语法树

func PrintPrettyVitessStmtNode

func PrintPrettyVitessStmtNode(sql string)

PrintPrettyVitessStmtNode print vitess AST struct data

func QueryType

func QueryType(sql string) string

QueryType get query type such as SELECT/INSERT/DELETE/CREATE/ALTER

func RewriteRuleMatch

func RewriteRuleMatch(name string) bool

RewriteRuleMatch 检查重写规则是否生效

func SchemaMetaInfo

func SchemaMetaInfo(sql string, defaultDatabase string) []string

SchemaMetaInfo get used database, table name from SQL

func SplitStatement

func SplitStatement(buf []byte, delimiter []byte) (string, string, []byte)

SplitStatement SQL切分 return 1. original sql, 2. remove comment sql, 3. left over buf

func StmtNode2JSON

func StmtNode2JSON(sql, charset, collation string) string

StmtNode2JSON TiParse AST tree into json format

func TiParse

func TiParse(sql, charset, collation string) ([]ast.StmtNode, error)

TiParse TiDB 语法解析

func VitessStmtNode2JSON

func VitessStmtNode2JSON(sql string) string

VitessStmtNode2JSON vitess AST tree into json format

Types

type Expression

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

type NodeList struct {
	Length  int
	Head    *NodeItem
	NodeMap map[int]*NodeItem
}

NodeList 链表结构体

func NewNodeList

func NewNodeList(statement sqlparser.Statement) *NodeList

NewNodeList 从抽象语法树中构造一个链表

func (*NodeList) Add

func (l *NodeList) Add(node sqlparser.SQLNode) *NodeItem

Add 将会把一个sqlparser.SQLNode添加到节点中

func (*NodeList) First

func (l *NodeList) First() *NodeItem

First 返回链表头结点

func (*NodeList) Last

func (l *NodeList) Last() *NodeItem

Last 返回链表末尾节点

func (*NodeList) Remove

func (l *NodeList) Remove(node *NodeItem) error

Remove 从链表中移除一个节点

type Rewrite

type Rewrite struct {
	SQL     string
	NewSQL  string
	Stmt    sqlparser.Statement
	Columns common.TableColumns
}

Rewrite 用于重写SQL

func NewRewrite

func NewRewrite(sql string) *Rewrite

NewRewrite 返回一个*Rewrite对象,如果SQL无法被正常解析,将错误输出到日志中,返回一个nil

func (*Rewrite) Rewrite

func (rw *Rewrite) Rewrite() *Rewrite

Rewrite 入口函数

func (*Rewrite) RewriteAddOrderByNull

func (rw *Rewrite) RewriteAddOrderByNull() *Rewrite

RewriteAddOrderByNull orderbynull: 对应 CLA.008,GROUP BY 无排序要求时添加 ORDER BY NULL

func (*Rewrite) RewriteAlwaysTrue

func (rw *Rewrite) RewriteAlwaysTrue() (reWriter *Rewrite)

RewriteAlwaysTrue always true: 删除恒真条件

func (*Rewrite) RewriteAutoIncrement

func (rw *Rewrite) RewriteAutoIncrement() *Rewrite

RewriteAutoIncrement autoincrement: 将auto_increment设置为1

func (*Rewrite) RewriteCountStar

func (rw *Rewrite) RewriteCountStar() *Rewrite

RewriteCountStar countstar: 将COUNT(col)改写为COUNT(*) COUNT(DISTINCT col)不能替换为COUNT(*)

func (*Rewrite) RewriteDML2Select

func (rw *Rewrite) RewriteDML2Select() *Rewrite

RewriteDML2Select dml2select: DML 转成 SELECT,兼容低版本的 EXPLAIN

func (*Rewrite) RewriteDelimiter

func (rw *Rewrite) RewriteDelimiter() *Rewrite

RewriteDelimiter delimiter: 补分号,可以指定不同的DELIMITER

func (*Rewrite) RewriteDistinctStar

func (rw *Rewrite) RewriteDistinctStar() *Rewrite

RewriteDistinctStar distinctstar: 对应DIS.003,将多余的`DISTINCT *`删除

func (*Rewrite) RewriteGroupByConst

func (rw *Rewrite) RewriteGroupByConst() *Rewrite

RewriteGroupByConst 对应CLA.004,将GROUP BY CONST替换为列名 TODO:

func (*Rewrite) RewriteHaving

func (rw *Rewrite) RewriteHaving() *Rewrite

RewriteHaving having: 对应CLA.013,使用 WHERE 过滤条件替代 HAVING

func (*Rewrite) RewriteInNull

func (rw *Rewrite) RewriteInNull() *Rewrite

RewriteInNull innull: TODO: 对应ARG.004

func (*Rewrite) RewriteInnoDB

func (rw *Rewrite) RewriteInnoDB() *Rewrite

RewriteInnoDB InnoDB: 为未指定 Engine 的表默认添加 InnoDB 引擎,将其他存储引擎转为 InnoDB

func (*Rewrite) RewriteInsertColumns

func (rw *Rewrite) RewriteInsertColumns() *Rewrite

RewriteInsertColumns insertcolumns: 对应COL.002,INSERT补全列名

func (*Rewrite) RewriteIntWidth

func (rw *Rewrite) RewriteIntWidth() *Rewrite

RewriteIntWidth intwidth: int 类型转为 int(10),bigint 类型转为 bigint(20)

func (*Rewrite) RewriteJoin2SubQuery

func (rw *Rewrite) RewriteJoin2SubQuery() *Rewrite

RewriteJoin2SubQuery join2sub: TODO: https://mariadb.com/kb/en/library/subqueries-and-joins/

func (*Rewrite) RewriteOr2In

func (rw *Rewrite) RewriteOr2In() *Rewrite

RewriteOr2In or2in: 同一列的 OR 过滤条件使用 IN() 替代,如果值有相等的会进行合并

func (*Rewrite) RewriteOr2Union

func (rw *Rewrite) RewriteOr2Union() *Rewrite

RewriteOr2Union or2union: 将 OR 查询转写为 UNION ALL TODO: 暂无对应 HeuristicRules TODO: https://sqlperformance.com/2014/09/sql-plan/rewriting-queries-improve-performance

func (*Rewrite) RewriteReg2Select

func (rw *Rewrite) RewriteReg2Select() *Rewrite

func (*Rewrite) RewriteRemoveDMLOrderBy

func (rw *Rewrite) RewriteRemoveDMLOrderBy() *Rewrite

RewriteRemoveDMLOrderBy dmlorderby: 对应 RES.004,删除无 LIMIT 条件时 UPDATE, DELETE 中包含的 ORDER BY

func (*Rewrite) RewriteRmParenthesis

func (rw *Rewrite) RewriteRmParenthesis() *Rewrite

RewriteRmParenthesis rmparenthesis: 去除无意义的括号

func (*Rewrite) RewriteStandard

func (rw *Rewrite) RewriteStandard() *Rewrite

RewriteStandard standard: 使用 vitess 提供的 String 功能将抽象语法树转写回 SQL,注意:这可能转写失败。

func (*Rewrite) RewriteStar2Columns

func (rw *Rewrite) RewriteStar2Columns() *Rewrite

RewriteStar2Columns star2columns: 对应COL.001,SELECT补全*指代的列名

func (*Rewrite) RewriteSubQuery2Join

func (rw *Rewrite) RewriteSubQuery2Join() *Rewrite

RewriteSubQuery2Join 将 subquery 转写成 join

func (*Rewrite) RewriteTruncate

func (rw *Rewrite) RewriteTruncate() *Rewrite

RewriteTruncate truncate: DELETE 全表修改为 TRUNCATE TABLE

func (*Rewrite) RewriteUnionAll

func (rw *Rewrite) RewriteUnionAll() *Rewrite

RewriteUnionAll unionall: 不介意重复数据的情况下使用 union all 替换 union

type Rule

type Rule struct {
	Name        string                  `json:"Name"`
	Description string                  `json:"Description"`
	Original    string                  `json:"Original"` // 错误示范。为空或"暂不支持"不会出现在list-rewrite-rules中
	Suggest     string                  `json:"Suggest"`  // 正确示范。
	Func        func(*Rewrite) *Rewrite `json:"-"`        // 如果不定义 Func 需要多条 SQL 联动改写
}

Rule SQL重写规则

type Token

type Token struct {
	Type int
	Val  string
	// contains filtered or unexported fields
}

Token 基本定义

func Tokenize

func Tokenize(sql string) []Token

Tokenize 序列化 token,区别于 Tokenizer 函数,这个函数是 soar built-in 实现的切词

func Tokenizer

func Tokenizer(sql string) []Token

Tokenizer 用于初始化 token,区别于 Tokenize 函数,这个函数使用 vitess 的切词方式

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL