datafusion icon indicating copy to clipboard operation
datafusion copied to clipboard

Unparse TableScan with pushdown projection

Open goldmedal opened this issue 1 year ago • 1 comments

Is your feature request related to a problem or challenge?

Consider the following case:

    let ctx = SessionContext::new();
    ctx.sql("create table t1 (a int, b int)").await?;
    ctx.sql("insert into t1 values (1, 2), (3, 4)").await?;

    let plan = ctx.sql(r#"select s1.a from (select s1.a, s1.b from (select a, b from t1) s1) s1"#).await?.into_optimized_plan()?;
    println!("optimized plan: {plan}");
    let unparse = plan_to_sql(&plan)?.to_string();
    println!("{}", format!("Unparsed SQL: {}", unparse));

The output result is

Optimized plan: SubqueryAlias: s1
  SubqueryAlias: s1
    TableScan: t1 projection=[a]
Unparsed SQL: SELECT * FROM t1 AS s1

The unparsing isn't equal to the original SQL. I think it should be

select s1.a from t1 as s1

Describe the solution you'd like

Handle the pushdown projection at Unparser:select_to_sql_recursively https://github.com/apache/datafusion/blob/23ccca9cd9bdf1791985f2b4abb6d5d8a4fc5ece/datafusion/sql/src/unparser/plan.rs#L240-L243

Describe alternatives you've considered

No response

Additional context

No response

goldmedal avatar Aug 25 '24 08:08 goldmedal

take

goldmedal avatar Aug 25 '24 08:08 goldmedal