From 741b4e955a4a494dc4271d62b27166529dc15386 Mon Sep 17 00:00:00 2001 From: irenjj Date: Sat, 8 Mar 2025 08:55:46 +0800 Subject: [PATCH 1/2] Implement `tree` explain for `SortExec` --- datafusion/physical-plan/src/sorts/sort.rs | 23 +++++----- .../sqllogictest/test_files/explain_tree.slt | 46 +++++++++++++++++++ 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/datafusion/physical-plan/src/sorts/sort.rs b/datafusion/physical-plan/src/sorts/sort.rs index 55ba77096a7d..08b41e2c17aa 100644 --- a/datafusion/physical-plan/src/sorts/sort.rs +++ b/datafusion/physical-plan/src/sorts/sort.rs @@ -1005,10 +1005,15 @@ impl DisplayAs for SortExec { None => write!(f, "SortExec: expr=[{}], preserve_partitioning=[{preserve_partitioning}]", self.expr), } } - DisplayFormatType::TreeRender => { - // TODO: collect info - write!(f, "") - } + DisplayFormatType::TreeRender => match self.fetch { + Some(fetch) => { + writeln!(f, "fetch={fetch}")?; + writeln!(f, "sort=[{}]", self.expr) + } + None => { + writeln!(f, "sort=[{}]", self.expr) + } + }, } } } @@ -1225,13 +1230,9 @@ mod tests { impl DisplayAs for SortedUnboundedExec { fn fmt_as(&self, t: DisplayFormatType, f: &mut Formatter) -> fmt::Result { match t { - DisplayFormatType::Default | DisplayFormatType::Verbose => { - write!(f, "UnboundableExec",).unwrap() - } - DisplayFormatType::TreeRender => { - // TODO: collect info - write!(f, "").unwrap() - } + DisplayFormatType::Default + | DisplayFormatType::Verbose + | DisplayFormatType::TreeRender => write!(f, "UnboundableExec",).unwrap(), } Ok(()) } diff --git a/datafusion/sqllogictest/test_files/explain_tree.slt b/datafusion/sqllogictest/test_files/explain_tree.slt index 9659bdae195d..e2358759e57f 100644 --- a/datafusion/sqllogictest/test_files/explain_tree.slt +++ b/datafusion/sqllogictest/test_files/explain_tree.slt @@ -519,6 +519,52 @@ physical_plan 17)│ format: arrow │ 18)└───────────────────────────┘ +# Query for sort. +query TT +explain SELECT * FROM table1 ORDER BY string_col; +---- +logical_plan +01)Sort: table1.string_col ASC NULLS LAST +02)--TableScan: table1 projection=[int_col, string_col, bigint_col, date_col] +physical_plan +01)┌───────────────────────────┐ +02)│ SortExec │ +03)│ -------------------- │ +04)│ sort: │ +05)│ [string_col@1 ASC NULLS │ +06)│ LAST] │ +07)└─────────────┬─────────────┘ +08)┌─────────────┴─────────────┐ +09)│ DataSourceExec │ +10)│ -------------------- │ +11)│ files: 1 │ +12)│ format: csv │ +13)└───────────────────────────┘ + +# Query for sort with limit. +query TT +explain SELECT * FROM table1 ORDER BY string_col LIMIT 1; +---- +logical_plan +01)Sort: table1.string_col ASC NULLS LAST, fetch=1 +02)--TableScan: table1 projection=[int_col, string_col, bigint_col, date_col] +physical_plan +01)┌───────────────────────────┐ +02)│ SortExec │ +03)│ -------------------- │ +04)│ fetch: 1 │ +05)│ │ +06)│ sort: │ +07)│ [string_col@1 ASC NULLS │ +08)│ LAST] │ +09)└─────────────┬─────────────┘ +10)┌─────────────┴─────────────┐ +11)│ DataSourceExec │ +12)│ -------------------- │ +13)│ files: 1 │ +14)│ format: csv │ +15)└───────────────────────────┘ + # cleanup statement ok drop table table1; From e74e9f4c6f673568a23f206fad10be63f931f4cc Mon Sep 17 00:00:00 2001 From: irenjj Date: Sat, 8 Mar 2025 12:37:12 +0800 Subject: [PATCH 2/2] fix issues --- datafusion/physical-plan/src/sorts/sort.rs | 6 +++--- datafusion/sqllogictest/test_files/explain_tree.slt | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/datafusion/physical-plan/src/sorts/sort.rs b/datafusion/physical-plan/src/sorts/sort.rs index 08b41e2c17aa..0b629254267c 100644 --- a/datafusion/physical-plan/src/sorts/sort.rs +++ b/datafusion/physical-plan/src/sorts/sort.rs @@ -1007,11 +1007,11 @@ impl DisplayAs for SortExec { } DisplayFormatType::TreeRender => match self.fetch { Some(fetch) => { - writeln!(f, "fetch={fetch}")?; - writeln!(f, "sort=[{}]", self.expr) + writeln!(f, "limit={fetch}")?; + writeln!(f, "sort keys=[{}]", self.expr) } None => { - writeln!(f, "sort=[{}]", self.expr) + writeln!(f, "sort keys=[{}]", self.expr) } }, } diff --git a/datafusion/sqllogictest/test_files/explain_tree.slt b/datafusion/sqllogictest/test_files/explain_tree.slt index e2358759e57f..7e413a356902 100644 --- a/datafusion/sqllogictest/test_files/explain_tree.slt +++ b/datafusion/sqllogictest/test_files/explain_tree.slt @@ -530,7 +530,7 @@ physical_plan 01)┌───────────────────────────┐ 02)│ SortExec │ 03)│ -------------------- │ -04)│ sort: │ +04)│ sort keys: │ 05)│ [string_col@1 ASC NULLS │ 06)│ LAST] │ 07)└─────────────┬─────────────┘ @@ -552,9 +552,9 @@ physical_plan 01)┌───────────────────────────┐ 02)│ SortExec │ 03)│ -------------------- │ -04)│ fetch: 1 │ +04)│ limit: 1 │ 05)│ │ -06)│ sort: │ +06)│ sort keys: │ 07)│ [string_col@1 ASC NULLS │ 08)│ LAST] │ 09)└─────────────┬─────────────┘