Skip to content

Commit 076c734

Browse files
committed
repr: split explain items in text/json/dot submodules
1 parent 78f904a commit 076c734

File tree

11 files changed

+442
-399
lines changed

11 files changed

+442
-399
lines changed

src/adapter/src/coord/peek.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ use mz_ore::cast::CastFrom;
3232
use mz_ore::str::Indent;
3333
use mz_ore::str::StrExt;
3434
use mz_ore::tracing::OpenTelemetryContext;
35-
use mz_repr::explain::{
36-
fmt_text_constant_rows, separated_text, CompactScalarSeq, DisplayText, ExprHumanizer, Indices,
37-
};
35+
use mz_repr::explain::text::{fmt_text_constant_rows, separated_text, DisplayText};
36+
use mz_repr::explain::{CompactScalarSeq, ExprHumanizer, Indices};
3837
use mz_repr::{Diff, GlobalId, RelationType, Row};
3938

4039
use crate::client::ConnectionId;
@@ -564,10 +563,9 @@ fn consolidate_constant_updates(rows: Vec<(Row, Diff)>) -> Vec<(Row, Diff)> {
564563
mod tests {
565564
use mz_expr::{func::IsNull, MapFilterProject, UnaryFunc};
566565
use mz_ore::str::Indent;
567-
use mz_repr::{
568-
explain::{text_string_at, DummyHumanizer, RenderingContext},
569-
ColumnType, Datum, ScalarType,
570-
};
566+
use mz_repr::explain::text::text_string_at;
567+
use mz_repr::explain::{DummyHumanizer, RenderingContext};
568+
use mz_repr::{ColumnType, Datum, ScalarType};
571569

572570
use super::*;
573571

src/adapter/src/explain/optimizer_trace.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ use std::fmt::Debug;
1313

1414
use mz_compute_client::{plan::Plan, types::dataflows::DataflowDescription};
1515
use mz_expr::{MirRelationExpr, MirScalarExpr, OptimizedMirRelationExpr, RowSetFinishing};
16-
use mz_repr::explain::{
17-
text_string, DisplayText, Explain, ExplainConfig, ExplainError, ExplainFormat, PlanTrace,
18-
TraceEntry,
19-
};
16+
use mz_repr::explain::text::{text_string, DisplayText};
17+
use mz_repr::explain::tracing::{PlanTrace, TraceEntry};
18+
use mz_repr::explain::{Explain, ExplainConfig, ExplainError, ExplainFormat};
2019
use mz_sql::plan::{HirRelationExpr, HirScalarExpr};
2120
use tracing::dispatcher::{self, with_default};
2221
use tracing_subscriber::prelude::*;

src/compute-client/src/explain/text.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ use std::{collections::BTreeMap, fmt, ops::Deref};
2424

2525
use mz_expr::{Id, MirScalarExpr};
2626
use mz_ore::str::{bracketed, separated, IndentLike, StrExt};
27-
use mz_repr::explain::{
28-
fmt_text_constant_rows, CompactScalarSeq, DisplayText, Indices, PlanRenderingContext,
29-
};
27+
use mz_repr::explain::text::{fmt_text_constant_rows, DisplayText};
28+
use mz_repr::explain::{CompactScalarSeq, Indices, PlanRenderingContext};
3029

3130
use crate::plan::{
3231
join::{

src/expr/src/explain/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
//! `EXPLAIN AS JSON` support for structures defined in this crate.
1111
12-
use mz_repr::explain::DisplayJson;
12+
use mz_repr::explain::json::DisplayJson;
1313

1414
use super::{ExplainMultiPlan, ExplainSinglePlan};
1515

src/expr/src/explain/text.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use std::fmt;
1313

1414
use mz_ore::soft_assert;
1515
use mz_ore::str::{bracketed, separated, Indent, IndentLike, StrExt};
16+
use mz_repr::explain::text::{fmt_text_constant_rows, separated_text, DisplayText};
1617
use mz_repr::explain::{
17-
fmt_text_constant_rows, separated_text, CompactScalarSeq, DisplayText, ExprHumanizer, Indices,
18-
PlanRenderingContext, RenderingContext,
18+
CompactScalarSeq, ExprHumanizer, Indices, PlanRenderingContext, RenderingContext,
1919
};
2020
use mz_repr::{GlobalId, Row};
2121

src/expr/src/relation/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use mz_ore::stack::RecursionLimitError;
2727
use mz_ore::str::Indent;
2828
use mz_proto::{IntoRustIfSome, ProtoType, RustType, TryFromProtoError};
2929
use mz_repr::adt::numeric::NumericMaxScale;
30-
use mz_repr::explain::{text_string_at, DummyHumanizer, ExplainConfig, PlanRenderingContext};
30+
use mz_repr::explain::text::text_string_at;
31+
use mz_repr::explain::{DummyHumanizer, ExplainConfig, PlanRenderingContext};
3132
use mz_repr::{ColumnName, ColumnType, Datum, Diff, GlobalId, RelationType, Row, ScalarType};
3233

3334
use crate::visit::{Visit, VisitChildren};
@@ -2980,7 +2981,7 @@ mod tests {
29802981
use proptest::prelude::*;
29812982

29822983
use mz_proto::protobuf_roundtrip;
2983-
use mz_repr::explain::text_string_at;
2984+
use mz_repr::explain::text::text_string_at;
29842985

29852986
use super::*;
29862987

src/repr/src/explain/dot.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright Materialize, Inc. and contributors. All rights reserved.
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the LICENSE file.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0.
9+
10+
//! Structs and traits for `EXPLAIN AS DOT`.
11+
12+
use super::*;
13+
14+
/// A trait implemented by explanation types that can be rendered as
15+
/// [`ExplainFormat::Dot`].
16+
pub trait DisplayDot<C = ()>
17+
where
18+
Self: Sized,
19+
{
20+
fn fmt_dot(&self, f: &mut fmt::Formatter<'_>, ctx: &mut C) -> fmt::Result;
21+
}
22+
23+
impl<A, C> DisplayDot<C> for Box<A>
24+
where
25+
A: DisplayDot<C>,
26+
{
27+
fn fmt_dot(&self, f: &mut fmt::Formatter<'_>, ctx: &mut C) -> fmt::Result {
28+
self.as_ref().fmt_dot(f, ctx)
29+
}
30+
}
31+
32+
impl<A, C> DisplayDot<C> for Option<A>
33+
where
34+
A: DisplayDot<C>,
35+
{
36+
fn fmt_dot(&self, f: &mut fmt::Formatter<'_>, ctx: &mut C) -> fmt::Result {
37+
if let Some(val) = self {
38+
val.fmt_dot(f, ctx)
39+
} else {
40+
fmt::Result::Ok(())
41+
}
42+
}
43+
}
44+
45+
impl DisplayDot for UnsupportedFormat {
46+
fn fmt_dot(&self, _f: &mut fmt::Formatter<'_>, _ctx: &mut ()) -> fmt::Result {
47+
unreachable!()
48+
}
49+
}
50+
51+
/// Render a type `t: T` as [`ExplainFormat::Dot`].
52+
///
53+
/// # Panics
54+
///
55+
/// Panics if the [`DisplayDot::fmt_dot`] call returns a [`fmt::Error`].
56+
pub fn dot_string<T: DisplayDot<()>>(t: &T) -> String {
57+
struct DotString<'a, T>(&'a T);
58+
59+
impl<'a, T: DisplayDot> fmt::Display for DotString<'a, T> {
60+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61+
self.0.fmt_dot(f, &mut ())
62+
}
63+
}
64+
65+
DotString::<'_>(t).to_string()
66+
}
67+
68+
/// Apply `f: F` to create a rendering context of type `C` and render the given
69+
/// type `t: T` as [`ExplainFormat::Dot`] within that context.
70+
///
71+
/// # Panics
72+
///
73+
/// Panics if the [`DisplayDot::fmt_dot`] call returns a [`fmt::Error`].
74+
pub fn dot_string_at<'a, T: DisplayDot<C>, C, F: Fn() -> C>(t: &'a T, f: F) -> String {
75+
struct DotStringAt<'a, T, C, F: Fn() -> C> {
76+
t: &'a T,
77+
f: F,
78+
}
79+
80+
impl<T: DisplayDot<C>, C, F: Fn() -> C> DisplayDot<()> for DotStringAt<'_, T, C, F> {
81+
fn fmt_dot(&self, f: &mut fmt::Formatter<'_>, _ctx: &mut ()) -> fmt::Result {
82+
let mut ctx = (self.f)();
83+
self.t.fmt_dot(f, &mut ctx)
84+
}
85+
}
86+
87+
dot_string(&DotStringAt { t, f })
88+
}

src/repr/src/explain/json.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright Materialize, Inc. and contributors. All rights reserved.
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the LICENSE file.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0.
9+
10+
//! Structs and traits for `EXPLAIN AS JSON`.
11+
12+
use super::*;
13+
14+
/// A trait implemented by explanation types that can be rendered as
15+
/// [`ExplainFormat::Json`].
16+
pub trait DisplayJson
17+
where
18+
Self: Sized,
19+
{
20+
fn to_serde_value(&self) -> serde_json::Result<serde_json::Value>;
21+
}
22+
23+
/// Render a type `t: T` as [`ExplainFormat::Json`].
24+
///
25+
/// # Panics
26+
///
27+
/// Panics if the [`DisplayJson::to_serde_value`] or the subsequent
28+
/// [`serde_json::to_string_pretty`] call return a [`serde_json::Error`].
29+
pub fn json_string<T: DisplayJson>(t: &T) -> String {
30+
let value = t.to_serde_value().expect("serde_json::Value");
31+
serde_json::to_string_pretty(&value).expect("JSON string")
32+
}
33+
34+
impl DisplayJson for String {
35+
fn to_serde_value(&self) -> serde_json::Result<serde_json::Value> {
36+
Ok(serde_json::Value::String(self.clone()))
37+
}
38+
}
39+
40+
impl DisplayJson for UnsupportedFormat {
41+
fn to_serde_value(&self) -> serde_json::Result<serde_json::Value> {
42+
unreachable!()
43+
}
44+
}

0 commit comments

Comments
 (0)