ClickHouse: `Display` implementation converts types to uppercase, causing `UNKNOWN_TYPE` errors
Maintainer thường phản hồi trong vòng 2 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Bắt đầu bằng cách kiểm tra triển khai DataType Display và các kiểu AST được liệt kê trong issue, bao gồm Statement, Query, Expr, ColumnDef và các node liên quan. Theo dõi cách quá trình serialization tiếp cận các kiểu dữ liệu lồng nhau, sau đó đánh giá API to_sql có nhận biết dialect được đề xuất trên toàn bộ hierarchy. Công việc được xem là hoàn tất khi ví dụ ClickHouse round-trip với các kiểu PascalCase và hành vi Display hiện có vẫn tương thích ngược.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Problem
ClickHouse data types are case-sensitive and require PascalCase (e.g., String, Int32, Nullable). However, the sqlparser-rs library's Display implementation for DataType converts certain types to uppercase, causing UNKNOWN_TYPE errors when round-tripping SQL through ClickHouse.
Example
use sqlparser::dialect::ClickHouseDialect;
use sqlparser::parser::Parser;
let sql = "CREATE TABLE t (col Nullable(String))";
let dialect = ClickHouseDialect {};
let ast = Parser::parse_sql(&dialect, sql).unwrap();
// Round-trip: parse and convert back to string
let regenerated = ast[0].to_string();
// Result: "CREATE TABLE t (col Nullable(STRING))"
// ^^^^^^ uppercase!
When this regenerated SQL is executed against ClickHouse, it fails with:
Code: 47. DB::Exception: Unknown type STRING. (UNKNOWN_TYPE)
Affected Types
| Type | Current Output | ClickHouse Requires |
|---|---|---|
DataType::Int8 |
INT8 |
Int8 |
DataType::Int64 |
INT64 |
Int64 |
DataType::Float64 |
FLOAT64 |
Float64 |
DataType::String |
STRING |
String |
DataType::Bool |
BOOL |
Bool |
DataType::Date |
DATE |
Date |
DataType::Datetime |
DATETIME |
DateTime |
Types already correct (PascalCase):
Int16,Int32,Int128,Int256UInt8,UInt16,UInt32,UInt64,UInt128,UInt256Float32Nullable,LowCardinality,Array,Map,Tuple,Nested
Root Cause
The Display trait implementation for DataType uses uppercase for type names (e.g., write!(f, "STRING")), which is standard for most SQL dialects but incorrect for ClickHouse.
The challenge is that Display doesn't have access to dialect context, so it can't conditionally format based on the active dialect.
The Problem with Display
Most users serialize SQL by calling Display on top-level AST types:
let ast = Parser::parse_sql(&dialect, sql).unwrap();
let regenerated = ast[0].to_string(); // Uses Display on Statement
// or
let regenerated = format!("{}", query); // Uses Display on Query
The Display trait signature doesn't allow passing context:
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
Proposed Solution
To solve this, we need dialect-aware serialization throughout the AST hierarchy:
Add to_sql(&dyn Dialect) to all AST types
- Add
to_sql(&dyn Dialect) -> Stringmethod toStatement,Query,Expr,ColumnDef, and other AST types - Each type's implementation calls
to_sql()on its children, propagating the dialect - Keep existing
Displayimplementations unchanged for backwards compatibility
// Example usage after fix:
let ast = Parser::parse_sql(&dialect, sql).unwrap();
let regenerated = ast[0].to_sql(&dialect); // Correct PascalCase for ClickHouse
Implementation Scope
The affected types include (non-exhaustive):
Statement(top-level)Query,SetExpr,SelectExpr(especiallyCast,TryCast,SafeCast)ColumnDef,ColumnOptionTableConstraintAlterTableOperationFunctionArg,FunctionArgExpr
This is a significant change but provides the cleanest API and maintains backwards compatibility.
Workarounds
Currently, users must post-process the SQL string output to fix casing. See 514-labs/moosestack#3152 for an example regex-based workaround.
References
- ClickHouse Data Types Documentation
- Related workaround: 514-labs/moosestack#3152
- Ngôn ngữ chính
- Rust
- Star
- 3.5k
- Fork
- 780
- Merge trung bình
- 4 ngày 10 giờ
- Pull request đã merge (30 ngày)
- 60
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Không có hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của apache/datafusion-sqlparser-rs
-
ClickHouse
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
apache/datafusion-sqlparser-rs#2599 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
ClickHouse
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
apache/datafusion-sqlparser-rs#2597 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Hive
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
apache/datafusion-sqlparser-rs#2589 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
apache/datafusion-sqlparser-rs#2461 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 73/100
apache/datafusion-sqlparser-rs#2407 ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của apache/datafusion-sqlparser-rs
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
canonical/opentelemetry-collector-operator#409 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
codegen: memref.collapse_shape in an mlir! block leaves an affine.apply that is never loweredĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug CLI exec tool-calls
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày