This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git
The following commit(s) were added to refs/heads/master by this push:
new 9c1b46201 feat: Support CompoundIdentifier access as GetIndexedField
(#2454)
9c1b46201 is described below
commit 9c1b4620110396a87cee4d9a9a60238c74255aff
Author: Dmitry Patsura <[email protected]>
AuthorDate: Fri May 6 21:06:29 2022 +0300
feat: Support CompoundIdentifier access as GetIndexedField (#2454)
---
datafusion/core/src/sql/planner.rs | 18 +++++++++++++-----
datafusion/core/tests/sql/select.rs | 15 +++++++++++++++
2 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/datafusion/core/src/sql/planner.rs
b/datafusion/core/src/sql/planner.rs
index 33915300c..33391d91e 100644
--- a/datafusion/core/src/sql/planner.rs
+++ b/datafusion/core/src/sql/planner.rs
@@ -1648,11 +1648,19 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
} else {
match (var_names.pop(), var_names.pop()) {
(Some(name), Some(relation)) if var_names.is_empty()
=> {
- // table.column identifier
- Ok(Expr::Column(Column {
- relation: Some(relation),
- name,
- }))
+ if let Some(field) =
schema.fields().iter().find(|f| f.name().eq(&relation)) {
+ // Access to a field of a column which is a
structure, example: SELECT my_struct.key
+ Ok(Expr::GetIndexedField {
+ expr:
Box::new(Expr::Column(field.qualified_column())),
+ key: ScalarValue::Utf8(Some(name)),
+ })
+ } else {
+ // table.column identifier
+ Ok(Expr::Column(Column {
+ relation: Some(relation),
+ name,
+ }))
+ }
}
_ => Err(DataFusionError::NotImplemented(format!(
"Unsupported compound identifier '{:?}'",
diff --git a/datafusion/core/tests/sql/select.rs
b/datafusion/core/tests/sql/select.rs
index 4ab3a83be..1bf3cb06c 100644
--- a/datafusion/core/tests/sql/select.rs
+++ b/datafusion/core/tests/sql/select.rs
@@ -651,6 +651,21 @@ async fn query_nested_get_indexed_field_on_struct() ->
Result<()> {
"+----------------+",
];
assert_batches_eq!(expected, &actual);
+
+ // Access to field of struct by CompoundIdentifier
+ let sql = "SELECT some_struct.bar as l0 FROM structs LIMIT 3";
+ let actual = execute_to_batches(&ctx, sql).await;
+ let expected = vec![
+ "+----------------+",
+ "| l0 |",
+ "+----------------+",
+ "| [0, 1, 2, 3] |",
+ "| [4, 5, 6, 7] |",
+ "| [8, 9, 10, 11] |",
+ "+----------------+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
let sql = "SELECT some_struct[\"bar\"][0] as i0 FROM structs LIMIT 3";
let actual = execute_to_batches(&ctx, sql).await;
#[rustfmt::skip]