Skip to content

Commit cad04da

Browse files
committed
feat!: tokenize with locations by default
- in parse_sql and try_with_sql - update test_parser_error_loc - change test_utils to parse without locations to avoid maintenance overhead BREAKING CHANGE: changes the default ParserError messages
1 parent a452a80 commit cad04da

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

src/parser/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ impl<'a> Parser<'a> {
368368
debug!("Parsing sql '{}'...", sql);
369369
let tokens = Tokenizer::new(self.dialect, sql)
370370
.with_unescape(self.options.unescape)
371-
.tokenize()?;
372-
Ok(self.with_tokens(tokens))
371+
.tokenize_with_location()?;
372+
Ok(self.with_tokens_with_locations(tokens))
373373
}
374374

375375
/// Parse potentially multiple statements
@@ -7962,14 +7962,12 @@ mod tests {
79627962

79637963
#[test]
79647964
fn test_parser_error_loc() {
7965-
// TODO: Once we thread token locations through the parser, we should update this
7966-
// test to assert the locations of the referenced token
79677965
let sql = "SELECT this is a syntax error";
79687966
let ast = Parser::parse_sql(&GenericDialect, sql);
79697967
assert_eq!(
79707968
ast,
79717969
Err(ParserError::ParserError(
7972-
"Expected [NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS, found: a"
7970+
"Expected [NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS, found: a at Line: 1, Column 16"
79737971
.to_string()
79747972
))
79757973
);

src/test_utils.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use core::fmt::Debug;
2828

2929
use crate::dialect::*;
3030
use crate::parser::{Parser, ParserError};
31+
use crate::tokenizer::Tokenizer;
3132
use crate::{ast::*, parser::ParserOptions};
3233

3334
/// Tests use the methods on this struct to invoke the parser on one or
@@ -82,8 +83,13 @@ impl TestedDialects {
8283
/// the result is the same for all tested dialects.
8384
pub fn parse_sql_statements(&self, sql: &str) -> Result<Vec<Statement>, ParserError> {
8485
self.one_of_identical_results(|dialect| {
86+
let mut tokenizer = Tokenizer::new(dialect, sql);
87+
if let Some(options) = &self.options {
88+
tokenizer = tokenizer.with_unescape(options.unescape);
89+
}
90+
let tokens = tokenizer.tokenize()?;
8591
self.new_parser(dialect)
86-
.try_with_sql(sql)?
92+
.with_tokens(tokens)
8793
.parse_statements()
8894
})
8995
// To fail the `ensure_multiple_dialects_are_tested` test:

tests/sqlparser_common.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,12 @@ fn test_eof_after_as() {
836836

837837
#[test]
838838
fn test_no_infix_error() {
839-
let res = Parser::parse_sql(&ClickHouseDialect {}, "ASSERT-URA<<");
839+
let dialects = TestedDialects {
840+
dialects: vec![Box::new(ClickHouseDialect {})],
841+
options: None,
842+
};
843+
844+
let res = dialects.parse_sql_statements("ASSERT-URA<<");
840845
assert_eq!(
841846
ParserError::ParserError("No infix parser for token ShiftLeft".to_string()),
842847
res.unwrap_err()
@@ -3238,19 +3243,21 @@ fn parse_alter_table_alter_column_type() {
32383243
_ => unreachable!(),
32393244
}
32403245

3241-
let res = Parser::parse_sql(
3242-
&GenericDialect {},
3243-
&format!("{alter_stmt} ALTER COLUMN is_active TYPE TEXT"),
3244-
);
3246+
let dialect = TestedDialects {
3247+
dialects: vec![Box::new(GenericDialect {})],
3248+
options: None,
3249+
};
3250+
3251+
let res =
3252+
dialect.parse_sql_statements(&format!("{alter_stmt} ALTER COLUMN is_active TYPE TEXT"));
32453253
assert_eq!(
32463254
ParserError::ParserError("Expected SET/DROP NOT NULL, SET DEFAULT, SET DATA TYPE after ALTER COLUMN, found: TYPE".to_string()),
32473255
res.unwrap_err()
32483256
);
32493257

3250-
let res = Parser::parse_sql(
3251-
&GenericDialect {},
3252-
&format!("{alter_stmt} ALTER COLUMN is_active SET DATA TYPE TEXT USING 'text'"),
3253-
);
3258+
let res = dialect.parse_sql_statements(&format!(
3259+
"{alter_stmt} ALTER COLUMN is_active SET DATA TYPE TEXT USING 'text'"
3260+
));
32543261
assert_eq!(
32553262
ParserError::ParserError("Expected end of statement, found: USING".to_string()),
32563263
res.unwrap_err()
@@ -3295,10 +3302,7 @@ fn parse_alter_table_drop_constraint() {
32953302
_ => unreachable!(),
32963303
}
32973304

3298-
let res = Parser::parse_sql(
3299-
&GenericDialect {},
3300-
&format!("{alter_stmt} DROP CONSTRAINT is_active TEXT"),
3301-
);
3305+
let res = parse_sql_statements(&format!("{alter_stmt} DROP CONSTRAINT is_active TEXT"));
33023306
assert_eq!(
33033307
ParserError::ParserError("Expected end of statement, found: TEXT".to_string()),
33043308
res.unwrap_err()

0 commit comments

Comments
 (0)