Skip to content

Conversation

vegarsti
Copy link
Contributor

@vegarsti vegarsti commented Oct 2, 2025

Which issue does this PR close?

What changes are included in this PR?

Makes convert_array_to_scalar_vec return None instead of the empty list when list elements are null.

Are these changes tested?

Covered by existing tests.

Are there any user-facing changes?

I don't think so?

@github-actions github-actions bot added core Core DataFusion crate common Related to common crate functions Changes to functions implementation labels Oct 2, 2025
@vegarsti vegarsti changed the title feat: convert_array_to_scalar_vec returns optional arrays feat: convert_array_to_scalar_vec respects null elements Oct 2, 2025
@vegarsti vegarsti force-pushed the convert-array-to-scalar-vec-optional branch from 9c6db36 to b1bb29c Compare October 2, 2025 20:03
Comment on lines +693 to +695
} else {
partition_values.push(vec![].into());
}
Copy link
Contributor Author

@vegarsti vegarsti Oct 2, 2025

Choose a reason for hiding this comment

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

I'm not sure about this one! With the other changes, the group_by slt tests fail without this change. It is because in merge_ordered_arrays we require that partition_values and partition_ordering_values have the same length (see code snippet below). But that seems weird since we now only do partition_ordering_values.push(ordering_value) if the element in orderings is Some.

