feat: Add dialect-aware SQL serialization with ToSql trait for ClickHouse PascalCase types#2154
Draft
callicles wants to merge 2 commits intoapache:mainfrom
Draft
feat: Add dialect-aware SQL serialization with ToSql trait for ClickHouse PascalCase types#2154callicles wants to merge 2 commits intoapache:mainfrom
ToSql trait for ClickHouse PascalCase types#2154callicles wants to merge 2 commits intoapache:mainfrom
Conversation
Contributor
|
Hi @callicles! I don't think this approach will be feasible unfortunately, since it's essentially duplicating all display logic |
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.
feat: Add dialect-aware SQL serialization with
ToSqltrait for ClickHouse PascalCase typesSummary
This PR introduces a new
ToSqltrait that enables dialect-aware SQL serialization, specifically addressing ClickHouse's requirement for PascalCase type names (e.g.,String,Int64,Nullable(String)) instead of the standard uppercase convention (STRING,INT64).Related Issue: #2153
Problem
ClickHouse is case-sensitive for data type names and requires PascalCase. When using
sqlparser-rsto parse and regenerate SQL:The regenerated SQL fails with
UNKNOWN_TYPEerrors when executed against ClickHouse becauseSTRINGis not recognized—onlyStringis valid.Solution
Introduce a
ToSqltrait that accepts dialect context:Usage:
Design Decisions
Coexistence with
Display: The existingDisplaytrait continues to work unchanged (uppercase types).ToSqlis opt-in for users who need dialect-aware formatting.impl_to_sql_display!macro: Types withoutDataTypefields delegate to theirDisplayimplementation via macro, avoiding code duplication.Recursive propagation: Types containing
DataTypefields implementwrite_sqlexplicitly, callingwrite_sqlon nested types to propagate dialect context through the AST.requires_pascalcase_types()dialect method: A new method on theDialecttrait that returnstruefor ClickHouse.Changes
src/ast/to_sql.rsToSqltrait,impl_to_sql_display!macro, and helperssrc/ast/data_type.rsToSqlimpl forDataTypewith PascalCase formattingsrc/ast/mod.rsToSqlimpls forExpr,Statement,Function, window types, etc.src/ast/ddl.rsToSqlimpls forCreateTable,AlterTable,ColumnDef, etc.src/ast/query.rsToSqlimpls forQuery,Select,TableFactor, etc.src/dialect/mod.rsrequires_pascalcase_types()toDialecttraitsrc/dialect/clickhouse.rsrequires_pascalcase_types()to returntruetests/sqlparser_clickhouse.rsCoverage
ToSqlis implemented for all major AST types users are likely to serialize:Statement,Query,Select,CreateTable,AlterTable,CreateView,CreateFunction,Declare,PrepareExpr(all variants includingCast,Case,BinaryOp,Function, etc.)DataType,ColumnDef,ViewColumnDef,StructField,UnionFieldTableFactor,Join,OrderByExpr,WindowSpec,Cte,WithFunction,FunctionArg,FunctionArguments, window functionsTesting
Added 8 new tests covering:
DataTypePascalCase formattingCREATE TABLEwith nested types (Nullable,Array,Map)ALTER TABLEoperationsSELECTwithCASTexpressionsCASE, nestedCAST)CREATE VIEWstatementsto_sql→ parse → verify equivalenceMigration Guide
If you currently use
Displayand need dialect-aware formatting:Breaking Changes
None. This is a purely additive change. Existing code using
Displaycontinues to work unchanged.Disclaimer: This was worked on with AI