Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion datafusion/sql/src/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,44 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
}
};

if name.eq("make_map") {
// handle make_map and map functions
// make_map always uses plan_make_map: make_map(k1, v1, k2, v2, ...)
// map has 2 syntaxes:
// 1. map([keys], [values]) - two arrays that get zipped
// 2. map(k1, v1, k2, v2, ...) - variadic pairs (uses plan_make_map)
let use_plan_make_map = match name.as_str() {
"make_map" => true,
"map" => {
// for map, check if this is the first syntax variant (two-array)
let args =
self.function_args_to_expr(args.clone(), schema, planner_context)?;

let is_two_array_syntax = args.len() == 2
&& args.iter().all(|arg| {
matches!(
arg.get_type(schema),
Ok(DataType::List(_))
| Ok(DataType::LargeList(_))
| Ok(DataType::FixedSizeList(_, _))
)
});

// map function with variadic syntax requires non-empty list of arguments
if !is_two_array_syntax && args.is_empty() {
return plan_err!(
"Function 'map' expected at least one argument but received 0"
);
}

!is_two_array_syntax
}
_ => false,
};

if use_plan_make_map {
let mut fn_args =
self.function_args_to_expr(args.clone(), schema, planner_context)?;

for planner in self.context_provider.get_expr_planners().iter() {
match planner.plan_make_map(fn_args)? {
PlannerResult::Planned(expr) => return Ok(expr),
Expand Down
20 changes: 17 additions & 3 deletions datafusion/sqllogictest/test_files/map.slt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ SELECT MAP(['POST', 'HEAD', 'PATCH'], [41, 33, 30]);
----
{POST: 41, HEAD: 33, PATCH: 30}

query ?
SELECT MAP('type', 'test');
----
{type: test}

query ?
SELECT MAP('a', 2, 'b', 3);
----
{a: 2, b: 3}

query ?
SELECT MAP(['POST', 'HEAD', 'PATCH'], [41, 33, null]);
----
Expand All @@ -187,10 +197,10 @@ SELECT MAP([[1,2], [3,4]], ['a', 'b']);
query error
SELECT MAP()

query error DataFusion error: Execution error: map function requires 2 arguments, got 1
query error DataFusion error: Error during planning: make_map requires an even number of arguments
SELECT MAP(['POST', 'HEAD'])

query error DataFusion error: Execution error: Expected list, large_list or fixed_size_list, got Null
query error DataFusion error: Execution error: map key cannot be null
SELECT MAP(null, [41, 33, 30]);

query error DataFusion error: Execution error: map requires key and value lists to have the same length
Expand Down Expand Up @@ -280,8 +290,12 @@ SELECT map(column8, column9) FROM t;
{[4]: b}
{[1, 2]: c}

query error
query ?
SELECT map(column6, column7) FROM t;
----
{[1, 2]: POST}
{[3]: PUT}
{[5]: NULL}

query ?
select Map {column6: column7} from t;
Expand Down