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/02 07:46:24 UTC
[GitHub] [arrow-datafusion] realno opened a new pull request #1729: Add median operator
realno opened a new pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729
# Which issue does this PR close?
Closes #1486 .
# Rationale for this change
Add the median operator, this should close #1486 and unblock the perf test work.
The current implementation uses `approx_percentile_cont` under the hood. We can look into implement the "exact" version later. More discussions around this can be found in the issue.
# 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 there any user-facing changes?
No change to existing APIs. New `median` operator added.
--
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
[GitHub] [arrow-datafusion] realno commented on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
realno commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1028676609
@matthewmturner Great questions. `approx_percentile_cont` is based on `tdigest` to calculate the statistics of the input online, so it does not require the data set to be sorted. This is also the reason that exact median is a bit tricky to be calculated efficiently.
To clarifu I added a new test case hopefully can make it more clear
` fn approx_median_i32_with_nulls_2() -> Result<()> {
let a: ArrayRef = Arc::new(Int32Array::from(vec![
Some(5),
Some(1),
None,
None,
Some(3),
Some(4),
]));
generic_test_op!(
a,
DataType::Int32,
ApproxMedian,
ScalarValue::from(2),
DataType::Int32
)
}`
It also demonstrates how `null` values are handled. They are included in the result, that is, the median is the value (may be Interpolation) positioned in the center of sorted inputs.
--
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
[GitHub] [arrow-datafusion] realno commented on pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
realno commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1032300254
The PR is ready for final review before merge.
FYI, I decided not to add the changes for handling Projection and Alias because the change became quite cumbersome to handle all the corner cases - it needs to traverse the plan twice in order to properly register expression names and schemas; it also does a lot of copies along the way. With the existing approach, the only caveat is `ToApproxPerc` must be applied last. Considering it is simple and relatively efficient I decided to go with this approach.
--
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
[GitHub] [arrow-datafusion] realno edited a comment on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
realno edited a comment on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1028676609
--
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
[GitHub] [arrow-datafusion] realno edited a comment on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
realno edited a comment on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1028676609
@matthewmturner Great questions. `approx_percentile_cont` is based on `tdigest` to calculate the statistics of the input online, so it does not require the data set to be sorted. This (sort) is also the reason that exact median is a bit tricky to be calculated efficiently.
To help clarify I added a new test case hopefully can make it more clear
```
fn approx_median_i32_with_nulls_2() -> Result<()> {
let a: ArrayRef = Arc::new(Int32Array::from(vec![
Some(5),
Some(1),
None,
None,
Some(3),
Some(4),
]));
generic_test_op!(
a,
DataType::Int32,
ApproxMedian,
ScalarValue::from(2),
DataType::Int32
)
}
```
It also demonstrates how `null` values are handled. They are included in the result, that is, the median is the value (may be Interpolation) positioned in the center of sorted inputs.
--
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
[GitHub] [arrow-datafusion] alamb commented on a change in pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
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
[GitHub] [arrow-datafusion] realno commented on a change in pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
realno commented on a change in pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#discussion_r801015816
##########
File path: datafusion/src/physical_plan/expressions/approx_median_old.rs
##########
@@ -0,0 +1,320 @@
+// Licensed to the Apache Software Foundation (ASF) under one
Review comment:
👍 It is here just for comparison purposes, will clean up before merge.
--
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
[GitHub] [arrow-datafusion] domodwyer commented on a change in pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
domodwyer commented on a change in pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#discussion_r800695370
##########
File path: datafusion/src/physical_plan/coercion_rule/aggregate_rule.rs
##########
@@ -154,6 +155,15 @@ pub(crate) fn coerce_types(
}
Ok(input_types.to_vec())
}
+ AggregateFunction::ApproxMedian => {
+ if !is_approx_median_support_arg_type(&input_types[0]) {
Review comment:
Perhaps this could use `is_approx_percentile_cont_supported_arg_type` to ensure it doesn't ever drift from the underlying implementation and remove duplication?
##########
File path: datafusion/src/physical_plan/expressions/approx_median_old.rs
##########
@@ -0,0 +1,320 @@
+// Licensed to the Apache Software Foundation (ASF) under one
Review comment:
Looks like `approx_median_old.rs` is checked in by accident after the rewrite 👍
--
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
[GitHub] [arrow-datafusion] realno commented on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
realno commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1029176197
> I had a thought this morning: what if we didn't introduce an `Aggregate` function at all, and instead simply rewrote queries
@alamb I think this is a actually a good idea. Let me look at the code you shared.
I was thinking about something like this when started looking at median (the exact version). If we have some official support for rewrite query and logical plan that'll help. I was thinking to implement a `CombinedOperator` that can be expanded during planning phase but didn't have enough time to explore too much - it does not appear to be trivial. For example,
```
PROJECT
MEDIAN c1
...
```
can be rewrite to something like
```
PROJECT
FINDN
SORT c1
...
```
Though rewriting query will introduce potential security risks I think rewriting logical plan is a better option.
--
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
[GitHub] [arrow-datafusion] matthewmturner commented on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
matthewmturner commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1029196026
@realno thank you for the explanation and added test - it makes sense.
--
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
[GitHub] [arrow-datafusion] realno commented on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
realno commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1030570504
@alamb I add another version using optimizer, I think it works too. It is a little cleaner, it does introduced a bit complexity but should run a little faster too. Please take another look and we can decide which version to merge.
--
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
[GitHub] [arrow-datafusion] Dandandan commented on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
Dandandan commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1030654717
@realno Thanks.
I agree that rewriting the query to use the percentile function is conceptually a bit easier.
So a +1 on the optimizer rule approach.
--
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
[GitHub] [arrow-datafusion] realno commented on pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
realno commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1031867012
Sounds good - will proceed with the optimizer route.
@alamb
> In terms of the planning overhead, I agree it isn't ideal, though I think we can improve things over time by consolidating several of the optimizer passes together
There are a few things I noticed that are not ideal, I will update the PR later we can discuss in more details. I think it is a good idea to have a plan to improve over time - I will create the issues after discussion. Here are the things I noticed for now:
1. For at least aggregate functions some traits/structs from `use crate::physical_plan::aggregates` are leaked into the logical planning phase, e.g. `fun` in this code block
```
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;
}
```
2. Changing functions also includes rewriting Projections and Aliases, this is very tedious especially some util functions are not public and it replicates part of the work for building the plan. Also it may potentially have conflicts with other optimize rules if not super careful. Ideally these kind of of rewrites could happen before building the logical plan, maybe we can introduce a pre-build phase? I am working the part handling Projection and Alias, it'll be more clear looking at the code - I am using string replacement.
> If we need more drastic performance improvements I have wanted to make a LogicalPlanRewriter (like ExprRewriter) for a while now that would avoid so many copies -- it is a fair amount of work but all pretty mechanical.
I like this idea, it may also help with the issues I mentioned above.
--
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
[GitHub] [arrow-datafusion] alamb commented on pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1031932588
🤔 I am not sure about how the DF 7.0.0 release is going to to go down (as in if we should wait for specific things or just cut the release "when we are ready") 🤔
--
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
[GitHub] [arrow-datafusion] realno commented on pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
realno commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1031875520
> @alamb @realno do you think that this could be finished in time for inclusion in the 7.0 release? I was hoping it would so we could use in the python bindings for refreshing benchmarks.
Functionality-wise I think I can make it happen. There is a part for dealing with Projection and Alias (will push the code soon) can be a bit controversial, we'll see if we are comfortable with merging as-is.
Or if we really want to make it happen, another option is to merge with the wrapper implementation first while working on the optimizer rule.
Let me push the code first then we can discuss again with @alamb .
--
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
[GitHub] [arrow-datafusion] realno commented on a change in pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
realno commented on a change in pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#discussion_r802189686
##########
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:
It is still needed as a wrapper for the expression.
--
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
[GitHub] [arrow-datafusion] realno commented on a change in pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
realno commented on a change in pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#discussion_r802189224
##########
File path: datafusion/src/physical_plan/expressions/approx_percentile_cont.rs
##########
@@ -203,6 +203,10 @@ impl ApproxPercentileAccumulator {
return_type,
}
}
+
Review comment:
Cleaned up.
--
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
[GitHub] [arrow-datafusion] realno commented on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
realno commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1027666728
@matthewmturner This should close #1486
--
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
[GitHub] [arrow-datafusion] alamb commented on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1028915016
> @alamb Am I missing something by any chance?
I had a thought this morning: what if we didn't introduce an `Aggregate` function at all, and instead simply rewrote queries
so a query that has
```sql
select approx_median(x) from foo;
```
could be rewritten to
```sql
select approx_percentile(x, 5.0) as "approx_median(x)" from foo;
```
Similar to how we transform `SELECT count(distinct x) from foo` to `select count(*) from (select x from foo group by x)` in https://github.com/realno/arrow-datafusion/blob/add-median-operator/datafusion/src/optimizer/single_distinct_to_groupby.rs#L44
this may be a silly idea, but I wanted to write it down
--
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
[GitHub] [arrow-datafusion] matthewmturner commented on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
matthewmturner commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1028183168
@realno this is great, thank you. im excited to be able to refresh the db-benchmarks results with this and your other work. just a couple questions.
I believe all the tests currently have the array data sorted already. Do you think we should have some where it is not sorted? i'm assuming that the `Median` function and `approx_percentile_cont` dont require input data to be sorted in order to use (sry if thats wrong assumption, i havent had chance to look into implementation of `approx_percentile_cont`).
Can you also provide more color on the intended handling of nulls? It would help me to understand the below test.
```
#[test]
fn median_i32_with_nulls() -> Result<()> {
let a: ArrayRef = Arc::new(Int32Array::from(vec![
Some(1),
None,
Some(3),
Some(4),
Some(5),
]));
generic_test_op!(
a,
DataType::Int32,
Median,
ScalarValue::from(3),
DataType::Int32
)
}
```
--
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
[GitHub] [arrow-datafusion] Dandandan edited a comment on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
Dandandan edited a comment on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1030654717
@realno Thanks for this PR!
I agree that rewriting the query to use the percentile function is conceptually a bit easier.
So a +1 on the optimizer rule approach.
--
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
[GitHub] [arrow-datafusion] realno commented on a change in pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
realno commented on a change in pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#discussion_r801015119
##########
File path: datafusion/src/physical_plan/coercion_rule/aggregate_rule.rs
##########
@@ -154,6 +155,15 @@ pub(crate) fn coerce_types(
}
Ok(input_types.to_vec())
}
+ AggregateFunction::ApproxMedian => {
+ if !is_approx_median_support_arg_type(&input_types[0]) {
Review comment:
Good point.
--
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
[GitHub] [arrow-datafusion] alamb merged pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
alamb merged pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729
--
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
[GitHub] [arrow-datafusion] alamb commented on a change in pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#discussion_r798021657
##########
File path: datafusion/src/physical_plan/expressions/median.rs
##########
@@ -0,0 +1,298 @@
+// 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.
+
+//! Defines physical expressions that can evaluated at runtime during query execution
Review comment:
```suggestion
//! Defines physical expression for `MEDIAN` that can evaluated at runtime during query execution
```
##########
File path: datafusion/tests/sql/aggregates.rs
##########
@@ -219,6 +219,42 @@ async fn csv_query_stddev_6() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn csv_query_median_1() -> Result<()> {
+ let mut ctx = ExecutionContext::new();
+ register_aggregate_csv(&mut ctx).await?;
+ let sql = "SELECT median(c2) FROM aggregate_test_100";
+ let mut actual = execute(&mut ctx, sql).await;
+ actual.sort();
+ let expected = vec![vec!["3"]];
+ assert_float_eq(&expected, &actual);
+ Ok(())
+}
+
+#[tokio::test]
+async fn csv_query_median_2() -> Result<()> {
+ let mut ctx = ExecutionContext::new();
+ register_aggregate_csv(&mut ctx).await?;
+ let sql = "SELECT median(c6) FROM aggregate_test_100";
+ let mut actual = execute(&mut ctx, sql).await;
+ actual.sort();
Review comment:
these call to `sort` are likely not necessary (as there is a single element in the output)
--
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
[GitHub] [arrow-datafusion] realno commented on pull request #1729: Add median operator
Posted by GitBox <gi...@apache.org>.
realno commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1028669036
> I left some suggestions about how we might be able to reuse some more of the ApproxQuantile code to avoid repetition
@alamb Am I missing something by any chance?
--
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
[GitHub] [arrow-datafusion] matthewmturner commented on pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
matthewmturner commented on pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#issuecomment-1031738719
@alamb @realno do you think that this could be finished in time for inclusion in the 7.0 release? I was hoping it would so we could use in the python bindings for refreshing benchmarks.
--
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
[GitHub] [arrow-datafusion] realno commented on a change in pull request #1729: Add approx-median operator
Posted by GitBox <gi...@apache.org>.
realno commented on a change in pull request #1729:
URL: https://github.com/apache/arrow-datafusion/pull/1729#discussion_r801015816
##########
File path: datafusion/src/physical_plan/expressions/approx_median_old.rs
##########
@@ -0,0 +1,320 @@
+// Licensed to the Apache Software Foundation (ASF) under one
Review comment:
👍 It is here just for comparison purposed, will clean up before merge.
--
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