Skip to content
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
22 changes: 22 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,17 @@ pub enum Expr {
expr: Box<Expr>,
data_type: DataType,
},
/// EXTRACT(DateTimeField FROM <expr>)
Extract {
field: DateTimeField,
expr: Box<Expr>,
},
/// SUBSTRING(<expr> [FROM <expr>] [FOR <expr>])
Substring {
expr: Box<Expr>,
substring_from: Option<Box<Expr>>,
substring_for: Option<Box<Expr>>,
},
/// `expr COLLATE collation`
Collate {
expr: Box<Expr>,
Expand Down Expand Up @@ -333,6 +340,21 @@ impl fmt::Display for Expr {
Expr::Exists(s) => write!(f, "EXISTS ({})", s),
Expr::Subquery(s) => write!(f, "({})", s),
Expr::ListAgg(listagg) => write!(f, "{}", listagg),
Expr::Substring {
expr,
substring_from,
substring_for,
} => {
write!(f, "SUBSTRING({}", expr)?;
if let Some(from_part) = substring_from {
write!(f, " FROM {}", from_part)?;
}
if let Some(from_part) = substring_for {
write!(f, " FOR {}", from_part)?;
}

write!(f, ")")
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ impl<'a> Parser<'a> {
Keyword::CAST => self.parse_cast_expr(),
Keyword::EXISTS => self.parse_exists_expr(),
Keyword::EXTRACT => self.parse_extract_expr(),
Keyword::SUBSTRING => self.parse_substring_expr(),
Keyword::INTERVAL => self.parse_literal_interval(),
Keyword::LISTAGG => self.parse_listagg_expr(),
Keyword::NOT => Ok(Expr::UnaryOp {
Expand Down Expand Up @@ -606,6 +607,27 @@ impl<'a> Parser<'a> {
})
}

pub fn parse_substring_expr(&mut self) -> Result<Expr, ParserError> {
// PARSE SUBSTRING (EXPR [FROM 1] [FOR 3])
self.expect_token(&Token::LParen)?;
let expr = self.parse_expr()?;
let mut from_expr = None;
let mut to_expr = None;
if self.parse_keyword(Keyword::FROM) {
from_expr = Some(self.parse_expr()?);
}
if self.parse_keyword(Keyword::FOR) {
to_expr = Some(self.parse_expr()?);
}
self.expect_token(&Token::RParen)?;

Ok(Expr::Substring {
expr: Box::new(expr),
substring_from: from_expr.map(Box::new),
substring_for: to_expr.map(Box::new),
})
}

/// Parse a SQL LISTAGG expression, e.g. `LISTAGG(...) WITHIN GROUP (ORDER BY ...)`.
pub fn parse_listagg_expr(&mut self) -> Result<Expr, ParserError> {
self.expect_token(&Token::LParen)?;
Expand Down
17 changes: 17 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2598,6 +2598,23 @@ fn parse_scalar_subqueries() {
);
}

#[test]
fn parse_substring() {
one_statement_parses_to("SELECT SUBSTRING('1')", "SELECT SUBSTRING('1')");

one_statement_parses_to(
"SELECT SUBSTRING('1' FROM 1)",
"SELECT SUBSTRING('1' FROM 1)",
);

one_statement_parses_to(
"SELECT SUBSTRING('1' FROM 1 FOR 3)",
"SELECT SUBSTRING('1' FROM 1 FOR 3)",
);

one_statement_parses_to("SELECT SUBSTRING('1' FOR 3)", "SELECT SUBSTRING('1' FOR 3)");
}

#[test]
fn parse_exists_subquery() {
let expected_inner = verified_query("SELECT 1");
Expand Down
5 changes: 2 additions & 3 deletions tests/sqlparser_regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ macro_rules! tpch_tests {
#[test]
fn $name() {
let dialect = GenericDialect {};

let res = Parser::parse_sql(&dialect, QUERIES[$value -1]);
// Ignore 6.sql and 22.sql
if $value != 6 && $value != 22 {
// Ignore 6.sql
if $value != 6 {
assert!(res.is_ok());
}
}
Expand Down