Skip to content

Commit da7b8d5

Browse files
committed
Fix error matching issue due to wrong error.query trimming in find_suitable_error
1 parent 58765b9 commit da7b8d5

File tree

3 files changed

+88
-20
lines changed

3 files changed

+88
-20
lines changed

cratedb_sqlparse_js/cratedb_sqlparse/parser.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import SqlBaseParser from "./generated_parser/SqlBaseParser.js";
33
import {CommonTokenStream, ErrorListener, InputStream, Interval, Token} from "antlr4";
44
import {AstBuilder} from "./AstBuilder.js";
55
import {Metadata} from "./models.js"
6+
67
function BEGIN_DOLLAR_QUOTED_STRING_action(localctx, actionIndex) {
78
if (actionIndex === 0) {
89
this.tags.push(this.text);
@@ -180,16 +181,6 @@ export class Statement {
180181
}
181182
}
182183

183-
/**
184-
*
185-
* @param {string} string
186-
* @returns {string}
187-
*/
188-
function trim(string) {
189-
return string.replace(/^\s+|\s+$/gm, '');
190-
}
191-
192-
193184
function findSuitableError(statement, errors) {
194185
for (const error of errors) {
195186
let errorQuery = error.query;
@@ -198,7 +189,7 @@ function findSuitableError(statement, errors) {
198189
errorQuery = errorQuery.substring(0, errorQuery.length - 1);
199190
}
200191

201-
errorQuery = trim(errorQuery);
192+
errorQuery = errorQuery.trimStart().trimEnd()
202193

203194
// If a good match error_query contains statement.query
204195
if (errorQuery.includes(statement.query)) {
@@ -263,7 +254,6 @@ export function sqlparse(query, raise_exception = false) {
263254
console.error("Could not match errors to queries, too much ambiguity, please report it opening an issue with the query.")
264255
}
265256

266-
267257
const stmtEnricher = new AstBuilder()
268258

269259
for (const stmt of statements) {

cratedb_sqlparse_js/tests/exceptions.test.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ test('White or special characters should not avoid exception catching', () => {
9494
}
9595
})
9696

97-
test('Missing token error should not panic', ()=> {
97+
test('Missing token error should not panic', ()=> {
9898
// See https://github.com/crate/cratedb-sqlparse/issues/66
9999
sqlparse(`
100100
CREATE TABLE t01 (
@@ -104,8 +104,7 @@ test('Missing token error should not panic', ()=> {
104104
`)
105105
})
106106

107-
108-
test('Whitetest or special characters should not avoid exception catching', () => {
107+
test('Special characters should not avoid exception catching', () => {
109108
// https://github.com/crate/cratedb-sqlparse/issues/67
110109
const stmts = [
111110
`SELECT 1\n limit `,
@@ -117,4 +116,38 @@ test('Whitetest or special characters should not avoid exception catching', () =
117116
let r = sqlparse(stmt)
118117
expect(r[0].exception).toBeDefined();
119118
}
119+
})
120+
121+
test('Special query with several errors should correctly be matched regardless of spaces', () => {
122+
// See https://github.com/crate/cratedb-sqlparse/issues/107
123+
const stmts = [
124+
`
125+
SELECT A FROM tbl1 where ;
126+
SELECT 1;
127+
SELECT D, A FROM tbl1 WHERE;
128+
`,
129+
130+
`
131+
SELECT
132+
A
133+
FROM
134+
tbl1
135+
WHERE;
136+
137+
SELECT
138+
1;
139+
140+
SELECT
141+
B
142+
FROM
143+
tbl1
144+
WHERE;
145+
`
146+
]
147+
for (const stmt of stmts) {
148+
const r = sqlparse(stmt)
149+
expect(r[0].exception).toBeDefined()
150+
expect(r[1].exception).toBeNull()
151+
expect(r[2].exception).toBeDefined()
152+
}
120153
})

cratedb_sqlparse_py/tests/test_exceptions.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_sqlparse_collects_exceptions():
5858
def test_sqlparse_collects_exceptions_2():
5959
from cratedb_sqlparse import sqlparse
6060

61-
# Different combination of the query to validate
61+
# Different combination of the queries to validate
6262
r = sqlparse("""
6363
SELEC 1;
6464
SELECT A, B, C, D FROM tbl1;
@@ -73,7 +73,7 @@ def test_sqlparse_collects_exceptions_2():
7373
def test_sqlparse_collects_exceptions_3():
7474
from cratedb_sqlparse import sqlparse
7575

76-
# Different combination of the query to validate
76+
# Different combination of the queries to validate
7777
r = sqlparse("""
7878
SELECT 1;
7979
SELECT A, B, C, D FROM tbl1;
@@ -86,10 +86,13 @@ def test_sqlparse_collects_exceptions_3():
8686

8787

8888
def test_sqlparse_catches_exception():
89+
"""
90+
Special characters should not stop sqlparse from creating and matching the exception.
91+
92+
See https://github.com/crate/cratedb-sqlparse/issues/67
93+
"""
8994
from cratedb_sqlparse import sqlparse
9095

91-
# Special characters shouldn't avoid exception catching.
92-
# https://github.com/crate/cratedb-sqlparse/issues/67
9396
stmts = """
9497
SELECT 1\n limit,
9598
SELECT 1\r limit,
@@ -101,6 +104,11 @@ def test_sqlparse_catches_exception():
101104

102105

103106
def test_sqlparse_should_not_panic():
107+
"""
108+
Missing token ')' in this case, should not throw a Runtime Exception.
109+
110+
See https://github.com/crate/cratedb-sqlparse/issues/66
111+
"""
104112
from cratedb_sqlparse import sqlparse
105113

106114
sqlparse("""
@@ -110,4 +118,41 @@ def test_sqlparse_should_not_panic():
110118
);
111119
""")[0]
112120

113-
# That's it, it shouldn't raise a runtime Exception.
121+
122+
def test_sqlparse_match_exceptions_spaces():
123+
"""
124+
Regardless of spaces, errors should be correctly matched to their original statement.
125+
126+
See https://github.com/crate/cratedb-sqlparse/issues/107
127+
"""
128+
from cratedb_sqlparse import sqlparse
129+
130+
stmts = [
131+
"""
132+
SELECT A FROM tbl1 where ;
133+
SELECT 1;
134+
SELECT D, A FROM tbl1 WHERE;
135+
""",
136+
"""
137+
SELECT
138+
A
139+
FROM
140+
tbl1
141+
WHERE;
142+
143+
SELECT
144+
1;
145+
146+
SELECT
147+
B
148+
FROM
149+
tbl1
150+
WHERE;
151+
""",
152+
]
153+
154+
for stmt in stmts:
155+
r = sqlparse(stmt)
156+
assert r[0]
157+
assert r[1]
158+
assert r[2]

0 commit comments

Comments
 (0)