1use itertools::Itertools;
34use proptest_derive::Arbitrary;
35use serde::{Deserialize, Serialize};
36use std::borrow::Cow;
37use std::collections::{BTreeMap, BTreeSet};
38use std::fmt;
39use std::fmt::{Display, Formatter};
40
41use mz_ore::stack::RecursionLimitError;
42use mz_ore::str::{Indent, bracketed, separated};
43
44use crate::explain::dot::{DisplayDot, dot_string};
45use crate::explain::json::{DisplayJson, json_string};
46use crate::explain::text::{DisplayText, text_string};
47use crate::optimize::OptimizerFeatureOverrides;
48use crate::{ColumnType, GlobalId, ScalarType};
49
50pub mod dot;
51pub mod json;
52pub mod text;
53#[cfg(feature = "tracing")]
54pub mod tracing;
55
56#[cfg(feature = "tracing")]
57pub use crate::explain::tracing::trace_plan;
58
59#[derive(Debug, Clone, Copy, Eq, PartialEq)]
61pub enum ExplainFormat {
62 Text,
63 Json,
64 Dot,
65}
66
67impl fmt::Display for ExplainFormat {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 match self {
70 ExplainFormat::Text => f.write_str("TEXT"),
71 ExplainFormat::Json => f.write_str("JSON"),
72 ExplainFormat::Dot => f.write_str("DOT"),
73 }
74 }
75}
76
77#[allow(missing_debug_implementations)]
81pub enum UnsupportedFormat {}
82
83#[derive(Debug)]
86pub enum ExplainError {
87 UnsupportedFormat(ExplainFormat),
88 FormatError(fmt::Error),
89 AnyhowError(anyhow::Error),
90 RecursionLimitError(RecursionLimitError),
91 SerdeJsonError(serde_json::Error),
92 LinearChainsPlusRecursive,
93 UnknownError(String),
94}
95
96impl fmt::Display for ExplainError {
97 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98 write!(f, "error while rendering explain output: ")?;
99 match self {
100 ExplainError::UnsupportedFormat(format) => {
101 write!(f, "{} format is not supported", format)
102 }
103 ExplainError::FormatError(error) => {
104 write!(f, "{}", error)
105 }
106 ExplainError::AnyhowError(error) => {
107 write!(f, "{}", error)
108 }
109 ExplainError::RecursionLimitError(error) => {
110 write!(f, "{}", error)
111 }
112 ExplainError::SerdeJsonError(error) => {
113 write!(f, "{}", error)
114 }
115 ExplainError::LinearChainsPlusRecursive => {
116 write!(
117 f,
118 "The linear_chains option is not supported with WITH MUTUALLY RECURSIVE."
119 )
120 }
121 ExplainError::UnknownError(error) => {
122 write!(f, "{}", error)
123 }
124 }
125 }
126}
127
128impl From<fmt::Error> for ExplainError {
129 fn from(error: fmt::Error) -> Self {
130 ExplainError::FormatError(error)
131 }
132}
133
134impl From<anyhow::Error> for ExplainError {
135 fn from(error: anyhow::Error) -> Self {
136 ExplainError::AnyhowError(error)
137 }
138}
139
140impl From<RecursionLimitError> for ExplainError {
141 fn from(error: RecursionLimitError) -> Self {
142 ExplainError::RecursionLimitError(error)
143 }
144}
145
146impl From<serde_json::Error> for ExplainError {
147 fn from(error: serde_json::Error) -> Self {
148 ExplainError::SerdeJsonError(error)
149 }
150}
151
152#[derive(Clone, Debug)]
154pub struct ExplainConfig {
155 pub subtree_size: bool,
159 pub arity: bool,
161 pub types: bool,
163 pub keys: bool,
165 pub non_negative: bool,
167 pub cardinality: bool,
169 pub column_names: bool,
171 pub equivalences: bool,
173 pub join_impls: bool,
179 pub humanized_exprs: bool,
181 pub linear_chains: bool,
183 pub no_fast_path: bool,
186 pub no_notices: bool,
188 pub node_ids: bool,
190 pub raw_plans: bool,
192 pub raw_syntax: bool,
194 pub verbose_syntax: bool,
196 pub redacted: bool,
198 pub timing: bool,
200 pub filter_pushdown: bool,
202
203 pub features: OptimizerFeatureOverrides,
205}
206
207impl Default for ExplainConfig {
208 fn default() -> Self {
209 Self {
210 redacted: !mz_ore::assert::soft_assertions_enabled(),
212 arity: false,
213 cardinality: false,
214 column_names: false,
215 filter_pushdown: false,
216 humanized_exprs: false,
217 join_impls: true,
218 keys: false,
219 linear_chains: false,
220 no_fast_path: true,
221 no_notices: false,
222 node_ids: false,
223 non_negative: false,
224 raw_plans: true,
225 raw_syntax: false,
226 verbose_syntax: false,
227 subtree_size: false,
228 timing: false,
229 types: false,
230 equivalences: false,
231 features: Default::default(),
232 }
233 }
234}
235
236impl ExplainConfig {
237 pub fn requires_analyses(&self) -> bool {
238 self.subtree_size
239 || self.non_negative
240 || self.arity
241 || self.types
242 || self.keys
243 || self.cardinality
244 || self.column_names
245 || self.equivalences
246 }
247}
248
249#[derive(Clone, Debug)]
251pub enum Explainee {
252 MaterializedView(GlobalId),
254 Index(GlobalId),
256 Dataflow(GlobalId),
260 Select,
263}
264
265pub trait Explain<'a>: 'a {
271 type Context;
274
275 type Text: DisplayText;
278
279 type Json: DisplayJson;
282
283 type Dot: DisplayDot;
286
287 fn explain(
302 &'a mut self,
303 format: &'a ExplainFormat,
304 context: &'a Self::Context,
305 ) -> Result<String, ExplainError> {
306 match format {
307 ExplainFormat::Text => self.explain_text(context).map(|e| text_string(&e)),
308 ExplainFormat::Json => self.explain_json(context).map(|e| json_string(&e)),
309 ExplainFormat::Dot => self.explain_dot(context).map(|e| dot_string(&e)),
310 }
311 }
312
313 #[allow(unused_variables)]
325 fn explain_text(&'a mut self, context: &'a Self::Context) -> Result<Self::Text, ExplainError> {
326 Err(ExplainError::UnsupportedFormat(ExplainFormat::Text))
327 }
328
329 #[allow(unused_variables)]
341 fn explain_json(&'a mut self, context: &'a Self::Context) -> Result<Self::Json, ExplainError> {
342 Err(ExplainError::UnsupportedFormat(ExplainFormat::Json))
343 }
344
345 #[allow(unused_variables)]
357 fn explain_dot(&'a mut self, context: &'a Self::Context) -> Result<Self::Dot, ExplainError> {
358 Err(ExplainError::UnsupportedFormat(ExplainFormat::Dot))
359 }
360}
361
362#[derive(Debug)]
366pub struct RenderingContext<'a> {
367 pub indent: Indent,
368 pub humanizer: &'a dyn ExprHumanizer,
369}
370
371impl<'a> RenderingContext<'a> {
372 pub fn new(indent: Indent, humanizer: &'a dyn ExprHumanizer) -> RenderingContext<'a> {
373 RenderingContext { indent, humanizer }
374 }
375}
376
377impl<'a> AsMut<Indent> for RenderingContext<'a> {
378 fn as_mut(&mut self) -> &mut Indent {
379 &mut self.indent
380 }
381}
382
383impl<'a> AsRef<&'a dyn ExprHumanizer> for RenderingContext<'a> {
384 fn as_ref(&self) -> &&'a dyn ExprHumanizer {
385 &self.humanizer
386 }
387}
388
389#[allow(missing_debug_implementations)]
390pub struct PlanRenderingContext<'a, T> {
391 pub indent: Indent,
392 pub humanizer: &'a dyn ExprHumanizer,
393 pub annotations: BTreeMap<&'a T, Analyses>,
394 pub config: &'a ExplainConfig,
395}
396
397impl<'a, T> PlanRenderingContext<'a, T> {
398 pub fn new(
399 indent: Indent,
400 humanizer: &'a dyn ExprHumanizer,
401 annotations: BTreeMap<&'a T, Analyses>,
402 config: &'a ExplainConfig,
403 ) -> PlanRenderingContext<'a, T> {
404 PlanRenderingContext {
405 indent,
406 humanizer,
407 annotations,
408 config,
409 }
410 }
411}
412
413impl<'a, T> AsMut<Indent> for PlanRenderingContext<'a, T> {
414 fn as_mut(&mut self) -> &mut Indent {
415 &mut self.indent
416 }
417}
418
419impl<'a, T> AsRef<&'a dyn ExprHumanizer> for PlanRenderingContext<'a, T> {
420 fn as_ref(&self) -> &&'a dyn ExprHumanizer {
421 &self.humanizer
422 }
423}
424
425pub trait ExprHumanizer: fmt::Debug {
430 fn humanize_id(&self, id: GlobalId) -> Option<String>;
433
434 fn humanize_id_unqualified(&self, id: GlobalId) -> Option<String>;
436
437 fn humanize_id_parts(&self, id: GlobalId) -> Option<Vec<String>>;
440
441 fn humanize_scalar_type(&self, ty: &ScalarType, postgres_compat: bool) -> String;
446
447 fn humanize_column_type(&self, typ: &ColumnType, postgres_compat: bool) -> String {
452 format!(
453 "{}{}",
454 self.humanize_scalar_type(&typ.scalar_type, postgres_compat),
455 if typ.nullable { "?" } else { "" }
456 )
457 }
458
459 fn column_names_for_id(&self, id: GlobalId) -> Option<Vec<String>>;
461
462 fn humanize_column(&self, id: GlobalId, column: usize) -> Option<String>;
464
465 fn id_exists(&self, id: GlobalId) -> bool;
467}
468
469#[derive(Debug)]
472pub struct ExprHumanizerExt<'a> {
473 items: BTreeMap<GlobalId, TransientItem>,
476 inner: &'a dyn ExprHumanizer,
479}
480
481impl<'a> ExprHumanizerExt<'a> {
482 pub fn new(items: BTreeMap<GlobalId, TransientItem>, inner: &'a dyn ExprHumanizer) -> Self {
483 Self { items, inner }
484 }
485}
486
487impl<'a> ExprHumanizer for ExprHumanizerExt<'a> {
488 fn humanize_id(&self, id: GlobalId) -> Option<String> {
489 match self.items.get(&id) {
490 Some(item) => item
491 .humanized_id_parts
492 .as_ref()
493 .map(|parts| parts.join(".")),
494 None => self.inner.humanize_id(id),
495 }
496 }
497
498 fn humanize_id_unqualified(&self, id: GlobalId) -> Option<String> {
499 match self.items.get(&id) {
500 Some(item) => item
501 .humanized_id_parts
502 .as_ref()
503 .and_then(|parts| parts.last().cloned()),
504 None => self.inner.humanize_id_unqualified(id),
505 }
506 }
507
508 fn humanize_id_parts(&self, id: GlobalId) -> Option<Vec<String>> {
509 match self.items.get(&id) {
510 Some(item) => item.humanized_id_parts.clone(),
511 None => self.inner.humanize_id_parts(id),
512 }
513 }
514
515 fn humanize_scalar_type(&self, ty: &ScalarType, postgres_compat: bool) -> String {
516 self.inner.humanize_scalar_type(ty, postgres_compat)
517 }
518
519 fn column_names_for_id(&self, id: GlobalId) -> Option<Vec<String>> {
520 match self.items.get(&id) {
521 Some(item) => item.column_names.clone(),
522 None => self.inner.column_names_for_id(id),
523 }
524 }
525
526 fn humanize_column(&self, id: GlobalId, column: usize) -> Option<String> {
527 match self.items.get(&id) {
528 Some(item) => match &item.column_names {
529 Some(column_names) => Some(column_names[column].clone()),
530 None => None,
531 },
532 None => self.inner.humanize_column(id, column),
533 }
534 }
535
536 fn id_exists(&self, id: GlobalId) -> bool {
537 self.items.contains_key(&id) || self.inner.id_exists(id)
538 }
539}
540
541#[derive(Debug)]
545pub struct TransientItem {
546 humanized_id_parts: Option<Vec<String>>,
547 column_names: Option<Vec<String>>,
548}
549
550impl TransientItem {
551 pub fn new(humanized_id_parts: Option<Vec<String>>, column_names: Option<Vec<String>>) -> Self {
552 Self {
553 humanized_id_parts,
554 column_names,
555 }
556 }
557}
558
559#[derive(Debug)]
565pub struct DummyHumanizer;
566
567impl ExprHumanizer for DummyHumanizer {
568 fn humanize_id(&self, _: GlobalId) -> Option<String> {
569 None
572 }
573
574 fn humanize_id_unqualified(&self, _id: GlobalId) -> Option<String> {
575 None
576 }
577
578 fn humanize_id_parts(&self, _id: GlobalId) -> Option<Vec<String>> {
579 None
580 }
581
582 fn humanize_scalar_type(&self, ty: &ScalarType, _postgres_compat: bool) -> String {
583 format!("{:?}", ty)
585 }
586
587 fn column_names_for_id(&self, _id: GlobalId) -> Option<Vec<String>> {
588 None
589 }
590
591 fn humanize_column(&self, _id: GlobalId, _column: usize) -> Option<String> {
592 None
593 }
594
595 fn id_exists(&self, _id: GlobalId) -> bool {
596 false
597 }
598}
599
600#[derive(Debug)]
602pub struct Indices<'a>(pub &'a [usize]);
603
604#[derive(Debug)]
609pub struct CompactScalarSeq<'a, T: ScalarOps>(pub &'a [T]); #[derive(Debug)]
616pub struct CompactScalars<T, I>(pub I)
617where
618 T: ScalarOps,
619 I: Iterator<Item = T> + Clone;
620
621pub trait ScalarOps {
622 fn match_col_ref(&self) -> Option<usize>;
623
624 fn references(&self, col_ref: usize) -> bool;
625}
626
627#[allow(missing_debug_implementations)]
630pub struct AnnotatedPlan<'a, T> {
631 pub plan: &'a T,
632 pub annotations: BTreeMap<&'a T, Analyses>,
633}
634
635#[derive(Clone, Default, Debug)]
637pub struct Analyses {
638 pub non_negative: Option<bool>,
639 pub subtree_size: Option<usize>,
640 pub arity: Option<usize>,
641 pub types: Option<Option<Vec<ColumnType>>>,
642 pub keys: Option<Vec<Vec<usize>>>,
643 pub cardinality: Option<String>,
644 pub column_names: Option<Vec<String>>,
645 pub equivalences: Option<String>,
646}
647
648#[derive(Debug, Clone)]
649pub struct HumanizedAnalyses<'a> {
650 analyses: &'a Analyses,
651 humanizer: &'a dyn ExprHumanizer,
652 config: &'a ExplainConfig,
653}
654
655impl<'a> HumanizedAnalyses<'a> {
656 pub fn new<T>(analyses: &'a Analyses, ctx: &PlanRenderingContext<'a, T>) -> Self {
657 Self {
658 analyses,
659 humanizer: ctx.humanizer,
660 config: ctx.config,
661 }
662 }
663}
664
665impl<'a> Display for HumanizedAnalyses<'a> {
666 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
671 let mut builder = f.debug_struct("//");
672
673 if self.config.subtree_size {
674 let subtree_size = self.analyses.subtree_size.expect("subtree_size");
675 builder.field("subtree_size", &subtree_size);
676 }
677
678 if self.config.non_negative {
679 let non_negative = self.analyses.non_negative.expect("non_negative");
680 builder.field("non_negative", &non_negative);
681 }
682
683 if self.config.arity {
684 let arity = self.analyses.arity.expect("arity");
685 builder.field("arity", &arity);
686 }
687
688 if self.config.types {
689 let types = match self.analyses.types.as_ref().expect("types") {
690 Some(types) => {
691 let types = types
692 .into_iter()
693 .map(|c| self.humanizer.humanize_column_type(c, false))
694 .collect::<Vec<_>>();
695
696 bracketed("(", ")", separated(", ", types)).to_string()
697 }
698 None => "(<error>)".to_string(),
699 };
700 builder.field("types", &types);
701 }
702
703 if self.config.keys {
704 let keys = self
705 .analyses
706 .keys
707 .as_ref()
708 .expect("keys")
709 .into_iter()
710 .map(|key| bracketed("[", "]", separated(", ", key)).to_string());
711 let keys = bracketed("(", ")", separated(", ", keys)).to_string();
712 builder.field("keys", &keys);
713 }
714
715 if self.config.cardinality {
716 let cardinality = self.analyses.cardinality.as_ref().expect("cardinality");
717 builder.field("cardinality", cardinality);
718 }
719
720 if self.config.column_names {
721 let column_names = self.analyses.column_names.as_ref().expect("column_names");
722 let column_names = column_names.into_iter().enumerate().map(|(i, c)| {
723 if c.is_empty() {
724 Cow::Owned(format!("#{i}"))
725 } else {
726 Cow::Borrowed(c)
727 }
728 });
729 let column_names = bracketed("(", ")", separated(", ", column_names)).to_string();
730 builder.field("column_names", &column_names);
731 }
732
733 if self.config.equivalences {
734 let equivs = self.analyses.equivalences.as_ref().expect("equivalences");
735 builder.field("equivs", equivs);
736 }
737
738 builder.finish()
739 }
740}
741
742#[derive(Clone, Debug, Default)]
751pub struct UsedIndexes(BTreeSet<(GlobalId, Vec<IndexUsageType>)>);
752
753impl UsedIndexes {
754 pub fn new(values: BTreeSet<(GlobalId, Vec<IndexUsageType>)>) -> UsedIndexes {
755 UsedIndexes(values)
756 }
757
758 pub fn is_empty(&self) -> bool {
759 self.0.is_empty()
760 }
761}
762
763#[derive(Debug, Clone, Arbitrary, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Hash)]
764pub enum IndexUsageType {
765 FullScan,
767 DifferentialJoin,
769 DeltaJoin(DeltaJoinIndexUsageType),
771 Lookup(GlobalId),
776 PlanRootNoArrangement,
782 SinkExport,
785 IndexExport,
788 FastPathLimit,
793 DanglingArrangeBy,
799 Unknown,
802}
803
804#[derive(Debug, Clone, Arbitrary, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Hash)]
808pub enum DeltaJoinIndexUsageType {
809 Unknown,
810 Lookup,
811 FirstInputFullScan,
812}
813
814impl std::fmt::Display for IndexUsageType {
815 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
816 write!(
817 f,
818 "{}",
819 match self {
820 IndexUsageType::FullScan => "*** full scan ***",
821 IndexUsageType::Lookup(_idx_id) => "lookup",
822 IndexUsageType::DifferentialJoin => "differential join",
823 IndexUsageType::DeltaJoin(DeltaJoinIndexUsageType::FirstInputFullScan) =>
824 "delta join 1st input (full scan)",
825 IndexUsageType::DeltaJoin(DeltaJoinIndexUsageType::Lookup) => "delta join lookup",
832 IndexUsageType::DeltaJoin(DeltaJoinIndexUsageType::Unknown) =>
833 "*** INTERNAL ERROR (unknown delta join usage) ***",
834 IndexUsageType::PlanRootNoArrangement => "plan root (no new arrangement)",
835 IndexUsageType::SinkExport => "sink export",
836 IndexUsageType::IndexExport => "index export",
837 IndexUsageType::FastPathLimit => "fast path limit",
838 IndexUsageType::DanglingArrangeBy => "*** INTERNAL ERROR (dangling ArrangeBy) ***",
839 IndexUsageType::Unknown => "*** INTERNAL ERROR (unknown usage) ***",
840 }
841 )
842 }
843}
844
845impl IndexUsageType {
846 pub fn display_vec<'a, I>(usage_types: I) -> impl Display + Sized + 'a
847 where
848 I: IntoIterator<Item = &'a IndexUsageType>,
849 {
850 separated(", ", usage_types.into_iter().sorted().dedup())
851 }
852}
853
854#[cfg(test)]
855mod tests {
856 use mz_ore::assert_ok;
857
858 use super::*;
859
860 struct Environment {
861 name: String,
862 }
863
864 impl Default for Environment {
865 fn default() -> Self {
866 Environment {
867 name: "test env".to_string(),
868 }
869 }
870 }
871
872 struct Frontiers<T> {
873 since: T,
874 upper: T,
875 }
876
877 impl<T> Frontiers<T> {
878 fn new(since: T, upper: T) -> Self {
879 Self { since, upper }
880 }
881 }
882
883 struct ExplainContext<'a> {
884 env: &'a mut Environment,
885 config: &'a ExplainConfig,
886 frontiers: Frontiers<u64>,
887 }
888
889 struct TestExpr {
891 lhs: i32,
892 rhs: i32,
893 }
894
895 struct TestExplanation<'a> {
896 expr: &'a TestExpr,
897 context: &'a ExplainContext<'a>,
898 }
899
900 impl<'a> DisplayText for TestExplanation<'a> {
901 fn fmt_text(&self, f: &mut fmt::Formatter<'_>, _ctx: &mut ()) -> fmt::Result {
902 let lhs = &self.expr.lhs;
903 let rhs = &self.expr.rhs;
904 writeln!(f, "expr = {lhs} + {rhs}")?;
905
906 if self.context.config.timing {
907 let since = &self.context.frontiers.since;
908 let upper = &self.context.frontiers.upper;
909 writeln!(f, "at t ∊ [{since}, {upper})")?;
910 }
911
912 let name = &self.context.env.name;
913 writeln!(f, "env = {name}")?;
914
915 Ok(())
916 }
917 }
918
919 impl<'a> Explain<'a> for TestExpr {
920 type Context = ExplainContext<'a>;
921 type Text = TestExplanation<'a>;
922 type Json = UnsupportedFormat;
923 type Dot = UnsupportedFormat;
924
925 fn explain_text(
926 &'a mut self,
927 context: &'a Self::Context,
928 ) -> Result<Self::Text, ExplainError> {
929 Ok(TestExplanation {
930 expr: self,
931 context,
932 })
933 }
934 }
935
936 fn do_explain(
937 env: &mut Environment,
938 frontiers: Frontiers<u64>,
939 ) -> Result<String, ExplainError> {
940 let mut expr = TestExpr { lhs: 1, rhs: 2 };
941
942 let format = ExplainFormat::Text;
943 let config = &ExplainConfig {
944 redacted: false,
945 arity: false,
946 cardinality: false,
947 column_names: false,
948 filter_pushdown: false,
949 humanized_exprs: false,
950 join_impls: false,
951 keys: false,
952 linear_chains: false,
953 no_fast_path: false,
954 no_notices: false,
955 node_ids: false,
956 non_negative: false,
957 raw_plans: false,
958 raw_syntax: false,
959 verbose_syntax: true,
960 subtree_size: false,
961 equivalences: false,
962 timing: true,
963 types: false,
964 features: Default::default(),
965 };
966 let context = ExplainContext {
967 env,
968 config,
969 frontiers,
970 };
971
972 expr.explain(&format, &context)
973 }
974
975 #[mz_ore::test]
976 fn test_mutable_context() {
977 let mut env = Environment::default();
978 let frontiers = Frontiers::<u64>::new(3, 7);
979
980 let act = do_explain(&mut env, frontiers);
981 let exp = "expr = 1 + 2\nat t ∊ [3, 7)\nenv = test env\n".to_string();
982
983 assert_ok!(act);
984 assert_eq!(act.unwrap(), exp);
985 }
986}