-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(spark): implement Spark map function map_from_entries
#17779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jefffrey
merged 2 commits into
apache:main
from
SparkApplicationMaster:feature/map_from_entries
Sep 29, 2025
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use std::any::Any; | ||
|
|
||
| use crate::function::map::utils::{ | ||
| get_element_type, get_list_offsets, get_list_values, | ||
| map_from_keys_values_offsets_nulls, map_type_from_key_value_types, | ||
| }; | ||
| use arrow::array::{Array, ArrayRef, StructArray}; | ||
| use arrow::datatypes::DataType; | ||
| use datafusion_common::utils::take_function_args; | ||
| use datafusion_common::{exec_err, Result}; | ||
| use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; | ||
| use datafusion_functions::utils::make_scalar_function; | ||
|
|
||
| /// Spark-compatible `map_from_entries` expression | ||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#map_from_entries> | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct MapFromEntries { | ||
| signature: Signature, | ||
| } | ||
|
|
||
| impl Default for MapFromEntries { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl MapFromEntries { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::array(Volatility::Immutable), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for MapFromEntries { | ||
| fn as_any(&self) -> &dyn Any { | ||
| self | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "map_from_entries" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> { | ||
| let [entries_type] = take_function_args("map_from_entries", arg_types)?; | ||
| let entries_element_type = get_element_type(entries_type)?; | ||
| let (keys_type, values_type) = match entries_element_type { | ||
| DataType::Struct(fields) if fields.len() == 2 => { | ||
| Ok((fields[0].data_type(), fields[1].data_type())) | ||
| } | ||
| wrong_type => exec_err!( | ||
| "map_from_entries: expected array<struct<key, value>>, got {:?}", | ||
| wrong_type | ||
| ), | ||
| }?; | ||
| Ok(map_type_from_key_value_types(keys_type, values_type)) | ||
| } | ||
|
|
||
| fn invoke_with_args( | ||
| &self, | ||
| args: datafusion_expr::ScalarFunctionArgs, | ||
| ) -> Result<ColumnarValue> { | ||
| make_scalar_function(map_from_entries_inner, vec![])(&args.args) | ||
| } | ||
| } | ||
|
|
||
| fn map_from_entries_inner(args: &[ArrayRef]) -> Result<ArrayRef> { | ||
| let [entries] = take_function_args("map_from_entries", args)?; | ||
| let entries_offsets = get_list_offsets(entries)?; | ||
| let entries_values = get_list_values(entries)?; | ||
|
|
||
| let (flat_keys, flat_values) = | ||
| match entries_values.as_any().downcast_ref::<StructArray>() { | ||
| Some(a) => Ok((a.column(0), a.column(1))), | ||
| None => exec_err!( | ||
| "map_from_entries: expected array<struct<key, value>>, got {:?}", | ||
| entries_values.data_type() | ||
| ), | ||
| }?; | ||
|
|
||
| map_from_keys_values_offsets_nulls( | ||
| flat_keys, | ||
| flat_values, | ||
| &entries_offsets, | ||
| &entries_offsets, | ||
| entries.nulls(), | ||
| entries.nulls(), | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
datafusion/sqllogictest/test_files/spark/map/map_from_entries.slt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| # Spark doctests | ||
| query ? | ||
| SELECT map_from_entries(array[struct(1, 'a'), struct(2, 'b')]); | ||
| ---- | ||
| {1: a, 2: b} | ||
|
|
||
| query ? | ||
| SELECT map_from_entries(array[struct(1, cast(null as string)), struct(2, 'b')]); | ||
| ---- | ||
| {1: NULL, 2: b} | ||
|
|
||
| query ? | ||
| SELECT map_from_entries(data) | ||
| from values | ||
| (array[struct(1, 'a'), struct(2, 'b')]), | ||
| (array[struct(3, 'c')]) | ||
| as tab(data); | ||
| ---- | ||
| {1: a, 2: b} | ||
| {3: c} | ||
|
|
||
| # Tests with NULL and empty input structarrays | ||
| query ? | ||
| SELECT map_from_entries(data) | ||
| from values | ||
| (cast(array[] as array<struct<int, string>>)), | ||
| (cast(NULL as array<struct<int, string>>)) | ||
| as tab(data); | ||
| ---- | ||
| {} | ||
| NULL | ||
|
|
||
| #Test with multiple rows: good, empty and nullable | ||
| query ? | ||
| SELECT map_from_entries(data) | ||
| from values | ||
| (NULL), | ||
| (array[ | ||
| struct(1 as a, 'b' as b), | ||
| struct(2 as a, cast(NULL as string) as b), | ||
| struct(3 as a, 'd' as b) | ||
| ]), | ||
| (array[]), | ||
| (NULL) | ||
| as tab(data); | ||
| ---- | ||
| NULL | ||
| {1: b, 2: NULL, 3: d} | ||
| {} | ||
| NULL | ||
|
|
||
| # Test with complex types | ||
| query ? | ||
| SELECT map_from_entries(array[ | ||
| struct(array('a', 'b'), struct(1, 2, 3)), | ||
| struct(array('c', 'd'), struct(4, 5, 6)) | ||
| ]); | ||
| ---- | ||
| {[a, b]: {c0: 1, c1: 2, c2: 3}, [c, d]: {c0: 4, c1: 5, c2: 6}} | ||
|
|
||
| # Test with nested function calls | ||
| query ? | ||
| SELECT | ||
| map_from_entries( | ||
| array[ | ||
| struct( | ||
| 'outer_key1', | ||
| -- value for outer_key1: a map itself | ||
| map_from_entries( | ||
| array[ | ||
| struct('inner_a', 1), | ||
| struct('inner_b', 2) | ||
| ] | ||
| ) | ||
| ), | ||
| struct( | ||
| 'outer_key2', | ||
| -- value for outer_key2: another map | ||
| map_from_entries( | ||
| array[ | ||
| struct('inner_x', 10), | ||
| struct('inner_y', 20), | ||
| struct('inner_z', 30) | ||
| ] | ||
| ) | ||
| ) | ||
| ] | ||
| ) AS nested_map; | ||
| ---- | ||
| {outer_key1: {inner_a: 1, inner_b: 2}, outer_key2: {inner_x: 10, inner_y: 20, inner_z: 30}} | ||
|
|
||
| # Test with duplicate keys | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, last win strategy in action |
||
| query ? | ||
| SELECT map_from_entries(array( | ||
| struct(true, 'a'), | ||
| struct(false, 'b'), | ||
| struct(true, 'c'), | ||
| struct(false, cast(NULL as string)), | ||
| struct(true, 'd') | ||
| )); | ||
| ---- | ||
| {false: NULL, true: d} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add tests for
SELECT map_from_entries(NULL)and also when you have a null key?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, there is a test from ([], null) we can also have otherwise (null, [])
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for examples!
Added some tests for input with nulls:
so added and rewritten some code to fix it