ozankabak commented on code in PR #11875:
URL: https://github.com/apache/datafusion/pull/11875#discussion_r1712172561
##########
datafusion/core/src/physical_optimizer/enforce_sorting.rs:
##########
@@ -1049,6 +1065,98 @@ mod tests {
Ok(())
}
+ #[tokio::test]
+ async fn test_remove_unnecessary_sort6() -> Result<()> {
+ let schema = create_test_schema()?;
+ let source = memory_exec(&schema);
+ let input = Arc::new(
+ SortExec::new(vec![sort_expr("non_nullable_col", &schema)], source)
+ .with_fetch(Some(2)),
+ );
+ let physical_plan = sort_exec(
+ vec![
+ sort_expr("non_nullable_col", &schema),
+ sort_expr("nullable_col", &schema),
+ ],
+ input,
+ );
+
+ let expected_input = [
+ "SortExec: expr=[non_nullable_col@1 ASC,nullable_col@0 ASC],
preserve_partitioning=[false]",
+ " SortExec: TopK(fetch=2), expr=[non_nullable_col@1 ASC],
preserve_partitioning=[false]",
+ " MemoryExec: partitions=1, partition_sizes=[0]",
+ ];
+ let expected_optimized = [
+ "SortExec: TopK(fetch=2), expr=[non_nullable_col@1
ASC,nullable_col@0 ASC], preserve_partitioning=[false]",
+ " MemoryExec: partitions=1, partition_sizes=[0]",
+ ];
+ assert_optimized!(expected_input, expected_optimized, physical_plan,
true);
+
+ Ok(())
+ }
+
+ #[tokio::test]
+ async fn test_remove_unnecessary_sort7() -> Result<()> {
+ let schema = create_test_schema()?;
+ let source = memory_exec(&schema);
+ let input = Arc::new(SortExec::new(
+ vec![sort_expr("non_nullable_col", &schema)],
+ source,
+ ));
+ let limit = Arc::new(LocalLimitExec::new(input, 2));
+ let physical_plan = sort_exec(
+ vec![
+ sort_expr("non_nullable_col", &schema),
+ sort_expr("nullable_col", &schema),
+ ],
+ limit,
+ );
+
+ let expected_input = [
+ "SortExec: expr=[non_nullable_col@1 ASC,nullable_col@0 ASC],
preserve_partitioning=[false]",
+ " LocalLimitExec: fetch=2",
+ " SortExec: expr=[non_nullable_col@1 ASC],
preserve_partitioning=[false]",
+ " MemoryExec: partitions=1, partition_sizes=[0]",
+ ];
+ let expected_optimized = [
+ "LocalLimitExec: fetch=2",
+ " SortExec: TopK(fetch=2), expr=[non_nullable_col@1
ASC,nullable_col@0 ASC], preserve_partitioning=[false]",
+ " MemoryExec: partitions=1, partition_sizes=[0]",
+ ];
+ assert_optimized!(expected_input, expected_optimized, physical_plan,
true);
+
+ Ok(())
+ }
+
+ #[tokio::test]
+ async fn test_do_not_pushdown_through_limit() -> Result<()> {
+ let schema = create_test_schema()?;
+ let source = memory_exec(&schema);
+ // let input = sort_exec(vec![sort_expr("non_nullable_col", &schema)],
source);
+ let input = Arc::new(SortExec::new(
+ vec![sort_expr("non_nullable_col", &schema)],
+ source,
+ ));
+ let limit = Arc::new(GlobalLimitExec::new(input, 0, Some(5))) as _;
+ let physical_plan = sort_exec(vec![sort_expr("nullable_col",
&schema)], limit);
+
+ let expected_input = [
+ "SortExec: expr=[nullable_col@0 ASC],
preserve_partitioning=[false]",
+ " GlobalLimitExec: skip=0, fetch=5",
+ " SortExec: expr=[non_nullable_col@1 ASC],
preserve_partitioning=[false]",
+ " MemoryExec: partitions=1, partition_sizes=[0]",
+ ];
+ let expected_optimized = [
Review Comment:
Yes, the limit pushdown rule will take care of 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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]