Support arbitrary composite access expressions (#1600)

This commit is contained in:
Ayman Elkfrawy 2024-12-22 06:22:16 -08:00 committed by GitHub
parent 84e82e6e2e
commit cd898cb6a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 96 additions and 20 deletions

View file

@ -12341,6 +12341,92 @@ fn parse_create_table_with_bit_types() {
}
}
#[test]
fn parse_composite_access_expr() {
assert_eq!(
verified_expr("f(a).b"),
Expr::CompositeAccess {
expr: Box::new(Expr::Function(Function {
name: ObjectName(vec![Ident::new("f")]),
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier(Ident::new("a"))
))],
clauses: vec![],
}),
null_treatment: None,
filter: None,
over: None,
within_group: vec![]
})),
key: Ident::new("b")
}
);
// Nested Composite Access
assert_eq!(
verified_expr("f(a).b.c"),
Expr::CompositeAccess {
expr: Box::new(Expr::CompositeAccess {
expr: Box::new(Expr::Function(Function {
name: ObjectName(vec![Ident::new("f")]),
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier(Ident::new("a"))
))],
clauses: vec![],
}),
null_treatment: None,
filter: None,
over: None,
within_group: vec![]
})),
key: Ident::new("b")
}),
key: Ident::new("c")
}
);
// Composite Access in Select and Where Clauses
let stmt = verified_only_select("SELECT f(a).b FROM t WHERE f(a).b IS NOT NULL");
let expr = Expr::CompositeAccess {
expr: Box::new(Expr::Function(Function {
name: ObjectName(vec![Ident::new("f")]),
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
duplicate_treatment: None,
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(
Expr::Identifier(Ident::new("a")),
))],
clauses: vec![],
}),
null_treatment: None,
filter: None,
over: None,
within_group: vec![],
})),
key: Ident::new("b"),
};
assert_eq!(stmt.projection[0], SelectItem::UnnamedExpr(expr.clone()));
assert_eq!(stmt.selection.unwrap(), Expr::IsNotNull(Box::new(expr)));
// Composite Access with quoted identifier
verified_only_select("SELECT f(a).\"an id\"");
// Composite Access in struct literal
all_dialects_where(|d| d.supports_struct_literal()).verified_stmt(
"SELECT * FROM t WHERE STRUCT(STRUCT(1 AS a, NULL AS b) AS c, NULL AS d).c.a IS NOT NULL",
);
}
#[test]
fn parse_create_table_with_enum_types() {
let sql = "CREATE TABLE t0 (foo ENUM8('a' = 1, 'b' = 2), bar ENUM16('a' = 1, 'b' = 2), baz ENUM('a', 'b'))";