Skip to content

Commit

Permalink
Fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Dandandan committed Feb 7, 2021
2 parents c2c83c1 + 6f0b2dc commit 90aeb19
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
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
4 changes: 0 additions & 4 deletions tests/sqlparser_regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ macro_rules! tpch_tests {
#[test]
fn $name() {
let dialect = GenericDialect {};

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

0 comments on commit 90aeb19

Please sign in to comment.