Skip to content

Apache DataFusion SQL Query Engine

feat: support get_field for map literal (#18371)

Chen Chongchen342cfdf0e110
diff --git a/datafusion/functions-nested/src/planner.rs b/datafusion/functions-nested/src/planner.rs
index f4fa8630a8..4fec5e3806 100644
--- a/datafusion/functions-nested/src/planner.rs
+++ b/datafusion/functions-nested/src/planner.rs
@@ -18,7 +18,6 @@
//! SQL planning extensions like [`NestedFunctionPlanner`] and [`FieldAccessPlanner`]
use arrow::datatypes::DataType;
-use datafusion_common::ExprSchema;
use datafusion_common::{plan_err, utils::list_ndims, DFSchema, Result};
use datafusion_expr::expr::ScalarFunction;
use datafusion_expr::expr::{AggregateFunction, AggregateFunctionParams};
@@ -177,9 +176,7 @@ impl ExprPlanner for FieldAccessPlanner {
)),
)),
// special case for map access with
- Expr::Column(ref c)
- if matches!(schema.data_type(c)?, DataType::Map(_, _)) =>
- {
+ _ if matches!(expr.get_type(schema)?, DataType::Map(_, _)) => {
Ok(PlannerResult::Planned(Expr::ScalarFunction(
ScalarFunction::new_udf(
get_field_inner(),
diff --git a/datafusion/sqllogictest/test_files/map.slt b/datafusion/sqllogictest/test_files/map.slt
index fc21638b3f..949edb8376 100644
--- a/datafusion/sqllogictest/test_files/map.slt
+++ b/datafusion/sqllogictest/test_files/map.slt
@@ -526,11 +526,23 @@ SELECT MAP { 'a': 1, 'b': 3 };
query error DataFusion error: Arrow error: Cast error: Cannot cast string 'a' to value of Int64 type
SELECT MAP { 'a': 1, 2: 3 };
-# TODO(https://github.com/apache/datafusion/issues/11785): fix accessing map with non-string key
-# query ?
-# SELECT MAP { 1: 'a', 2: 'b', 3: 'c' }[1];
-# ----
-# a
+# accessing map with non-string key
+query T
+SELECT MAP { 1: 'a', 2: 'b', 3: 'c' }[1];
+----
+a
+
+# accessing map with string key
+query I
+SELECT MAP { 'a': 1, 'b': 2, 'c': 3 }['a'];
+----
+1
+
+# accessing map with non-string key in case expression
+query I
+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 ?
@@ -538,11 +550,11 @@ SELECT MAP { 'a': 1, 2: 3 };
# ----
# 1
-# TODO(https://github.com/apache/datafusion/issues/11785): fix accessing map with non-string key
-# query ?
-# SELECT MAKE_MAP(1, null, 2, 33, 3, null)[2];
-# ----
-# 33
+# accessing map with non-string key
+query I
+SELECT MAKE_MAP(1, null, 2, 33, 3, null)[2];
+----
+33
## cardinality