qstommyshu commented on issue #5221: URL: https://github.com/apache/datafusion/issues/5221#issuecomment-2805686085
Hi @alamb , I've done some initial investigation into this issue, and I believe a good way to tackle it is by modifying the parsed SQL `Statement` before generating the `LogicalPlan`. Before we build the logical plan, the SQL parser parses the input SQL into a `Statement`. Here's an example structure: ```Rust Statement( Query( Query { with: None, body: Select( Select { select_token: TokenWithSpan { token: Word( Word { value: "select", quote_style: None, keyword: SELECT, }, ), span: Span(Location(1,1)..Location(1,7)), }, distinct: None, top: None, top_before_distinct: false, projection: [ UnnamedExpr( Cast { kind: Cast, expr: Value( ValueWithSpan { value: SingleQuotedString( "1", ), span: Span(Location(1,13)..Location(1,16)), }, ), data_type: Int( None, ), format: None, }, ), ], into: None, from: [], ...... }, ), ), ] ``` What we really care about here are the `projection` and `from` fields. - If projection contains an `UnnamedExpr::Cast`, and - from is empty (i.e., the query is not selecting from a table, hence there's no schema context to use as output column name), then the LogicalPlanBuilder will fallback to using the inner expression (i.e. `SingleQuotedString("1")` in the above statement) as the column name. This leads to names like `Utf8("1")` showing up in the output, which is confusing. If we detect the above pattern — casting a value without a `FROM` clause — we can modify the `UnnamedExpr::Cast` into an `Expr::Alias`, and use a friendlier alias name such as: `CAST('1' AS INT)`. This change is scoped specifically to queries without a `FROM` clause and with simple literal casts, so it should not affect common table-based queries. I've also explored modifying the `LogicalPlan` directly, but modifying the `Statement` feels more natural for this case. Would love to hear your thoughts on this approach — am I on the right track? Did I miss any cases? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org