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/12/15 11:32:02 UTC
[GitHub] [arrow-datafusion] mingmwang opened a new pull request, #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
mingmwang opened a new pull request, #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643
# 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:
#4635
#4641
# 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 these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
# 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] Ted-Jiang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050767717
##########
datafusion/core/tests/sql/window.rs:
##########
@@ -1642,7 +1642,120 @@ async fn test_window_agg_sort() -> Result<()> {
assert_eq!(
expected, actual_trim_last,
"\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
- expected, actual
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_prefix_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100";
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // Only 1 SortExec was added
+ let expected = {
+ vec![
+ "ProjectionExec: expr=[c2@3 as c2, MAX(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as MAX(aggregate_test_100.c9), SUM(aggregate_test_100.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@0 as SUM(aggregate_test_100.c9), MIN(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST, aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as MIN(aggregate_test_100.c9)]",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: \"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)) }]",
+ " WindowAggExec: wdw=[MAX(aggregate_test_100.c9): Ok(Field { name: \"MAX(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " WindowAggExec: wdw=[MIN(aggregate_test_100.c9): Ok(Field { name: \"MIN(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c2@0 ASC NULLS LAST,c9@1 ASC NULLS LAST]"
+ ]
+ };
+
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let actual_len = actual.len();
+ let actual_trim_last = &actual[..actual_len - 1];
+ assert_eq!(
+ expected, actual_trim_last,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+/// FIXME: for now we are not detecting prefix of sorting keys in order to re-arrange with global and save one SortExec
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_global_order_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c9, c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100 ORDER BY c2";
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // 3 SortExec are added
+ let expected = {
+ vec![
+ "SortExec: [c2@0 ASC NULLS LAST]",
+ " CoalescePartitionsExec",
+ " ProjectionExec: expr=[c2@3 as c2, MAX(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST, aggregate_test_100.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as MAX(aggregate_test_100.c9), SUM(aggregate_test_100.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@0 as SUM(aggregate_test_100.c9), MIN(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST, aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as MIN(aggregate_test_100.c9)]",
+ " RepartitionExec: partitioning=RoundRobinBatch(2)",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: \"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)) }]",
+ " WindowAggExec: wdw=[MAX(aggregate_test_100.c9): Ok(Field { name: \"MAX(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c9@2 ASC NULLS LAST,c2@1 ASC NULLS LAST]",
+ " WindowAggExec: wdw=[MIN(aggregate_test_100.c9): Ok(Field { name: \"MIN(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c2@0 ASC NULLS LAST,c9@1 ASC NULLS LAST]",
+ ]
+ };
+
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let actual_len = actual.len();
+ let actual_trim_last = &actual[..actual_len - 1];
+ assert_eq!(
+ expected, actual_trim_last,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn test_window_partition_by_order_by() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT \
+ SUM(c4) OVER(PARTITION BY c1, c2 ORDER BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),\
+ COUNT(*) OVER(PARTITION BY c1 ORDER BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) \
+ FROM aggregate_test_100";
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // Only 1 SortExec was added
+ let expected = {
+ vec![
+ "ProjectionExec: expr=[SUM(aggregate_test_100.c4) PARTITION BY [aggregate_test_100.c1, aggregate_test_100.c2] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@0 as SUM(aggregate_test_100.c4), COUNT(UInt8(1)) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@1 as COUNT(UInt8(1))]",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c4): Ok(Field { name: \"SUM(aggregate_test_100.c4)\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }, COUNT(UInt8(1)): Ok(Field { name: \"COUNT(UInt8(1))\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }]",
+ " SortExec: [c1@0 ASC NULLS LAST,c2@1 ASC NULLS LAST]",
+ " CoalesceBatchesExec: target_batch_size=4096",
Review Comment:
Thanks for explaining, print the origin plan for anyone need it:
```
[
"ProjectionExec: expr=[SUM(aggregate_test_100.c4) PARTITION BY [aggregate_test_100.c1, aggregate_test_100.c2] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@1 as SUM(aggregate_test_100.c4), COUNT(UInt8(1)) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@0 as COUNT(UInt8(1))]",
" WindowAggExec: wdw=[COUNT(UInt8(1)): Ok(Field { name: \"COUNT(UInt8(1))\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }]",
" SortExec: [c1@1 ASC,c2@2 ASC NULLS LAST]",
" CoalesceBatchesExec: target_batch_size=4096",
" RepartitionExec: partitioning=Hash([Column { name: \"c1\", index: 1 }], 2)",
" WindowAggExec: wdw=[SUM(aggregate_test_100.c4): Ok(Field { name: \"SUM(aggregate_test_100.c4)\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }]",
" SortExec: [c1@0 ASC,c2@1 ASC,c2@1 ASC NULLS LAST]",
" CoalesceBatchesExec: target_batch_size=4096",
" RepartitionExec: partitioning=Hash([Column { name: \"c1\", index: 0 }, Column { name: \"c2\", index: 1 }], 2)",
" RepartitionExec: partitioning=RoundRobinBatch(2)",
]
```
--
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] Ted-Jiang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050478591
##########
datafusion/core/tests/sql/window.rs:
##########
@@ -1642,7 +1642,120 @@ async fn test_window_agg_sort() -> Result<()> {
assert_eq!(
expected, actual_trim_last,
"\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
- expected, actual
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_prefix_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100";
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // Only 1 SortExec was added
+ let expected = {
+ vec![
+ "ProjectionExec: expr=[c2@3 as c2, MAX(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as MAX(aggregate_test_100.c9), SUM(aggregate_test_100.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@0 as SUM(aggregate_test_100.c9), MIN(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST, aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as MIN(aggregate_test_100.c9)]",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: \"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)) }]",
+ " WindowAggExec: wdw=[MAX(aggregate_test_100.c9): Ok(Field { name: \"MAX(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " WindowAggExec: wdw=[MIN(aggregate_test_100.c9): Ok(Field { name: \"MIN(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c2@0 ASC NULLS LAST,c9@1 ASC NULLS LAST]"
+ ]
+ };
+
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let actual_len = actual.len();
+ let actual_trim_last = &actual[..actual_len - 1];
+ assert_eq!(
+ expected, actual_trim_last,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+/// FIXME: for now we are not detecting prefix of sorting keys in order to re-arrange with global and save one SortExec
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_global_order_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c9, c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100 ORDER BY c2";
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // 3 SortExec are added
+ let expected = {
+ vec![
+ "SortExec: [c2@0 ASC NULLS LAST]",
+ " CoalescePartitionsExec",
+ " ProjectionExec: expr=[c2@3 as c2, MAX(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST, aggregate_test_100.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as MAX(aggregate_test_100.c9), SUM(aggregate_test_100.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@0 as SUM(aggregate_test_100.c9), MIN(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST, aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as MIN(aggregate_test_100.c9)]",
+ " RepartitionExec: partitioning=RoundRobinBatch(2)",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: \"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)) }]",
+ " WindowAggExec: wdw=[MAX(aggregate_test_100.c9): Ok(Field { name: \"MAX(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c9@2 ASC NULLS LAST,c2@1 ASC NULLS LAST]",
+ " WindowAggExec: wdw=[MIN(aggregate_test_100.c9): Ok(Field { name: \"MIN(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c2@0 ASC NULLS LAST,c9@1 ASC NULLS LAST]",
+ ]
+ };
+
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let actual_len = actual.len();
+ let actual_trim_last = &actual[..actual_len - 1];
+ assert_eq!(
+ expected, actual_trim_last,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn test_window_partition_by_order_by() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT \
+ SUM(c4) OVER(PARTITION BY c1, c2 ORDER BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),\
+ COUNT(*) OVER(PARTITION BY c1 ORDER BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) \
+ FROM aggregate_test_100";
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // Only 1 SortExec was added
+ let expected = {
+ vec![
+ "ProjectionExec: expr=[SUM(aggregate_test_100.c4) PARTITION BY [aggregate_test_100.c1, aggregate_test_100.c2] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@0 as SUM(aggregate_test_100.c4), COUNT(UInt8(1)) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@1 as COUNT(UInt8(1))]",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c4): Ok(Field { name: \"SUM(aggregate_test_100.c4)\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }, COUNT(UInt8(1)): Ok(Field { name: \"COUNT(UInt8(1))\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }]",
+ " SortExec: [c1@0 ASC NULLS LAST,c2@1 ASC NULLS LAST]",
+ " CoalesceBatchesExec: target_batch_size=4096",
Review Comment:
In this pr only deal with `sort`, who did thisπ€
--
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 #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#issuecomment-1358056548
Thanks @mingmwang @jackwener and @Ted-Jiang
--
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] mingmwang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050523235
##########
datafusion/expr/src/utils.rs:
##########
@@ -984,4 +1120,48 @@ mod tests {
assert_eq!(expected, result);
Ok(())
}
+
+ #[test]
+ fn avoid_generate_duplicate_sort_keys() -> Result<()> {
+ let asc_or_desc = [true, false];
+ let nulls_first_or_last = [true, false];
+ let partition_by = &[col("age"), col("name"), col("created_at")];
+ for asc_ in asc_or_desc {
+ for nulls_first_ in nulls_first_or_last {
+ let order_by = &[
+ Expr::Sort {
+ expr: Box::new(col("age")),
+ asc: asc_,
+ nulls_first: nulls_first_,
+ },
+ Expr::Sort {
+ expr: Box::new(col("name")),
+ asc: asc_,
+ nulls_first: nulls_first_,
+ },
+ ];
+
+ let expected = vec![
+ Expr::Sort {
+ expr: Box::new(col("age")),
+ asc: asc_,
+ nulls_first: nulls_first_,
+ },
+ Expr::Sort {
+ expr: Box::new(col("name")),
+ asc: asc_,
+ nulls_first: nulls_first_,
+ },
+ Expr::Sort {
+ expr: Box::new(col("created_at")),
+ asc: true,
+ nulls_first: false,
+ },
+ ];
+ let result = generate_sort_key(partition_by, order_by)?;
+ assert_eq!(expected, result);
Review Comment:
yes, this PR will do additional dedup to avoid generate duplicate sort keys.
--
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] mingmwang commented on pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
mingmwang commented on PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#issuecomment-1354457819
Thanks. Those clippy errors are not caused by this PR. I'm waiting for someone else to fix those in the master and I will rebase.
--
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] ursabot commented on pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
ursabot commented on PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#issuecomment-1358064791
Benchmark runs are scheduled for baseline = dba34fcf5ff7ed89f9f1ac774a90f149c2bf8b74 and contender = 891a800ebb170fed018e53846eb569f3e0638857. 891a800ebb170fed018e53846eb569f3e0638857 is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] [ec2-t3-xlarge-us-east-2](https://conbench.ursa.dev/compare/runs/6534f3b9b16441ccbd5e9faa7415a09c...af1e12490cc1400395ae5c79394db7f0/)
[Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] [test-mac-arm](https://conbench.ursa.dev/compare/runs/a4783deb9eb6487a8614a2bc0bd5f2af...e1f33bd4c35043a2b1678159de619883/)
[Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] [ursa-i9-9960x](https://conbench.ursa.dev/compare/runs/f886354f6240494cb771bc9181f13521...486091f69eb54673a21ca7c81552b63a/)
[Skipped :warning: Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] [ursa-thinkcentre-m75q](https://conbench.ursa.dev/compare/runs/69e38f50264342e6a4163cfc4eebc4d7...210fe74570fb4ac2a5cba89bdaef843d/)
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java
--
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] Ted-Jiang commented on pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#issuecomment-1354301001
Thanks for ping me β€οΈ, I will carefully review this today.
--
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] Ted-Jiang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050754846
##########
datafusion/core/tests/sql/window.rs:
##########
@@ -1642,7 +1642,120 @@ async fn test_window_agg_sort() -> Result<()> {
assert_eq!(
expected, actual_trim_last,
"\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
- expected, actual
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_prefix_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100";
Review Comment:
πfind here https://github.com/alamb/arrow-datafusion/blob/740a4fa2c6ba4b85875a433bb86e5b00435a5969/datafusion/core/src/physical_optimizer/enforcement.rs#L916
--
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 #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
alamb merged PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643
--
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 #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#issuecomment-1354909746
> Thanks. Those clippy errors are not caused by this PR. I'm waiting for someone else to fix those in the master and I will rebase.
I believe @jackwener has fixed them in https://github.com/apache/arrow-datafusion/pull/4652
--
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] Ted-Jiang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050424599
##########
datafusion/expr/src/utils.rs:
##########
@@ -202,20 +203,113 @@ pub fn expand_qualified_wildcard(
type WindowSortKey = Vec<Expr>;
/// Generate a sort key for a given window expr's partition_by and order_bu expr
-pub fn generate_sort_key(partition_by: &[Expr], order_by: &[Expr]) -> WindowSortKey {
- let mut sort_key = vec![];
+pub fn generate_sort_key(
+ partition_by: &[Expr],
+ order_by: &[Expr],
+) -> Result<WindowSortKey> {
+ let normalized_order_by_keys = order_by
+ .iter()
+ .map(|e| match e {
+ Expr::Sort {
+ expr,
+ asc: _,
+ nulls_first: _,
+ } => Ok(Expr::Sort {
+ expr: expr.clone(),
+ asc: true,
+ nulls_first: false,
Review Comment:
I think here `normalized_order_by_keys`, using the `order_by_keys` do sort partition inner each hashed partition. Only care about same value are adjacent. So no need call about the sort order ? am i right π€
--
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] Ted-Jiang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050458373
##########
datafusion/core/tests/sql/window.rs:
##########
@@ -1642,7 +1642,120 @@ async fn test_window_agg_sort() -> Result<()> {
assert_eq!(
expected, actual_trim_last,
"\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
- expected, actual
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_prefix_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100";
Review Comment:
I think without commit, this test runs well, Is `Enforcement` physical rule eliminate the `ORDER BY c2` π€ @mingmwang
--
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] Ted-Jiang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050767717
##########
datafusion/core/tests/sql/window.rs:
##########
@@ -1642,7 +1642,120 @@ async fn test_window_agg_sort() -> Result<()> {
assert_eq!(
expected, actual_trim_last,
"\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
- expected, actual
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_prefix_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100";
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // Only 1 SortExec was added
+ let expected = {
+ vec![
+ "ProjectionExec: expr=[c2@3 as c2, MAX(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as MAX(aggregate_test_100.c9), SUM(aggregate_test_100.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@0 as SUM(aggregate_test_100.c9), MIN(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST, aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as MIN(aggregate_test_100.c9)]",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: \"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)) }]",
+ " WindowAggExec: wdw=[MAX(aggregate_test_100.c9): Ok(Field { name: \"MAX(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " WindowAggExec: wdw=[MIN(aggregate_test_100.c9): Ok(Field { name: \"MIN(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c2@0 ASC NULLS LAST,c9@1 ASC NULLS LAST]"
+ ]
+ };
+
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let actual_len = actual.len();
+ let actual_trim_last = &actual[..actual_len - 1];
+ assert_eq!(
+ expected, actual_trim_last,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+/// FIXME: for now we are not detecting prefix of sorting keys in order to re-arrange with global and save one SortExec
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_global_order_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c9, c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100 ORDER BY c2";
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // 3 SortExec are added
+ let expected = {
+ vec![
+ "SortExec: [c2@0 ASC NULLS LAST]",
+ " CoalescePartitionsExec",
+ " ProjectionExec: expr=[c2@3 as c2, MAX(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST, aggregate_test_100.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as MAX(aggregate_test_100.c9), SUM(aggregate_test_100.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@0 as SUM(aggregate_test_100.c9), MIN(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST, aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as MIN(aggregate_test_100.c9)]",
+ " RepartitionExec: partitioning=RoundRobinBatch(2)",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: \"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)) }]",
+ " WindowAggExec: wdw=[MAX(aggregate_test_100.c9): Ok(Field { name: \"MAX(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c9@2 ASC NULLS LAST,c2@1 ASC NULLS LAST]",
+ " WindowAggExec: wdw=[MIN(aggregate_test_100.c9): Ok(Field { name: \"MIN(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c2@0 ASC NULLS LAST,c9@1 ASC NULLS LAST]",
+ ]
+ };
+
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let actual_len = actual.len();
+ let actual_trim_last = &actual[..actual_len - 1];
+ assert_eq!(
+ expected, actual_trim_last,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn test_window_partition_by_order_by() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT \
+ SUM(c4) OVER(PARTITION BY c1, c2 ORDER BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),\
+ COUNT(*) OVER(PARTITION BY c1 ORDER BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) \
+ FROM aggregate_test_100";
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // Only 1 SortExec was added
+ let expected = {
+ vec![
+ "ProjectionExec: expr=[SUM(aggregate_test_100.c4) PARTITION BY [aggregate_test_100.c1, aggregate_test_100.c2] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@0 as SUM(aggregate_test_100.c4), COUNT(UInt8(1)) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@1 as COUNT(UInt8(1))]",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c4): Ok(Field { name: \"SUM(aggregate_test_100.c4)\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }, COUNT(UInt8(1)): Ok(Field { name: \"COUNT(UInt8(1))\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }]",
+ " SortExec: [c1@0 ASC NULLS LAST,c2@1 ASC NULLS LAST]",
+ " CoalesceBatchesExec: target_batch_size=4096",
Review Comment:
Thanks for explaining, print the origin plan for anyone need it:
```
[
"ProjectionExec: expr=[SUM(aggregate_test_100.c4) PARTITION BY [aggregate_test_100.c1, aggregate_test_100.c2] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@1 as SUM(aggregate_test_100.c4), COUNT(UInt8(1)) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@0 as COUNT(UInt8(1))]",
" WindowAggExec: wdw=[COUNT(UInt8(1)): Ok(Field { name: \"COUNT(UInt8(1))\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }]",
" SortExec: [c1@1 ASC,c2@2 ASC NULLS LAST]",
" CoalesceBatchesExec: target_batch_size=4096",
" RepartitionExec: partitioning=Hash([Column { name: \"c1\", index: 1 }], 2)",
" WindowAggExec: wdw=[SUM(aggregate_test_100.c4): Ok(Field { name: \"SUM(aggregate_test_100.c4)\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }]",
" SortExec: [c1@0 ASC,c2@1 ASC,c2@1 ASC NULLS LAST]",
" CoalesceBatchesExec: target_batch_size=4096",
" RepartitionExec: partitioning=Hash([Column { name: \"c1\", index: 0 }, Column { name: \"c2\", index: 1 }], 2)",
" RepartitionExec: partitioning=RoundRobinBatch(2)",
]
```
After this pr, `let mut groups = group_window_expr_by_sort_keys(&window_exprs)?;` will generate one group, that's why WindowAggExecs are collapsed.
--
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] Ted-Jiang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050458373
##########
datafusion/core/tests/sql/window.rs:
##########
@@ -1642,7 +1642,120 @@ async fn test_window_agg_sort() -> Result<()> {
assert_eq!(
expected, actual_trim_last,
"\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
- expected, actual
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_prefix_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100";
Review Comment:
I think without this test runs well, Is `Enforcement` physical rule eliminate the `ORDER BY c2` π€ @mingmwang
--
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] Ted-Jiang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050465991
##########
datafusion/core/tests/sql/window.rs:
##########
@@ -1642,7 +1642,120 @@ async fn test_window_agg_sort() -> Result<()> {
assert_eq!(
expected, actual_trim_last,
"\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
- expected, actual
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_prefix_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100";
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // Only 1 SortExec was added
+ let expected = {
+ vec![
+ "ProjectionExec: expr=[c2@3 as c2, MAX(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as MAX(aggregate_test_100.c9), SUM(aggregate_test_100.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@0 as SUM(aggregate_test_100.c9), MIN(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST, aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as MIN(aggregate_test_100.c9)]",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: \"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)) }]",
+ " WindowAggExec: wdw=[MAX(aggregate_test_100.c9): Ok(Field { name: \"MAX(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " WindowAggExec: wdw=[MIN(aggregate_test_100.c9): Ok(Field { name: \"MIN(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c2@0 ASC NULLS LAST,c9@1 ASC NULLS LAST]"
+ ]
+ };
+
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let actual_len = actual.len();
+ let actual_trim_last = &actual[..actual_len - 1];
+ assert_eq!(
+ expected, actual_trim_last,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+/// FIXME: for now we are not detecting prefix of sorting keys in order to re-arrange with global and save one SortExec
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_global_order_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c9, c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100 ORDER BY c2";
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // 3 SortExec are added
+ let expected = {
+ vec![
+ "SortExec: [c2@0 ASC NULLS LAST]",
+ " CoalescePartitionsExec",
+ " ProjectionExec: expr=[c2@3 as c2, MAX(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST, aggregate_test_100.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as MAX(aggregate_test_100.c9), SUM(aggregate_test_100.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@0 as SUM(aggregate_test_100.c9), MIN(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST, aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as MIN(aggregate_test_100.c9)]",
+ " RepartitionExec: partitioning=RoundRobinBatch(2)",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: \"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)) }]",
+ " WindowAggExec: wdw=[MAX(aggregate_test_100.c9): Ok(Field { name: \"MAX(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c9@2 ASC NULLS LAST,c2@1 ASC NULLS LAST]",
+ " WindowAggExec: wdw=[MIN(aggregate_test_100.c9): Ok(Field { name: \"MIN(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c2@0 ASC NULLS LAST,c9@1 ASC NULLS LAST]",
+ ]
+ };
+
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let actual_len = actual.len();
+ let actual_trim_last = &actual[..actual_len - 1];
+ assert_eq!(
+ expected, actual_trim_last,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn test_window_partition_by_order_by() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT \
+ SUM(c4) OVER(PARTITION BY c1, c2 ORDER BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),\
+ COUNT(*) OVER(PARTITION BY c1 ORDER BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) \
+ FROM aggregate_test_100";
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // Only 1 SortExec was added
+ let expected = {
+ vec![
+ "ProjectionExec: expr=[SUM(aggregate_test_100.c4) PARTITION BY [aggregate_test_100.c1, aggregate_test_100.c2] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@0 as SUM(aggregate_test_100.c4), COUNT(UInt8(1)) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@1 as COUNT(UInt8(1))]",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c4): Ok(Field { name: \"SUM(aggregate_test_100.c4)\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }, COUNT(UInt8(1)): Ok(Field { name: \"COUNT(UInt8(1))\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }]",
+ " SortExec: [c1@0 ASC NULLS LAST,c2@1 ASC NULLS LAST]",
+ " CoalesceBatchesExec: target_batch_size=4096",
Review Comment:
Here i found this also eliminate the `RepartitionExec: partitioning=Hash([Column { name: \"c1\", index: 0 }, Column { name: \"c2\", index: 1 }], 2)`
--
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] mingmwang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050525095
##########
datafusion/core/tests/sql/window.rs:
##########
@@ -1642,7 +1642,120 @@ async fn test_window_agg_sort() -> Result<()> {
assert_eq!(
expected, actual_trim_last,
"\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
- expected, actual
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_prefix_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100";
Review Comment:
Yes.
--
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 diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1051384861
##########
datafusion/core/tests/sql/select.rs:
##########
@@ -835,6 +835,88 @@ async fn query_on_string_dictionary() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn sort_on_window_null_string() -> Result<()> {
+ let d1: DictionaryArray<Int32Type> =
+ vec![Some("one"), None, Some("three")].into_iter().collect();
+ let d2: StringArray = vec![Some("ONE"), None, Some("THREE")].into_iter().collect();
+ let d3: LargeStringArray =
+ vec![Some("One"), None, Some("Three")].into_iter().collect();
+
+ let batch = RecordBatch::try_from_iter(vec![
+ ("d1", Arc::new(d1) as ArrayRef),
+ ("d2", Arc::new(d2) as ArrayRef),
+ ("d3", Arc::new(d3) as ArrayRef),
+ ])
+ .unwrap();
+
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ ctx.register_batch("test", batch)?;
+
+ let sql =
+ "SELECT d1, row_number() OVER (partition by d1) as rn1 FROM test order by d1 asc";
+
+ let actual = execute_to_batches(&ctx, sql).await;
+ // NULLS LAST
+ let expected = vec![
+ "+-------+-----+",
+ "| d1 | rn1 |",
+ "+-------+-----+",
+ "| one | 1 |",
+ "| three | 1 |",
+ "| | 1 |",
+ "+-------+-----+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ let sql = "SELECT d2, row_number() OVER (partition by d2) as rn1 FROM test";
+ let actual = execute_to_batches(&ctx, sql).await;
+ // NULLS LAST
+ let expected = vec![
+ "+-------+-----+",
+ "| d2 | rn1 |",
+ "+-------+-----+",
+ "| ONE | 1 |",
+ "| THREE | 1 |",
+ "| | 1 |",
+ "+-------+-----+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ let sql =
+ "SELECT d2, row_number() OVER (partition by d2 order by d2 desc) as rn1 FROM test";
+
+ let actual = execute_to_batches(&ctx, sql).await;
+ // NULLS FIRST
+ let expected = vec![
+ "+-------+-----+",
+ "| d2 | rn1 |",
+ "+-------+-----+",
+ "| | 1 |",
+ "| THREE | 1 |",
+ "| ONE | 1 |",
+ "+-------+-----+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ // FIXME sort on LargeUtf8 String has bug.
+ // let sql =
+ // "SELECT d3, row_number() OVER (partition by d3) as rn1 FROM test";
+ // let actual = execute_to_batches(&ctx, sql).await;
+ // let expected = vec![
+ // "+-------+-----+",
+ // "| d3 | rn1 |",
+ // "+-------+-----+",
+ // "| | 1 |",
+ // "| One | 1 |",
+ // "| Three | 1 |",
+ // "+-------+-----+",
+ // ];
+ // assert_batches_eq!(expected, &actual);
Review Comment:
β€οΈ -- if anyone wants to help we are working on porting tests as part of https://github.com/apache/arrow-datafusion/issues/4495
--
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] mingmwang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050531869
##########
datafusion/core/tests/sql/window.rs:
##########
@@ -1642,7 +1642,120 @@ async fn test_window_agg_sort() -> Result<()> {
assert_eq!(
expected, actual_trim_last,
"\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
- expected, actual
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_prefix_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100";
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // Only 1 SortExec was added
+ let expected = {
+ vec![
+ "ProjectionExec: expr=[c2@3 as c2, MAX(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as MAX(aggregate_test_100.c9), SUM(aggregate_test_100.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@0 as SUM(aggregate_test_100.c9), MIN(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST, aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as MIN(aggregate_test_100.c9)]",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: \"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)) }]",
+ " WindowAggExec: wdw=[MAX(aggregate_test_100.c9): Ok(Field { name: \"MAX(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " WindowAggExec: wdw=[MIN(aggregate_test_100.c9): Ok(Field { name: \"MIN(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c2@0 ASC NULLS LAST,c9@1 ASC NULLS LAST]"
+ ]
+ };
+
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let actual_len = actual.len();
+ let actual_trim_last = &actual[..actual_len - 1];
+ assert_eq!(
+ expected, actual_trim_last,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+/// FIXME: for now we are not detecting prefix of sorting keys in order to re-arrange with global and save one SortExec
+#[tokio::test]
+async fn over_order_by_sort_keys_sorting_global_order_compacting() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT c2, MAX(c9) OVER (ORDER BY c9, c2), SUM(c9) OVER (), MIN(c9) OVER (ORDER BY c2, c9) from aggregate_test_100 ORDER BY c2";
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // 3 SortExec are added
+ let expected = {
+ vec![
+ "SortExec: [c2@0 ASC NULLS LAST]",
+ " CoalescePartitionsExec",
+ " ProjectionExec: expr=[c2@3 as c2, MAX(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST, aggregate_test_100.c2 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@1 as MAX(aggregate_test_100.c9), SUM(aggregate_test_100.c9) ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING@0 as SUM(aggregate_test_100.c9), MIN(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c2 ASC NULLS LAST, aggregate_test_100.c9 ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as MIN(aggregate_test_100.c9)]",
+ " RepartitionExec: partitioning=RoundRobinBatch(2)",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c9): Ok(Field { name: \"SUM(aggregate_test_100.c9)\", data_type: UInt64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(NULL)), end_bound: Following(UInt64(NULL)) }]",
+ " WindowAggExec: wdw=[MAX(aggregate_test_100.c9): Ok(Field { name: \"MAX(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c9@2 ASC NULLS LAST,c2@1 ASC NULLS LAST]",
+ " WindowAggExec: wdw=[MIN(aggregate_test_100.c9): Ok(Field { name: \"MIN(aggregate_test_100.c9)\", data_type: UInt32, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Range, start_bound: Preceding(UInt32(NULL)), end_bound: CurrentRow }]",
+ " SortExec: [c2@0 ASC NULLS LAST,c9@1 ASC NULLS LAST]",
+ ]
+ };
+
+ let actual: Vec<&str> = formatted.trim().lines().collect();
+ let actual_len = actual.len();
+ let actual_trim_last = &actual[..actual_len - 1];
+ assert_eq!(
+ expected, actual_trim_last,
+ "\n\nexpected:\n\n{:#?}\nactual:\n\n{:#?}\n\n",
+ expected, actual_trim_last
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn test_window_partition_by_order_by() -> Result<()> {
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ register_aggregate_csv(&ctx).await?;
+
+ let sql = "SELECT \
+ SUM(c4) OVER(PARTITION BY c1, c2 ORDER BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING),\
+ COUNT(*) OVER(PARTITION BY c1 ORDER BY c2 ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) \
+ FROM aggregate_test_100";
+
+ let msg = format!("Creating logical plan for '{}'", sql);
+ let plan = ctx.create_logical_plan(sql).expect(&msg);
+ let state = ctx.state();
+ let logical_plan = state.optimize(&plan)?;
+ let physical_plan = state.create_physical_plan(&logical_plan).await?;
+ let formatted = displayable(physical_plan.as_ref()).indent().to_string();
+ // Only 1 SortExec was added
+ let expected = {
+ vec![
+ "ProjectionExec: expr=[SUM(aggregate_test_100.c4) PARTITION BY [aggregate_test_100.c1, aggregate_test_100.c2] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@0 as SUM(aggregate_test_100.c4), COUNT(UInt8(1)) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c2 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING@1 as COUNT(UInt8(1))]",
+ " WindowAggExec: wdw=[SUM(aggregate_test_100.c4): Ok(Field { name: \"SUM(aggregate_test_100.c4)\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }, COUNT(UInt8(1)): Ok(Field { name: \"COUNT(UInt8(1))\", data_type: Int64, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: {} }), frame: WindowFrame { units: Rows, start_bound: Preceding(UInt64(1)), end_bound: Following(UInt64(1)) }]",
+ " SortExec: [c1@0 ASC NULLS LAST,c2@1 ASC NULLS LAST]",
+ " CoalesceBatchesExec: target_batch_size=4096",
Review Comment:
Yes, without this PR, the original physical plan added additional RepartitionExecs. this was because the additional Sort causes the two WindowAggExecs can not be collapsed.
--
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] jackwener commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
jackwener commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050815168
##########
datafusion/expr/src/logical_plan/builder.rs:
##########
@@ -249,16 +250,29 @@ impl LogicalPlanBuilder {
) -> Result<LogicalPlan> {
let mut plan = input;
let mut groups = group_window_expr_by_sort_keys(&window_exprs)?;
- // sort by sort_key len descending, so that more deeply sorted plans gets nested further
- // down as children; to further mimic the behavior of PostgreSQL, we want stable sort
- // and a reverse so that tieing sort keys are reversed in order; note that by this rule
- // if there's an empty over, it'll be at the top level
- groups.sort_by(|(key_a, _), (key_b, _)| key_a.len().cmp(&key_b.len()));
- groups.reverse();
+ // To align with the behavior of PostgreSQL, we want the sort_keys sorted as same rule as PostgreSQL that first
+ // we compare the sort key themselves and if one window's sort keys are a prefix of another
+ // put the window with more sort keys first. so more deeply sorted plans gets nested further down as children.
+ // The sort_by() implementation here is a stable sort.
+ // Note that by this rule if there's an empty over, it'll be at the top level
Review Comment:
π
##########
datafusion/core/tests/sql/select.rs:
##########
@@ -835,6 +835,88 @@ async fn query_on_string_dictionary() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn sort_on_window_null_string() -> Result<()> {
+ let d1: DictionaryArray<Int32Type> =
+ vec![Some("one"), None, Some("three")].into_iter().collect();
+ let d2: StringArray = vec![Some("ONE"), None, Some("THREE")].into_iter().collect();
+ let d3: LargeStringArray =
+ vec![Some("One"), None, Some("Three")].into_iter().collect();
+
+ let batch = RecordBatch::try_from_iter(vec![
+ ("d1", Arc::new(d1) as ArrayRef),
+ ("d2", Arc::new(d2) as ArrayRef),
+ ("d3", Arc::new(d3) as ArrayRef),
+ ])
+ .unwrap();
+
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ ctx.register_batch("test", batch)?;
+
+ let sql =
+ "SELECT d1, row_number() OVER (partition by d1) as rn1 FROM test order by d1 asc";
+
+ let actual = execute_to_batches(&ctx, sql).await;
+ // NULLS LAST
+ let expected = vec![
+ "+-------+-----+",
+ "| d1 | rn1 |",
+ "+-------+-----+",
+ "| one | 1 |",
+ "| three | 1 |",
+ "| | 1 |",
+ "+-------+-----+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ let sql = "SELECT d2, row_number() OVER (partition by d2) as rn1 FROM test";
+ let actual = execute_to_batches(&ctx, sql).await;
+ // NULLS LAST
+ let expected = vec![
+ "+-------+-----+",
+ "| d2 | rn1 |",
+ "+-------+-----+",
+ "| ONE | 1 |",
+ "| THREE | 1 |",
+ "| | 1 |",
+ "+-------+-----+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ let sql =
+ "SELECT d2, row_number() OVER (partition by d2 order by d2 desc) as rn1 FROM test";
+
+ let actual = execute_to_batches(&ctx, sql).await;
+ // NULLS FIRST
+ let expected = vec![
+ "+-------+-----+",
+ "| d2 | rn1 |",
+ "+-------+-----+",
+ "| | 1 |",
+ "| THREE | 1 |",
+ "| ONE | 1 |",
+ "+-------+-----+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ // FIXME sort on LargeUtf8 String has bug.
+ // let sql =
+ // "SELECT d3, row_number() OVER (partition by d3) as rn1 FROM test";
+ // let actual = execute_to_batches(&ctx, sql).await;
+ // let expected = vec![
+ // "+-------+-----+",
+ // "| d3 | rn1 |",
+ // "+-------+-----+",
+ // "| | 1 |",
+ // "| One | 1 |",
+ // "| Three | 1 |",
+ // "+-------+-----+",
+ // ];
+ // assert_batches_eq!(expected, &actual);
Review Comment:
π
##########
datafusion/core/tests/sql/select.rs:
##########
@@ -835,6 +835,88 @@ async fn query_on_string_dictionary() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn sort_on_window_null_string() -> Result<()> {
+ let d1: DictionaryArray<Int32Type> =
+ vec![Some("one"), None, Some("three")].into_iter().collect();
+ let d2: StringArray = vec![Some("ONE"), None, Some("THREE")].into_iter().collect();
+ let d3: LargeStringArray =
+ vec![Some("One"), None, Some("Three")].into_iter().collect();
+
+ let batch = RecordBatch::try_from_iter(vec![
+ ("d1", Arc::new(d1) as ArrayRef),
+ ("d2", Arc::new(d2) as ArrayRef),
+ ("d3", Arc::new(d3) as ArrayRef),
+ ])
+ .unwrap();
+
+ let ctx = SessionContext::with_config(SessionConfig::new().with_target_partitions(2));
+ ctx.register_batch("test", batch)?;
+
+ let sql =
+ "SELECT d1, row_number() OVER (partition by d1) as rn1 FROM test order by d1 asc";
+
+ let actual = execute_to_batches(&ctx, sql).await;
+ // NULLS LAST
+ let expected = vec![
+ "+-------+-----+",
+ "| d1 | rn1 |",
+ "+-------+-----+",
+ "| one | 1 |",
+ "| three | 1 |",
+ "| | 1 |",
+ "+-------+-----+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ let sql = "SELECT d2, row_number() OVER (partition by d2) as rn1 FROM test";
+ let actual = execute_to_batches(&ctx, sql).await;
+ // NULLS LAST
+ let expected = vec![
+ "+-------+-----+",
+ "| d2 | rn1 |",
+ "+-------+-----+",
+ "| ONE | 1 |",
+ "| THREE | 1 |",
+ "| | 1 |",
+ "+-------+-----+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ let sql =
+ "SELECT d2, row_number() OVER (partition by d2 order by d2 desc) as rn1 FROM test";
+
+ let actual = execute_to_batches(&ctx, sql).await;
+ // NULLS FIRST
+ let expected = vec![
+ "+-------+-----+",
+ "| d2 | rn1 |",
+ "+-------+-----+",
+ "| | 1 |",
+ "| THREE | 1 |",
+ "| ONE | 1 |",
+ "+-------+-----+",
+ ];
+ assert_batches_eq!(expected, &actual);
+
+ // FIXME sort on LargeUtf8 String has bug.
+ // let sql =
+ // "SELECT d3, row_number() OVER (partition by d3) as rn1 FROM test";
+ // let actual = execute_to_batches(&ctx, sql).await;
+ // let expected = vec![
+ // "+-------+-----+",
+ // "| d3 | rn1 |",
+ // "+-------+-----+",
+ // "| | 1 |",
+ // "| One | 1 |",
+ // "| Three | 1 |",
+ // "+-------+-----+",
+ // ];
+ // assert_batches_eq!(expected, &actual);
Review Comment:
BTW, we can add some test in `sqllogicaltest`
--
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] mingmwang commented on pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
mingmwang commented on PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#issuecomment-1352929995
@yahoNanJing @Ted-Jiang @alamb @jackwener
Please help to take a look.
--
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] Ted-Jiang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050454286
##########
datafusion/expr/src/utils.rs:
##########
@@ -984,4 +1120,48 @@ mod tests {
assert_eq!(expected, result);
Ok(())
}
+
+ #[test]
+ fn avoid_generate_duplicate_sort_keys() -> Result<()> {
+ let asc_or_desc = [true, false];
+ let nulls_first_or_last = [true, false];
+ let partition_by = &[col("age"), col("name"), col("created_at")];
+ for asc_ in asc_or_desc {
+ for nulls_first_ in nulls_first_or_last {
+ let order_by = &[
+ Expr::Sort {
+ expr: Box::new(col("age")),
+ asc: asc_,
+ nulls_first: nulls_first_,
+ },
+ Expr::Sort {
+ expr: Box::new(col("name")),
+ asc: asc_,
+ nulls_first: nulls_first_,
+ },
+ ];
+
+ let expected = vec![
+ Expr::Sort {
+ expr: Box::new(col("age")),
+ asc: asc_,
+ nulls_first: nulls_first_,
+ },
+ Expr::Sort {
+ expr: Box::new(col("name")),
+ asc: asc_,
+ nulls_first: nulls_first_,
+ },
+ Expr::Sort {
+ expr: Box::new(col("created_at")),
+ asc: true,
+ nulls_first: false,
+ },
+ ];
+ let result = generate_sort_key(partition_by, order_by)?;
+ assert_eq!(expected, result);
Review Comment:
I checked with this commit it will
change :
`[age ASC NULLS FIRST, name ASC NULLS FIRST, created_at ASC NULLS FIRST, age ASC NULLS LAST, name ASC NULLS LAST]`
to:
`[age DESC NULLS FIRST, name DESC NULLS FIRST, created_at ASC NULLS LAST]`
π
--
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 #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
alamb commented on PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#issuecomment-1354911729
cc @metesynnada and @retikulum as I believe you have interest in and are working on window expressions as well
--
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] Ted-Jiang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
Ted-Jiang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050424599
##########
datafusion/expr/src/utils.rs:
##########
@@ -202,20 +203,113 @@ pub fn expand_qualified_wildcard(
type WindowSortKey = Vec<Expr>;
/// Generate a sort key for a given window expr's partition_by and order_bu expr
-pub fn generate_sort_key(partition_by: &[Expr], order_by: &[Expr]) -> WindowSortKey {
- let mut sort_key = vec![];
+pub fn generate_sort_key(
+ partition_by: &[Expr],
+ order_by: &[Expr],
+) -> Result<WindowSortKey> {
+ let normalized_order_by_keys = order_by
+ .iter()
+ .map(|e| match e {
+ Expr::Sort {
+ expr,
+ asc: _,
+ nulls_first: _,
+ } => Ok(Expr::Sort {
+ expr: expr.clone(),
+ asc: true,
+ nulls_first: false,
Review Comment:
I think here `normalized_order_by_keys`, using the `order_by_keys` do sort partition inner each hashed partition. Only care about same value are adjacent. So no need call about the sort order ? am is right π€
--
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] mingmwang commented on a diff in pull request #4643: Avoid generate duplicate sort Keys from Window Expressions, fix bug when decide Window Expressions ordering
Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #4643:
URL: https://github.com/apache/arrow-datafusion/pull/4643#discussion_r1050520827
##########
datafusion/expr/src/utils.rs:
##########
@@ -202,20 +203,113 @@ pub fn expand_qualified_wildcard(
type WindowSortKey = Vec<Expr>;
/// Generate a sort key for a given window expr's partition_by and order_bu expr
-pub fn generate_sort_key(partition_by: &[Expr], order_by: &[Expr]) -> WindowSortKey {
- let mut sort_key = vec![];
+pub fn generate_sort_key(
+ partition_by: &[Expr],
+ order_by: &[Expr],
+) -> Result<WindowSortKey> {
+ let normalized_order_by_keys = order_by
+ .iter()
+ .map(|e| match e {
+ Expr::Sort {
+ expr,
+ asc: _,
+ nulls_first: _,
+ } => Ok(Expr::Sort {
+ expr: expr.clone(),
+ asc: true,
+ nulls_first: false,
Review Comment:
No, here the `normalized_order_by_keys` are for dedup purpose. The generated sort keys for window expressions are partition_by_keys + sort_keys. There might be over lap between partition_by_keys and sort_keys. The original implement leverage the `contains()` method to do the dedup but this is not enough.
--
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