diff --git a/lexer.go b/lexer.go index bd40056ec..f1f69cd5b 100644 --- a/lexer.go +++ b/lexer.go @@ -123,11 +123,17 @@ func (s *Scanner) stmtText() string { // Scanner satisfies yyLexer interface which need this function. func (s *Scanner) Errorf(format string, a ...interface{}) { str := fmt.Sprintf(format, a...) - val := s.r.s[s.r.pos().Offset:] + col := s.r.p.Col + startPos := s.stmtStartPos + if s.r.s[startPos] == '\n' { + startPos++ + col-- + } + val := s.r.s[startPos:] if len(val) > 2048 { val = val[:2048] } - err := fmt.Errorf("line %d column %d near \"%s\"%s (total length %d)", s.r.p.Line, s.r.p.Col, val, str, len(s.r.s)) + err := fmt.Errorf("line %d column %d near \"%s\"%s (total length %d)", s.r.p.Line, col, val, str, len(s.r.s)) s.errs = append(s.errs, err) } diff --git a/parser_test.go b/parser_test.go index 42e247c73..160e871c7 100644 --- a/parser_test.go +++ b/parser_test.go @@ -1833,13 +1833,13 @@ func (s *testParserSuite) TestHintError(c *C) { stmt, warns, err := parser.Parse("select /*+ tidb_unknow(T1,t2) */ c1, c2 from t1, t2 where t1.c1 = t2.c1", "", "") c.Assert(err, IsNil) c.Assert(len(warns), Equals, 1) - c.Assert(warns[0].Error(), Equals, "line 1 column 32 near \" c1, c2 from t1, t2 where t1.c1 = t2.c1\" (total length 71)") + c.Assert(warns[0].Error(), Equals, "line 1 column 32 near \"select /*+ tidb_unknow(T1,t2) */ c1, c2 from t1, t2 where t1.c1 = t2.c1\" (total length 71)") c.Assert(len(stmt[0].(*ast.SelectStmt).TableHints), Equals, 0) stmt, warns, err = parser.Parse("select /*+ tidb_unknow(T1,t2, 1) TIDB_INLJ(t1, T2) */ c1, c2 from t1, t2 where t1.c1 = t2.c1", "", "") c.Assert(len(stmt[0].(*ast.SelectStmt).TableHints), Equals, 0) c.Assert(err, IsNil) c.Assert(len(warns), Equals, 1) - c.Assert(warns[0].Error(), Equals, "line 1 column 53 near \" c1, c2 from t1, t2 where t1.c1 = t2.c1\" (total length 92)") + c.Assert(warns[0].Error(), Equals, "line 1 column 53 near \"select /*+ tidb_unknow(T1,t2, 1) TIDB_INLJ(t1, T2) */ c1, c2 from t1, t2 where t1.c1 = t2.c1\" (total length 92)") stmt, _, err = parser.Parse("select c1, c2 from /*+ tidb_unknow(T1,t2) */ t1, t2 where t1.c1 = t2.c1", "", "") c.Assert(err, NotNil) stmt, _, err = parser.Parse("select1 /*+ TIDB_INLJ(t1, T2) */ c1, c2 from t1, t2 where t1.c1 = t2.c1", "", "") @@ -1850,6 +1850,15 @@ func (s *testParserSuite) TestHintError(c *C) { c.Assert(err, IsNil) } +func (s *testParserSuite) TestErrorMsg(c *C) { + parser := New() + _, _, err := parser.Parse("select1 1", "", "") + c.Assert(err.Error(), Equals, "line 1 column 7 near \"select1 1\" (total length 9)") + + _, _, err = parser.Parse("select a1 from t1\nwhere t1.a2 = 1;\nselect1 1", "", "") + c.Assert(err.Error(), Equals, "line 3 column 7 near \"select1 1\" (total length 44)") +} + func (s *testParserSuite) TestOptimizerHints(c *C) { parser := New() stmt, _, err := parser.Parse("select /*+ tidb_SMJ(T1,t2) tidb_smj(T3,t4) */ c1, c2 from t1, t2 where t1.c1 = t2.c1", "", "")