Function datafusion::sql::unparser::plan_to_sql

source ·
pub fn plan_to_sql(plan: &LogicalPlan) -> Result<Statement, DataFusionError>
Expand description

Convert a DataFusion LogicalPlan to sqlparser::ast::Statement

This function is the opposite of SqlToRel::sql_statement_to_plan and can be used to, among other things, convert LogicalPlans to strings.

§Example

use arrow::datatypes::{DataType, Field, Schema};
use datafusion_expr::{col, logical_plan::table_scan};
use datafusion_sql::unparser::plan_to_sql;
let schema = Schema::new(vec![
    Field::new("id", DataType::Utf8, false),
    Field::new("value", DataType::Utf8, false),
]);
let plan = table_scan(Some("table"), &schema, None)
    .unwrap()
    .project(vec![col("id"), col("value")])
    .unwrap()
    .build()
    .unwrap();
let sql = plan_to_sql(&plan).unwrap();

assert_eq!(format!("{}", sql), "SELECT \"table\".\"id\", \"table\".\"value\" FROM \"table\"")