Skip to content

Apache DataFusion SQL Query Engine

feat: support nested key for get_field (#18394)

Chen Chongchend8845a61616d
diff --git a/datafusion/functions/src/core/getfield.rs b/datafusion/functions/src/core/getfield.rs
index d18bd6e31f..3be7dd6798 100644
--- a/datafusion/functions/src/core/getfield.rs
+++ b/datafusion/functions/src/core/getfield.rs
@@ -245,6 +245,46 @@ impl ScalarUDFImpl for GetFieldFunc {
Ok(ColumnarValue::Array(data))
}
+ fn process_map_with_nested_key(
+ array: Arc<dyn Array>,
+ key_array: Arc<dyn Array>,
+ ) -> Result<ColumnarValue> {
+ let map_array = as_map_array(array.as_ref())?;
+
+ let comparator = make_comparator(
+ map_array.keys().as_ref(),
+ key_array.as_ref(),
+ SortOptions::default(),
+ )?;
+
+ let original_data = map_array.entries().column(1).to_data();
+ let capacity = Capacities::Array(original_data.len());
+ let mut mutable =
+ MutableArrayData::with_capacities(vec![&original_data], true, capacity);
+
+ for entry in 0..map_array.len() {
+ let start = map_array.value_offsets()[entry] as usize;
+ let end = map_array.value_offsets()[entry + 1] as usize;
+
+ let mut found_match = false;
+ for i in start..end {
+ if comparator(i, 0).is_eq() {
+ mutable.extend(0, i, i + 1);
+ found_match = true;
+ break;
+ }
+ }
+
+ if !found_match {
+ mutable.extend_nulls(1);
+ }
+ }
+
+ let data = mutable.freeze();
+ let data = make_array(data);
+ Ok(ColumnarValue::Array(data))
+ }
+
match (array.data_type(), name) {
(DataType::Map(_, _), ScalarValue::List(arr)) => {
let key_array: Arc<dyn Array> = arr;
@@ -256,7 +296,7 @@ impl ScalarUDFImpl for GetFieldFunc {
(DataType::Map(_, _), other) => {
let data_type = other.data_type();
if data_type.is_nested() {
- exec_err!("unsupported type {} for map access", data_type)
+ process_map_with_nested_key(array, other.to_array()?)
} else {
process_map_array(array, other.to_array()?)
}
diff --git a/datafusion/sqllogictest/test_files/map.slt b/datafusion/sqllogictest/test_files/map.slt
index a3234b4e7e..45f8c5d25f 100644
--- a/datafusion/sqllogictest/test_files/map.slt
+++ b/datafusion/sqllogictest/test_files/map.slt
@@ -544,11 +544,19 @@ SELECT (CASE WHEN 1 > 0 THEN MAP {'x': 100} ELSE MAP {'y': 200} END)['x'];
----
100
-# TODO(https://github.com/apache/datafusion/issues/11785): fix accessing map with non-string key
-# query ?
-# SELECT MAP { MAP {1:'a', 2:'b'}:1, MAP {1:'c', 2:'d'}:2 }[MAP {1:'a', 2:'b'}];
-# ----
-# 1
+# fix accessing map with nested key
+query I
+SELECT MAP { MAP {1:'a', 2:'b'}:1, MAP {1:'c', 2:'d'}:2 }[MAP {1:'a', 2:'b'}];
+----
+1
+
+query I
+SELECT MAP { MAP {1:'a', 2:'b'}:1, MAP {1:'c', 2:'d'}:2 }[MAP {2:'b', 1:'a'}];
+----
+NULL
+
+# TODO(https://github.com/apache/datafusion/pull/18394): Test accessing map with empty map as key
+# TODO(https://github.com/apache/datafusion/pull/18394): Test accessing map with null map as key
# accessing map with non-string key
query I