You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "acking-you (via GitHub)" <gi...@apache.org> on 2025/07/27 09:10:48 UTC

[PR] fix error result in execute&pre_selection [datafusion]

acking-you opened a new pull request, #16930:
URL: https://github.com/apache/datafusion/pull/16930

   ## 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 #16928.
   
   ## 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.
   -->
   No
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   
   
   ## Some thoughts
   
   In current computing frameworks, all computation results are in the form of immutable shared ownership, which leads to many situations where new buffers have to be created instead of reusing existing ones.
   
   For example, in the case of early exit optimization, it should be possible to reuse and modify `lhs` to return the result.
   
   However, doing so under the current framework would lead to memory safety issues. For instance, the `evaluate` implementation of the Column expression reuses the original `Arc` to obtain the result instead of creating a new one, while in other cases a new structure is created (which can be safely modified).
   
   If there were a way to internally indicate whether the current result is newly created or reuses an `Arc`, and if we could design a mutable API for this scenario, it might be possible to reduce unnecessary copies in many computation processes across DataFusion.


-- 
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@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix error result in execute&pre_selection [datafusion]

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -375,7 +375,46 @@ impl PhysicalExpr for BinaryExpr {
                 // as it takes into account cases where the selection contains null values.
                 let batch = filter_record_batch(batch, selection)?;
                 let right_ret = self.right.evaluate(&batch)?;
-                return pre_selection_scatter(selection, right_ret);
+
+                match &right_ret {
+                    ColumnarValue::Array(array) => {
+                        // When the array on the right is all true or all false, skip the scatter process
+                        let boolean_array = array.as_boolean();
+                        let true_count = boolean_array.true_count();
+                        let length = boolean_array.len();
+                        if true_count == length {
+                            return Ok(lhs);
+                        } else if true_count == 0 && boolean_array.null_count() == 0 {
+                            // If the right-hand array is returned at this point,the lengths will be inconsistent;
+                            // returning a scalar can avoid this issue
+                            return Ok(ColumnarValue::Scalar(ScalarValue::Boolean(
+                                Some(false),
+                            )));
+                        }
+
+                        return pre_selection_scatter(selection, boolean_array);
+                    }
+                    ColumnarValue::Scalar(scalar) => {
+                        if let ScalarValue::Boolean(v) = scalar {
+                            // When the scalar is true or false, skip the scatter process
+                            if let Some(v) = v {
+                                if *v {
+                                    return Ok(lhs);
+                                } else {
+                                    return Ok(right_ret);
+                                }
+                            } else {
+                                let array =

Review Comment:
   I suspect anywhere we do a `BooleanArray::from(vec![..])` could be improved eventually if we care. It is likely not worth pursuing in this PR, but I figured I would bring it u



##########
datafusion/physical-expr-common/src/physical_expr.rs:
##########
@@ -106,6 +106,9 @@ pub trait PhysicalExpr: Send + Sync + Display + Debug + DynEq + DynHash {
             Ok(tmp_result)
         } else if let ColumnarValue::Array(a) = tmp_result {
             scatter(selection, a.as_ref()).map(ColumnarValue::Array)
+        } else if let ColumnarValue::Scalar(ScalarValue::Boolean(v)) = &tmp_result {
+            let a = BooleanArray::from(vec![*v; tmp_batch.num_rows()]);

Review Comment:
   we can probably make things faster if we avoid expanding out to a constant array here (avoid the `vec![]`).
   
   



-- 
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@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix error result in execute&pre_selection [datafusion]

Posted by "acking-you (via GitHub)" <gi...@apache.org>.
acking-you commented on code in PR #16930:
URL: https://github.com/apache/datafusion/pull/16930#discussion_r2249183827


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -375,7 +375,19 @@ impl PhysicalExpr for BinaryExpr {
                 // as it takes into account cases where the selection contains null values.
                 let batch = filter_record_batch(batch, selection)?;
                 let right_ret = self.right.evaluate(&batch)?;
-                return pre_selection_scatter(selection, right_ret);
+                match &right_ret {
+                    ColumnarValue::Array(array) => {
+                        return pre_selection_scatter(selection, array.as_boolean());
+                    }
+                    ColumnarValue::Scalar(scalar) => {
+                        if let ScalarValue::Boolean(v) = scalar {
+                            let array = BooleanArray::from(vec![*v; batch.num_rows()]);
+                            return pre_selection_scatter(selection, &array);
+                        } else {
+                            return Ok(right_ret);

Review Comment:
   Yes, there should not be other types of scalar values.



-- 
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@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix error result in execute&pre_selection [datafusion]

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

   Thanks again @acking-you and @mwylde 


-- 
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@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix error result in execute&pre_selection [datafusion]

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


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -375,7 +375,19 @@ impl PhysicalExpr for BinaryExpr {
                 // as it takes into account cases where the selection contains null values.
                 let batch = filter_record_batch(batch, selection)?;
                 let right_ret = self.right.evaluate(&batch)?;
-                return pre_selection_scatter(selection, right_ret);
+                match &right_ret {
+                    ColumnarValue::Array(array) => {
+                        return pre_selection_scatter(selection, array.as_boolean());
+                    }
+                    ColumnarValue::Scalar(scalar) => {
+                        if let ScalarValue::Boolean(v) = scalar {

Review Comment:
   You can probably do better here than expanding out the entire array -- 
   
   specifically if the right value is `ScalarValue::Boolean(false)` we know nothing matches so can't we return a single ColumnValue(ScalarValue::Boolean(false))`
   
   Likewise if we know it is `ScalarValue::Boolean(true)` we can avoid selection gathering at all (and just pass back the selection)
   



##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -375,7 +375,19 @@ impl PhysicalExpr for BinaryExpr {
                 // as it takes into account cases where the selection contains null values.
                 let batch = filter_record_batch(batch, selection)?;
                 let right_ret = self.right.evaluate(&batch)?;
-                return pre_selection_scatter(selection, right_ret);
+                match &right_ret {
+                    ColumnarValue::Array(array) => {
+                        return pre_selection_scatter(selection, array.as_boolean());
+                    }
+                    ColumnarValue::Scalar(scalar) => {
+                        if let ScalarValue::Boolean(v) = scalar {
+                            let array = BooleanArray::from(vec![*v; batch.num_rows()]);
+                            return pre_selection_scatter(selection, &array);
+                        } else {
+                            return Ok(right_ret);

Review Comment:
   What other kind of scalar value could there be here? Would it be better to return an internal error if we get some non boolean value
   
   



-- 
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@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix error result in execute&pre_selection [datafusion]

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

   Gah, somehow this PR caused a CI failure: https://github.com/apache/datafusion/actions/runs/16732639756/job/47364317932
   
   
   Perhaps it undid the fix by @adamreeve  here: 
   - https://github.com/apache/datafusion/pull/17026 🤔 
   


-- 
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@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix error result in execute&pre_selection [datafusion]

Posted by "acking-you (via GitHub)" <gi...@apache.org>.
acking-you commented on code in PR #16930:
URL: https://github.com/apache/datafusion/pull/16930#discussion_r2249190771


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -375,7 +375,19 @@ impl PhysicalExpr for BinaryExpr {
                 // as it takes into account cases where the selection contains null values.
                 let batch = filter_record_batch(batch, selection)?;
                 let right_ret = self.right.evaluate(&batch)?;
-                return pre_selection_scatter(selection, right_ret);
+                match &right_ret {
+                    ColumnarValue::Array(array) => {
+                        return pre_selection_scatter(selection, array.as_boolean());
+                    }
+                    ColumnarValue::Scalar(scalar) => {
+                        if let ScalarValue::Boolean(v) = scalar {

Review Comment:
   done



-- 
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@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix error result in execute&pre_selection [datafusion]

Posted by "acking-you (via GitHub)" <gi...@apache.org>.
acking-you commented on code in PR #16930:
URL: https://github.com/apache/datafusion/pull/16930#discussion_r2249965371


##########
datafusion/physical-expr-common/src/physical_expr.rs:
##########
@@ -106,6 +106,9 @@ pub trait PhysicalExpr: Send + Sync + Display + Debug + DynEq + DynHash {
             Ok(tmp_result)
         } else if let ColumnarValue::Array(a) = tmp_result {
             scatter(selection, a.as_ref()).map(ColumnarValue::Array)
+        } else if let ColumnarValue::Scalar(ScalarValue::Boolean(v)) = &tmp_result {
+            let a = BooleanArray::from(vec![*v; tmp_batch.num_rows()]);

Review Comment:
   done



-- 
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@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] fix error result in execute&pre_selection [datafusion]

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


-- 
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@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org