Skip to content

Parse SUBSTR as alias for SUBSTRING #1769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,10 @@ pub enum Expr {
/// true if the expression is represented using the `SUBSTRING(expr, start, len)` syntax
/// This flag is used for formatting.
special: bool,

/// true if the expression is represented using the `SUBSTR` shorthand
/// This flag is used for formatting.
shorthand: bool,
},
/// ```sql
/// TRIM([BOTH | LEADING | TRAILING] [<expr> FROM] <expr>)
Expand Down Expand Up @@ -1719,8 +1723,13 @@ impl fmt::Display for Expr {
substring_from,
substring_for,
special,
shorthand,
} => {
write!(f, "SUBSTRING({expr}")?;
f.write_str("SUBSTR")?;
if !*shorthand {
f.write_str("ING")?;
}
write!(f, "({expr}")?;
if let Some(from_part) = substring_from {
if *special {
write!(f, ", {from_part}")?;
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,7 @@ impl Spanned for Expr {
substring_from,
substring_for,
special: _,
shorthand: _,
} => union_spans(
core::iter::once(expr.span())
.chain(substring_from.as_ref().map(|i| i.span()))
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ define_keywords!(
STRING,
STRUCT,
SUBMULTISET,
SUBSTR,
SUBSTRING,
SUBSTRING_REGEX,
SUCCEEDS,
Expand Down
18 changes: 15 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,10 @@ impl<'a> Parser<'a> {
Keyword::POSITION if self.peek_token_ref().token == Token::LParen => {
Ok(Some(self.parse_position_expr(w.clone().into_ident(w_span))?))
}
Keyword::SUBSTRING => Ok(Some(self.parse_substring_expr()?)),
Keyword::SUBSTR | Keyword::SUBSTRING => {
self.prev_token();
Ok(Some(self.parse_substring()?))
}
Keyword::OVERLAY => Ok(Some(self.parse_overlay_expr()?)),
Keyword::TRIM => Ok(Some(self.parse_trim_expr()?)),
Keyword::INTERVAL => Ok(Some(self.parse_interval()?)),
Expand Down Expand Up @@ -2412,8 +2415,16 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_substring_expr(&mut self) -> Result<Expr, ParserError> {
// PARSE SUBSTRING (EXPR [FROM 1] [FOR 3])
// { SUBSTRING | SUBSTR } (<EXPR> [FROM 1] [FOR 3])
pub fn parse_substring(&mut self) -> Result<Expr, ParserError> {
let shorthand = match self.expect_one_of_keywords(&[Keyword::SUBSTR, Keyword::SUBSTRING])? {
Keyword::SUBSTR => true,
Keyword::SUBSTRING => false,
_ => {
self.prev_token();
return self.expected("SUBSTR or SUBSTRING", self.peek_token());
}
};
self.expect_token(&Token::LParen)?;
let expr = self.parse_expr()?;
let mut from_expr = None;
Expand All @@ -2433,6 +2444,7 @@ impl<'a> Parser<'a> {
substring_from: from_expr.map(Box::new),
substring_for: to_expr.map(Box::new),
special,
shorthand,
})
}

Expand Down
3 changes: 3 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7607,6 +7607,9 @@ fn parse_substring() {
verified_stmt("SELECT SUBSTRING('1', 1, 3)");
verified_stmt("SELECT SUBSTRING('1', 1)");
verified_stmt("SELECT SUBSTRING('1' FOR 3)");
verified_stmt("SELECT SUBSTRING('foo' FROM 1 FOR 2) FROM t");
verified_stmt("SELECT SUBSTR('foo' FROM 1 FOR 2) FROM t");
verified_stmt("SELECT SUBSTR('foo', 1, 2) FROM t");
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ fn parse_substring_in_select() {
(number("1")).with_empty_span()
))),
special: true,
shorthand: false,
})],
into: None,
from: vec![TableWithJoins {
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,7 @@ fn parse_substring_in_select() {
(number("1")).with_empty_span()
))),
special: true,
shorthand: false,
})],
into: None,
from: vec![TableWithJoins {
Expand Down