apache / datafusion

Apache DataFusion SQL Query Engine
https://datafusion.apache.org/
Apache License 2.0
6.15k stars 1.16k forks source link

Unparsing TableScan with an alias and a pushdown filter to SQL string #12368

Closed goldmedal closed 1 month ago

goldmedal commented 1 month ago

Is your feature request related to a problem or challenge?

In https://github.com/apache/datafusion/pull/12158 , we support unparse a TableScan with some pushdown options(e.g. projection, filter, or fetch ...). However, there are some issues with unparsing a TableScan with an alias and a filter. Consider the following case:

    let table_scan_with_filter_alias = table_scan_with_filters(
        Some("t1"),  // projection
        &schema, // schema
        None,  // alias
        vec![col("id").gt(col("age"))],  // filter
    )?.alias("ta")?.build()?;

The implementation of https://github.com/apache/datafusion/pull/12158 will produce a SQL like

SELECT * FROM t1 AS ta WHERE (t1.id > t1.age)

but the SQL should be

SELECT * FROM t1 AS ta WHERE (ta.id > ta.age)

We need also to process the expression of the filter.

Describe the solution you'd like

Rewrite the columns used by the filter. Make their relation prefix be the alias instead of the table name.

Describe alternatives you've considered

No response

Additional context

No response

Lordworms commented 1 month ago

take