if values.len() != ordering_values.len()
|| values
.iter()
.zip(ordering_values.iter())
.any(|(vals, ordering_vals)| vals.len() != ordering_vals.len())
{
return exec_err!(
"Expects values arguments and/or ordering_values arguments to have same size"
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's also an exact same change done in nth_values.rs

.iter()
.zip(ordering_values.iter())
.any(|(vals, ordering_vals)| vals.len() != ordering_vals.len())
if values.len() != ordering_values.len() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't exactly feel strongly about these changes, but since these two conditions are different I liked the error message telling me which was the case.

@vegarsti vegarsti force-pushed the convert-array-to-scalar-vec-optional branch 2 times, most recently from 217287c to 35358d6 Compare October 2, 2025 20:35
Copy link
Contributor

@comphead comphead left a comment

Choose a reason for hiding this comment

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

Thanks @vegarsti I would love to see if there any performance degradations, you can find benches in the project.

Maybe we can have a separate test for this issues?

@vegarsti
Copy link
Contributor Author

vegarsti commented Oct 3, 2025

Thanks @vegarsti I would love to see if there any performance degradations, you can find benches in the project.

Maybe we can have a separate test for this issues?

Good call, I will do both. Thanks!

@vegarsti
Copy link
Contributor Author

vegarsti commented Oct 4, 2025

Benchmark results cc @comphead

Comparing main and convert-array-to-scalar-vec-optional
--------------------
Benchmark tpch_sf1.json
--------------------
┏━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query        ┃     main ┃ convert-array-to-scalar-vec-optional ┃        Change ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1     │ 72.27 ms │                             73.81 ms │     no change │
│ QQuery 2     │ 21.19 ms │                             21.89 ms │     no change │
│ QQuery 3     │ 33.45 ms │                             32.10 ms │     no change │
│ QQuery 4     │ 24.40 ms │                             23.64 ms │     no change │
│ QQuery 5     │ 51.61 ms │                             50.10 ms │     no change │
│ QQuery 6     │ 21.93 ms │                             21.73 ms │     no change │
│ QQuery 7     │ 71.15 ms │                             71.24 ms │     no change │
│ QQuery 8     │ 47.88 ms │                             45.30 ms │ +1.06x faster │
│ QQuery 9     │ 63.38 ms │                             65.36 ms │     no change │
│ QQuery 10    │ 46.32 ms │                             47.16 ms │     no change │
│ QQuery 11    │ 15.68 ms │                             15.56 ms │     no change │
│ QQuery 12    │ 31.77 ms │                             31.90 ms │     no change │
│ QQuery 13    │ 35.49 ms │                             34.81 ms │     no change │
│ QQuery 14    │ 27.31 ms │                             27.71 ms │     no change │
│ QQuery 15    │ 38.38 ms │                             37.60 ms │     no change │
│ QQuery 16    │ 14.13 ms │                             14.26 ms │     no change │
│ QQuery 17    │ 87.88 ms │                             89.00 ms │     no change │
│ QQuery 18    │ 98.37 ms │                             97.68 ms │     no change │
│ QQuery 19    │ 42.45 ms │                             41.87 ms │     no change │
│ QQuery 20    │ 33.75 ms │                             33.08 ms │     no change │
│ QQuery 21    │ 75.81 ms │                             76.60 ms │     no change │
│ QQuery 22    │ 10.94 ms │                             11.10 ms │     no change │
└──────────────┴──────────┴──────────────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ Benchmark Summary                                   ┃          ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ Total Time (main)                                   │ 965.54ms │
│ Total Time (convert-array-to-scalar-vec-optional)   │ 963.50ms │
│ Average Time (main)                                 │  43.89ms │
│ Average Time (convert-array-to-scalar-vec-optional) │  43.80ms │
│ Queries Faster                                      │        1 │
│ Queries Slower                                      │        0 │
│ Queries with No Change                              │       21 │
│ Queries with Failure                                │        0 │
└─────────────────────────────────────────────────────┴──────────┘

@vegarsti vegarsti force-pushed the convert-array-to-scalar-vec-optional branch from 35358d6 to e232719 Compare October 4, 2025 06:27
Comment on lines +9128 to +9152
// 4: Offsets + Values looks like this: [[1, 2], [], [5]]
// But with NullBuffer it's like this: [[1, 2], NULL, [5]]
// The converted result is: [[1, 2], None, [5]]
let array4 = ListArray::new(
Field::new_list_field(DataType::Int64, true).into(),
OffsetBuffer::new(vec![0, 2, 2, 5].into()),
Arc::new(Int64Array::from(vec![1, 2, 3, 4, 5, 6])),
Some(NullBuffer::from(vec![true, false, true])),
);
let converted = ScalarValue::convert_array_to_scalar_vec(&array4).unwrap();
assert_eq!(
converted,
vec![
Some(vec![
ScalarValue::Int64(Some(1)),
ScalarValue::Int64(Some(2))
]),
None,
Some(vec![
ScalarValue::Int64(Some(3)),
ScalarValue::Int64(Some(4)),
ScalarValue::Int64(Some(5)),
]),
]
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

On main the middle element here would be an empty vec, not None.

Comment on lines +9154 to 9176
// 5: Offsets + Values looks like this: [[1, 2], [], [5]]
// Same as 4, but the middle array is not null, so after conversion it's empty.
let array5 = ListArray::new(
Field::new_list_field(DataType::Int64, true).into(),
OffsetBuffer::new(vec![0, 2, 2, 5].into()),
Arc::new(Int64Array::from(vec![1, 2, 3, 4, 5, 6])),
Some(NullBuffer::from(vec![true, true, true])),
);
let converted = ScalarValue::convert_array_to_scalar_vec(&array5).unwrap();
assert_eq!(
converted,
vec![
Some(vec![
ScalarValue::Int64(Some(1)),
ScalarValue::Int64(Some(2))
]),
Some(vec![]),
Some(vec![
ScalarValue::Int64(Some(3)),
ScalarValue::Int64(Some(4)),
ScalarValue::Int64(Some(5)),
]),
]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

On main the middle element here would also be an empty vec, not None.

@vegarsti
Copy link
Contributor Author

vegarsti commented Oct 4, 2025

Thanks @vegarsti I would love to see if there any performance degradations, you can find benches in the project.

Maybe we can have a separate test for this issues?

I added two test cases to show the change in this PR. On main these both return an empty vector, but with this PR the nullability is preserved, so in the first one (4) it's None and in the second one (5) it's Some (an empty vec).

https://github.com/apache/datafusion/pull/17891/files#diff-49e275af8f09685c7bbc491db8ab3b9479960878f42ac558ec0e3e39570590bd

https://github.com/vegarsti/datafusion/blob/e23271991bb2726df3ced25011cff0b08501b7b0/datafusion/common/src/scalar/mod.rs#L9128-L9177

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
common Related to common crate core Core DataFusion crate functions Changes to functions implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ScalarValue::convert_array_to_scalar_vec doesn't respect null list elements
2 participants