From ff82113b837fd89e16f0853f9448ce3f67164a0e Mon Sep 17 00:00:00 2001 From: Chris Allen Date: Tue, 5 Jul 2022 14:57:44 -0500 Subject: [PATCH] Support for empty array literals --- src/parser.rs | 14 +++++++++++--- tests/sqlparser_postgres.rs | 10 ++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 325d90c47..0a872ce87 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -883,9 +883,17 @@ 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 { - let exprs = self.parse_comma_separated(Parser::parse_expr)?; - self.expect_token(&Token::RBracket)?; - Ok(Expr::Array(Array { elem: exprs, named })) + if self.peek_token() == Token::RBracket { + let _ = self.next_token(); + Ok(Expr::Array(Array { + elem: vec![], + named, + })) + } else { + let exprs = self.parse_comma_separated(Parser::parse_expr)?; + self.expect_token(&Token::RBracket)?; + Ok(Expr::Array(Array { elem: exprs, named })) + } } /// Parse a SQL LISTAGG expression, e.g. `LISTAGG(...) WITHIN GROUP (ORDER BY ...)`. diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 8bb2dd56a..0d5b3f1d3 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -1228,6 +1228,16 @@ fn parse_array_index_expr() { }, expr_from_projection(only(&select.projection)), ); + + let sql = "SELECT ARRAY[]"; + let select = pg_and_generic().verified_only_select(sql); + assert_eq!( + &Expr::Array(sqlparser::ast::Array { + elem: vec![], + named: true + }), + expr_from_projection(only(&select.projection)), + ); } #[test]