mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-07-07 17:04:59 +00:00
Consolidate MapAccess
, and Subscript
into CompoundExpr
to handle the complex field access chain (#1551)
This commit is contained in:
parent
cd898cb6a4
commit
0647a4aa82
9 changed files with 455 additions and 287 deletions
|
@ -2095,11 +2095,11 @@ fn parse_array_index_expr() {
|
|||
let sql = "SELECT foo[0] FROM foos";
|
||||
let select = pg_and_generic().verified_only_select(sql);
|
||||
assert_eq!(
|
||||
&Expr::Subscript {
|
||||
expr: Box::new(Expr::Identifier(Ident::new("foo"))),
|
||||
subscript: Box::new(Subscript::Index {
|
||||
&Expr::CompoundFieldAccess {
|
||||
root: Box::new(Expr::Identifier(Ident::new("foo"))),
|
||||
access_chain: vec![AccessExpr::Subscript(Subscript::Index {
|
||||
index: num[0].clone()
|
||||
}),
|
||||
})],
|
||||
},
|
||||
expr_from_projection(only(&select.projection)),
|
||||
);
|
||||
|
@ -2107,16 +2107,16 @@ fn parse_array_index_expr() {
|
|||
let sql = "SELECT foo[0][0] FROM foos";
|
||||
let select = pg_and_generic().verified_only_select(sql);
|
||||
assert_eq!(
|
||||
&Expr::Subscript {
|
||||
expr: Box::new(Expr::Subscript {
|
||||
expr: Box::new(Expr::Identifier(Ident::new("foo"))),
|
||||
subscript: Box::new(Subscript::Index {
|
||||
&Expr::CompoundFieldAccess {
|
||||
root: Box::new(Expr::Identifier(Ident::new("foo"))),
|
||||
access_chain: vec![
|
||||
AccessExpr::Subscript(Subscript::Index {
|
||||
index: num[0].clone()
|
||||
}),
|
||||
}),
|
||||
subscript: Box::new(Subscript::Index {
|
||||
index: num[0].clone()
|
||||
}),
|
||||
AccessExpr::Subscript(Subscript::Index {
|
||||
index: num[0].clone()
|
||||
})
|
||||
],
|
||||
},
|
||||
expr_from_projection(only(&select.projection)),
|
||||
);
|
||||
|
@ -2124,29 +2124,27 @@ fn parse_array_index_expr() {
|
|||
let sql = r#"SELECT bar[0]["baz"]["fooz"] FROM foos"#;
|
||||
let select = pg_and_generic().verified_only_select(sql);
|
||||
assert_eq!(
|
||||
&Expr::Subscript {
|
||||
expr: Box::new(Expr::Subscript {
|
||||
expr: Box::new(Expr::Subscript {
|
||||
expr: Box::new(Expr::Identifier(Ident::new("bar"))),
|
||||
subscript: Box::new(Subscript::Index {
|
||||
index: num[0].clone()
|
||||
})
|
||||
&Expr::CompoundFieldAccess {
|
||||
root: Box::new(Expr::Identifier(Ident::new("bar"))),
|
||||
access_chain: vec![
|
||||
AccessExpr::Subscript(Subscript::Index {
|
||||
index: num[0].clone()
|
||||
}),
|
||||
subscript: Box::new(Subscript::Index {
|
||||
AccessExpr::Subscript(Subscript::Index {
|
||||
index: Expr::Identifier(Ident {
|
||||
value: "baz".to_string(),
|
||||
quote_style: Some('"'),
|
||||
span: Span::empty(),
|
||||
})
|
||||
})
|
||||
}),
|
||||
subscript: Box::new(Subscript::Index {
|
||||
index: Expr::Identifier(Ident {
|
||||
value: "fooz".to_string(),
|
||||
quote_style: Some('"'),
|
||||
span: Span::empty(),
|
||||
})
|
||||
})
|
||||
}),
|
||||
AccessExpr::Subscript(Subscript::Index {
|
||||
index: Expr::Identifier(Ident {
|
||||
value: "fooz".to_string(),
|
||||
quote_style: Some('"'),
|
||||
span: Span::empty(),
|
||||
})
|
||||
}),
|
||||
],
|
||||
},
|
||||
expr_from_projection(only(&select.projection)),
|
||||
);
|
||||
|
@ -2154,33 +2152,33 @@ fn parse_array_index_expr() {
|
|||
let sql = "SELECT (CAST(ARRAY[ARRAY[2, 3]] AS INT[][]))[1][2]";
|
||||
let select = pg_and_generic().verified_only_select(sql);
|
||||
assert_eq!(
|
||||
&Expr::Subscript {
|
||||
expr: Box::new(Expr::Subscript {
|
||||
expr: Box::new(Expr::Nested(Box::new(Expr::Cast {
|
||||
kind: CastKind::Cast,
|
||||
expr: Box::new(Expr::Array(Array {
|
||||
elem: vec![Expr::Array(Array {
|
||||
elem: vec![num[2].clone(), num[3].clone(),],
|
||||
named: true,
|
||||
})],
|
||||
&Expr::CompoundFieldAccess {
|
||||
root: Box::new(Expr::Nested(Box::new(Expr::Cast {
|
||||
kind: CastKind::Cast,
|
||||
expr: Box::new(Expr::Array(Array {
|
||||
elem: vec![Expr::Array(Array {
|
||||
elem: vec![num[2].clone(), num[3].clone(),],
|
||||
named: true,
|
||||
})),
|
||||
data_type: DataType::Array(ArrayElemTypeDef::SquareBracket(
|
||||
Box::new(DataType::Array(ArrayElemTypeDef::SquareBracket(
|
||||
Box::new(DataType::Int(None)),
|
||||
None
|
||||
))),
|
||||
})],
|
||||
named: true,
|
||||
})),
|
||||
data_type: DataType::Array(ArrayElemTypeDef::SquareBracket(
|
||||
Box::new(DataType::Array(ArrayElemTypeDef::SquareBracket(
|
||||
Box::new(DataType::Int(None)),
|
||||
None
|
||||
)),
|
||||
format: None,
|
||||
}))),
|
||||
subscript: Box::new(Subscript::Index {
|
||||
))),
|
||||
None
|
||||
)),
|
||||
format: None,
|
||||
}))),
|
||||
access_chain: vec![
|
||||
AccessExpr::Subscript(Subscript::Index {
|
||||
index: num[1].clone()
|
||||
}),
|
||||
}),
|
||||
subscript: Box::new(Subscript::Index {
|
||||
index: num[2].clone()
|
||||
}),
|
||||
AccessExpr::Subscript(Subscript::Index {
|
||||
index: num[2].clone()
|
||||
}),
|
||||
],
|
||||
},
|
||||
expr_from_projection(only(&select.projection)),
|
||||
);
|
||||
|
@ -2269,9 +2267,13 @@ fn parse_array_subscript() {
|
|||
),
|
||||
];
|
||||
for (sql, expect) in tests {
|
||||
let Expr::Subscript { subscript, .. } = pg_and_generic().verified_expr(sql) else {
|
||||
let Expr::CompoundFieldAccess { access_chain, .. } = pg_and_generic().verified_expr(sql)
|
||||
else {
|
||||
panic!("expected subscript expr");
|
||||
};
|
||||
let Some(AccessExpr::Subscript(subscript)) = access_chain.last() else {
|
||||
panic!("expected subscript");
|
||||
};
|
||||
assert_eq!(expect, *subscript);
|
||||
}
|
||||
|
||||
|
@ -2282,25 +2284,25 @@ fn parse_array_subscript() {
|
|||
fn parse_array_multi_subscript() {
|
||||
let expr = pg_and_generic().verified_expr("make_array(1, 2, 3)[1:2][2]");
|
||||
assert_eq!(
|
||||
Expr::Subscript {
|
||||
expr: Box::new(Expr::Subscript {
|
||||
expr: Box::new(call(
|
||||
"make_array",
|
||||
vec![
|
||||
Expr::Value(number("1")),
|
||||
Expr::Value(number("2")),
|
||||
Expr::Value(number("3"))
|
||||
]
|
||||
)),
|
||||
subscript: Box::new(Subscript::Slice {
|
||||
Expr::CompoundFieldAccess {
|
||||
root: Box::new(call(
|
||||
"make_array",
|
||||
vec![
|
||||
Expr::Value(number("1")),
|
||||
Expr::Value(number("2")),
|
||||
Expr::Value(number("3"))
|
||||
]
|
||||
)),
|
||||
access_chain: vec![
|
||||
AccessExpr::Subscript(Subscript::Slice {
|
||||
lower_bound: Some(Expr::Value(number("1"))),
|
||||
upper_bound: Some(Expr::Value(number("2"))),
|
||||
stride: None,
|
||||
}),
|
||||
}),
|
||||
subscript: Box::new(Subscript::Index {
|
||||
index: Expr::Value(number("2")),
|
||||
}),
|
||||
AccessExpr::Subscript(Subscript::Index {
|
||||
index: Expr::Value(number("2")),
|
||||
}),
|
||||
],
|
||||
},
|
||||
expr,
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue