Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: ignore unknown hint and return warning instead of return error(#8685) #8766

Merged
merged 5 commits into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 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 @@ -727,7 +727,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 @@ -796,7 +796,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 @@ -833,6 +833,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 @@ -124,7 +124,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