Parse SUBSTRING FROM syntax in all dialects, reflect change in the AST (#1173)

This commit is contained in:
Ophir LOJKINE 2024-03-13 16:08:27 +01:00 committed by GitHub
parent 929c646bba
commit 6b03a259aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 47 additions and 95 deletions

View file

@ -559,13 +559,18 @@ pub enum Expr {
/// ```sql /// ```sql
/// SUBSTRING(<expr> [FROM <expr>] [FOR <expr>]) /// SUBSTRING(<expr> [FROM <expr>] [FOR <expr>])
/// ``` /// ```
/// or
/// ```sql
/// SUBSTRING(<expr>, <expr>, <expr>)
/// ```
Substring { Substring {
expr: Box<Expr>, expr: Box<Expr>,
substring_from: Option<Box<Expr>>, substring_from: Option<Box<Expr>>,
substring_for: Option<Box<Expr>>, substring_for: Option<Box<Expr>>,
// Some dialects use `SUBSTRING(expr [FROM start] [FOR len])` syntax while others omit FROM, /// false if the expression is represented using the `SUBSTRING(expr [FROM start] [FOR len])` syntax
// FOR keywords (e.g. Microsoft SQL Server). This flags is used for formatting. /// true if the expression is represented using the `SUBSTRING(expr, start, len)` syntax
/// This flag is used for formatting.
special: bool, special: bool,
}, },
/// ```sql /// ```sql

View file

@ -135,10 +135,6 @@ pub trait Dialect: Debug + Any {
fn supports_group_by_expr(&self) -> bool { fn supports_group_by_expr(&self) -> bool {
false false
} }
/// Returns true if the dialect supports `SUBSTRING(expr [FROM start] [FOR len])` expressions
fn supports_substring_from_for_expr(&self) -> bool {
true
}
/// Returns true if the dialect supports `(NOT) IN ()` expressions /// Returns true if the dialect supports `(NOT) IN ()` expressions
fn supports_in_empty_list(&self) -> bool { fn supports_in_empty_list(&self) -> bool {
false false
@ -325,10 +321,6 @@ mod tests {
self.0.supports_group_by_expr() self.0.supports_group_by_expr()
} }
fn supports_substring_from_for_expr(&self) -> bool {
self.0.supports_substring_from_for_expr()
}
fn supports_in_empty_list(&self) -> bool { fn supports_in_empty_list(&self) -> bool {
self.0.supports_in_empty_list() self.0.supports_in_empty_list()
} }

View file

@ -40,8 +40,4 @@ impl Dialect for MsSqlDialect {
fn convert_type_before_value(&self) -> bool { fn convert_type_before_value(&self) -> bool {
true true
} }
fn supports_substring_from_for_expr(&self) -> bool {
false
}
} }

View file

@ -1525,12 +1525,12 @@ impl<'a> Parser<'a> {
} }
pub fn parse_substring_expr(&mut self) -> Result<Expr, ParserError> { pub fn parse_substring_expr(&mut self) -> Result<Expr, ParserError> {
if self.dialect.supports_substring_from_for_expr() {
// PARSE SUBSTRING (EXPR [FROM 1] [FOR 3]) // PARSE SUBSTRING (EXPR [FROM 1] [FOR 3])
self.expect_token(&Token::LParen)?; self.expect_token(&Token::LParen)?;
let expr = self.parse_expr()?; let expr = self.parse_expr()?;
let mut from_expr = None; let mut from_expr = None;
if self.parse_keyword(Keyword::FROM) || self.consume_token(&Token::Comma) { let special = self.consume_token(&Token::Comma);
if special || self.parse_keyword(Keyword::FROM) {
from_expr = Some(self.parse_expr()?); from_expr = Some(self.parse_expr()?);
} }
@ -1544,28 +1544,8 @@ impl<'a> Parser<'a> {
expr: Box::new(expr), expr: Box::new(expr),
substring_from: from_expr.map(Box::new), substring_from: from_expr.map(Box::new),
substring_for: to_expr.map(Box::new), substring_for: to_expr.map(Box::new),
special: false, special,
}) })
} else {
// PARSE SUBSTRING(EXPR, start, length)
self.expect_token(&Token::LParen)?;
let expr = self.parse_expr()?;
self.expect_token(&Token::Comma)?;
let from_expr = Some(self.parse_expr()?);
self.expect_token(&Token::Comma)?;
let 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),
special: true,
})
}
} }
pub fn parse_overlay_expr(&mut self) -> Result<Expr, ParserError> { pub fn parse_overlay_expr(&mut self) -> Result<Expr, ParserError> {

View file

@ -5761,45 +5761,12 @@ fn parse_scalar_subqueries() {
#[test] #[test]
fn parse_substring() { fn parse_substring() {
let from_for_supported_dialects = TestedDialects { verified_stmt("SELECT SUBSTRING('1')");
dialects: vec![ verified_stmt("SELECT SUBSTRING('1' FROM 1)");
Box::new(GenericDialect {}), verified_stmt("SELECT SUBSTRING('1' FROM 1 FOR 3)");
Box::new(PostgreSqlDialect {}), verified_stmt("SELECT SUBSTRING('1', 1, 3)");
Box::new(AnsiDialect {}), verified_stmt("SELECT SUBSTRING('1', 1)");
Box::new(SnowflakeDialect {}), verified_stmt("SELECT SUBSTRING('1' FOR 3)");
Box::new(HiveDialect {}),
Box::new(RedshiftSqlDialect {}),
Box::new(MySqlDialect {}),
Box::new(BigQueryDialect {}),
Box::new(SQLiteDialect {}),
Box::new(DuckDbDialect {}),
],
options: None,
};
let from_for_unsupported_dialects = TestedDialects {
dialects: vec![Box::new(MsSqlDialect {})],
options: None,
};
from_for_supported_dialects
.one_statement_parses_to("SELECT SUBSTRING('1')", "SELECT SUBSTRING('1')");
from_for_supported_dialects.one_statement_parses_to(
"SELECT SUBSTRING('1' FROM 1)",
"SELECT SUBSTRING('1' FROM 1)",
);
from_for_supported_dialects.one_statement_parses_to(
"SELECT SUBSTRING('1' FROM 1 FOR 3)",
"SELECT SUBSTRING('1' FROM 1 FOR 3)",
);
from_for_unsupported_dialects
.one_statement_parses_to("SELECT SUBSTRING('1', 1, 3)", "SELECT SUBSTRING('1', 1, 3)");
from_for_supported_dialects
.one_statement_parses_to("SELECT SUBSTRING('1' FOR 3)", "SELECT SUBSTRING('1' FOR 3)");
} }
#[test] #[test]

View file

@ -1911,7 +1911,7 @@ fn parse_substring_in_select() {
let sql = "SELECT DISTINCT SUBSTRING(description, 0, 1) FROM test"; let sql = "SELECT DISTINCT SUBSTRING(description, 0, 1) FROM test";
match mysql().one_statement_parses_to( match mysql().one_statement_parses_to(
sql, sql,
"SELECT DISTINCT SUBSTRING(description FROM 0 FOR 1) FROM test", "SELECT DISTINCT SUBSTRING(description, 0, 1) FROM test",
) { ) {
Statement::Query(query) => { Statement::Query(query) => {
assert_eq!( assert_eq!(
@ -1927,7 +1927,7 @@ fn parse_substring_in_select() {
})), })),
substring_from: Some(Box::new(Expr::Value(number("0")))), substring_from: Some(Box::new(Expr::Value(number("0")))),
substring_for: Some(Box::new(Expr::Value(number("1")))), substring_for: Some(Box::new(Expr::Value(number("1")))),
special: false, special: true,
})], })],
into: None, into: None,
from: vec![TableWithJoins { from: vec![TableWithJoins {

View file

@ -413,6 +413,18 @@ fn parse_single_quoted_identified() {
sqlite().verified_only_select("SELECT 't'.*, t.'x' FROM 't'"); sqlite().verified_only_select("SELECT 't'.*, t.'x' FROM 't'");
// TODO: add support for select 't'.x // TODO: add support for select 't'.x
} }
#[test]
fn parse_substring() {
// SQLite supports the SUBSTRING function since v3.34, but does not support the SQL standard
// SUBSTRING(expr FROM start FOR length) syntax.
// https://www.sqlite.org/lang_corefunc.html#substr
sqlite().verified_only_select("SELECT SUBSTRING('SQLITE', 3, 4)");
sqlite().verified_only_select("SELECT SUBSTR('SQLITE', 3, 4)");
sqlite().verified_only_select("SELECT SUBSTRING('SQLITE', 3)");
sqlite().verified_only_select("SELECT SUBSTR('SQLITE', 3)");
}
#[test] #[test]
fn parse_window_function_with_filter() { fn parse_window_function_with_filter() {
for func_name in [ for func_name in [