Skip to content

Conversation

goldmedal
Copy link
Contributor

@goldmedal goldmedal commented Jun 24, 2025

Which issue does this PR close?

No corresponding issue at DataFusion. I encountered it in the Wren AI case.

Rationale for this change

Consider the following SQL:

select count(*) from (select 1)

It will be simplified to the following SQL after we move CountWildcardRule to the logical planner #14689

SELECT count(1) AS \"count(*)\" FROM (SELECT 1)

However, some dialects (e.g. BigQuery) don't allow some special characters((, *, @, ...) in an alias name. We should have a way to handle this case for the unparser.

What changes are included in this PR?

  • Introduce the interface col_alias_overrides for the unparser dialect.
    /// Allows the dialect to override column alias unparsing if the dialect has specific rules.
    /// Returns None if the default unparsing should be used, or Some(String) if there is
    /// a custom implementation for the alias.
    fn col_alias_overrides(&self, _alias: &str) -> Result<Option<String>> {
        Ok(None)
    }
  • Add BigQueryDialect, which will encode the special character in the alias name.

Are these changes tested?

Are there any user-facing changes?

new method for the unparser dialect.

@github-actions github-actions bot added the sql SQL Planner label Jun 24, 2025
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me


#[test]
fn roundtrip_statement_with_dialect_special_char_alias() -> Result<(), DataFusionError> {
roundtrip_statement_with_dialect_helper!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might also help to add a test case here with unparser_dialect GenericDialect too to show the difference in dialects

@alamb alamb merged commit 6f2747f into apache:main Jun 25, 2025
27 checks passed
@alamb
Copy link
Contributor

alamb commented Jun 25, 2025

🚀

@goldmedal goldmedal deleted the feat/bigquery-alias branch June 26, 2025 00:30
@goldmedal
Copy link
Contributor Author

Thanks @alamb 👍

goldmedal added a commit to Canner/datafusion that referenced this pull request Jul 4, 2025
…pache#16540)

* allow override col alias for specific dialect

* improve test case

* add generic dialect case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants