use std::borrow::Cow;
use substrait::proto::Plan;
use substrait_explain::extensions::ExtensionRegistry;
use substrait_explain::parser::Parser;
use substrait_explain::textify::foundation::ErrorList;
use substrait_explain::textify::plan::PlanWriter;
use substrait_explain::textify::{ErrorQueue, OutputOptions, Visibility};
fn print_with_errors(plan: &Plan, options: Option<&OutputOptions>) -> Result<(), ErrorList> {
let options = match options {
Some(options) => Cow::Borrowed(options),
None => Cow::Owned(OutputOptions::default()),
};
let registry = ExtensionRegistry::default();
let (formatter, errors) = PlanWriter::<ErrorQueue>::new(&options, plan, ®istry);
println!("{formatter}");
errors.errs()
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let plan_text = r#"
=== Extensions
URNs:
@ 1: https://github.com/substrait-io/substrait/blob/main/extensions/functions_arithmetic.yaml
Functions:
# 10 @ 1: gt
# 11 @ 1: multiply
=== Plan
Root[revenue]
Filter[gt($2, 100) => $0, $1]
Project[$0, $1, multiply($0, $1)]
Read[orders => quantity:i32?, price:fp64?]
"#;
let plan = Parser::parse(plan_text)?;
println!("Plan Structure (YAML):");
match serde_yaml::to_string(&plan) {
Ok(yaml) => println!("{yaml}"),
Err(e) => println!("Error formatting YAML: {e}"),
}
println!();
println!("Standard Output:");
print_with_errors(&plan, None)?;
println!("Verbose Output:");
print_with_errors(&plan, Some(&OutputOptions::verbose()))?;
let custom_options = OutputOptions {
literal_types: Visibility::Always, indent: " ".to_string(), ..OutputOptions::default()
};
println!("Custom Output (4-space indent, always show types):");
print_with_errors(&plan, Some(&custom_options))?;
Ok(())
}