mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-04 06:18:17 +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)]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
/// Represents an Array Expression, either
|
||||
/// `ARRAY[..]`, or `[..]`
|
||||
pub struct Array {
|
||||
/// The list of expressions between brackets
|
||||
pub elem: Vec<Expr>,
|
||||
|
||||
/// `true` for `ARRAY[..]`, `false` for `[..]`
|
||||
pub named: bool,
|
||||
}
|
||||
|
||||
|
|
|
@ -420,7 +420,11 @@ impl<'a> Parser<'a> {
|
|||
Keyword::TRIM => self.parse_trim_expr(),
|
||||
Keyword::INTERVAL => self.parse_literal_interval(),
|
||||
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 {
|
||||
op: UnaryOperator::Not,
|
||||
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
|
||||
|
@ -450,6 +454,7 @@ impl<'a> Parser<'a> {
|
|||
_ => Ok(Expr::Identifier(w.to_ident())),
|
||||
},
|
||||
}, // End of Token::Word
|
||||
// array `[1, 2, 3]`
|
||||
Token::LBracket => self.parse_array_expr(false),
|
||||
tok @ Token::Minus | 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> {
|
||||
if named {
|
||||
self.expect_token(&Token::LBracket)?;
|
||||
}
|
||||
let exprs = self.parse_comma_separated(Parser::parse_expr)?;
|
||||
self.expect_token(&Token::RBracket)?;
|
||||
Ok(Expr::Array(Array { elem: exprs, named }))
|
||||
|
|
|
@ -2246,19 +2246,24 @@ fn parse_bad_constraint() {
|
|||
|
||||
#[test]
|
||||
fn parse_scalar_function_in_projection() {
|
||||
let sql = "SELECT sqrt(id) FROM foo";
|
||||
let select = verified_only_select(sql);
|
||||
assert_eq!(
|
||||
&Expr::Function(Function {
|
||||
name: ObjectName(vec![Ident::new("sqrt")]),
|
||||
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
|
||||
Expr::Identifier(Ident::new("id"))
|
||||
))],
|
||||
over: None,
|
||||
distinct: false,
|
||||
}),
|
||||
expr_from_projection(only(&select.projection))
|
||||
);
|
||||
let names = vec!["sqrt", "array", "foo"];
|
||||
|
||||
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!(
|
||||
&Expr::Function(Function {
|
||||
name: ObjectName(vec![Ident::new(function_name)]),
|
||||
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
|
||||
Expr::Identifier(Ident::new("id"))
|
||||
))],
|
||||
over: None,
|
||||
distinct: false,
|
||||
}),
|
||||
expr_from_projection(only(&select.projection))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn run_explain_analyze(query: &str, expected_verbose: bool, expected_analyze: bool) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue