You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "viirya (via GitHub)" <gi...@apache.org> on 2023/10/29 08:08:48 UTC
[PR] Fix `try_from_array` data type for NULL value in ListArray [arrow-datafusion]
viirya opened a new pull request, #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #7968.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `try_from_array` data type for NULL value in ListArray [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969#discussion_r1375524276
##########
datafusion/common/src/scalar.rs:
##########
@@ -2234,28 +2239,20 @@ impl ScalarValue {
}
DataType::Utf8 => typed_cast!(array, index, StringArray, Utf8),
DataType::LargeUtf8 => typed_cast!(array, index, LargeStringArray, LargeUtf8),
- DataType::List(nested_type) => {
+ DataType::List(_) => {
let list_array = as_list_array(array);
- let arr = match list_array.is_null(index) {
- true => new_null_array(nested_type.data_type(), 0),
Review Comment:
This condition is redundant. `try_from_array` handles NULL value at the beginning.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `ScalarValue` handling of NULL values for ListArray [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969#discussion_r1375572207
##########
datafusion/common/src/scalar.rs:
##########
@@ -1699,22 +1702,24 @@ impl ScalarValue {
for scalar in scalars {
if let ScalarValue::List(arr) = scalar {
- // i.e. NullArray(1)
- if arr.as_any().downcast_ref::<NullArray>().is_some() {
+ // `ScalarValue::List` contains a single element `ListArray`.
+ let list_arr = as_list_array(&arr);
+
+ if list_arr.is_null(0) {
// Repeat previous offset index
offsets.push(0);
// Element is null
valid.append(false);
- } else {
- let list_arr = as_list_array(&arr);
- let arr = list_arr.values().to_owned();
- offsets.push(arr.len());
- elements.push(arr);
-
- // Element is valid
- valid.append(true);
+ continue;
Review Comment:
I think if-else statement here is more readable.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `try_from_array` data type for NULL value in ListArray [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969#discussion_r1375524276
##########
datafusion/common/src/scalar.rs:
##########
@@ -2234,28 +2239,20 @@ impl ScalarValue {
}
DataType::Utf8 => typed_cast!(array, index, StringArray, Utf8),
DataType::LargeUtf8 => typed_cast!(array, index, LargeStringArray, LargeUtf8),
- DataType::List(nested_type) => {
+ DataType::List(_) => {
let list_array = as_list_array(array);
- let arr = match list_array.is_null(index) {
- true => new_null_array(nested_type.data_type(), 0),
Review Comment:
This condition is redundant and not reached. `try_from_array` handles NULL value at the beginning.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `ScalarValue` handling of NULL values for ListArray [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `ScalarValue` handling of NULL values for ListArray [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969#discussion_r1375527801
##########
datafusion/common/src/scalar.rs:
##########
@@ -3885,6 +3889,77 @@ mod tests {
);
}
+ #[test]
+ fn scalar_try_from_array_list_array_null() {
+ let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
+ Some(vec![Some(1), Some(2)]),
+ None,
+ ]);
+
+ let non_null_list_scalar = ScalarValue::try_from_array(&list, 0).unwrap();
Review Comment:
Previously, for a ScalarValue NULL value from ListArray, its datatype is `DataType::Null`. That's is not correct.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `ScalarValue` handling of NULL values for ListArray [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969#issuecomment-1785989011
Thank you @alamb @comphead @jackwener @jayzhan211
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `try_from_array` data type for NULL value in ListArray [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969#discussion_r1375523966
##########
datafusion/common/src/scalar.rs:
##########
@@ -3885,6 +3889,77 @@ mod tests {
);
}
+ #[test]
+ fn scalar_try_from_array_list_array_null() {
+ let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
+ Some(vec![Some(1), Some(2)]),
+ None,
+ ]);
+
+ let non_null_list_scalar = ScalarValue::try_from_array(&list, 0).unwrap();
+ let null_list_scalar = ScalarValue::try_from_array(&list, 1).unwrap();
+
+ assert_eq!(
+ non_null_list_scalar.data_type(),
+ null_list_scalar.data_type()
+ );
+ }
+
+ #[test]
+ fn scalar_try_from_list() {
+ let data_type =
+ DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
+ let data_type = &data_type;
+ let scalar: ScalarValue = data_type.try_into().unwrap();
+
+ let expected = ScalarValue::List(new_null_array(
+ &DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
+ 1,
+ ));
+
+ assert_eq!(expected, scalar)
+ }
+
+ #[test]
+ fn scalar_try_from_list_of_list() {
+ let data_type = DataType::List(Arc::new(Field::new(
+ "item",
+ DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
+ true,
+ )));
+ let data_type = &data_type;
+ let scalar: ScalarValue = data_type.try_into().unwrap();
+
+ let expected = ScalarValue::List(new_null_array(
+ &DataType::List(Arc::new(Field::new(
+ "item",
+ DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
+ true,
+ ))),
+ 1,
+ ));
+
+ assert_eq!(expected, scalar)
+ }
+
+ #[test]
+ fn scalar_try_from_not_equal_list_nested_list() {
+ let list_data_type =
+ DataType::List(Arc::new(Field::new("item", DataType::Int32, true)));
+ let data_type = &list_data_type;
+ let list_scalar: ScalarValue = data_type.try_into().unwrap();
+
+ let nested_list_data_type = DataType::List(Arc::new(Field::new(
+ "item",
+ DataType::List(Arc::new(Field::new("item", DataType::Int32, true))),
+ true,
+ )));
+ let data_type = &nested_list_data_type;
+ let nested_list_scalar: ScalarValue = data_type.try_into().unwrap();
+
+ assert_ne!(list_scalar, nested_list_scalar);
Review Comment:
Previously the `ScalarValue`s for list and nested list NULL values are equal, that is incorrect.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `ScalarValue` handling of NULL values for ListArray [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969#discussion_r1375527801
##########
datafusion/common/src/scalar.rs:
##########
@@ -3885,6 +3889,77 @@ mod tests {
);
}
+ #[test]
+ fn scalar_try_from_array_list_array_null() {
+ let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
+ Some(vec![Some(1), Some(2)]),
+ None,
+ ]);
+
+ let non_null_list_scalar = ScalarValue::try_from_array(&list, 0).unwrap();
Review Comment:
Previously, for a NULL value from `ListArray`, the generated `ScalarValue::List` value's datatype is `DataType::Null`. That's is not correct.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `ScalarValue` handling of NULL values for ListArray [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969#issuecomment-1786006577
> Thank you @alamb @comphead @jackwener @jayzhan211
Team effort 🦾
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `try_from_array` data type for NULL value in ListArray [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969#discussion_r1375524481
##########
datafusion/common/src/scalar.rs:
##########
@@ -2234,28 +2239,20 @@ impl ScalarValue {
}
DataType::Utf8 => typed_cast!(array, index, StringArray, Utf8),
DataType::LargeUtf8 => typed_cast!(array, index, LargeStringArray, LargeUtf8),
- DataType::List(nested_type) => {
+ DataType::List(_) => {
let list_array = as_list_array(array);
- let arr = match list_array.is_null(index) {
- true => new_null_array(nested_type.data_type(), 0),
- false => {
- let nested_array = list_array.value(index);
- Arc::new(wrap_into_list_array(nested_array))
- }
- };
+ let nested_array = list_array.value(index);
+ // Produces a single element `ListArray` with the value at `index`.
+ let arr = Arc::new(wrap_into_list_array(nested_array));
ScalarValue::List(arr)
}
// TODO: There is no test for FixedSizeList now, add it later
- DataType::FixedSizeList(nested_type, _len) => {
Review Comment:
We can add some tests for `FixedSizeList`. I can do it in other follow up.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `ScalarValue` handling of NULL values for ListArray [arrow-datafusion]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #7969:
URL: https://github.com/apache/arrow-datafusion/pull/7969#discussion_r1375527801
##########
datafusion/common/src/scalar.rs:
##########
@@ -3885,6 +3889,77 @@ mod tests {
);
}
+ #[test]
+ fn scalar_try_from_array_list_array_null() {
+ let list = ListArray::from_iter_primitive::<Int32Type, _, _>(vec![
+ Some(vec![Some(1), Some(2)]),
+ None,
+ ]);
+
+ let non_null_list_scalar = ScalarValue::try_from_array(&list, 0).unwrap();
Review Comment:
Previously, for a `ScalarValue` NULL value from `ListArray`, its datatype is `DataType::Null`. That's is not correct.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org