Skip to content

Commit

Permalink
*: ignore unknown hint and return warning instead of return error(#8685
Browse files Browse the repository at this point in the history
…) (#8766)
  • Loading branch information
lysu authored and zz-jason committed Dec 22, 2018
1 parent 8ba8096 commit 4926253
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ast/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (ts *testAstFormatSuite) TestAstFormat(c *C) {
for _, tt := range testcases {
expr := fmt.Sprintf("select %s", tt.input)
charset, collation := getDefaultCharsetAndCollate()
stmts, err := parser.New().Parse(expr, charset, collation)
stmts, _, err := parser.New().Parse(expr, charset, collation)
node := stmts[0].(*ast.SelectStmt).Fields.Fields[0].Expr
c.Assert(err, IsNil)

Expand Down
4 changes: 2 additions & 2 deletions ast/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ constraint foreign key (jobabbr) references ffxi_jobtype (jobabbr) on delete cas
);
`
parse := parser.New()
stmts, err := parse.Parse(sql, "", "")
stmts, _, err := parse.Parse(sql, "", "")
c.Assert(err, IsNil)
for _, stmt := range stmts {
stmt.Accept(visitor{})
Expand All @@ -119,7 +119,7 @@ show create table t;
load data infile '/tmp/t.csv' into table t fields terminated by 'ab' enclosed by 'b';`

p := parser.New()
stmts, err := p.Parse(sql, "", "")
stmts, _, err := p.Parse(sql, "", "")
c.Assert(err, IsNil)
for _, stmt := range stmts {
stmt.Accept(visitor{})
Expand Down
6 changes: 3 additions & 3 deletions cmd/explaintest/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ function build_importer()
importer="./importer"
echo "building importer binary: $importer"
rm -rf $importer
go build -o $importer github.com/pingcap/tidb/cmd/importer
GO111MODULE=on go build -o $importer github.com/pingcap/tidb/cmd/importer
}

function build_tidb_server()
{
tidb_server="./explaintest_tidb-server"
echo "building tidb-server binary: $tidb_server"
rm -rf $tidb_server
go build -race -o $tidb_server github.com/pingcap/tidb/tidb-server
GO111MODULE=on go build -race -o $tidb_server github.com/pingcap/tidb/tidb-server
}

function build_explain_test()
{
echo "building explain-test binary: $explain_test"
rm -rf $explain_test
go build -o $explain_test
GO111MODULE=on go build -o $explain_test
}

while getopts "t:s:r:b:v:c:i:h" opt; do
Expand Down
6 changes: 5 additions & 1 deletion executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ func (e *PrepareExec) Next(ctx context.Context, chk *chunk.Chunk) error {
if sqlParser, ok := e.ctx.(sqlexec.SQLParser); ok {
stmts, err = sqlParser.ParseSQL(e.sqlText, charset, collation)
} else {
stmts, err = parser.New().Parse(e.sqlText, charset, collation)
var warns []error
stmts, warns, err = parser.New().Parse(e.sqlText, charset, collation)
for _, warn := range warns {
e.ctx.GetSessionVars().StmtCtx.AppendWarning(warn)
}
}
if err != nil {
return errors.Trace(err)
Expand Down
10 changes: 10 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3653,6 +3653,16 @@ func (s *testIntegrationSuite) TestDecimalMul(c *C) {
res.Check(testkit.Rows("0.55125221922461136"))
}

func (s *testIntegrationSuite) TestUnknowHintIgnore(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test")
tk.MustExec("create table t(a int)")
tk.MustQuery("select /*+ unkown_hint(c1)*/ 1").Check(testkit.Rows("1"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 line 1 column 28 near \" 1\" (total length 30)"))
_, err := tk.Exec("select 1 from /*+ test1() */ t")
c.Assert(err, NotNil)
}

func (s *testIntegrationSuite) TestValuesInNonInsertStmt(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
Expand Down
10 changes: 8 additions & 2 deletions expression/simple_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ type simpleRewriter struct {
// The expression string must only reference the column in table Info.
func ParseSimpleExprWithTableInfo(ctx sessionctx.Context, exprStr string, tableInfo *model.TableInfo) (Expression, error) {
exprStr = "select " + exprStr
stmts, err := parser.New().Parse(exprStr, "", "")
stmts, warns, err := parser.New().Parse(exprStr, "", "")
for _, warn := range warns {
ctx.GetSessionVars().StmtCtx.AppendWarning(warn)
}
if err != nil {
return nil, errors.Trace(err)
}
Expand Down Expand Up @@ -72,7 +75,10 @@ func RewriteSimpleExprWithTableInfo(ctx sessionctx.Context, tbl *model.TableInfo
// The expression string must only reference the column in the given schema.
func ParseSimpleExprsWithSchema(ctx sessionctx.Context, exprStr string, schema *Schema) ([]Expression, error) {
exprStr = "select " + exprStr
stmts, err := parser.New().Parse(exprStr, "", "")
stmts, warns, err := parser.New().Parse(exprStr, "", "")
for _, warn := range warns {
ctx.GetSessionVars().StmtCtx.AppendWarning(warn)
}
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ require (
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.2.1 // indirect
)

replace github.com/pingcap/parser => github.com/lysu/parser v0.0.0-20181221063414-a86a9ff7a7de
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/lysu/parser v0.0.0-20181221063414-a86a9ff7a7de h1:CLePfxW7/iqT99EOviIa+TKMQDOP4JVg77Pak5hQeFM=
github.com/lysu/parser v0.0.0-20181221063414-a86a9ff7a7de/go.mod h1:CJk6LPzPxAcwHIcTugQaKxzvTR10NDJ5ln8XR7uYTJk=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/montanaflynn/stats v0.0.0-20151014174947-eeaced052adb h1:bsjNADsjHq0gjU7KO7zwoX5k3HtFdf6TDzB3ncl5iUs=
Expand Down
8 changes: 6 additions & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ func (s *session) SetGlobalSysVar(name, value string) error {
return errors.Trace(err)
}

func (s *session) ParseSQL(ctx context.Context, sql, charset, collation string) ([]ast.StmtNode, error) {
func (s *session) ParseSQL(ctx context.Context, sql, charset, collation string) ([]ast.StmtNode, []error, error) {
s.parser.SetSQLMode(s.sessionVars.SQLMode)
return s.parser.Parse(sql, charset, collation)
}
Expand Down Expand Up @@ -799,7 +799,7 @@ func (s *session) execute(ctx context.Context, sql string) (recordSets []sqlexec

// Step1: Compile query string to abstract syntax trees(ASTs).
startTS := time.Now()
stmtNodes, err := s.ParseSQL(ctx, sql, charsetInfo, collation)
stmtNodes, warns, err := s.ParseSQL(ctx, sql, charsetInfo, collation)
if err != nil {
s.rollbackOnError(ctx)
log.Warnf("con:%d parse error:\n%v\n%s", connID, err, sql)
Expand Down Expand Up @@ -836,6 +836,10 @@ func (s *session) execute(ctx context.Context, sql string) (recordSets []sqlexec
// return the first recordset if client doesn't support ClientMultiResults.
recordSets = recordSets[:1]
}

for _, warn := range warns {
s.sessionVars.StmtCtx.AppendWarning(warn)
}
return recordSets, nil
}

Expand Down
5 changes: 4 additions & 1 deletion session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ func Parse(ctx sessionctx.Context, src string) ([]ast.StmtNode, error) {
charset, collation := ctx.GetSessionVars().GetCharsetInfo()
p := parser.New()
p.SetSQLMode(ctx.GetSessionVars().SQLMode)
stmts, err := p.Parse(src, charset, collation)
stmts, warns, err := p.Parse(src, charset, collation)
for _, warn := range warns {
ctx.GetSessionVars().StmtCtx.AppendWarning(warn)
}
if err != nil {
log.Warnf("compiling %s, error: %v", src, err)
return nil, errors.Trace(err)
Expand Down
2 changes: 1 addition & 1 deletion table/tables/gen_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (nr *nameResolver) Leave(inNode ast.Node) (node ast.Node, ok bool) {
func parseExpression(expr string) (node ast.ExprNode, err error) {
expr = fmt.Sprintf("select %s", expr)
charset, collation := charset.GetDefaultCharsetAndCollate()
stmts, err := parser.New().Parse(expr, charset, collation)
stmts, _, err := parser.New().Parse(expr, charset, collation)
if err == nil {
node = stmts[0].(*ast.SelectStmt).Fields.Fields[0].Expr
}
Expand Down

0 comments on commit 4926253

Please sign in to comment.