You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/02/08 11:53:07 UTC

[GitHub] [arrow-datafusion] alamb commented on a change in pull request #1729: Add approx-median operator

alamb commented on a change in pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#discussion_r801549186



##########
File path: datafusion/src/physical_plan/aggregates.rs
##########
@@ -924,6 +939,62 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_median_expr() -> Result<()> {
+        let funcs = vec![AggregateFunction::ApproxMedian];
+        let data_types = vec![
+            DataType::UInt32,
+            DataType::UInt64,
+            DataType::Int32,
+            DataType::Int64,
+            DataType::Float32,
+            DataType::Float64,
+        ];
+        for fun in funcs {
+            for data_type in &data_types {
+                let input_schema =
+                    Schema::new(vec![Field::new("c1", data_type.clone(), true)]);
+                let input_phy_exprs: Vec<Arc<dyn PhysicalExpr>> = vec![Arc::new(
+                    expressions::Column::new_with_schema("c1", &input_schema).unwrap(),
+                )];
+                let result_agg_phy_exprs = create_aggregate_expr(
+                    &fun,
+                    false,
+                    &input_phy_exprs[0..1],
+                    &input_schema,
+                    "c1",
+                )?;
+
+                if fun == AggregateFunction::ApproxMedian {
+                    assert!(result_agg_phy_exprs.as_any().is::<ApproxMedian>());
+                    assert_eq!("c1", result_agg_phy_exprs.name());
+                    assert_eq!(
+                        Field::new("c1", data_type.clone(), true),
+                        result_agg_phy_exprs.field().unwrap()
+                    );
+                }
+            }
+        }
+        Ok(())
+    }
+
+    #[test]
+    fn test_median() -> Result<()> {
+        let observed = return_type(&AggregateFunction::ApproxMedian, &[DataType::Utf8]);
+        assert!(observed.is_err());

Review comment:
       👍 

##########
File path: datafusion/src/physical_plan/expressions/approx_median.rs
##########
@@ -0,0 +1,75 @@
+// Licensed to the Apache Software Foundation (ASF) under one

Review comment:
       Is this file needed anymore? I think it can be removed

##########
File path: datafusion/src/optimizer/to_approx_perc.rs
##########
@@ -0,0 +1,161 @@
+// 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.
+
+//! espression/function to approx_percentile optimizer rule
+
+use crate::error::Result;
+use crate::execution::context::ExecutionProps;
+use crate::logical_plan::plan::Aggregate;
+use crate::logical_plan::{Expr, LogicalPlan};
+use crate::optimizer::optimizer::OptimizerRule;
+use crate::optimizer::utils;
+use crate::physical_plan::aggregates;
+use crate::scalar::ScalarValue;
+
+/// espression/function to approx_percentile optimizer rule
+///  ```text
+///    SELECT F1(s)
+///    ...
+///
+///    Into
+///
+///    SELECT APPROX_PERCENTILE_CONT(s, lit(n)) as "F1(s)"
+///    ...
+///  ```
+pub struct ToApproxPerc {}
+
+impl ToApproxPerc {
+    #[allow(missing_docs)]
+    pub fn new() -> Self {
+        Self {}
+    }
+}
+
+impl Default for ToApproxPerc {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+fn optimize(plan: &LogicalPlan) -> Result<LogicalPlan> {
+    match plan {
+        LogicalPlan::Aggregate(Aggregate {
+            input,
+            aggr_expr,
+            schema,
+            group_expr,
+        }) => {
+            let new_aggr_expr = aggr_expr
+                .iter()
+                .map(|agg_expr| replace_with_percentile(agg_expr).unwrap())
+                .collect::<Vec<_>>();
+
+            Ok(LogicalPlan::Aggregate(Aggregate {
+                input: input.clone(),
+                aggr_expr: new_aggr_expr,
+                schema: schema.clone(),
+                group_expr: group_expr.clone(),
+            }))
+        }
+        _ => optimize_children(plan),
+    }
+}
+
+fn optimize_children(plan: &LogicalPlan) -> Result<LogicalPlan> {
+    let expr = plan.expressions();
+    let inputs = plan.inputs();
+    let new_inputs = inputs
+        .iter()
+        .map(|plan| optimize(plan))
+        .collect::<Result<Vec<_>>>()?;
+    utils::from_plan(plan, &expr, &new_inputs)
+}
+
+fn replace_with_percentile(expr: &Expr) -> Result<Expr> {
+    match expr {
+        Expr::AggregateFunction {
+            fun,
+            args,
+            distinct,
+        } => {
+            let mut new_args = args.clone();
+            let mut new_func = fun.clone();
+            if fun == &aggregates::AggregateFunction::ApproxMedian {
+                new_args.push(Expr::Literal(ScalarValue::Float64(Some(0.5_f64))));
+                new_func = aggregates::AggregateFunction::ApproxPercentileCont;
+            }
+
+            Ok(Expr::AggregateFunction {
+                fun: new_func,
+                args: new_args,
+                distinct: *distinct,
+            })
+        }
+        _ => Ok(expr.clone()),
+    }
+}
+
+impl OptimizerRule for ToApproxPerc {
+    fn optimize(
+        &self,
+        plan: &LogicalPlan,
+        _execution_props: &ExecutionProps,
+    ) -> Result<LogicalPlan> {
+        optimize(plan)
+    }
+    fn name(&self) -> &str {
+        "ToApproxPerc"
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::logical_plan::{col, LogicalPlanBuilder};
+    use crate::physical_plan::aggregates;
+    use crate::test::*;
+
+    fn assert_optimized_plan_eq(plan: &LogicalPlan, expected: &str) {
+        let rule = ToApproxPerc::new();
+        let optimized_plan = rule
+            .optimize(plan, &ExecutionProps::new())
+            .expect("failed to optimize plan");
+        let formatted_plan = format!("{}", optimized_plan.display_indent_schema());
+        assert_eq!(formatted_plan, expected);
+    }
+
+    #[test]
+    fn median_1() -> Result<()> {
+        let table_scan = test_table_scan()?;
+        let expr = Expr::AggregateFunction {
+            fun: aggregates::AggregateFunction::ApproxMedian,
+            distinct: false,
+            args: vec![col("b")],
+        };
+
+        let plan = LogicalPlanBuilder::from(table_scan)
+            .aggregate(Vec::<Expr>::new(), vec![expr])?
+            .build()?;
+
+        // Do nothing

Review comment:
       ```suggestion
           // Rewrite to use approx_percentile
   ```

##########
File path: datafusion/src/physical_plan/expressions/approx_percentile_cont.rs
##########
@@ -203,6 +203,10 @@ impl ApproxPercentileAccumulator {
             return_type,
         }
     }
+

Review comment:
       left over?




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