Skip to content

Commit c6edf5f

Browse files
committed
Support PostgreSQL array subquery constructor
1 parent 231370a commit c6edf5f

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

src/ast/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ pub enum Expr {
355355
/// A parenthesized subquery `(SELECT ...)`, used in expression like
356356
/// `SELECT (subquery) AS x` or `WHERE (subquery) = x`
357357
Subquery(Box<Query>),
358+
/// An array subquery constructor, e.g. `SELECT ARRAY(SELECT 1 UNION SELECT 2)`
359+
ArraySubquery(Box<Query>),
358360
/// The `LISTAGG` function `SELECT LISTAGG(...) WITHIN GROUP (ORDER BY ...)`
359361
ListAgg(ListAgg),
360362
/// The `GROUPING SETS` expr.
@@ -486,6 +488,7 @@ impl fmt::Display for Expr {
486488
subquery
487489
),
488490
Expr::Subquery(s) => write!(f, "({})", s),
491+
Expr::ArraySubquery(s) => write!(f, "ARRAY({})", s),
489492
Expr::ListAgg(listagg) => write!(f, "{}", listagg),
490493
Expr::GroupingSets(sets) => {
491494
write!(f, "GROUPING SETS (")?;

src/parser.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,18 @@ impl<'a> Parser<'a> {
434434
Keyword::TRIM => self.parse_trim_expr(),
435435
Keyword::INTERVAL => self.parse_literal_interval(),
436436
Keyword::LISTAGG => self.parse_listagg_expr(),
437-
// Treat ARRAY[1,2,3] as an array [1,2,3], otherwise try as function call
437+
// Treat ARRAY[1,2,3] as an array [1,2,3], otherwise try as subquery or a function call
438438
Keyword::ARRAY if self.peek_token() == Token::LBracket => {
439439
self.expect_token(&Token::LBracket)?;
440440
self.parse_array_expr(true)
441441
}
442+
Keyword::ARRAY
443+
if dialect_of!(self is PostgreSqlDialect | GenericDialect)
444+
&& self.peek_token() == Token::LParen =>
445+
{
446+
self.expect_token(&Token::LParen)?;
447+
self.parse_array_subquery()
448+
}
442449
Keyword::NOT => self.parse_not(),
443450
// Here `w` is a word, check if it's a part of a multi-part
444451
// identifier, a function call, or a simple identifier:
@@ -910,6 +917,13 @@ impl<'a> Parser<'a> {
910917
}
911918
}
912919

920+
// Parses an array constructed from a subquery
921+
pub fn parse_array_subquery(&mut self) -> Result<Expr, ParserError> {
922+
let query = self.parse_query()?;
923+
self.expect_token(&Token::RParen)?;
924+
Ok(Expr::ArraySubquery(Box::new(query)))
925+
}
926+
913927
/// Parse a SQL LISTAGG expression, e.g. `LISTAGG(...) WITHIN GROUP (ORDER BY ...)`.
914928
pub fn parse_listagg_expr(&mut self) -> Result<Expr, ParserError> {
915929
self.expect_token(&Token::LParen)?;

tests/sqlparser_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2518,7 +2518,7 @@ fn parse_bad_constraint() {
25182518

25192519
#[test]
25202520
fn parse_scalar_function_in_projection() {
2521-
let names = vec!["sqrt", "array", "foo"];
2521+
let names = vec!["sqrt", "foo"];
25222522

25232523
for function_name in names {
25242524
// like SELECT sqrt(id) FROM foo

tests/sqlparser_postgres.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,69 @@ fn parse_array_index_expr() {
12411241
);
12421242
}
12431243

1244+
#[test]
1245+
fn parse_array_subquery_expr() {
1246+
let sql = "SELECT ARRAY(SELECT 1 UNION SELECT 2)";
1247+
let select = pg().verified_only_select(sql);
1248+
assert_eq!(
1249+
&Expr::ArraySubquery(Box::new(Query {
1250+
with: None,
1251+
body: Box::new(SetExpr::SetOperation {
1252+
op: SetOperator::Union,
1253+
all: false,
1254+
left: Box::new(SetExpr::Select(Box::new(Select {
1255+
distinct: false,
1256+
top: None,
1257+
projection: vec![SelectItem::UnnamedExpr(Expr::Value(Value::Number(
1258+
#[cfg(not(feature = "bigdecimal"))]
1259+
"1".to_string(),
1260+
#[cfg(feature = "bigdecimal")]
1261+
bigdecimal::BigDecimal::from("1"),
1262+
false,
1263+
)))],
1264+
into: None,
1265+
from: vec![],
1266+
lateral_views: vec![],
1267+
selection: None,
1268+
group_by: vec![],
1269+
cluster_by: vec![],
1270+
distribute_by: vec![],
1271+
sort_by: vec![],
1272+
having: None,
1273+
qualify: None,
1274+
}))),
1275+
right: Box::new(SetExpr::Select(Box::new(Select {
1276+
distinct: false,
1277+
top: None,
1278+
projection: vec![SelectItem::UnnamedExpr(Expr::Value(Value::Number(
1279+
#[cfg(not(feature = "bigdecimal"))]
1280+
"2".to_string(),
1281+
#[cfg(feature = "bigdecimal")]
1282+
bigdecimal::BigDecimal::from("2"),
1283+
false,
1284+
)))],
1285+
into: None,
1286+
from: vec![],
1287+
lateral_views: vec![],
1288+
selection: None,
1289+
group_by: vec![],
1290+
cluster_by: vec![],
1291+
distribute_by: vec![],
1292+
sort_by: vec![],
1293+
having: None,
1294+
qualify: None,
1295+
}))),
1296+
}),
1297+
order_by: vec![],
1298+
limit: None,
1299+
offset: None,
1300+
fetch: None,
1301+
lock: None,
1302+
})),
1303+
expr_from_projection(only(&select.projection)),
1304+
);
1305+
}
1306+
12441307
#[test]
12451308
fn test_transaction_statement() {
12461309
let statement = pg().verified_stmt("SET TRANSACTION SNAPSHOT '000003A1-1'");

0 commit comments

Comments
 (0)