Skip to content

Commit

Permalink
better error message (pingcap#1) (pingcap#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
lnhote authored and ngaut committed Jan 6, 2019
1 parent 1252487 commit 6934202
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
13 changes: 11 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "", "")
Expand All @@ -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", "", "")
Expand Down

0 comments on commit 6934202

Please sign in to comment.