Skip to content
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

Fix NULL values in FixedSizeList creation #9141

Merged
merged 3 commits into from
Feb 7, 2024
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
21 changes: 17 additions & 4 deletions datafusion/common/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,24 +1385,37 @@ impl ScalarValue {
.map(|s| s.to_array())
.collect::<Result<Vec<_>>>()?;

let capacity = Capacities::Array(arrays.iter().map(|arr| arr.len()).sum());
let capacity = Capacities::Array(
arrays
.iter()
Copy link
Contributor

Choose a reason for hiding this comment

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

was thinking if filter_map will be handy here to avoid double iteration?

arrays.iter().filter_map(|arr| {if arr.is_null(0) {None} else Some(arr.len()) })

.filter(|arr| !arr.is_null(0))
.map(|arr| arr.len())
.sum(),
);
// ScalarValue::List contains a single element ListArray.
let nulls = arrays
.iter()
.map(|arr| arr.is_null(0))
.collect::<Vec<bool>>();
let arrays_data = arrays.iter().map(|arr| arr.to_data()).collect::<Vec<_>>();
let arrays_data = arrays
.iter()
.filter(|arr| !arr.is_null(0))
.map(|arr| arr.to_data())
.collect::<Vec<_>>();

let arrays_ref = arrays_data.iter().collect::<Vec<_>>();
let mut mutable =
MutableArrayData::with_capacities(arrays_ref, true, capacity);

// ScalarValue::List contains a single element ListArray.
let mut cnt = 0;
for (index, is_null) in (0..arrays.len()).zip(nulls.into_iter()) {
if is_null {
mutable.extend_nulls(1)
mutable.extend_nulls(1);
cnt += 1;
} else {
mutable.extend(index, 0, 1);
// mutable array contains non-null elements
mutable.extend(index - cnt, 0, 1);
}
}
let data = mutable.freeze();
Expand Down
8 changes: 3 additions & 5 deletions datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,16 @@ AS
FROM arrays
;

#TODO: create FixedSizeList with NULL column
statement ok
CREATE TABLE fixed_size_arrays
AS VALUES
(arrow_cast(make_array(make_array(NULL, 2),make_array(3, NULL)), 'FixedSizeList(2, List(Int64))'), arrow_cast(make_array(1.1, 2.2, 3.3), 'FixedSizeList(3, Float64)'), arrow_cast(make_array('L', 'o', 'r', 'e', 'm'), 'FixedSizeList(5, Utf8)')),
(arrow_cast(make_array(make_array(3, 4),make_array(5, 6)), 'FixedSizeList(2, List(Int64))'), arrow_cast(make_array(NULL, 5.5, 6.6), 'FixedSizeList(3, Float64)'), arrow_cast(make_array('i', 'p', NULL, 'u', 'm'), 'FixedSizeList(5, Utf8)')),
(arrow_cast(make_array(make_array(5, 6),make_array(7, 8)), 'FixedSizeList(2, List(Int64))'), arrow_cast(make_array(7.7, 8.8, 9.9), 'FixedSizeList(3, Float64)'), arrow_cast(make_array('d', NULL, 'l', 'o', 'r'), 'FixedSizeList(5, Utf8)')),
(arrow_cast(make_array(make_array(7, NULL),make_array(9, 10)), 'FixedSizeList(2, List(Int64))'), arrow_cast(make_array(10.1, NULL, 12.2), 'FixedSizeList(3, Float64)'), arrow_cast(make_array('s', 'i', 't', 'a', 'b'), 'FixedSizeList(5, Utf8)')),
(arrow_cast(make_array(make_array(7, NULL),make_array(9, 10)), 'FixedSizeList(2, List(Int64))'), arrow_cast(make_array(13.3, 14.4, 15.5), 'FixedSizeList(3, Float64)'), arrow_cast(make_array('a', 'm', 'e', 't', 'x'), 'FixedSizeList(5, Utf8)')),
(arrow_cast(make_array(make_array(11, 12),make_array(13, 14)), 'FixedSizeList(2, List(Int64))'), arrow_cast(make_array(13.3, 14.4, 15.5), 'FixedSizeList(3, Float64)'), arrow_cast(make_array(',','a','b','c','d'), 'FixedSizeList(5, Utf8)')),
(arrow_cast(make_array(make_array(15, 16),make_array(NULL, 18)), 'FixedSizeList(2, List(Int64))'), arrow_cast(make_array(16.6, 17.7, 18.8), 'FixedSizeList(3, Float64)'), arrow_cast(make_array(',','a','b','c','d'), 'FixedSizeList(5, Utf8)'))
(NULL, arrow_cast(make_array(13.3, 14.4, 15.5), 'FixedSizeList(3, Float64)'), arrow_cast(make_array('a', 'm', 'e', 't', 'x'), 'FixedSizeList(5, Utf8)')),
(arrow_cast(make_array(make_array(11, 12),make_array(13, 14)), 'FixedSizeList(2, List(Int64))'), NULL, arrow_cast(make_array(',','a','b','c','d'), 'FixedSizeList(5, Utf8)')),
(arrow_cast(make_array(make_array(15, 16),make_array(NULL, 18)), 'FixedSizeList(2, List(Int64))'), arrow_cast(make_array(16.6, 17.7, 18.8), 'FixedSizeList(3, Float64)'), NULL)
;

statement ok
Expand Down Expand Up @@ -4707,7 +4706,6 @@ false false false true
true false true false
true false false true
false true false false
false true false false
false false false false
false false false false

Expand Down
Loading