From 17750efb71e51c77320c558452fbccf4c837afa8 Mon Sep 17 00:00:00 2001 From: Alan Tang Date: Tue, 11 Mar 2025 11:10:40 +0800 Subject: [PATCH 1/2] feat: implement tree rendering for SortPreservingMergeExec Signed-off-by: Alan Tang --- .../src/sorts/sort_preserving_merge.rs | 11 ++++- .../sqllogictest/test_files/explain_tree.slt | 48 +++++++++---------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs b/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs index 00fa78ce5229f..1670dc9decf5c 100644 --- a/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs +++ b/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs @@ -186,8 +186,15 @@ impl DisplayAs for SortPreservingMergeExec { Ok(()) } DisplayFormatType::TreeRender => { - // TODO: collect info - write!(f, "") + for (i, e) in self.expr().iter().enumerate() { + let e = e.to_string(); + writeln!(f, "expr{i}={e}")?; + } + if let Some(fetch) = self.fetch { + writeln!(f, "fetch={fetch}")?; + }; + + Ok(()) } } } diff --git a/datafusion/sqllogictest/test_files/explain_tree.slt b/datafusion/sqllogictest/test_files/explain_tree.slt index 7f2f280d3dc5b..d0e98f3b77ca9 100644 --- a/datafusion/sqllogictest/test_files/explain_tree.slt +++ b/datafusion/sqllogictest/test_files/explain_tree.slt @@ -1353,15 +1353,15 @@ ORDER BY "date", "time"; physical_plan 01)┌───────────────────────────┐ 02)│ SortPreservingMergeExec │ -03)└─────────────┬─────────────┘ -04)┌─────────────┴─────────────┐ -05)│ CoalesceBatchesExec │ -06)└─────────────┬─────────────┘ -07)┌─────────────┴─────────────┐ -08)│ FilterExec │ -09)│ -------------------- │ -10)│ predicate: │ -11)│ ticker@1 = A │ +03)│ -------------------- │ +04)│ expr0: │ +05)│ date@0 ASC NULLS LAST │ +06)│ │ +07)│ expr1: │ +08)│ time@2 ASC NULLS LAST │ +09)└─────────────┬─────────────┘ +10)┌─────────────┴─────────────┐ +11)│ CoalesceBatchesExec │ 12)└─────────────┬─────────────┘ 13)┌─────────────┴─────────────┐ 14)│ RepartitionExec │ @@ -1389,9 +1389,9 @@ ORDER BY "time" physical_plan 01)┌───────────────────────────┐ 02)│ SortPreservingMergeExec │ -03)└─────────────┬─────────────┘ -04)┌─────────────┴─────────────┐ -05)│ CoalesceBatchesExec │ +03)│ -------------------- │ +04)│ expr0: │ +05)│ time@2 ASC NULLS LAST │ 06)└─────────────┬─────────────┘ 07)┌─────────────┴─────────────┐ 08)│ FilterExec │ @@ -1425,9 +1425,9 @@ ORDER BY "date" physical_plan 01)┌───────────────────────────┐ 02)│ SortPreservingMergeExec │ -03)└─────────────┬─────────────┘ -04)┌─────────────┴─────────────┐ -05)│ CoalesceBatchesExec │ +03)│ -------------------- │ +04)│ expr0: │ +05)│ date@0 ASC NULLS LAST │ 06)└─────────────┬─────────────┘ 07)┌─────────────┴─────────────┐ 08)│ FilterExec │ @@ -1537,15 +1537,15 @@ ORDER BY "ticker", "time"; physical_plan 01)┌───────────────────────────┐ 02)│ SortPreservingMergeExec │ -03)└─────────────┬─────────────┘ -04)┌─────────────┴─────────────┐ -05)│ CoalesceBatchesExec │ -06)└─────────────┬─────────────┘ -07)┌─────────────┴─────────────┐ -08)│ FilterExec │ -09)│ -------------------- │ -10)│ predicate: │ -11)│ date@0 = 2006-01-02 │ +03)│ -------------------- │ +04)│ expr0: │ +05)│ ticker@1 ASC NULLS LAST │ +06)│ │ +07)│ expr1: │ +08)│ time@2 ASC NULLS LAST │ +09)└─────────────┬─────────────┘ +10)┌─────────────┴─────────────┐ +11)│ CoalesceBatchesExec │ 12)└─────────────┬─────────────┘ 13)┌─────────────┴─────────────┐ 14)│ RepartitionExec │ From 4d1c51baf943b3e7d51c3a0a62e77c418bb207b9 Mon Sep 17 00:00:00 2001 From: Alan Tang Date: Wed, 12 Mar 2025 09:44:08 +0800 Subject: [PATCH 2/2] feat: print sort keys together Signed-off-by: Alan Tang --- .../src/sorts/sort_preserving_merge.rs | 8 +- .../sqllogictest/test_files/explain_tree.slt | 249 +++++++++--------- 2 files changed, 137 insertions(+), 120 deletions(-) diff --git a/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs b/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs index 1670dc9decf5c..68593fe6b05d5 100644 --- a/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs +++ b/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs @@ -188,10 +188,14 @@ impl DisplayAs for SortPreservingMergeExec { DisplayFormatType::TreeRender => { for (i, e) in self.expr().iter().enumerate() { let e = e.to_string(); - writeln!(f, "expr{i}={e}")?; + if i == self.expr().len() - 1 { + writeln!(f, "{e}")?; + } else { + write!(f, "{e}, ")?; + } } if let Some(fetch) = self.fetch { - writeln!(f, "fetch={fetch}")?; + writeln!(f, "limit={fetch}")?; }; Ok(()) diff --git a/datafusion/sqllogictest/test_files/explain_tree.slt b/datafusion/sqllogictest/test_files/explain_tree.slt index d0e98f3b77ca9..caae784c9de8b 100644 --- a/datafusion/sqllogictest/test_files/explain_tree.slt +++ b/datafusion/sqllogictest/test_files/explain_tree.slt @@ -1354,30 +1354,33 @@ physical_plan 01)┌───────────────────────────┐ 02)│ SortPreservingMergeExec │ 03)│ -------------------- │ -04)│ expr0: │ -05)│ date@0 ASC NULLS LAST │ -06)│ │ -07)│ expr1: │ -08)│ time@2 ASC NULLS LAST │ +04)│ date@0 ASC NULLS LAST, │ +05)│ time@2 ASC NULLS LAST │ +06)└─────────────┬─────────────┘ +07)┌─────────────┴─────────────┐ +08)│ CoalesceBatchesExec │ 09)└─────────────┬─────────────┘ 10)┌─────────────┴─────────────┐ -11)│ CoalesceBatchesExec │ -12)└─────────────┬─────────────┘ -13)┌─────────────┴─────────────┐ -14)│ RepartitionExec │ -15)│ -------------------- │ -16)│ output_partition_count: │ -17)│ 1 │ -18)│ │ -19)│ partitioning_scheme: │ -20)│ RoundRobinBatch(4) │ -21)└─────────────┬─────────────┘ -22)┌─────────────┴─────────────┐ -23)│ StreamingTableExec │ -24)│ -------------------- │ -25)│ infinite: true │ -26)│ limit: None │ -27)└───────────────────────────┘ +11)│ FilterExec │ +12)│ -------------------- │ +13)│ predicate: │ +14)│ ticker@1 = A │ +15)└─────────────┬─────────────┘ +16)┌─────────────┴─────────────┐ +17)│ RepartitionExec │ +18)│ -------------------- │ +19)│ output_partition_count: │ +20)│ 1 │ +21)│ │ +22)│ partitioning_scheme: │ +23)│ RoundRobinBatch(4) │ +24)└─────────────┬─────────────┘ +25)┌─────────────┴─────────────┐ +26)│ StreamingTableExec │ +27)│ -------------------- │ +28)│ infinite: true │ +29)│ limit: None │ +30)└───────────────────────────┘ # constant ticker, CAST(time AS DATE) = time, order by time @@ -1390,31 +1393,33 @@ physical_plan 01)┌───────────────────────────┐ 02)│ SortPreservingMergeExec │ 03)│ -------------------- │ -04)│ expr0: │ -05)│ time@2 ASC NULLS LAST │ -06)└─────────────┬─────────────┘ -07)┌─────────────┴─────────────┐ -08)│ FilterExec │ -09)│ -------------------- │ -10)│ predicate: │ -11)│ ticker@1 = A AND CAST(time│ -12)│ @2 AS Date32) = date@0 │ -13)└─────────────┬─────────────┘ -14)┌─────────────┴─────────────┐ -15)│ RepartitionExec │ -16)│ -------------------- │ -17)│ output_partition_count: │ -18)│ 1 │ -19)│ │ -20)│ partitioning_scheme: │ -21)│ RoundRobinBatch(4) │ -22)└─────────────┬─────────────┘ -23)┌─────────────┴─────────────┐ -24)│ StreamingTableExec │ -25)│ -------------------- │ -26)│ infinite: true │ -27)│ limit: None │ -28)└───────────────────────────┘ +04)│ time@2 ASC NULLS LAST │ +05)└─────────────┬─────────────┘ +06)┌─────────────┴─────────────┐ +07)│ CoalesceBatchesExec │ +08)└─────────────┬─────────────┘ +09)┌─────────────┴─────────────┐ +10)│ FilterExec │ +11)│ -------------------- │ +12)│ predicate: │ +13)│ ticker@1 = A AND CAST(time│ +14)│ @2 AS Date32) = date@0 │ +15)└─────────────┬─────────────┘ +16)┌─────────────┴─────────────┐ +17)│ RepartitionExec │ +18)│ -------------------- │ +19)│ output_partition_count: │ +20)│ 1 │ +21)│ │ +22)│ partitioning_scheme: │ +23)│ RoundRobinBatch(4) │ +24)└─────────────┬─────────────┘ +25)┌─────────────┴─────────────┐ +26)│ StreamingTableExec │ +27)│ -------------------- │ +28)│ infinite: true │ +29)│ limit: None │ +30)└───────────────────────────┘ # same thing but order by date query TT @@ -1426,31 +1431,33 @@ physical_plan 01)┌───────────────────────────┐ 02)│ SortPreservingMergeExec │ 03)│ -------------------- │ -04)│ expr0: │ -05)│ date@0 ASC NULLS LAST │ -06)└─────────────┬─────────────┘ -07)┌─────────────┴─────────────┐ -08)│ FilterExec │ -09)│ -------------------- │ -10)│ predicate: │ -11)│ ticker@1 = A AND CAST(time│ -12)│ @2 AS Date32) = date@0 │ -13)└─────────────┬─────────────┘ -14)┌─────────────┴─────────────┐ -15)│ RepartitionExec │ -16)│ -------------------- │ -17)│ output_partition_count: │ -18)│ 1 │ -19)│ │ -20)│ partitioning_scheme: │ -21)│ RoundRobinBatch(4) │ -22)└─────────────┬─────────────┘ -23)┌─────────────┴─────────────┐ -24)│ StreamingTableExec │ -25)│ -------------------- │ -26)│ infinite: true │ -27)│ limit: None │ -28)└───────────────────────────┘ +04)│ date@0 ASC NULLS LAST │ +05)└─────────────┬─────────────┘ +06)┌─────────────┴─────────────┐ +07)│ CoalesceBatchesExec │ +08)└─────────────┬─────────────┘ +09)┌─────────────┴─────────────┐ +10)│ FilterExec │ +11)│ -------------------- │ +12)│ predicate: │ +13)│ ticker@1 = A AND CAST(time│ +14)│ @2 AS Date32) = date@0 │ +15)└─────────────┬─────────────┘ +16)┌─────────────┴─────────────┐ +17)│ RepartitionExec │ +18)│ -------------------- │ +19)│ output_partition_count: │ +20)│ 1 │ +21)│ │ +22)│ partitioning_scheme: │ +23)│ RoundRobinBatch(4) │ +24)└─────────────┬─────────────┘ +25)┌─────────────┴─────────────┐ +26)│ StreamingTableExec │ +27)│ -------------------- │ +28)│ infinite: true │ +29)│ limit: None │ +30)└───────────────────────────┘ # same thing but order by ticker query TT @@ -1498,32 +1505,35 @@ ORDER BY "time", "date"; physical_plan 01)┌───────────────────────────┐ 02)│ SortPreservingMergeExec │ -03)└─────────────┬─────────────┘ -04)┌─────────────┴─────────────┐ -05)│ CoalesceBatchesExec │ +03)│ -------------------- │ +04)│ time@2 ASC NULLS LAST, │ +05)│ date@0 ASC NULLS LAST │ 06)└─────────────┬─────────────┘ 07)┌─────────────┴─────────────┐ -08)│ FilterExec │ -09)│ -------------------- │ -10)│ predicate: │ -11)│ ticker@1 = A AND CAST(time│ -12)│ @2 AS Date32) = date@0 │ -13)└─────────────┬─────────────┘ -14)┌─────────────┴─────────────┐ -15)│ RepartitionExec │ -16)│ -------------------- │ -17)│ output_partition_count: │ -18)│ 1 │ -19)│ │ -20)│ partitioning_scheme: │ -21)│ RoundRobinBatch(4) │ -22)└─────────────┬─────────────┘ -23)┌─────────────┴─────────────┐ -24)│ StreamingTableExec │ -25)│ -------------------- │ -26)│ infinite: true │ -27)│ limit: None │ -28)└───────────────────────────┘ +08)│ CoalesceBatchesExec │ +09)└─────────────┬─────────────┘ +10)┌─────────────┴─────────────┐ +11)│ FilterExec │ +12)│ -------------------- │ +13)│ predicate: │ +14)│ ticker@1 = A AND CAST(time│ +15)│ @2 AS Date32) = date@0 │ +16)└─────────────┬─────────────┘ +17)┌─────────────┴─────────────┐ +18)│ RepartitionExec │ +19)│ -------------------- │ +20)│ output_partition_count: │ +21)│ 1 │ +22)│ │ +23)│ partitioning_scheme: │ +24)│ RoundRobinBatch(4) │ +25)└─────────────┬─────────────┘ +26)┌─────────────┴─────────────┐ +27)│ StreamingTableExec │ +28)│ -------------------- │ +29)│ infinite: true │ +30)│ limit: None │ +31)└───────────────────────────┘ @@ -1538,30 +1548,33 @@ physical_plan 01)┌───────────────────────────┐ 02)│ SortPreservingMergeExec │ 03)│ -------------------- │ -04)│ expr0: │ -05)│ ticker@1 ASC NULLS LAST │ -06)│ │ -07)│ expr1: │ -08)│ time@2 ASC NULLS LAST │ +04)│ ticker@1 ASC NULLS LAST, │ +05)│ time@2 ASC NULLS LAST │ +06)└─────────────┬─────────────┘ +07)┌─────────────┴─────────────┐ +08)│ CoalesceBatchesExec │ 09)└─────────────┬─────────────┘ 10)┌─────────────┴─────────────┐ -11)│ CoalesceBatchesExec │ -12)└─────────────┬─────────────┘ -13)┌─────────────┴─────────────┐ -14)│ RepartitionExec │ -15)│ -------------------- │ -16)│ output_partition_count: │ -17)│ 1 │ -18)│ │ -19)│ partitioning_scheme: │ -20)│ RoundRobinBatch(4) │ -21)└─────────────┬─────────────┘ -22)┌─────────────┴─────────────┐ -23)│ StreamingTableExec │ -24)│ -------------------- │ -25)│ infinite: true │ -26)│ limit: None │ -27)└───────────────────────────┘ +11)│ FilterExec │ +12)│ -------------------- │ +13)│ predicate: │ +14)│ date@0 = 2006-01-02 │ +15)└─────────────┬─────────────┘ +16)┌─────────────┴─────────────┐ +17)│ RepartitionExec │ +18)│ -------------------- │ +19)│ output_partition_count: │ +20)│ 1 │ +21)│ │ +22)│ partitioning_scheme: │ +23)│ RoundRobinBatch(4) │ +24)└─────────────┬─────────────┘ +25)┌─────────────┴─────────────┐ +26)│ StreamingTableExec │ +27)│ -------------------- │ +28)│ infinite: true │ +29)│ limit: None │ +30)└───────────────────────────┘