Skip to content

Apache DataFusion SQL Query Engine

feat: Support CompoundIdentifier access as GetIndexedField (#2454)

Dmitry Patsura9c1b46201103
diff --git a/datafusion/core/src/sql/planner.rs b/datafusion/core/src/sql/planner.rs
index 33915300c6..33391d91e8 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 4ab3a83be1..1bf3cb06c7 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]