feat: add custom_string_literal_override to unparser Dialect trait#20590
Merged
alamb merged 5 commits intoapache:mainfrom Mar 16, 2026
Merged
feat: add custom_string_literal_override to unparser Dialect trait#20590alamb merged 5 commits intoapache:mainfrom
custom_string_literal_override to unparser Dialect trait#20590alamb merged 5 commits intoapache:mainfrom
Conversation
kosiew
reviewed
Mar 12, 2026
Contributor
kosiew
left a comment
There was a problem hiding this comment.
@goldmedal
Thanks for working on this.
| /// | ||
| /// For example, MSSQL requires non-ASCII strings to use national string | ||
| /// literal syntax (`N'datafusion資料融合'`). | ||
| fn custom_string_literal_override(&self, _s: &str) -> Option<ast::Expr> { |
Contributor
There was a problem hiding this comment.
Since this only affects scalar UTF8 literal unparsing today, a narrower name or a helper scoped to scalar string literals may make the API easier to understand and extend. Perhaps string_literal_to_sql – to keep the focus on literals, not “any scalar”?
datafusion/sql/src/unparser/expr.rs
Outdated
| let unparser = Unparser::new(&dialect); | ||
|
|
||
| let expr = | ||
| Expr::Literal(ScalarValue::Utf8(Some("national string".to_string())), None); |
Contributor
There was a problem hiding this comment.
The regression test currently exercises ScalarValue::Utf8, but the change also covers Utf8View and LargeUtf8.
Adding tests for each would make the coverage line up with the implementation.
Contributor
Author
|
Thanks @kosiew for reviewing. All the comments has been addressed. |
Contributor
|
Thanks -- looks good to me @kosiew and @goldmedal |
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
When unparsing queries targeting databases like MSSQL, non-ASCII string literals need special handling. MSSQL requires the
N'...'(national string literal) prefix for strings containing Unicode characters. Currently the unparser always emits single-quoted strings with no way for dialects to customize this behavior.What changes are included in this PR?
custom_string_literal_overridemethod to theDialecttrait with a default implementation returningNone(no override).Utf8,Utf8View, andLargeUtf8match arms inscalar_value_to_sqland route them through the new dialect hook.Are these changes tested?
Yes. A test-only
MsSqlDialectis defined in the test module to verify:Nprefix)N'...')Nprefix regardless of content)It's used by Wren AI in production for a while: Canner#8
Are there any user-facing changes?
Yes. The
Dialecttrait gains a new methodcustom_string_literal_override. This is a non-breaking change since the method has a default implementation. Dialect implementors can override it to customize string literal unparsing.