Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 29 additions & 2 deletions datafusion/functions-nested/src/map_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,36 @@ fn map_keys_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
};

Ok(Arc::new(ListArray::new(
Arc::new(Field::new_list_field(map_array.key_type().clone(), false)),
Arc::new(Field::new_list_field(map_array.key_type().clone(), true)),
map_array.offsets().clone(),
Arc::clone(map_array.keys()),
None,
map_array.nulls().cloned(),
)))
}

#[cfg(test)]
mod test {
Copy link
Contributor Author

@cht42 cht42 Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can test it using slt with this trick (create a column with non null value and then insert a NULL into it)

DataFusion CLI v44.0.0
> create table foo as values (MAP {'foo': 'bar'});
0 row(s) fetched.
Elapsed 0.037 seconds.

> insert into foo values (NULL);
+-------+
| count |
+-------+
| 1     |
+-------+
1 row(s) fetched.
Elapsed 0.016 seconds.

> select * from foo;
+------------+
| column1    |
+------------+
| {foo: bar} |
| NULL       |
+------------+
2 row(s) fetched.
Elapsed 0.004 seconds.

> select column1, arrow_typeof(column1) from foo;
+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| column1    | arrow_typeof(foo.column1)                                                                                                                                                                                                                                                                                                                            |
+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {foo: bar} | Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, false) |
| NULL       | Map(Field { name: "entries", data_type: Struct([Field { name: "key", data_type: Utf8, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, Field { name: "value", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }]), nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }, false) |
+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 row(s) fetched.
Elapsed 0.006 seconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sweet, will update

use super::*;
use arrow::array::{as_list_array, Int32Builder, MapBuilder, StringBuilder};

#[test]
fn test_map_null() -> Result<()> {
let string_builder = StringBuilder::new();
let int_builder = Int32Builder::with_capacity(1);
let mut builder = MapBuilder::new(None, string_builder, int_builder);
builder.append(false).unwrap();
let map_array = builder.finish();

let result = MapKeysFunc::new()
.invoke_batch(&[ColumnarValue::Array(Arc::new(map_array))], 1)
.unwrap();
match result {
ColumnarValue::Array(array) => {
let array = as_list_array(&array);
assert!(array.is_null(0));
}
_ => panic!("Expected an array value"),
}
Ok(())
}
}
29 changes: 28 additions & 1 deletion datafusion/functions-nested/src/map_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,33 @@ fn map_values_inner(args: &[ArrayRef]) -> Result<ArrayRef> {
Arc::new(Field::new_list_field(map_array.value_type().clone(), true)),
map_array.offsets().clone(),
Arc::clone(map_array.values()),
None,
map_array.nulls().cloned(),
)))
}

#[cfg(test)]
mod test {
use super::*;
use arrow::array::{as_list_array, Int32Builder, MapBuilder, StringBuilder};

#[test]
fn test_map_null() -> Result<()> {
let string_builder = StringBuilder::new();
let int_builder = Int32Builder::with_capacity(1);
let mut builder = MapBuilder::new(None, string_builder, int_builder);
builder.append(false).unwrap();
let map_array = builder.finish();

let result = MapValuesFunc::new()
.invoke_batch(&[ColumnarValue::Array(Arc::new(map_array))], 1)
.unwrap();
match result {
ColumnarValue::Array(array) => {
let array = as_list_array(&array);
assert!(array.is_null(0));
}
_ => panic!("Expected an array value"),
}
Ok(())
}
}
Loading