Skip to content

Commit 6b03a25

Browse files
authored
Parse SUBSTRING FROM syntax in all dialects, reflect change in the AST (#1173)
1 parent 929c646 commit 6b03a25

File tree

7 files changed

+46
-94
lines changed

7 files changed

+46
-94
lines changed

src/ast/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,13 +559,18 @@ pub enum Expr {
559559
/// ```sql
560560
/// SUBSTRING(<expr> [FROM <expr>] [FOR <expr>])
561561
/// ```
562+
/// or
563+
/// ```sql
564+
/// SUBSTRING(<expr>, <expr>, <expr>)
565+
/// ```
562566
Substring {
563567
expr: Box<Expr>,
564568
substring_from: Option<Box<Expr>>,
565569
substring_for: Option<Box<Expr>>,
566570

567-
// Some dialects use `SUBSTRING(expr [FROM start] [FOR len])` syntax while others omit FROM,
568-
// FOR keywords (e.g. Microsoft SQL Server). This flags is used for formatting.
571+
/// false if the expression is represented using the `SUBSTRING(expr [FROM start] [FOR len])` syntax
572+
/// true if the expression is represented using the `SUBSTRING(expr, start, len)` syntax
573+
/// This flag is used for formatting.
569574
special: bool,
570575
},
571576
/// ```sql

src/dialect/mod.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ pub trait Dialect: Debug + Any {
135135
fn supports_group_by_expr(&self) -> bool {
136136
false
137137
}
138-
/// Returns true if the dialect supports `SUBSTRING(expr [FROM start] [FOR len])` expressions
139-
fn supports_substring_from_for_expr(&self) -> bool {
140-
true
141-
}
142138
/// Returns true if the dialect supports `(NOT) IN ()` expressions
143139
fn supports_in_empty_list(&self) -> bool {
144140
false
@@ -325,10 +321,6 @@ mod tests {
325321
self.0.supports_group_by_expr()
326322
}
327323

328-
fn supports_substring_from_for_expr(&self) -> bool {
329-
self.0.supports_substring_from_for_expr()
330-
}
331-
332324
fn supports_in_empty_list(&self) -> bool {
333325
self.0.supports_in_empty_list()
334326
}

src/dialect/mssql.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,4 @@ impl Dialect for MsSqlDialect {
4040
fn convert_type_before_value(&self) -> bool {
4141
true
4242
}
43-
44-
fn supports_substring_from_for_expr(&self) -> bool {
45-
false
46-
}
4743
}

src/parser/mod.rs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,47 +1525,27 @@ impl<'a> Parser<'a> {
15251525
}
15261526

15271527
pub fn parse_substring_expr(&mut self) -> Result<Expr, ParserError> {
1528-
if self.dialect.supports_substring_from_for_expr() {
1529-
// PARSE SUBSTRING (EXPR [FROM 1] [FOR 3])
1530-
self.expect_token(&Token::LParen)?;
1531-
let expr = self.parse_expr()?;
1532-
let mut from_expr = None;
1533-
if self.parse_keyword(Keyword::FROM) || self.consume_token(&Token::Comma) {
1534-
from_expr = Some(self.parse_expr()?);
1535-
}
1536-
1537-
let mut to_expr = None;
1538-
if self.parse_keyword(Keyword::FOR) || self.consume_token(&Token::Comma) {
1539-
to_expr = Some(self.parse_expr()?);
1540-
}
1541-
self.expect_token(&Token::RParen)?;
1542-
1543-
Ok(Expr::Substring {
1544-
expr: Box::new(expr),
1545-
substring_from: from_expr.map(Box::new),
1546-
substring_for: to_expr.map(Box::new),
1547-
special: false,
1548-
})
1549-
} else {
1550-
// PARSE SUBSTRING(EXPR, start, length)
1551-
self.expect_token(&Token::LParen)?;
1552-
let expr = self.parse_expr()?;
1553-
1554-
self.expect_token(&Token::Comma)?;
1555-
let from_expr = Some(self.parse_expr()?);
1556-
1557-
self.expect_token(&Token::Comma)?;
1558-
let to_expr = Some(self.parse_expr()?);
1559-
1560-
self.expect_token(&Token::RParen)?;
1528+
// PARSE SUBSTRING (EXPR [FROM 1] [FOR 3])
1529+
self.expect_token(&Token::LParen)?;
1530+
let expr = self.parse_expr()?;
1531+
let mut from_expr = None;
1532+
let special = self.consume_token(&Token::Comma);
1533+
if special || self.parse_keyword(Keyword::FROM) {
1534+
from_expr = Some(self.parse_expr()?);
1535+
}
15611536

1562-
Ok(Expr::Substring {
1563-
expr: Box::new(expr),
1564-
substring_from: from_expr.map(Box::new),
1565-
substring_for: to_expr.map(Box::new),
1566-
special: true,
1567-
})
1537+
let mut to_expr = None;
1538+
if self.parse_keyword(Keyword::FOR) || self.consume_token(&Token::Comma) {
1539+
to_expr = Some(self.parse_expr()?);
15681540
}
1541+
self.expect_token(&Token::RParen)?;
1542+
1543+
Ok(Expr::Substring {
1544+
expr: Box::new(expr),
1545+
substring_from: from_expr.map(Box::new),
1546+
substring_for: to_expr.map(Box::new),
1547+
special,
1548+
})
15691549
}
15701550

15711551
pub fn parse_overlay_expr(&mut self) -> Result<Expr, ParserError> {

tests/sqlparser_common.rs

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5761,45 +5761,12 @@ fn parse_scalar_subqueries() {
57615761

57625762
#[test]
57635763
fn parse_substring() {
5764-
let from_for_supported_dialects = TestedDialects {
5765-
dialects: vec![
5766-
Box::new(GenericDialect {}),
5767-
Box::new(PostgreSqlDialect {}),
5768-
Box::new(AnsiDialect {}),
5769-
Box::new(SnowflakeDialect {}),
5770-
Box::new(HiveDialect {}),
5771-
Box::new(RedshiftSqlDialect {}),
5772-
Box::new(MySqlDialect {}),
5773-
Box::new(BigQueryDialect {}),
5774-
Box::new(SQLiteDialect {}),
5775-
Box::new(DuckDbDialect {}),
5776-
],
5777-
options: None,
5778-
};
5779-
5780-
let from_for_unsupported_dialects = TestedDialects {
5781-
dialects: vec![Box::new(MsSqlDialect {})],
5782-
options: None,
5783-
};
5784-
5785-
from_for_supported_dialects
5786-
.one_statement_parses_to("SELECT SUBSTRING('1')", "SELECT SUBSTRING('1')");
5787-
5788-
from_for_supported_dialects.one_statement_parses_to(
5789-
"SELECT SUBSTRING('1' FROM 1)",
5790-
"SELECT SUBSTRING('1' FROM 1)",
5791-
);
5792-
5793-
from_for_supported_dialects.one_statement_parses_to(
5794-
"SELECT SUBSTRING('1' FROM 1 FOR 3)",
5795-
"SELECT SUBSTRING('1' FROM 1 FOR 3)",
5796-
);
5797-
5798-
from_for_unsupported_dialects
5799-
.one_statement_parses_to("SELECT SUBSTRING('1', 1, 3)", "SELECT SUBSTRING('1', 1, 3)");
5800-
5801-
from_for_supported_dialects
5802-
.one_statement_parses_to("SELECT SUBSTRING('1' FOR 3)", "SELECT SUBSTRING('1' FOR 3)");
5764+
verified_stmt("SELECT SUBSTRING('1')");
5765+
verified_stmt("SELECT SUBSTRING('1' FROM 1)");
5766+
verified_stmt("SELECT SUBSTRING('1' FROM 1 FOR 3)");
5767+
verified_stmt("SELECT SUBSTRING('1', 1, 3)");
5768+
verified_stmt("SELECT SUBSTRING('1', 1)");
5769+
verified_stmt("SELECT SUBSTRING('1' FOR 3)");
58035770
}
58045771

58055772
#[test]

tests/sqlparser_mysql.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,7 +1911,7 @@ fn parse_substring_in_select() {
19111911
let sql = "SELECT DISTINCT SUBSTRING(description, 0, 1) FROM test";
19121912
match mysql().one_statement_parses_to(
19131913
sql,
1914-
"SELECT DISTINCT SUBSTRING(description FROM 0 FOR 1) FROM test",
1914+
"SELECT DISTINCT SUBSTRING(description, 0, 1) FROM test",
19151915
) {
19161916
Statement::Query(query) => {
19171917
assert_eq!(
@@ -1927,7 +1927,7 @@ fn parse_substring_in_select() {
19271927
})),
19281928
substring_from: Some(Box::new(Expr::Value(number("0")))),
19291929
substring_for: Some(Box::new(Expr::Value(number("1")))),
1930-
special: false,
1930+
special: true,
19311931
})],
19321932
into: None,
19331933
from: vec![TableWithJoins {

tests/sqlparser_sqlite.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,18 @@ fn parse_single_quoted_identified() {
413413
sqlite().verified_only_select("SELECT 't'.*, t.'x' FROM 't'");
414414
// TODO: add support for select 't'.x
415415
}
416+
417+
#[test]
418+
fn parse_substring() {
419+
// SQLite supports the SUBSTRING function since v3.34, but does not support the SQL standard
420+
// SUBSTRING(expr FROM start FOR length) syntax.
421+
// https://www.sqlite.org/lang_corefunc.html#substr
422+
sqlite().verified_only_select("SELECT SUBSTRING('SQLITE', 3, 4)");
423+
sqlite().verified_only_select("SELECT SUBSTR('SQLITE', 3, 4)");
424+
sqlite().verified_only_select("SELECT SUBSTRING('SQLITE', 3)");
425+
sqlite().verified_only_select("SELECT SUBSTR('SQLITE', 3)");
426+
}
427+
416428
#[test]
417429
fn parse_window_function_with_filter() {
418430
for func_name in [

0 commit comments

Comments
 (0)