Skip to content

Commit 31ba001

Browse files
authored
Support PostgreSQL array subquery constructor (apache#566)
1 parent 61dc3e9 commit 31ba001

File tree

5 files changed

+101
-2
lines changed

5 files changed

+101
-2
lines changed

src/ast/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ pub enum Expr {
376376
/// A parenthesized subquery `(SELECT ...)`, used in expression like
377377
/// `SELECT (subquery) AS x` or `WHERE (subquery) = x`
378378
Subquery(Box<Query>),
379+
/// An array subquery constructor, e.g. `SELECT ARRAY(SELECT 1 UNION SELECT 2)`
380+
ArraySubquery(Box<Query>),
379381
/// The `LISTAGG` function `SELECT LISTAGG(...) WITHIN GROUP (ORDER BY ...)`
380382
ListAgg(ListAgg),
381383
/// The `GROUPING SETS` expr.
@@ -573,6 +575,7 @@ impl fmt::Display for Expr {
573575
subquery
574576
),
575577
Expr::Subquery(s) => write!(f, "({})", s),
578+
Expr::ArraySubquery(s) => write!(f, "ARRAY({})", s),
576579
Expr::ListAgg(listagg) => write!(f, "{}", listagg),
577580
Expr::GroupingSets(sets) => {
578581
write!(f, "GROUPING SETS (")?;

src/parser.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,18 @@ impl<'a> Parser<'a> {
449449
Keyword::TRIM => self.parse_trim_expr(),
450450
Keyword::INTERVAL => self.parse_literal_interval(),
451451
Keyword::LISTAGG => self.parse_listagg_expr(),
452-
// Treat ARRAY[1,2,3] as an array [1,2,3], otherwise try as function call
452+
// Treat ARRAY[1,2,3] as an array [1,2,3], otherwise try as subquery or a function call
453453
Keyword::ARRAY if self.peek_token() == Token::LBracket => {
454454
self.expect_token(&Token::LBracket)?;
455455
self.parse_array_expr(true)
456456
}
457+
Keyword::ARRAY
458+
if self.peek_token() == Token::LParen
459+
&& !dialect_of!(self is ClickHouseDialect) =>
460+
{
461+
self.expect_token(&Token::LParen)?;
462+
self.parse_array_subquery()
463+
}
457464
Keyword::NOT => self.parse_not(),
458465
// Here `w` is a word, check if it's a part of a multi-part
459466
// identifier, a function call, or a simple identifier:
@@ -927,6 +934,13 @@ impl<'a> Parser<'a> {
927934
}
928935
}
929936

937+
// Parses an array constructed from a subquery
938+
pub fn parse_array_subquery(&mut self) -> Result<Expr, ParserError> {
939+
let query = self.parse_query()?;
940+
self.expect_token(&Token::RParen)?;
941+
Ok(Expr::ArraySubquery(Box::new(query)))
942+
}
943+
930944
/// Parse a SQL LISTAGG expression, e.g. `LISTAGG(...) WITHIN GROUP (ORDER BY ...)`.
931945
pub fn parse_listagg_expr(&mut self) -> Result<Expr, ParserError> {
932946
self.expect_token(&Token::LParen)?;

tests/sqpparser_clickhouse.rs renamed to tests/sqlparser_clickhouse.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,25 @@ fn parse_array_expr() {
121121
)
122122
}
123123

124+
#[test]
125+
fn parse_array_fn() {
126+
let sql = "SELECT array(x1, x2) FROM foo";
127+
let select = clickhouse().verified_only_select(sql);
128+
assert_eq!(
129+
&Expr::Function(Function {
130+
name: ObjectName(vec![Ident::new("array")]),
131+
args: vec![
132+
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(Ident::new("x1")))),
133+
FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(Ident::new("x2")))),
134+
],
135+
over: None,
136+
distinct: false,
137+
special: false,
138+
}),
139+
expr_from_projection(only(&select.projection))
140+
);
141+
}
142+
124143
#[test]
125144
fn parse_kill() {
126145
let stmt = clickhouse().verified_stmt("KILL MUTATION 5");

tests/sqlparser_common.rs

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

25982598
#[test]
25992599
fn parse_scalar_function_in_projection() {
2600-
let names = vec!["sqrt", "array", "foo"];
2600+
let names = vec!["sqrt", "foo"];
26012601

26022602
for function_name in names {
26032603
// 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)