Skip to content

Allow array to be used as a function name again #432

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

Merged
merged 5 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
12 changes: 8 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 }))
Expand Down
31 changes: 18 additions & 13 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down