From 0b4a70a80202227570c13a5a686d46a09c4649ce Mon Sep 17 00:00:00 2001 From: Hangjie Mo Date: Fri, 29 Apr 2022 13:06:53 +0800 Subject: [PATCH] session: add parser failed error to sessionVars.StmtCtx (#34311) close pingcap/tidb#34276 --- executor/show_test.go | 5 +++++ server/server_test.go | 7 ++++--- session/session.go | 4 +++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/executor/show_test.go b/executor/show_test.go index 0889b36359dfd..f8772d3a4b50a 100644 --- a/executor/show_test.go +++ b/executor/show_test.go @@ -141,6 +141,11 @@ func TestShowErrors(t *testing.T) { _, _ = tk.Exec(testSQL) tk.MustQuery("show errors").Check(testkit.RowsWithSep("|", "Error|1050|Table 'test.show_errors' already exists")) + + // eliminate previous errors + tk.MustExec("select 1") + _, _ = tk.Exec("create invalid") + tk.MustQuery("show errors").Check(testkit.RowsWithSep("|", "Error|1064|You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 14 near \"invalid\" ")) } func TestShowWarningsForExprPushdown(t *testing.T) { diff --git a/server/server_test.go b/server/server_test.go index c09bfbd7a9c20..afd53f547d7df 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -2057,9 +2057,10 @@ func (cli *testServerClient) runTestInfoschemaClientErrors(t *testing.T) { errCode: 1068, // multiple pkeys }, { - stmt: "gibberish", - incrementErrors: true, - errCode: 1064, // parse error + stmt: "gibberish", + incrementWarnings: true, + incrementErrors: true, + errCode: 1064, // parse error }, } diff --git a/session/session.go b/session/session.go index 5dd260959112c..12be11d302975 100644 --- a/session/session.go +++ b/session/session.go @@ -1503,6 +1503,7 @@ func (s *session) Parse(ctx context.Context, sql string) ([]ast.StmtNode, error) stmts, warns, err := s.ParseSQL(ctx, sql, s.sessionVars.GetParseParams()...) if err != nil { s.rollbackOnError(ctx) + err = util.SyntaxError(err) // Only print log message when this SQL is from the user. // Mute the warning for internal SQLs. @@ -1512,8 +1513,9 @@ func (s *session) Parse(ctx context.Context, sql string) ([]ast.StmtNode, error) } else { logutil.Logger(ctx).Warn("parse SQL failed", zap.Error(err), zap.String("SQL", sql)) } + s.sessionVars.StmtCtx.AppendError(err) } - return nil, util.SyntaxError(err) + return nil, err } durParse := time.Since(parseStartTime)