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.
910//! `EXPLAIN AS JSON` support for structures defined in this crate.
1112use mz_repr::explain::json::DisplayJson;
1314use crate::explain::{ExplainMultiPlan, ExplainSinglePlan, ExplainSource, PushdownInfo};
1516impl<'a, T: 'a> DisplayJson for ExplainSinglePlan<'a, T>
17where
18T: serde::Serialize,
19{
20fn to_serde_value(&self) -> serde_json::Result<serde_json::Value> {
21 serde_json::to_value(self.plan.plan)
22 }
23}
2425impl<'a, T: 'a> DisplayJson for ExplainMultiPlan<'a, T>
26where
27T: serde::Serialize,
28{
29fn to_serde_value(&self) -> serde_json::Result<serde_json::Value> {
30let plans = self
31.plans
32 .iter()
33 .map(|(id, plan)| {
34// TODO: fix plans with Constants
35serde_json::json!({
36"id": id,
37"plan": &plan.plan
38 })
39 })
40 .collect::<Vec<_>>();
4142let sources = self
43.sources
44 .iter()
45 .map(
46 |ExplainSource {
47 id,
48 op,
49 pushdown_info,
50 }| {
51let mut json = serde_json::json!({
52"id": id,
53"op": op,
54 });
5556if let Some(PushdownInfo { pushdown }) = pushdown_info {
57let object = json.as_object_mut().unwrap();
58 object.insert("pushdown".to_owned(), serde_json::json!(pushdown));
59 }
6061 json
62 },
63 )
64 .collect::<Vec<_>>();
6566let result = serde_json::json!({ "plans": plans, "sources": sources });
6768Ok(result)
69 }
70}