@@ -20,7 +20,7 @@ use serde::{Deserialize, Serialize};
20
20
#[ cfg( feature = "visitor" ) ]
21
21
use sqlparser_derive:: { Visit , VisitMut } ;
22
22
23
- use crate :: ast:: ObjectName ;
23
+ use crate :: ast:: { display_comma_separated , ObjectName , StructField } ;
24
24
25
25
use super :: value:: escape_single_quote_string;
26
26
@@ -71,6 +71,10 @@ pub enum DataType {
71
71
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-large-object-string-type
72
72
/// [Oracle]: https://docs.oracle.com/javadb/10.8.3.0/ref/rrefblob.html
73
73
Blob ( Option < u64 > ) ,
74
+ /// Variable-length binary data with optional length.
75
+ ///
76
+ /// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#bytes_type
77
+ Bytes ( Option < u64 > ) ,
74
78
/// Numeric type with optional precision and scale e.g. NUMERIC(10,2), [standard][1]
75
79
///
76
80
/// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type
@@ -125,6 +129,10 @@ pub enum DataType {
125
129
///
126
130
/// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
127
131
Int4 ( Option < u64 > ) ,
132
+ /// Integer type in [bigquery]
133
+ ///
134
+ /// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#integer_types
135
+ Int64 ,
128
136
/// Integer with optional display width e.g. INTEGER or INTEGER(11)
129
137
Integer ( Option < u64 > ) ,
130
138
/// Unsigned int with optional display width e.g. INT UNSIGNED or INT(11) UNSIGNED
@@ -149,6 +157,10 @@ pub enum DataType {
149
157
///
150
158
/// [postgresql]: https://www.postgresql.org/docs/15/datatype.html
151
159
Float4 ,
160
+ /// Floating point in [bigquery]
161
+ ///
162
+ /// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#floating_point_types
163
+ Float64 ,
152
164
/// Floating point e.g. REAL
153
165
Real ,
154
166
/// Float8 as alias for Double in [postgresql]
@@ -190,18 +202,23 @@ pub enum DataType {
190
202
Regclass ,
191
203
/// Text
192
204
Text ,
193
- /// String
194
- String ,
205
+ /// String with optional length.
206
+ String ( Option < u64 > ) ,
195
207
/// Bytea
196
208
Bytea ,
197
209
/// Custom type such as enums
198
210
Custom ( ObjectName , Vec < String > ) ,
199
211
/// Arrays
200
- Array ( Option < Box < DataType > > ) ,
212
+ Array ( ArrayElemTypeDef ) ,
201
213
/// Enums
202
214
Enum ( Vec < String > ) ,
203
215
/// Set
204
216
Set ( Vec < String > ) ,
217
+ /// Struct
218
+ ///
219
+ /// [hive]: https://docs.cloudera.com/cdw-runtime/cloud/impala-sql-reference/topics/impala-struct.html
220
+ /// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#struct_type
221
+ Struct ( Vec < StructField > ) ,
205
222
}
206
223
207
224
impl fmt:: Display for DataType {
@@ -231,6 +248,7 @@ impl fmt::Display for DataType {
231
248
format_type_with_optional_length ( f, "VARBINARY" , size, false )
232
249
}
233
250
DataType :: Blob ( size) => format_type_with_optional_length ( f, "BLOB" , size, false ) ,
251
+ DataType :: Bytes ( size) => format_type_with_optional_length ( f, "BYTES" , size, false ) ,
234
252
DataType :: Numeric ( info) => {
235
253
write ! ( f, "NUMERIC{info}" )
236
254
}
@@ -274,6 +292,9 @@ impl fmt::Display for DataType {
274
292
DataType :: Int4 ( zerofill) => {
275
293
format_type_with_optional_length ( f, "INT4" , zerofill, false )
276
294
}
295
+ DataType :: Int64 => {
296
+ write ! ( f, "INT64" )
297
+ }
277
298
DataType :: UnsignedInt4 ( zerofill) => {
278
299
format_type_with_optional_length ( f, "INT4" , zerofill, true )
279
300
}
@@ -297,6 +318,7 @@ impl fmt::Display for DataType {
297
318
}
298
319
DataType :: Real => write ! ( f, "REAL" ) ,
299
320
DataType :: Float4 => write ! ( f, "FLOAT4" ) ,
321
+ DataType :: Float64 => write ! ( f, "FLOAT64" ) ,
300
322
DataType :: Double => write ! ( f, "DOUBLE" ) ,
301
323
DataType :: Float8 => write ! ( f, "FLOAT8" ) ,
302
324
DataType :: DoublePrecision => write ! ( f, "DOUBLE PRECISION" ) ,
@@ -316,15 +338,13 @@ impl fmt::Display for DataType {
316
338
DataType :: JSON => write ! ( f, "JSON" ) ,
317
339
DataType :: Regclass => write ! ( f, "REGCLASS" ) ,
318
340
DataType :: Text => write ! ( f, "TEXT" ) ,
319
- DataType :: String => write ! ( f, "STRING" ) ,
341
+ DataType :: String ( size ) => format_type_with_optional_length ( f, "STRING" , size , false ) ,
320
342
DataType :: Bytea => write ! ( f, "BYTEA" ) ,
321
- DataType :: Array ( ty) => {
322
- if let Some ( t) = & ty {
323
- write ! ( f, "{t}[]" )
324
- } else {
325
- write ! ( f, "ARRAY" )
326
- }
327
- }
343
+ DataType :: Array ( ty) => match ty {
344
+ ArrayElemTypeDef :: None => write ! ( f, "ARRAY" ) ,
345
+ ArrayElemTypeDef :: SquareBracket ( t) => write ! ( f, "{t}[]" ) ,
346
+ ArrayElemTypeDef :: AngleBracket ( t) => write ! ( f, "ARRAY<{t}>" ) ,
347
+ } ,
328
348
DataType :: Custom ( ty, modifiers) => {
329
349
if modifiers. is_empty ( ) {
330
350
write ! ( f, "{ty}" )
@@ -352,6 +372,13 @@ impl fmt::Display for DataType {
352
372
}
353
373
write ! ( f, ")" )
354
374
}
375
+ DataType :: Struct ( fields) => {
376
+ if !fields. is_empty ( ) {
377
+ write ! ( f, "STRUCT<{}>" , display_comma_separated( fields) )
378
+ } else {
379
+ write ! ( f, "STRUCT" )
380
+ }
381
+ }
355
382
}
356
383
}
357
384
}
@@ -533,3 +560,19 @@ impl fmt::Display for CharLengthUnits {
533
560
}
534
561
}
535
562
}
563
+
564
+ /// Represents the data type of the elements in an array (if any) as well as
565
+ /// the syntax used to declare the array.
566
+ ///
567
+ /// For example: Bigquery/Hive use `ARRAY<INT>` whereas snowflake uses ARRAY.
568
+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
569
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
570
+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
571
+ pub enum ArrayElemTypeDef {
572
+ /// `ARRAY`
573
+ None ,
574
+ /// `ARRAY<INT>`
575
+ AngleBracket ( Box < DataType > ) ,
576
+ /// `[]INT`
577
+ SquareBracket ( Box < DataType > ) ,
578
+ }
0 commit comments