quill_sql/plan/logical_planner/
plan_explain.rs

1use std::sync::Arc;
2
3use crate::catalog::{Column, DataType, Schema};
4use crate::error::QuillSQLResult;
5use crate::plan::logical_plan::{LogicalPlan, Values};
6use crate::plan::LogicalPlanner;
7use crate::utils::util::pretty_format_logical_plan;
8
9impl LogicalPlanner<'_> {
10    /// Build a plan that returns the formatted logical plan as rows of text.
11    pub fn plan_explain(
12        &mut self,
13        statement: &sqlparser::ast::Statement,
14    ) -> QuillSQLResult<LogicalPlan> {
15        let inner_plan = self.plan(statement)?;
16        let text = pretty_format_logical_plan(&inner_plan);
17        let lines: Vec<Vec<crate::expression::Expr>> = text
18            .lines()
19            .map(|s| {
20                vec![crate::expression::Expr::Literal(
21                    crate::expression::Literal {
22                        value: s.to_string().into(),
23                    },
24                )]
25            })
26            .collect();
27
28        let schema = Arc::new(Schema::new(vec![Column::new(
29            "plan",
30            DataType::Varchar(None),
31            false,
32        )]));
33        Ok(LogicalPlan::Values(Values {
34            schema,
35            values: lines,
36        }))
37    }
38}