Skip to content

Commit 342cfdf

Browse files
authored
feat: support get_field for map literal (#18371)
## Which issue does this PR close? ## Rationale for this change currently, get_field for map only supports column. ## What changes are included in this PR? support get_field for map literal ## Are these changes tested? UT ## Are there any user-facing changes? No
1 parent 7b8ae01 commit 342cfdf

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

datafusion/functions-nested/src/planner.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//! SQL planning extensions like [`NestedFunctionPlanner`] and [`FieldAccessPlanner`]
1919
2020
use arrow::datatypes::DataType;
21-
use datafusion_common::ExprSchema;
2221
use datafusion_common::{plan_err, utils::list_ndims, DFSchema, Result};
2322
use datafusion_expr::expr::ScalarFunction;
2423
use datafusion_expr::expr::{AggregateFunction, AggregateFunctionParams};
@@ -177,9 +176,7 @@ impl ExprPlanner for FieldAccessPlanner {
177176
)),
178177
)),
179178
// special case for map access with
180-
Expr::Column(ref c)
181-
if matches!(schema.data_type(c)?, DataType::Map(_, _)) =>
182-
{
179+
_ if matches!(expr.get_type(schema)?, DataType::Map(_, _)) => {
183180
Ok(PlannerResult::Planned(Expr::ScalarFunction(
184181
ScalarFunction::new_udf(
185182
get_field_inner(),

datafusion/sqllogictest/test_files/map.slt

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -526,23 +526,35 @@ SELECT MAP { 'a': 1, 'b': 3 };
526526
query error DataFusion error: Arrow error: Cast error: Cannot cast string 'a' to value of Int64 type
527527
SELECT MAP { 'a': 1, 2: 3 };
528528

529-
# TODO(https://github.com/apache/datafusion/issues/11785): fix accessing map with non-string key
530-
# query ?
531-
# SELECT MAP { 1: 'a', 2: 'b', 3: 'c' }[1];
532-
# ----
533-
# a
529+
# accessing map with non-string key
530+
query T
531+
SELECT MAP { 1: 'a', 2: 'b', 3: 'c' }[1];
532+
----
533+
a
534+
535+
# accessing map with string key
536+
query I
537+
SELECT MAP { 'a': 1, 'b': 2, 'c': 3 }['a'];
538+
----
539+
1
540+
541+
# accessing map with non-string key in case expression
542+
query I
543+
SELECT (CASE WHEN 1 > 0 THEN MAP {'x': 100} ELSE MAP {'y': 200} END)['x'];
544+
----
545+
100
534546

535547
# TODO(https://github.com/apache/datafusion/issues/11785): fix accessing map with non-string key
536548
# query ?
537549
# SELECT MAP { MAP {1:'a', 2:'b'}:1, MAP {1:'c', 2:'d'}:2 }[MAP {1:'a', 2:'b'}];
538550
# ----
539551
# 1
540552

541-
# TODO(https://github.com/apache/datafusion/issues/11785): fix accessing map with non-string key
542-
# query ?
543-
# SELECT MAKE_MAP(1, null, 2, 33, 3, null)[2];
544-
# ----
545-
# 33
553+
# accessing map with non-string key
554+
query I
555+
SELECT MAKE_MAP(1, null, 2, 33, 3, null)[2];
556+
----
557+
33
546558

547559
## cardinality
548560

0 commit comments

Comments
 (0)