alamb commented on code in PR #27321:
URL: https://github.com/apache/datafusion/pull/27321#discussion_r4719836479
##########
datafusion/physical-plan/src/aggregates/aggregate_argument.rs:
##########
@@ -174,16 +190,53 @@ impl AggregateFilter {
&self.predicate
}
- /// Evaluates the filter predicate for every row of `batch`.
+ /// Evaluates the filter predicate for `batch` and returns the rows it
+ /// selects. A null rejects its row, like `false`.
+ ///
+ /// The predicate is evaluated for every batch, even if it returns a
+ /// scalar: a runtime scalar, such as the result of a UDF, can change
+ /// between batches.
pub(in crate::aggregates) fn evaluate(
&self,
batch: &RecordBatch,
- ) -> Result<BooleanArray> {
- let selection = self
- .predicate
- .evaluate(batch)?
- .into_array(batch.num_rows())?;
- Ok(as_boolean_array(&selection)?.clone())
+ ) -> Result<AggregateSelection> {
+ let selection = match self.predicate.evaluate(batch)? {
Review Comment:
this is the special case I think
##########
datafusion/physical-plan/src/aggregates/aggregate_argument.rs:
##########
@@ -151,6 +153,20 @@ pub(in crate::aggregates) struct AggregateFilter {
selected_schema: Option<(SchemaRef, SchemaRef)>,
}
+/// The rows of a batch that an [`AggregateFilter`] selects.
+///
+/// A filter often selects every row of a batch or none of them, for example
Review Comment:
I am not sure about "often" (how common is it that the filter is a literal 🤔
##########
datafusion/physical-plan/src/aggregates/aggregate_argument.rs:
##########
@@ -174,16 +190,53 @@ impl AggregateFilter {
&self.predicate
}
- /// Evaluates the filter predicate for every row of `batch`.
+ /// Evaluates the filter predicate for `batch` and returns the rows it
+ /// selects. A null rejects its row, like `false`.
+ ///
+ /// The predicate is evaluated for every batch, even if it returns a
+ /// scalar: a runtime scalar, such as the result of a UDF, can change
+ /// between batches.
pub(in crate::aggregates) fn evaluate(
&self,
batch: &RecordBatch,
- ) -> Result<BooleanArray> {
- let selection = self
- .predicate
- .evaluate(batch)?
- .into_array(batch.num_rows())?;
- Ok(as_boolean_array(&selection)?.clone())
+ ) -> Result<AggregateSelection> {
+ let selection = match self.predicate.evaluate(batch)? {
+ ColumnarValue::Scalar(ScalarValue::Boolean(Some(true))) => {
+ return Ok(AggregateSelection::All);
+ }
+ ColumnarValue::Scalar(ScalarValue::Boolean(_)) => {
+ return Ok(AggregateSelection::Empty);
+ }
+ value => value.into_array(batch.num_rows())?,
+ };
+ let selection = as_boolean_array(&selection)?;
+ // Nulls are counted neither by `has_true` nor by `has_false`.
+ Ok(if !selection.has_true() {
+ AggregateSelection::Empty
+ } else if selection.null_count() == 0 && !selection.has_false() {
+ AggregateSelection::All
+ } else {
+ AggregateSelection::Rows(selection.clone())
+ })
+ }
+
+ /// Returns the rows of `batch` that `selection` selects, for evaluating
+ /// the arguments of the aggregate, or `None` if no row is selected.
+ ///
+ /// If every row is selected, `batch` itself is returned. Otherwise the
+ /// rows are copied by [`Self::select`].
+ pub(in crate::aggregates) fn selected_batch<'a>(
Review Comment:
This feels like it should logically belong on `AggregateSelection` itself
So something like `AggregateSelection::select(batch)` 🤔
##########
datafusion/physical-plan/src/aggregates/aggregate_argument.rs:
##########
@@ -376,6 +429,72 @@ mod tests {
Ok(())
}
+ #[test]
+ fn filter_selection_is_normalized() -> Result<()> {
Review Comment:
I wonder how much additional coverage this adds over .slt tests? I think we
could probably avoid these unit tests
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]