2010YOUY01 commented on code in PR #24444:
URL: https://github.com/apache/datafusion/pull/24444#discussion_r3811023077


##########
datafusion/physical-plan/src/aggregates/mod.rs:
##########
@@ -4368,6 +4411,60 @@ mod tests {
         Ok(())
     }
 
+    #[tokio::test]
+    async fn ordered_single_filter_skips_fallible_arguments() -> Result<()> {

Review Comment:
   If we have already tested in the `slt`, I think it's not necessary to test 
again here at `ExecutionPlan` level.



##########
datafusion/sqllogictest/test_files/aggregate_filter_selection.slt:
##########
@@ -0,0 +1,130 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+
+#   http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Aggregate FILTER must select rows before evaluating fallible arguments.
+
+statement ok
+CREATE TABLE aggregate_filter_selection (g INT, v BIGINT);
+
+statement ok
+INSERT INTO aggregate_filter_selection VALUES
+  (1, 0),
+  (2, 2),
+  (3, 0),
+  (4, 5);
+
+statement ok
+SET datafusion.execution.skip_partial_aggregation_probe_rows_threshold = 
100000;
+
+statement ok
+SET datafusion.execution.enable_migration_aggregate = true;
+
+query II
+SELECT g, SUM(10 / v) FILTER (WHERE v <> 0)
+FROM aggregate_filter_selection
+GROUP BY g
+ORDER BY g;
+----
+1 NULL
+2 5
+3 NULL
+4 2
+
+statement ok
+SET datafusion.execution.enable_migration_aggregate = false;
+
+query II
+SELECT g, SUM(10 / v) FILTER (WHERE v <> 0)
+FROM aggregate_filter_selection
+GROUP BY g
+ORDER BY g;
+----
+1 NULL
+2 5
+3 NULL
+4 2
+
+statement ok
+SET datafusion.execution.target_partitions = 2;
+
+statement ok
+SET datafusion.execution.batch_size = 1;
+
+statement ok
+SET datafusion.execution.skip_partial_aggregation_probe_rows_threshold = 0;
+
+statement ok
+SET datafusion.execution.skip_partial_aggregation_probe_ratio_threshold = 0.0;
+
+statement ok
+CREATE TABLE aggregate_filter_selection_skip (g INT, v BIGINT);
+
+statement ok
+INSERT INTO aggregate_filter_selection_skip VALUES (1, 2);
+
+statement ok
+INSERT INTO aggregate_filter_selection_skip VALUES (3, 5);
+
+statement ok
+INSERT INTO aggregate_filter_selection_skip VALUES (2, 0);

Review Comment:
   nit: we could merge those inserts into a single statement



##########
datafusion/physical-plan/src/aggregates/aggregate_hash_table/common.rs:
##########
@@ -544,36 +552,26 @@ impl HashAggregateAccumulator {
         ))
     }
 
