You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jayzhan211 (via GitHub)" <gi...@apache.org> on 2024/02/09 04:14:26 UTC

[PR] Use concat to simplify Nested Scalar creation [arrow-datafusion]

jayzhan211 opened a new pull request, #9174:
URL: https://github.com/apache/arrow-datafusion/pull/9174

   ## Which issue does this PR close?
   
   Use concat to simplify Nested Scalar creation
   Except FixedsizeList
   
   <!--
   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 #9145.
   
   ## 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] Use concat to simplify Nested Scalar creation [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #9174:
URL: https://github.com/apache/arrow-datafusion/pull/9174#issuecomment-1936974234

   Thanks again @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] Use concat to simplify Nested Scalar creation [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #9174:
URL: https://github.com/apache/arrow-datafusion/pull/9174#issuecomment-1936617269

   > lgtm thanks @jayzhan211 I think some time ago it was a comment to use a concat, not sure if for this exact purpose
   > 
   > @Weijun-H cc
   
   Perhaps https://github.com/apache/arrow-datafusion/pull/7893#discussion_r1478478867


-- 
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] Use concat to simplify Nested Scalar creation [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #9174:
URL: https://github.com/apache/arrow-datafusion/pull/9174#discussion_r1484814513


##########
datafusion/common/src/scalar.rs:
##########
@@ -1591,10 +1476,34 @@ impl ScalarValue {
             DataType::Interval(IntervalUnit::MonthDayNano) => {
                 build_array_primitive!(IntervalMonthDayNanoArray, IntervalMonthDayNano)
             }
-            DataType::Struct(_) => build_struct_array(scalars)?,
-            DataType::List(_)
-            | DataType::LargeList(_)
-            | DataType::FixedSizeList(_, _) => build_list_array(scalars)?,
+            DataType::FixedSizeList(_, _) => {
+                // arrow::compute::concat does not allow inconsistent types including the size of FixedSizeList.
+                // The length of nulls here we got is 1, so we need to resize the length of nulls to
+                // the length of non-nulls.
+                let mut arrays =
+                    scalars.map(|s| s.to_array()).collect::<Result<Vec<_>>>()?;
+                let first_non_null_data_type = arrays
+                    .iter()
+                    .find(|sv| !sv.is_null(0))
+                    .map(|sv| sv.data_type().to_owned());
+                if let Some(DataType::FixedSizeList(f, l)) = first_non_null_data_type {
+                    for array in arrays.iter_mut() {
+                        if array.is_null(0) {
+                            *array =
+                                Arc::new(FixedSizeListArray::new_null(f.clone(), l, 1));
+                        }
+                    }
+                }
+                let arrays = arrays.iter().map(|a| a.as_ref()).collect::<Vec<_>>();
+                arrow::compute::concat(arrays.as_slice())
+                    .map_err(|e| arrow_datafusion_err!(e))?

Review Comment:
   Since there is an automatic conversion from `ArrowError` to `DataFusionError` you can do this:
   
   ```suggestion
                   arrow::compute::concat(arrays.as_slice())?
   ```
   
   I verified that this works locally. However I think the code in this PR just follows the pattern as the reset of this file. I'll make a PR to clean that up



##########
datafusion/common/src/scalar.rs:
##########
@@ -1591,10 +1476,34 @@ impl ScalarValue {
             DataType::Interval(IntervalUnit::MonthDayNano) => {
                 build_array_primitive!(IntervalMonthDayNanoArray, IntervalMonthDayNano)
             }
-            DataType::Struct(_) => build_struct_array(scalars)?,
-            DataType::List(_)
-            | DataType::LargeList(_)
-            | DataType::FixedSizeList(_, _) => build_list_array(scalars)?,
+            DataType::FixedSizeList(_, _) => {
+                // arrow::compute::concat does not allow inconsistent types including the size of FixedSizeList.
+                // The length of nulls here we got is 1, so we need to resize the length of nulls to
+                // the length of non-nulls.
+                let mut arrays =
+                    scalars.map(|s| s.to_array()).collect::<Result<Vec<_>>>()?;
+                let first_non_null_data_type = arrays
+                    .iter()
+                    .find(|sv| !sv.is_null(0))
+                    .map(|sv| sv.data_type().to_owned());
+                if let Some(DataType::FixedSizeList(f, l)) = first_non_null_data_type {
+                    for array in arrays.iter_mut() {
+                        if array.is_null(0) {
+                            *array =
+                                Arc::new(FixedSizeListArray::new_null(f.clone(), l, 1));
+                        }
+                    }
+                }
+                let arrays = arrays.iter().map(|a| a.as_ref()).collect::<Vec<_>>();
+                arrow::compute::concat(arrays.as_slice())
+                    .map_err(|e| arrow_datafusion_err!(e))?
+            }
+            DataType::List(_) | DataType::LargeList(_) | DataType::Struct(_) => {
+                let arrays = scalars.map(|s| s.to_array()).collect::<Result<Vec<_>>>()?;
+                let arrays = arrays.iter().map(|a| a.as_ref()).collect::<Vec<_>>();
+                arrow::compute::concat(arrays.as_slice())
+                    .map_err(|e| arrow_datafusion_err!(e))?

Review Comment:
   ```suggestion
                   arrow::compute::concat(arrays.as_slice())?
   ```



-- 
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] Use concat to simplify Nested Scalar creation [arrow-datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #9174:
URL: https://github.com/apache/arrow-datafusion/pull/9174


-- 
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