Skip to content

Commit

Permalink
Eliminate duplicated SQL parsing (#76)
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoguang Sun <[email protected]>
  • Loading branch information
sunxiaoguang authored Jan 5, 2022
1 parent b758675 commit b511fd4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/proxy/driver/queryctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (q *QueryCtxImpl) Execute(ctx context.Context, sql string) (*gomysql.Result
tableName := wast.ExtractFirstTableNameFromStmt(stmt)
ctx = wast.CtxWithAstTableName(ctx, tableName)

sqlParadigm, err := q.extractSqlParadigm(ctx, sql)
sqlParadigm, err := extractStmtParadigm(stmt)
if err != nil {
return nil, err
}
Expand Down
18 changes: 11 additions & 7 deletions pkg/proxy/driver/queryctx_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"hash/crc32"
"strings"

"github.com/tidb-incubator/weir/pkg/proxy/constant"
wast "github.com/tidb-incubator/weir/pkg/util/ast"
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/logutil"
gomysql "github.com/siddontang/go-mysql/mysql"
"github.com/tidb-incubator/weir/pkg/proxy/constant"
wast "github.com/tidb-incubator/weir/pkg/util/ast"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -60,17 +60,21 @@ func (q *QueryCtxImpl) getRateLimiterKey(ctx context.Context, rateLimiter RateLi
}
}

func (q *QueryCtxImpl) extractSqlParadigm(ctx context.Context, sql string) (string, error) {
charsetInfo, collation := q.sessionVars.GetCharsetInfo()
featureStmt, err := q.parser.ParseOneStmt(sql, charsetInfo, collation)
func extractStmtParadigm(stmt ast.StmtNode) (string, error) {
visitor, err := wast.ExtractAstVisit(stmt)
if err != nil {
return "", err
}
visitor, err := wast.ExtractAstVisit(featureStmt)
return visitor.SqlFeature(), nil
}

func (q *QueryCtxImpl) extractSqlParadigm(ctx context.Context, sql string) (string, error) {
charsetInfo, collation := q.sessionVars.GetCharsetInfo()
featureStmt, err := q.parser.ParseOneStmt(sql, charsetInfo, collation)
if err != nil {
return "", err
}
return visitor.SqlFeature(), nil
return extractStmtParadigm(featureStmt)
}

func (q *QueryCtxImpl) executeStmt(ctx context.Context, sql string, stmtNode ast.StmtNode) (*gomysql.Result, error) {
Expand Down

0 comments on commit b511fd4

Please sign in to comment.