-    /// Evaluate aggregate arguments and filter for one input batch.
-    ///
-    /// For example, `AVG(x + 1) FILTER (WHERE x > 0)` evaluates both `x + 1`
-    /// and `x > 0`.
-    ///
-    /// These arrays can be passed directly to [`GroupsAccumulator`] next.
-    pub(super) fn evaluate_acc_args(

Review Comment:
   I think we can achieve the goal by only changing the internal of this 
function to make it simpler



##########
datafusion/physical-plan/src/aggregates/mod.rs:
##########
@@ -5372,6 +5469,115 @@ mod tests {
         Ok(())
     }
 
+    fn fallible_skip_partial_aggregate() -> Result<Arc<AggregateExec>> {
+        let schema = Arc::new(Schema::new(vec![
+            Field::new("group_col", DataType::Int32, false),
+            Field::new("value_col", DataType::Int64, false),
+        ]));
+        let input_batches = vec![
+            RecordBatch::try_new(
+                Arc::clone(&schema),
+                vec![
+                    Arc::new(Int32Array::from(vec![1])),
+                    Arc::new(Int64Array::from(vec![2])),
+                ],
+            )?,
+            RecordBatch::try_new(
+                Arc::clone(&schema),
+                vec![
+                    Arc::new(Int32Array::from(vec![3])),
+                    Arc::new(Int64Array::from(vec![5])),
+                ],
+            )?,
+            RecordBatch::try_new(
+                Arc::clone(&schema),
+                vec![
+                    Arc::new(Int32Array::from(vec![2])),
+                    Arc::new(Int64Array::from(vec![0])),
+                ],
+            )?,
+        ];
+        let input =
+            TestMemoryExec::try_new_exec(&[input_batches], 
Arc::clone(&schema), None)?;
+        let (aggregate_expr, filter) = fallible_sum_expr_and_filter(&schema)?;
+
+        Ok(Arc::new(AggregateExec::try_new(
+            AggregateMode::Partial,
+            PhysicalGroupBy::new_single(vec![(
+                col("group_col", &schema)?,
+                "group_col".to_string(),
+            )]),
+            vec![aggregate_expr],
+            vec![Some(filter)],
+            input,
+            Arc::clone(&schema),
+        )?))
+    }
+
+    #[tokio::test]
+    async fn partial_hash_skip_filter_skips_fallible_arguments() -> Result<()> 
{
+        let aggregate = fallible_skip_partial_aggregate()?;
+        let session_config = SessionConfig::new()
+            .set_bool("datafusion.execution.enable_migration_aggregate", true)
+            .set(
+                
"datafusion.execution.skip_partial_aggregation_probe_rows_threshold",
+                &ScalarValue::Int64(Some(0)),
+            )
+            .set(
+                
"datafusion.execution.skip_partial_aggregation_probe_ratio_threshold",
+                &ScalarValue::Float64(Some(0.0)),
+            );
+        let task_ctx =
+            
Arc::new(TaskContext::default().with_session_config(session_config));
+
+        let stream = aggregate.execute_typed(0, &task_ctx)?;
+        assert!(matches!(stream, StreamType::PartialHash(_)));
+        let stream: SendableRecordBatchStream = stream.into();
+        collect(stream).await?;
+
+        let skipped_rows = aggregate
+            .metrics()
+            .unwrap()
+            .sum_by_name("skipped_aggregation_rows")
+            .map(|metric| metric.as_usize())
+            .unwrap_or(0);
+        assert!(skipped_rows > 0);
+
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn legacy_skip_filter_skips_fallible_arguments() -> Result<()> {

Review Comment:
   Similarly here.



##########
datafusion/physical-plan/src/aggregates/mod.rs:
##########
@@ -5372,6 +5469,115 @@ mod tests {
         Ok(())
     }
 
+    fn fallible_skip_partial_aggregate() -> Result<Arc<AggregateExec>> {
+        let schema = Arc::new(Schema::new(vec![
+            Field::new("group_col", DataType::Int32, false),
+            Field::new("value_col", DataType::Int64, false),
+        ]));
+        let input_batches = vec![
+            RecordBatch::try_new(
+                Arc::clone(&schema),
+                vec![
+                    Arc::new(Int32Array::from(vec![1])),
+                    Arc::new(Int64Array::from(vec![2])),
+                ],
+            )?,
+            RecordBatch::try_new(
+                Arc::clone(&schema),
+                vec![
+                    Arc::new(Int32Array::from(vec![3])),
+                    Arc::new(Int64Array::from(vec![5])),
+                ],
+            )?,
+            RecordBatch::try_new(
+                Arc::clone(&schema),
+                vec![
+                    Arc::new(Int32Array::from(vec![2])),
+                    Arc::new(Int64Array::from(vec![0])),
+                ],
+            )?,
+        ];
+        let input =
+            TestMemoryExec::try_new_exec(&[input_batches], 
Arc::clone(&schema), None)?;
+        let (aggregate_expr, filter) = fallible_sum_expr_and_filter(&schema)?;
+
+        Ok(Arc::new(AggregateExec::try_new(
+            AggregateMode::Partial,
+            PhysicalGroupBy::new_single(vec![(
+                col("group_col", &schema)?,
+                "group_col".to_string(),
+            )]),
+            vec![aggregate_expr],
+            vec![Some(filter)],
+            input,
+            Arc::clone(&schema),
+        )?))
+    }
+
+    #[tokio::test]
+    async fn partial_hash_skip_filter_skips_fallible_arguments() -> Result<()> 
{

Review Comment:
   Similarly here.



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

Reply via email to