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 2021/12/29 19:25:38 UTC
[GitHub] [arrow-datafusion] viirya opened a new pull request #1506: Fix ORDER BY on aggregate
viirya opened a new pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506
# 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 #1479.
# 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 there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
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] xudong963 commented on a change in pull request #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
xudong963 commented on a change in pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#discussion_r776934499
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1319,83 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
+#[inline]
Review comment:
> In Rust, a unit of (separate) compilation is a crate. If a function f is defined in a crate A, then all calls to f from within A can be inlined, as the compiler has full access to f. If, however, f is called from some downstream crate B, such calls can’t be inlined. B has access only to the signature of f, not its body.
So I think both of them don't need `#[inline]`, let the compiler do it.
--
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] Jimexist commented on a change in pull request #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
Jimexist commented on a change in pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#discussion_r776912327
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1319,83 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
+#[inline]
Review comment:
why would this need to be inlined? or can't the compiler infer that?
--
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 #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
alamb commented on a change in pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#discussion_r777023721
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1318,79 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
+pub fn rewrite_sort_cols_by_aggs(
+ exprs: impl IntoIterator<Item = impl Into<Expr>>,
+ plan: &LogicalPlan,
+) -> Result<Vec<Expr>> {
+ exprs
+ .into_iter()
+ .map(|e| {
+ let expr = e.into();
+ match expr.clone() {
+ Expr::Sort {
+ expr,
+ asc,
+ nulls_first,
+ } => {
+ let sort = Expr::Sort {
+ expr: Box::new(rewrite_sort_col_by_aggs(*expr, plan)?),
+ asc,
+ nulls_first,
+ };
+ Ok(sort)
+ }
+ _ => Ok(expr),
Review comment:
I think you can avoid this clone via:
```suggestion
match expr {
Expr::Sort {
expr,
asc,
nulls_first,
} => {
let sort = Expr::Sort {
expr: Box::new(rewrite_sort_col_by_aggs(*expr, plan)?),
asc,
nulls_first,
};
Ok(sort)
}
expr => Ok(expr),
```
##########
File path: datafusion/tests/sql/order.rs
##########
@@ -32,6 +32,27 @@ async fn test_sort_unprojected_col() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn test_order_by_agg_expr() -> Result<()> {
+ let mut ctx = ExecutionContext::new();
+ register_aggregate_csv(&mut ctx).await?;
+ let sql = "SELECT MIN(c12) FROM aggregate_test_100 ORDER BY MIN(c12)";
+ let actual = execute_to_batches(&mut ctx, sql).await;
+ let expected = vec![
+ "+-----------------------------+",
+ "| MIN(aggregate_test_100.c12) |",
+ "+-----------------------------+",
+ "| 0.01479305307777301 |",
+ "+-----------------------------+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ let sql = "SELECT MIN(c12) FROM aggregate_test_100 ORDER BY MIN(c12) + 0.1";
Review comment:
👍
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1318,79 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
+pub fn rewrite_sort_cols_by_aggs(
+ exprs: impl IntoIterator<Item = impl Into<Expr>>,
+ plan: &LogicalPlan,
+) -> Result<Vec<Expr>> {
+ exprs
+ .into_iter()
+ .map(|e| {
+ let expr = e.into();
+ match expr.clone() {
+ Expr::Sort {
+ expr,
+ asc,
+ nulls_first,
+ } => {
+ let sort = Expr::Sort {
+ expr: Box::new(rewrite_sort_col_by_aggs(*expr, plan)?),
+ asc,
+ nulls_first,
+ };
+ Ok(sort)
+ }
+ _ => Ok(expr),
+ }
+ })
+ .collect()
+}
+
+fn rewrite_sort_col_by_aggs(expr: Expr, plan: &LogicalPlan) -> Result<Expr> {
+ match plan {
+ LogicalPlan::Aggregate(Aggregate {
+ input, aggr_expr, ..
+ }) => {
+ struct Rewriter<'a> {
+ plan: &'a LogicalPlan,
+ input: &'a LogicalPlan,
+ aggr_expr: &'a Vec<Expr>,
+ }
+
+ impl<'a> ExprRewriter for Rewriter<'a> {
+ fn mutate(&mut self, expr: Expr) -> Result<Expr> {
+ let normalized_expr = normalize_col(expr.clone(), self.plan);
+ if normalized_expr.is_err() {
+ // The expr is not based on Aggregate plan output. Skip it.
+ return Ok(expr);
+ }
+ let normalized_expr = normalized_expr.unwrap();
+ let found_agg =
+ self.aggr_expr.iter().find(|a| (**a) == normalized_expr);
+ if found_agg.is_some() {
+ let agg = normalize_col(found_agg.unwrap().clone(), self.plan)?;
+ let col = Expr::Column(
+ agg.to_field(self.input.schema())
+ .map(|f| f.qualified_column())?,
+ );
+ Ok(col)
+ } else {
+ Ok(expr)
+ }
Review comment:
```suggestion
if let Some(found_agg) = self.aggr_expr.iter().find(|a| (**a) == normalized_expr) {
let agg = normalize_col(found_agg, self.plan)?;
let col = Expr::Column(
agg.to_field(self.input.schema())
.map(|f| f.qualified_column())?,
);
Ok(col)
} else {
Ok(expr)
}
```
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1318,79 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
+pub fn rewrite_sort_cols_by_aggs(
+ exprs: impl IntoIterator<Item = impl Into<Expr>>,
+ plan: &LogicalPlan,
+) -> Result<Vec<Expr>> {
+ exprs
+ .into_iter()
+ .map(|e| {
+ let expr = e.into();
+ match expr.clone() {
+ Expr::Sort {
+ expr,
+ asc,
+ nulls_first,
+ } => {
+ let sort = Expr::Sort {
+ expr: Box::new(rewrite_sort_col_by_aggs(*expr, plan)?),
+ asc,
+ nulls_first,
+ };
+ Ok(sort)
+ }
+ _ => Ok(expr),
+ }
+ })
+ .collect()
+}
+
+fn rewrite_sort_col_by_aggs(expr: Expr, plan: &LogicalPlan) -> Result<Expr> {
+ match plan {
+ LogicalPlan::Aggregate(Aggregate {
+ input, aggr_expr, ..
+ }) => {
+ struct Rewriter<'a> {
+ plan: &'a LogicalPlan,
+ input: &'a LogicalPlan,
+ aggr_expr: &'a Vec<Expr>,
+ }
+
+ impl<'a> ExprRewriter for Rewriter<'a> {
+ fn mutate(&mut self, expr: Expr) -> Result<Expr> {
+ let normalized_expr = normalize_col(expr.clone(), self.plan);
+ if normalized_expr.is_err() {
+ // The expr is not based on Aggregate plan output. Skip it.
+ return Ok(expr);
+ }
+ let normalized_expr = normalized_expr.unwrap();
+ let found_agg =
+ self.aggr_expr.iter().find(|a| (**a) == normalized_expr);
+ if found_agg.is_some() {
+ let agg = normalize_col(found_agg.unwrap().clone(), self.plan)?;
+ let col = Expr::Column(
+ agg.to_field(self.input.schema())
+ .map(|f| f.qualified_column())?,
+ );
+ Ok(col)
+ } else {
+ Ok(expr)
+ }
Review comment:
I think you can avoid some `is_some()` and `unwrap()` calls here to make the code stylistically nicer
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1318,79 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
Review comment:
```suggestion
/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
/// For example, `max(x)` is written to `col("MAX(x)")`
```
It took me a bit of thought to understand what this was doing so I think an extra comment might help.
--
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] viirya commented on a change in pull request #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
viirya commented on a change in pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#discussion_r776937453
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1319,83 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
+#[inline]
Review comment:
Ok, removed.
--
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] viirya commented on pull request #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
viirya commented on pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#issuecomment-1003591468
Thank you @alamb @Jimexist @xudong963 !
--
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] Jimexist commented on a change in pull request #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
Jimexist commented on a change in pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#discussion_r776912209
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1319,83 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
+#[inline]
+pub fn rewrite_sort_cols_by_aggs(
+ exprs: impl IntoIterator<Item = impl Into<Expr>>,
+ plan: &LogicalPlan,
+) -> Result<Vec<Expr>> {
+ exprs
+ .into_iter()
+ .map(|e| {
+ let expr = e.into();
+ match expr.clone() {
+ Expr::Sort {
+ expr,
+ asc,
+ nulls_first,
+ } => {
+ let sort = Expr::Sort {
+ expr: Box::new(rewrite_sort_col_by_aggs(*expr, plan)?),
+ asc,
+ nulls_first,
+ };
+ Ok(sort)
+ }
+ _ => Ok(expr),
+ }
+ })
+ .collect()
+}
+
+fn rewrite_sort_col_by_aggs(expr: Expr, plan: &LogicalPlan) -> Result<Expr> {
+ match plan {
+ LogicalPlan::Aggregate(Aggregate {
+ input,
+ group_expr: _,
+ aggr_expr,
+ schema: _,
Review comment:
i guess you can use `..` to ignore these fields?
--
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 #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
alamb merged pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506
--
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 #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
alamb commented on pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#issuecomment-1003548982
Thanks again @viirya !
--
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] viirya commented on pull request #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
viirya commented on pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#issuecomment-1003433724
Thank you @alamb . Committed the suggestions.
--
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] viirya commented on a change in pull request #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
viirya commented on a change in pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#discussion_r776923345
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1319,83 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
+#[inline]
+pub fn rewrite_sort_cols_by_aggs(
+ exprs: impl IntoIterator<Item = impl Into<Expr>>,
+ plan: &LogicalPlan,
+) -> Result<Vec<Expr>> {
+ exprs
+ .into_iter()
+ .map(|e| {
+ let expr = e.into();
+ match expr.clone() {
+ Expr::Sort {
+ expr,
+ asc,
+ nulls_first,
+ } => {
+ let sort = Expr::Sort {
+ expr: Box::new(rewrite_sort_col_by_aggs(*expr, plan)?),
+ asc,
+ nulls_first,
+ };
+ Ok(sort)
+ }
+ _ => Ok(expr),
+ }
+ })
+ .collect()
+}
+
+fn rewrite_sort_col_by_aggs(expr: Expr, plan: &LogicalPlan) -> Result<Expr> {
+ match plan {
+ LogicalPlan::Aggregate(Aggregate {
+ input,
+ group_expr: _,
+ aggr_expr,
+ schema: _,
Review comment:
yea. i will use it.
--
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] viirya commented on a change in pull request #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
viirya commented on a change in pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#discussion_r776923224
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1319,83 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
+#[inline]
Review comment:
Hmm, I saw there is `#[inline]` for `normalize_cols` above. `rewrite_sort_cols_by_aggs`'s looks similar (i.e., `into_iter().map().collect()`), so I added it too here. Should I remove it?
--
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] xudong963 commented on a change in pull request #1506: Fix ORDER BY on aggregate
Posted by GitBox <gi...@apache.org>.
xudong963 commented on a change in pull request #1506:
URL: https://github.com/apache/arrow-datafusion/pull/1506#discussion_r776934499
##########
File path: datafusion/src/logical_plan/expr.rs
##########
@@ -1317,6 +1319,83 @@ pub fn normalize_cols(
.collect()
}
+/// Rewrite sort on aggregate expressions to sort on the column of aggregate output
+#[inline]
Review comment:
> In Rust, a unit of (separate) compilation is a crate. If a function f is defined in a crate A, then all calls to f from within A can be inlined, as the compiler has full access to f. If, however, f is called from some downstream crate B, such calls can’t be inlined. B has access only to the signature of f, not its body.
So I think both of them don't need `#[inline]`.
--
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