11//! Presentation of query plans for use in tests.
22
33use bigdecimal:: ToPrimitive ;
4+ use datafusion:: arrow:: datatypes:: Schema ;
45use datafusion:: common:: tree_node:: { TreeNode , TreeNodeRecursion , TreeNodeVisitor } ;
6+ use datafusion:: common:: DFSchema ;
57use datafusion:: datasource:: physical_plan:: ParquetExec ;
68use datafusion:: datasource:: { DefaultTableSource , TableProvider } ;
79use datafusion:: error:: DataFusionError ;
@@ -34,7 +36,7 @@ use crate::queryplanner::tail_limit::TailLimitExec;
3436use crate :: queryplanner:: topk:: SortColumn ;
3537use crate :: queryplanner:: topk:: { AggregateTopKExec , ClusterAggregateTopKUpper , ClusterAggregateTopKLower } ;
3638use crate :: queryplanner:: trace_data_loaded:: TraceDataLoadedExec ;
37- use crate :: queryplanner:: { CubeTableLogical , InfoSchemaTableProvider } ;
39+ use crate :: queryplanner:: { CubeTableLogical , InfoSchemaTableProvider , QueryPlan } ;
3840use crate :: streaming:: topic_table_provider:: TopicTableProvider ;
3941use datafusion:: physical_plan:: empty:: EmptyExec ;
4042use datafusion:: physical_plan:: expressions:: Column ;
@@ -51,29 +53,24 @@ pub struct PPOptions {
5153 pub show_filters : bool ,
5254 pub show_sort_by : bool ,
5355 pub show_aggregations : bool ,
54- // TODO: Maybe prettify output, name this show_schema.
55- pub debug_schema : bool ,
56+ pub show_schema : bool ,
5657 // Applies only to physical plan.
5758 pub show_output_hints : bool ,
5859 pub show_check_memory_nodes : bool ,
60+ pub show_partitions : bool ,
5961}
6062
6163impl PPOptions {
62- pub fn not_everything ( ) -> PPOptions {
64+ #[ allow( unused) ]
65+ pub fn everything ( ) -> PPOptions {
6366 PPOptions {
6467 show_filters : true ,
6568 show_sort_by : true ,
6669 show_aggregations : true ,
67- debug_schema : false ,
70+ show_schema : true ,
6871 show_output_hints : true ,
6972 show_check_memory_nodes : true ,
70- }
71- }
72-
73- pub fn truly_everything ( ) -> PPOptions {
74- PPOptions {
75- debug_schema : true ,
76- ..PPOptions :: not_everything ( )
73+ show_partitions : true ,
7774 }
7875 }
7976
@@ -93,7 +90,18 @@ pub fn pp_phys_plan_ext(p: &dyn ExecutionPlan, o: &PPOptions) -> String {
9390}
9491
9592pub fn pp_plan ( p : & LogicalPlan ) -> String {
96- pp_plan_ext ( p, & PPOptions :: default ( ) )
93+ pp_plan_ext ( p, & PPOptions :: none ( ) )
94+ }
95+
96+ pub fn pp_query_plan_ext ( qp : & QueryPlan , o : & PPOptions ) -> String {
97+ pp_plan_ext ( match qp {
98+ QueryPlan :: Meta ( p) => p,
99+ QueryPlan :: Select ( pre_serialized_plan, _) => pre_serialized_plan. logical_plan ( )
100+ } , o)
101+ }
102+
103+ pub fn pp_query_plan ( p : & QueryPlan ) -> String {
104+ pp_query_plan_ext ( p, & PPOptions :: none ( ) )
97105}
98106
99107pub fn pp_plan_ext ( p : & LogicalPlan , opts : & PPOptions ) -> String {
@@ -178,7 +186,7 @@ pub fn pp_plan_ext(p: &LogicalPlan, opts: &PPOptions) -> String {
178186 }
179187 }
180188 LogicalPlan :: Union ( Union { schema, .. } ) => {
181- self . output += & format ! ( "Union, schema: {}" , schema)
189+ self . output += & format ! ( "Union, schema: {}" , pp_df_schema ( schema. as_ref ( ) ) )
182190 }
183191 LogicalPlan :: Join ( Join { on, .. } ) => {
184192 self . output += & format ! (
@@ -372,8 +380,8 @@ pub fn pp_plan_ext(p: &LogicalPlan, opts: &PPOptions) -> String {
372380 }
373381 }
374382
375- if self . opts . debug_schema {
376- self . output += & format ! ( ", debug_schema : {:? }" , plan. schema( ) ) ;
383+ if self . opts . show_schema {
384+ self . output += & format ! ( ", schema : {}" , pp_df_schema ( plan. schema( ) . as_ref ( ) ) ) ;
377385 }
378386
379387 if !saw_expected_topk_lower {
@@ -475,6 +483,8 @@ fn pp_phys_plan_indented(p: &dyn ExecutionPlan, indent: usize, o: &PPOptions, ou
475483 }
476484 out. extend ( repeat_n ( ' ' , indent) ) ;
477485
486+ let mut skip_show_partitions = false ;
487+
478488 let a = p. as_any ( ) ;
479489 if let Some ( t) = a. downcast_ref :: < CubeTableExec > ( ) {
480490 * out += & format ! ( "Scan, index: {}" , pp_index( & t. index_snapshot) ) ;
@@ -588,6 +598,7 @@ fn pp_phys_plan_indented(p: &dyn ExecutionPlan, indent: usize, o: &PPOptions, ou
588598 } )
589599 . join( ", " )
590600 ) ;
601+ skip_show_partitions = true ;
591602 } else if let Some ( topk) = a. downcast_ref :: < AggregateTopKExec > ( ) {
592603 * out += & format ! ( "AggregateTopK, limit: {:?}" , topk. limit) ;
593604 if o. show_aggregations {
@@ -661,14 +672,6 @@ fn pp_phys_plan_indented(p: &dyn ExecutionPlan, indent: usize, o: &PPOptions, ou
661672 * out += & to_string. split ( " " ) . next ( ) . unwrap_or ( & to_string) ;
662673 }
663674
664- // TODO upgrade DF - remove
665- // *out += &format!(", schema: {}", p.schema());
666- // *out += &format!(
667- // ", partitions: {}, output_ordering: {:?}",
668- // p.properties().partitioning.partition_count(),
669- // p.output_ordering()
670- // );
671-
672675 if o. show_output_hints {
673676 let properties: & PlanProperties = p. properties ( ) ;
674677
@@ -728,8 +731,12 @@ fn pp_phys_plan_indented(p: &dyn ExecutionPlan, indent: usize, o: &PPOptions, ou
728731 }
729732 }
730733
731- if o. debug_schema {
732- * out += & format ! ( ", debug_schema: {:?}" , p. schema( ) ) ;
734+ if o. show_schema {
735+ * out += & format ! ( ", schema: {}" , pp_schema( p. schema( ) . as_ref( ) ) ) ;
736+ }
737+
738+ if o. show_partitions && !skip_show_partitions {
739+ * out += & format ! ( ", partitions: {}" , p. properties( ) . output_partitioning( ) . partition_count( ) ) ;
733740 }
734741 }
735742}
@@ -752,3 +759,15 @@ fn pp_row_range(r: &RowRange) -> String {
752759fn pp_exprs ( v : & Vec < Expr > ) -> String {
753760 "[" . to_owned ( ) + & v. iter ( ) . map ( |e : & Expr | format ! ( "{}" , e) ) . join ( ", " ) + "]"
754761}
762+
763+ fn pp_df_schema ( schema : & DFSchema ) -> String {
764+ // Like pp_schema but with qualifiers.
765+ format ! ( "{}" , schema)
766+ }
767+
768+ fn pp_schema ( schema : & Schema ) -> String {
769+ // Mimicking DFSchema's Display
770+ format ! ( "fields:[{}], metadata:{:?}" ,
771+ schema. fields. iter( ) . map( |f| f. name( ) ) . join( ", " ) ,
772+ schema. metadata)
773+ }
0 commit comments