mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-20 12:49:47 +00:00
Allow array
to be used as a function name again (#432)
* Allow `array` to be used as a function * clarify code, add docstrings * fix docs * cleanup * fmt
This commit is contained in:
parent
3f5619446f
commit
a28bbcd74c
3 changed files with 31 additions and 17 deletions
|
@ -159,8 +159,13 @@ impl fmt::Display for ObjectName {
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
/// Represents an Array Expression, either
|
||||||
|
/// `ARRAY[..]`, or `[..]`
|
||||||
pub struct Array {
|
pub struct Array {
|
||||||
|
/// The list of expressions between brackets
|
||||||
pub elem: Vec<Expr>,
|
pub elem: Vec<Expr>,
|
||||||
|
|
||||||
|
/// `true` for `ARRAY[..]`, `false` for `[..]`
|
||||||
pub named: bool,
|
pub named: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -420,7 +420,11 @@ impl<'a> Parser<'a> {
|
||||||
Keyword::TRIM => self.parse_trim_expr(),
|
Keyword::TRIM => self.parse_trim_expr(),
|
||||||
Keyword::INTERVAL => self.parse_literal_interval(),
|
Keyword::INTERVAL => self.parse_literal_interval(),
|
||||||
Keyword::LISTAGG => self.parse_listagg_expr(),
|
Keyword::LISTAGG => self.parse_listagg_expr(),
|
||||||
Keyword::ARRAY => self.parse_array_expr(true),
|
// Treat ARRAY[1,2,3] as an array [1,2,3], otherwise try as function call
|
||||||
|
Keyword::ARRAY if self.peek_token() == Token::LBracket => {
|
||||||
|
self.expect_token(&Token::LBracket)?;
|
||||||
|
self.parse_array_expr(true)
|
||||||
|
}
|
||||||
Keyword::NOT => Ok(Expr::UnaryOp {
|
Keyword::NOT => Ok(Expr::UnaryOp {
|
||||||
op: UnaryOperator::Not,
|
op: UnaryOperator::Not,
|
||||||
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
|
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
|
||||||
|
@ -450,6 +454,7 @@ impl<'a> Parser<'a> {
|
||||||
_ => Ok(Expr::Identifier(w.to_ident())),
|
_ => Ok(Expr::Identifier(w.to_ident())),
|
||||||
},
|
},
|
||||||
}, // End of Token::Word
|
}, // End of Token::Word
|
||||||
|
// array `[1, 2, 3]`
|
||||||
Token::LBracket => self.parse_array_expr(false),
|
Token::LBracket => self.parse_array_expr(false),
|
||||||
tok @ Token::Minus | tok @ Token::Plus => {
|
tok @ Token::Minus | tok @ Token::Plus => {
|
||||||
let op = if tok == Token::Plus {
|
let op = if tok == Token::Plus {
|
||||||
|
@ -826,10 +831,9 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parses an array expression `[ex1, ex2, ..]`
|
||||||
|
/// if `named` is `true`, came from an expression like `ARRAY[ex1, ex2]`
|
||||||
pub fn parse_array_expr(&mut self, named: bool) -> Result<Expr, ParserError> {
|
pub fn parse_array_expr(&mut self, named: bool) -> Result<Expr, ParserError> {
|
||||||
if named {
|
|
||||||
self.expect_token(&Token::LBracket)?;
|
|
||||||
}
|
|
||||||
let exprs = self.parse_comma_separated(Parser::parse_expr)?;
|
let exprs = self.parse_comma_separated(Parser::parse_expr)?;
|
||||||
self.expect_token(&Token::RBracket)?;
|
self.expect_token(&Token::RBracket)?;
|
||||||
Ok(Expr::Array(Array { elem: exprs, named }))
|
Ok(Expr::Array(Array { elem: exprs, named }))
|
||||||
|
|
|
@ -2246,11 +2246,15 @@ fn parse_bad_constraint() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_scalar_function_in_projection() {
|
fn parse_scalar_function_in_projection() {
|
||||||
let sql = "SELECT sqrt(id) FROM foo";
|
let names = vec!["sqrt", "array", "foo"];
|
||||||
let select = verified_only_select(sql);
|
|
||||||
|
for function_name in names {
|
||||||
|
// like SELECT sqrt(id) FROM foo
|
||||||
|
let sql = dbg!(format!("SELECT {}(id) FROM foo", function_name));
|
||||||
|
let select = verified_only_select(&sql);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&Expr::Function(Function {
|
&Expr::Function(Function {
|
||||||
name: ObjectName(vec![Ident::new("sqrt")]),
|
name: ObjectName(vec![Ident::new(function_name)]),
|
||||||
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
|
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
|
||||||
Expr::Identifier(Ident::new("id"))
|
Expr::Identifier(Ident::new("id"))
|
||||||
))],
|
))],
|
||||||
|
@ -2260,6 +2264,7 @@ fn parse_scalar_function_in_projection() {
|
||||||
expr_from_projection(only(&select.projection))
|
expr_from_projection(only(&select.projection))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn run_explain_analyze(query: &str, expected_verbose: bool, expected_analyze: bool) {
|
fn run_explain_analyze(query: &str, expected_verbose: bool, expected_analyze: bool) {
|
||||||
match verified_stmt(query) {
|
match verified_stmt(query) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue