@@ -22,7 +22,7 @@ use sqlparser_derive::{Visit, VisitMut};
22
22
23
23
use crate :: ast:: { display_comma_separated, ObjectName , StructField } ;
24
24
25
- use super :: value:: escape_single_quote_string;
25
+ use super :: { value:: escape_single_quote_string, ColumnDef } ;
26
26
27
27
/// SQL data types
28
28
#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
@@ -129,10 +129,39 @@ pub enum DataType {
129
129
///
130
130
/// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
131
131
Int4 ( Option < u64 > ) ,
132
- /// Integer type in [bigquery]
132
+ /// Int8 as alias for Bigint in [postgresql] and integer type in [clickhouse]
133
+ /// Note: Int8 mean 8 bytes in [postgresql] (not 8 bits)
134
+ /// Int8 with optional display width e.g. INT8 or INT8(11)
135
+ /// Note: Int8 mean 8 bits in [clickhouse]
136
+ ///
137
+ /// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
138
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
139
+ Int8 ( Option < u64 > ) ,
140
+ /// Integer type in [clickhouse]
141
+ /// Note: Int16 mean 16 bits in [clickhouse]
142
+ ///
143
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
144
+ Int16 ,
145
+ /// Integer type in [clickhouse]
146
+ /// Note: Int16 mean 32 bits in [clickhouse]
147
+ ///
148
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
149
+ Int32 ,
150
+ /// Integer type in [bigquery], [clickhouse]
133
151
///
134
152
/// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#integer_types
153
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
135
154
Int64 ,
155
+ /// Integer type in [clickhouse]
156
+ /// Note: Int128 mean 128 bits in [clickhouse]
157
+ ///
158
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
159
+ Int128 ,
160
+ /// Integer type in [clickhouse]
161
+ /// Note: Int256 mean 256 bits in [clickhouse]
162
+ ///
163
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
164
+ Int256 ,
136
165
/// Integer with optional display width e.g. INTEGER or INTEGER(11)
137
166
Integer ( Option < u64 > ) ,
138
167
/// Unsigned int with optional display width e.g. INT UNSIGNED or INT(11) UNSIGNED
@@ -141,25 +170,54 @@ pub enum DataType {
141
170
UnsignedInt4 ( Option < u64 > ) ,
142
171
/// Unsigned integer with optional display width e.g. INTGER UNSIGNED or INTEGER(11) UNSIGNED
143
172
UnsignedInteger ( Option < u64 > ) ,
173
+ /// Unsigned integer type in [clickhouse]
174
+ /// Note: UInt8 mean 8 bits in [clickhouse]
175
+ ///
176
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
177
+ UInt8 ,
178
+ /// Unsigned integer type in [clickhouse]
179
+ /// Note: UInt16 mean 16 bits in [clickhouse]
180
+ ///
181
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
182
+ UInt16 ,
183
+ /// Unsigned integer type in [clickhouse]
184
+ /// Note: UInt32 mean 32 bits in [clickhouse]
185
+ ///
186
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
187
+ UInt32 ,
188
+ /// Unsigned integer type in [clickhouse]
189
+ /// Note: UInt64 mean 64 bits in [clickhouse]
190
+ ///
191
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
192
+ UInt64 ,
193
+ /// Unsigned integer type in [clickhouse]
194
+ /// Note: UInt128 mean 128 bits in [clickhouse]
195
+ ///
196
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
197
+ UInt128 ,
198
+ /// Unsigned integer type in [clickhouse]
199
+ /// Note: UInt256 mean 256 bits in [clickhouse]
200
+ ///
201
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
202
+ UInt256 ,
144
203
/// Big integer with optional display width e.g. BIGINT or BIGINT(20)
145
204
BigInt ( Option < u64 > ) ,
146
205
/// Unsigned big integer with optional display width e.g. BIGINT UNSIGNED or BIGINT(20) UNSIGNED
147
206
UnsignedBigInt ( Option < u64 > ) ,
148
- /// Int8 as alias for Bigint in [postgresql]
149
- /// Note: Int8 mean 8 bytes in postgres (not 8 bits)
150
- /// Int8 with optional display width e.g. INT8 or INT8(11)
151
- ///
152
- /// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
153
- Int8 ( Option < u64 > ) ,
154
207
/// Unsigned Int8 with optional display width e.g. INT8 UNSIGNED or INT8(11) UNSIGNED
155
208
UnsignedInt8 ( Option < u64 > ) ,
156
209
/// Float4 as alias for Real in [postgresql]
157
210
///
158
211
/// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
159
212
Float4 ,
213
+ /// Floating point in [clickhouse]
214
+ ///
215
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/float
216
+ Float32 ,
160
217
/// Floating point in [bigquery]
161
218
///
162
219
/// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#floating_point_types
220
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/float
163
221
Float64 ,
164
222
/// Floating point e.g. REAL
165
223
Real ,
@@ -182,6 +240,10 @@ pub enum DataType {
182
240
Boolean ,
183
241
/// Date
184
242
Date ,
243
+ /// Date32 with the same range as Datetime64
244
+ ///
245
+ /// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/date32
246
+ Date32 ,
185
247
/// Time with optional time precision and time zone information e.g. [standard][1].
186
248
///
187
249
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
@@ -190,6 +252,10 @@ pub enum DataType {
190
252
///
191
253
/// [1]: https://dev.mysql.com/doc/refman/8.0/en/datetime.html
192
254
Datetime ( Option < u64 > ) ,
255
+ /// Datetime with time precision and optional timezone e.g. [ClickHouse][1].
256
+ ///
257
+ /// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/datetime64
258
+ Datetime64 ( u64 , Option < String > ) ,
193
259
/// Timestamp with optional time precision and time zone information e.g. [standard][1].
194
260
///
195
261
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#datetime-type
@@ -206,12 +272,28 @@ pub enum DataType {
206
272
Text ,
207
273
/// String with optional length.
208
274
String ( Option < u64 > ) ,
275
+ /// A fixed-length string e.g [ClickHouse][1].
276
+ ///
277
+ /// [1]: https://clickhouse.com/docs/en/sql-reference/data-types/fixedstring
278
+ FixedString ( u64 ) ,
209
279
/// Bytea
210
280
Bytea ,
211
281
/// Custom type such as enums
212
282
Custom ( ObjectName , Vec < String > ) ,
213
283
/// Arrays
214
284
Array ( ArrayElemTypeDef ) ,
285
+ /// Map
286
+ ///
287
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/map
288
+ Map ( Box < DataType > , Box < DataType > ) ,
289
+ /// Tuple
290
+ ///
291
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/tuple
292
+ Tuple ( Vec < StructField > ) ,
293
+ /// Nested
294
+ ///
295
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/nested-data-structures/nested
296
+ Nested ( Vec < ColumnDef > ) ,
215
297
/// Enums
216
298
Enum ( Vec < String > ) ,
217
299
/// Set
@@ -221,6 +303,14 @@ pub enum DataType {
221
303
/// [hive]: https://docs.cloudera.com/cdw-runtime/cloud/impala-sql-reference/topics/impala-struct.html
222
304
/// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
223
305
Struct ( Vec < StructField > ) ,
306
+ /// Nullable - special marker NULL represents in ClickHouse as a data type.
307
+ ///
308
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/nullable
309
+ Nullable ( Box < DataType > ) ,
310
+ /// LowCardinality - changes the internal representation of other data types to be dictionary-encoded.
311
+ ///
312
+ /// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/lowcardinality
313
+ LowCardinality ( Box < DataType > ) ,
224
314
/// No type specified - only used with
225
315
/// [`SQLiteDialect`](crate::dialect::SQLiteDialect), from statements such
226
316
/// as `CREATE TABLE t1 (a)`.
@@ -296,9 +386,24 @@ impl fmt::Display for DataType {
296
386
DataType :: Int4 ( zerofill) => {
297
387
format_type_with_optional_length ( f, "INT4" , zerofill, false )
298
388
}
389
+ DataType :: Int8 ( zerofill) => {
390
+ format_type_with_optional_length ( f, "INT8" , zerofill, false )
391
+ }
392
+ DataType :: Int16 => {
393
+ write ! ( f, "Int16" )
394
+ }
395
+ DataType :: Int32 => {
396
+ write ! ( f, "Int32" )
397
+ }
299
398
DataType :: Int64 => {
300
399
write ! ( f, "INT64" )
301
400
}
401
+ DataType :: Int128 => {
402
+ write ! ( f, "Int128" )
403
+ }
404
+ DataType :: Int256 => {
405
+ write ! ( f, "Int256" )
406
+ }
302
407
DataType :: UnsignedInt4 ( zerofill) => {
303
408
format_type_with_optional_length ( f, "INT4" , zerofill, true )
304
409
}
@@ -314,21 +419,38 @@ impl fmt::Display for DataType {
314
419
DataType :: UnsignedBigInt ( zerofill) => {
315
420
format_type_with_optional_length ( f, "BIGINT" , zerofill, true )
316
421
}
317
- DataType :: Int8 ( zerofill) => {
318
- format_type_with_optional_length ( f, "INT8" , zerofill, false )
319
- }
320
422
DataType :: UnsignedInt8 ( zerofill) => {
321
423
format_type_with_optional_length ( f, "INT8" , zerofill, true )
322
424
}
425
+ DataType :: UInt8 => {
426
+ write ! ( f, "UInt8" )
427
+ }
428
+ DataType :: UInt16 => {
429
+ write ! ( f, "UInt16" )
430
+ }
431
+ DataType :: UInt32 => {
432
+ write ! ( f, "UInt32" )
433
+ }
434
+ DataType :: UInt64 => {
435
+ write ! ( f, "UInt64" )
436
+ }
437
+ DataType :: UInt128 => {
438
+ write ! ( f, "UInt128" )
439
+ }
440
+ DataType :: UInt256 => {
441
+ write ! ( f, "UInt256" )
442
+ }
323
443
DataType :: Real => write ! ( f, "REAL" ) ,
324
444
DataType :: Float4 => write ! ( f, "FLOAT4" ) ,
445
+ DataType :: Float32 => write ! ( f, "Float32" ) ,
325
446
DataType :: Float64 => write ! ( f, "FLOAT64" ) ,
326
447
DataType :: Double => write ! ( f, "DOUBLE" ) ,
327
448
DataType :: Float8 => write ! ( f, "FLOAT8" ) ,
328
449
DataType :: DoublePrecision => write ! ( f, "DOUBLE PRECISION" ) ,
329
450
DataType :: Bool => write ! ( f, "BOOL" ) ,
330
451
DataType :: Boolean => write ! ( f, "BOOLEAN" ) ,
331
452
DataType :: Date => write ! ( f, "DATE" ) ,
453
+ DataType :: Date32 => write ! ( f, "Date32" ) ,
332
454
DataType :: Time ( precision, timezone_info) => {
333
455
format_datetime_precision_and_tz ( f, "TIME" , precision, timezone_info)
334
456
}
@@ -338,6 +460,14 @@ impl fmt::Display for DataType {
338
460
DataType :: Timestamp ( precision, timezone_info) => {
339
461
format_datetime_precision_and_tz ( f, "TIMESTAMP" , precision, timezone_info)
340
462
}
463
+ DataType :: Datetime64 ( precision, timezone) => {
464
+ format_clickhouse_datetime_precision_and_timezone (
465
+ f,
466
+ "DateTime64" ,
467
+ precision,
468
+ timezone,
469
+ )
470
+ }
341
471
DataType :: Interval => write ! ( f, "INTERVAL" ) ,
342
472
DataType :: JSON => write ! ( f, "JSON" ) ,
343
473
DataType :: JSONB => write ! ( f, "JSONB" ) ,
@@ -350,6 +480,7 @@ impl fmt::Display for DataType {
350
480
ArrayElemTypeDef :: SquareBracket ( t, None ) => write ! ( f, "{t}[]" ) ,
351
481
ArrayElemTypeDef :: SquareBracket ( t, Some ( size) ) => write ! ( f, "{t}[{size}]" ) ,
352
482
ArrayElemTypeDef :: AngleBracket ( t) => write ! ( f, "ARRAY<{t}>" ) ,
483
+ ArrayElemTypeDef :: Parenthesis ( t) => write ! ( f, "Array({t})" ) ,
353
484
} ,
354
485
DataType :: Custom ( ty, modifiers) => {
355
486
if modifiers. is_empty ( ) {
@@ -385,6 +516,25 @@ impl fmt::Display for DataType {
385
516
write ! ( f, "STRUCT" )
386
517
}
387
518
}
519
+ // ClickHouse
520
+ DataType :: Nullable ( data_type) => {
521
+ write ! ( f, "Nullable({})" , data_type)
522
+ }
523
+ DataType :: FixedString ( character_length) => {
524
+ write ! ( f, "FixedString({})" , character_length)
525
+ }
526
+ DataType :: LowCardinality ( data_type) => {
527
+ write ! ( f, "LowCardinality({})" , data_type)
528
+ }
529
+ DataType :: Map ( key_data_type, value_data_type) => {
530
+ write ! ( f, "Map({}, {})" , key_data_type, value_data_type)
531
+ }
532
+ DataType :: Tuple ( fields) => {
533
+ write ! ( f, "Tuple({})" , display_comma_separated( fields) )
534
+ }
535
+ DataType :: Nested ( fields) => {
536
+ write ! ( f, "Nested({})" , display_comma_separated( fields) )
537
+ }
388
538
DataType :: Unspecified => Ok ( ( ) ) ,
389
539
}
390
540
}
@@ -439,6 +589,23 @@ fn format_datetime_precision_and_tz(
439
589
Ok ( ( ) )
440
590
}
441
591
592
+ fn format_clickhouse_datetime_precision_and_timezone (
593
+ f : & mut fmt:: Formatter ,
594
+ sql_type : & ' static str ,
595
+ len : & u64 ,
596
+ time_zone : & Option < String > ,
597
+ ) -> fmt:: Result {
598
+ write ! ( f, "{sql_type}({len}" ) ?;
599
+
600
+ if let Some ( time_zone) = time_zone {
601
+ write ! ( f, ", '{time_zone}'" ) ?;
602
+ }
603
+
604
+ write ! ( f, ")" ) ?;
605
+
606
+ Ok ( ( ) )
607
+ }
608
+
442
609
/// Timestamp and Time data types information about TimeZone formatting.
443
610
///
444
611
/// This is more related to a display information than real differences between each variant. To
@@ -593,4 +760,6 @@ pub enum ArrayElemTypeDef {
593
760
AngleBracket ( Box < DataType > ) ,
594
761
/// `INT[]` or `INT[2]`
595
762
SquareBracket ( Box < DataType > , Option < u64 > ) ,
763
+ /// `Array(Int64)`
764
+ Parenthesis ( Box < DataType > ) ,
596
765
}
0 commit comments