mz_expr/explain/
json.rs

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