Support TRIM from with optional FROM clause (#573)

* Split trimwhereFlag and trim_char expr to seperate members of Expr::Trim

* update trim parsing logic to handle no flag + char from expr combo

* add tests

* Allow trim flag without trim_what expr

* Add trim flag without trim_what test
This commit is contained in:
Ayush Dattagupta 2022-08-15 14:58:43 -07:00 committed by GitHub
parent 31ba0012f7
commit 42c5d43b45
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 18 deletions

View file

@ -881,29 +881,37 @@ impl<'a> Parser<'a> {
})
}
/// TRIM (WHERE 'text' FROM 'text')\
/// TRIM ([WHERE] ['text' FROM] 'text')\
/// TRIM ('text')
pub fn parse_trim_expr(&mut self) -> Result<Expr, ParserError> {
self.expect_token(&Token::LParen)?;
let mut where_expr = None;
let mut trim_where = None;
if let Token::Word(word) = self.peek_token() {
if [Keyword::BOTH, Keyword::LEADING, Keyword::TRAILING]
.iter()
.any(|d| word.keyword == *d)
{
let trim_where = self.parse_trim_where()?;
let sub_expr = self.parse_expr()?;
self.expect_keyword(Keyword::FROM)?;
where_expr = Some((trim_where, Box::new(sub_expr)));
trim_where = Some(self.parse_trim_where()?);
}
}
let expr = self.parse_expr()?;
self.expect_token(&Token::RParen)?;
Ok(Expr::Trim {
expr: Box::new(expr),
trim_where: where_expr,
})
if self.parse_keyword(Keyword::FROM) {
let trim_what = Box::new(expr);
let expr = self.parse_expr()?;
self.expect_token(&Token::RParen)?;
Ok(Expr::Trim {
expr: Box::new(expr),
trim_where,
trim_what: Some(trim_what),
})
} else {
self.expect_token(&Token::RParen)?;
Ok(Expr::Trim {
expr: Box::new(expr),
trim_where,
trim_what: None,
})
}
}
pub fn parse_trim_where(&mut self) -> Result<TrimWhereField, ParserError> {