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 a parser error #8685

Merged
merged 9 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 5 additions & 1 deletion executor/prepared.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ func (e *PrepareExec) Next(ctx context.Context, chk *chunk.Chunk) error {
} else {
p := parser.New()
p.EnableWindowFunc(vars.EnableWindowFunction)
stmts, err = p.Parse(e.sqlText, charset, collation)
var warns []error
stmts, warns, err = p.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 @@ -3697,6 +3697,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, 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, err
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ require (
sourcegraph.com/sourcegraph/appdash v0.0.0-20180531100431-4c381bd170b4
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67
)

replace github.com/pingcap/parser => github.com/lysu/parser v0.0.0-20181221031749-30d2d93c9182
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5 h1:2U0HzY8BJ8hVwDK
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/lysu/parser v0.0.0-20181218132805-b5f5b4fbae0a h1:sJ+0Jo2G1SZxt1Obequ9Hk/jtVK4x0Y2UM9CXASaiL0=
github.com/lysu/parser v0.0.0-20181218132805-b5f5b4fbae0a/go.mod h1:CJk6LPzPxAcwHIcTugQaKxzvTR10NDJ5ln8XR7uYTJk=
github.com/lysu/parser v0.0.0-20181221031749-30d2d93c9182 h1:XIPHxFmMZ9dfDXbFWfQ5p6kuwkTF6VRgbF3wLaYioKo=
github.com/lysu/parser v0.0.0-20181221031749-30d2d93c9182/go.mod h1:CJk6LPzPxAcwHIcTugQaKxzvTR10NDJ5ln8XR7uYTJk=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
Expand Down
8 changes: 6 additions & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,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) {
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
span1 := span.Tracer().StartSpan("session.ParseSQL", opentracing.ChildOf(span.Context()))
defer span1.Finish()
Expand Down Expand Up @@ -885,7 +885,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 @@ -922,6 +922,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) {
p := parser.New()
p.EnableWindowFunc(ctx.GetSessionVars().EnableWindowFunction)
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