Skip to content
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

refactor(parser): use v2 parser for cast/extract/substring/position/overlay #17053

Merged
merged 7 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
49 changes: 4 additions & 45 deletions src/sqlparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,28 +1009,12 @@ impl Parser<'_> {

/// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`
pub fn parse_cast_expr(&mut self) -> PResult<Expr> {
self.expect_token(&Token::LParen)?;
let expr = self.parse_expr()?;
self.expect_keyword(Keyword::AS)?;
let data_type = self.parse_data_type()?;
self.expect_token(&Token::RParen)?;
Ok(Expr::Cast {
expr: Box::new(expr),
data_type,
})
parser_v2::expr_cast(self)
wangrunji0408 marked this conversation as resolved.
Show resolved Hide resolved
}

/// Parse a SQL TRY_CAST function e.g. `TRY_CAST(expr AS FLOAT)`
pub fn parse_try_cast_expr(&mut self) -> PResult<Expr> {
self.expect_token(&Token::LParen)?;
let expr = self.parse_expr()?;
self.expect_keyword(Keyword::AS)?;
let data_type = self.parse_data_type()?;
self.expect_token(&Token::RParen)?;
Ok(Expr::TryCast {
expr: Box::new(expr),
data_type,
})
parser_v2::expr_try_cast(self)
}

/// Parse a SQL EXISTS expression e.g. `WHERE EXISTS(SELECT ...)`.
Expand All @@ -1042,37 +1026,12 @@ impl Parser<'_> {
}

pub fn parse_extract_expr(&mut self) -> PResult<Expr> {
self.expect_token(&Token::LParen)?;
let field = self.parse_date_time_field_in_extract()?;
self.expect_keyword(Keyword::FROM)?;
let expr = self.parse_expr()?;
self.expect_token(&Token::RParen)?;
Ok(Expr::Extract {
field,
expr: Box::new(expr),
})
parser_v2::expr_extract(self)
}

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

let mut to_expr = None;
if self.parse_keyword(Keyword::FOR) || self.consume_token(&Token::Comma) {
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),
})
parser_v2::expr_substring(self)
}

/// `POSITION(<expr> IN <expr>)`
Expand Down
93 changes: 91 additions & 2 deletions src/sqlparser/src/parser_v2/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use winnow::combinator::{cut_err, opt, preceded, repeat, trace};
use winnow::combinator::{alt, cut_err, opt, preceded, repeat, trace};
use winnow::{PResult, Parser};

use super::TokenStream;
use super::{data_type, token, ParserExt, TokenStream};
use crate::ast::Expr;
use crate::keywords::Keyword;
use crate::tokenizer::Token;

fn expr<S>(input: &mut S) -> PResult<Expr>
where
Expand Down Expand Up @@ -58,3 +59,91 @@ where

trace("expr_case", parse).parse_next(input)
}

/// Consome a SQL CAST function e.g. `CAST(expr AS FLOAT)`
pub fn expr_cast<S>(input: &mut S) -> PResult<Expr>
where
S: TokenStream,
{
let parse = (
cut_err(Token::LParen),
cut_err(expr).map(Box::new),
cut_err(Keyword::AS),
cut_err(data_type),
cut_err(Token::RParen),
)
TennyZhuang marked this conversation as resolved.
Show resolved Hide resolved
.map(|(_, expr, _, data_type, _)| Expr::Cast { expr, data_type });
TennyZhuang marked this conversation as resolved.
Show resolved Hide resolved

trace("expr_cast", parse).parse_next(input)
}

/// Consume a SQL TRY_CAST function e.g. `TRY_CAST(expr AS FLOAT)`
pub fn expr_try_cast<S>(input: &mut S) -> PResult<Expr>
where
S: TokenStream,
{
let parse = (
cut_err(Token::LParen),
cut_err(expr).map(Box::new),
cut_err(Keyword::AS),
cut_err(data_type),
cut_err(Token::RParen),
)
.map(|(_, expr, _, data_type, _)| Expr::TryCast { expr, data_type });

trace("expr_try_cast", parse).parse_next(input)
}

/// Consume a SQL EXTRACT function e.g. `EXTRACT(YEAR FROM expr)`
pub fn expr_extract<S>(input: &mut S) -> PResult<Expr>
where
S: TokenStream,
{
let date_time_field = token
.verify_map(|token| match token.token {
Token::Word(w) => Some(w.value.to_uppercase()),
Token::SingleQuotedString(s) => Some(s.to_uppercase()),
_ => None,
})
.expect("date/time field");

let parse = (
cut_err(Token::LParen),
cut_err(date_time_field),
cut_err(Keyword::FROM),
cut_err(expr).map(Box::new),
cut_err(Token::RParen),
)
.map(|(_, field, _, expr, _)| Expr::Extract { field, expr });

trace("expr_extract", parse).parse_next(input)
}

/// Consume `SUBSTRING (EXPR [FROM 1] [FOR 3])`
pub fn expr_substring<S>(input: &mut S) -> PResult<Expr>
where
S: TokenStream,
{
let parse = (
cut_err(Token::LParen),
cut_err(expr).map(Box::new),
opt(preceded(
alt((Token::Comma.void(), Keyword::FROM.void())),
cut_err(expr).map(Box::new),
)),
opt(preceded(
alt((Token::Comma.void(), Keyword::FOR.void())),
cut_err(expr).map(Box::new),
)),
cut_err(Token::RParen),
)
.map(
|(_, expr, substring_from, substring_for, _)| Expr::Substring {
expr,
substring_from,
substring_for,
},
);

trace("expr_substring", parse).parse_next(input)
}
3 changes: 0 additions & 3 deletions src/sqlparser/tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,9 +1268,6 @@ fn parse_extract() {
verified_stmt("SELECT EXTRACT(HOUR FROM d)");
verified_stmt("SELECT EXTRACT(MINUTE FROM d)");
verified_stmt("SELECT EXTRACT(SECOND FROM d)");

let res = parse_sql_statements("SELECT EXTRACT(0 FROM d)");
assert!(format!("{}", res.unwrap_err()).contains("expected date/time field, found: 0"));
}

#[test]
Expand Down
6 changes: 6 additions & 0 deletions src/sqlparser/tests/testdata/extract.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file is automatically generated by `src/sqlparser/tests/parser_test.rs`.
- input: SELECT EXTRACT(0 FROM d)
error_msg: |-
sql parser error: expected date/time field
LINE 1: SELECT EXTRACT(0 FROM d)
^
Loading