Skip to content

Commit e91dd17

Browse files
committed
support arbitrary composite access expressions
1 parent fac84d8 commit e91dd17

File tree

3 files changed

+96
-20
lines changed

3 files changed

+96
-20
lines changed

src/ast/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ pub enum Expr {
640640
/// The path to the data to extract.
641641
path: JsonPath,
642642
},
643-
/// CompositeAccess (postgres) eg: SELECT (information_schema._pg_expandarray(array['i','i'])).n
643+
/// CompositeAccess eg: SELECT foo(bar).z, (information_schema._pg_expandarray(array['i','i'])).n
644644
CompositeAccess {
645645
expr: Box<Expr>,
646646
key: Ident,

src/parser/mod.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,14 @@ impl<'a> Parser<'a> {
962962
let _guard = self.recursion_counter.try_decrease()?;
963963
debug!("parsing expr");
964964
let mut expr = self.parse_prefix()?;
965+
// Attempt to parse composite access. Example `SELECT f(x).a`
966+
while self.consume_token(&Token::Period) {
967+
expr = Expr::CompositeAccess {
968+
expr: Box::new(expr),
969+
key: self.parse_identifier(false)?,
970+
}
971+
}
972+
965973
debug!("prefix: {:?}", expr);
966974
loop {
967975
let next_precedence = self.get_next_precedence()?;
@@ -1386,25 +1394,7 @@ impl<'a> Parser<'a> {
13861394
}
13871395
};
13881396
self.expect_token(&Token::RParen)?;
1389-
let expr = self.try_parse_method(expr)?;
1390-
if !self.consume_token(&Token::Period) {
1391-
Ok(expr)
1392-
} else {
1393-
let tok = self.next_token();
1394-
let key = match tok.token {
1395-
Token::Word(word) => word.to_ident(tok.span),
1396-
_ => {
1397-
return parser_err!(
1398-
format!("Expected identifier, found: {tok}"),
1399-
tok.span.start
1400-
)
1401-
}
1402-
};
1403-
Ok(Expr::CompositeAccess {
1404-
expr: Box::new(expr),
1405-
key,
1406-
})
1407-
}
1397+
self.try_parse_method(expr)
14081398
}
14091399
Token::Placeholder(_) | Token::Colon | Token::AtSign => {
14101400
self.prev_token();

tests/sqlparser_common.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12334,6 +12334,92 @@ fn parse_create_table_with_bit_types() {
1233412334
}
1233512335
}
1233612336

12337+
#[test]
12338+
fn parse_composed_access_expr() {
12339+
assert_eq!(
12340+
verified_expr("f(a).b"),
12341+
Expr::CompositeAccess {
12342+
expr: Box::new(Expr::Function(Function {
12343+
name: ObjectName(vec![Ident::new("f")]),
12344+
uses_odbc_syntax: false,
12345+
parameters: FunctionArguments::None,
12346+
args: FunctionArguments::List(FunctionArgumentList {
12347+
duplicate_treatment: None,
12348+
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
12349+
Expr::Identifier(Ident::new("a"))
12350+
))],
12351+
clauses: vec![],
12352+
}),
12353+
null_treatment: None,
12354+
filter: None,
12355+
over: None,
12356+
within_group: vec![]
12357+
})),
12358+
key: Ident::new("b")
12359+
}
12360+
);
12361+
12362+
// Nested Composite Access
12363+
assert_eq!(
12364+
verified_expr("f(a).b.c"),
12365+
Expr::CompositeAccess {
12366+
expr: Box::new(Expr::CompositeAccess {
12367+
expr: Box::new(Expr::Function(Function {
12368+
name: ObjectName(vec![Ident::new("f")]),
12369+
uses_odbc_syntax: false,
12370+
parameters: FunctionArguments::None,
12371+
args: FunctionArguments::List(FunctionArgumentList {
12372+
duplicate_treatment: None,
12373+
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
12374+
Expr::Identifier(Ident::new("a"))
12375+
))],
12376+
clauses: vec![],
12377+
}),
12378+
null_treatment: None,
12379+
filter: None,
12380+
over: None,
12381+
within_group: vec![]
12382+
})),
12383+
key: Ident::new("b")
12384+
}),
12385+
key: Ident::new("c")
12386+
}
12387+
);
12388+
12389+
// Composite Access in Select and Where Clauses
12390+
let stmt = verified_only_select("SELECT f(a).b FROM t WHERE f(a).b IS NOT NULL");
12391+
let expr = Expr::CompositeAccess {
12392+
expr: Box::new(Expr::Function(Function {
12393+
name: ObjectName(vec![Ident::new("f")]),
12394+
uses_odbc_syntax: false,
12395+
parameters: FunctionArguments::None,
12396+
args: FunctionArguments::List(FunctionArgumentList {
12397+
duplicate_treatment: None,
12398+
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
12399+
Expr::Identifier(Ident::new("a")),
12400+
))],
12401+
clauses: vec![],
12402+
}),
12403+
null_treatment: None,
12404+
filter: None,
12405+
over: None,
12406+
within_group: vec![],
12407+
})),
12408+
key: Ident::new("b"),
12409+
};
12410+
12411+
assert_eq!(stmt.projection[0], SelectItem::UnnamedExpr(expr.clone()));
12412+
assert_eq!(stmt.selection.unwrap(), Expr::IsNotNull(Box::new(expr)));
12413+
12414+
// Composite Access with quoted identifier
12415+
verified_only_select("SELECT f(a).\"an id\"");
12416+
12417+
// Composite Access in struct literal
12418+
all_dialects_where(|d| d.supports_struct_literal()).verified_stmt(
12419+
"SELECT * FROM t WHERE STRUCT(STRUCT(1 AS a, NULL AS b) AS c, NULL AS d).c.a IS NOT NULL",
12420+
);
12421+
}
12422+
1233712423
#[test]
1233812424
fn parse_create_table_with_enum_types() {
1233912425
let sql = "CREATE TABLE t0 (foo ENUM8('a' = 1, 'b' = 2), bar ENUM16('a' = 1, 'b' = 2), baz ENUM('a', 'b'))";

0 commit comments

Comments
 (0)