Skip to content

Commit 3f872f7

Browse files
committed
Add byte and short data types
1 parent 56f89a9 commit 3f872f7

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

src/ast/data_type.rs

+20
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ pub enum DataType {
196196
///
197197
/// [clickhouse]: https://clickhouse.com/docs/en/sql-reference/data-types/int-uint
198198
Int256,
199+
/// Byte with optional display width e.g. BYTE or BYTE(6)
200+
Byte(Option<u64>),
201+
/// Short with optional display width e.g. SHORT or SHORT(9)
202+
Short(Option<u64>),
199203
/// Integer with optional display width e.g. INTEGER or INTEGER(11)
200204
Integer(Option<u64>),
201205
/// Long with optional display width e.g. LONG or LONG(20)
@@ -204,6 +208,10 @@ pub enum DataType {
204208
UnsignedInt(Option<u64>),
205209
/// Unsigned int4 with optional display width e.g. INT4 UNSIGNED or INT4(11) UNSIGNED
206210
UnsignedInt4(Option<u64>),
211+
/// Unsigned byte with optional display width e.g. BYTE UNSIGNED or BYTE(6) UNSIGNED
212+
UnsignedByte(Option<u64>),
213+
/// Unsigned short with optional display width e.g. SHORT UNSIGNED or SHORT(9) UNSIGNED
214+
UnsignedShort(Option<u64>),
207215
/// Unsigned integer with optional display width e.g. INTEGER UNSIGNED or INTEGER(11) UNSIGNED
208216
UnsignedInteger(Option<u64>),
209217
/// Unsigned long with optional display width e.g. LONG UNSIGNED or LONG(20) UNSIGNED
@@ -483,6 +491,18 @@ impl fmt::Display for DataType {
483491
DataType::UnsignedInt4(zerofill) => {
484492
format_type_with_optional_length(f, "INT4", zerofill, true)
485493
}
494+
DataType::Byte(zerofill) => {
495+
format_type_with_optional_length(f, "BYTE", zerofill, false)
496+
}
497+
DataType::UnsignedByte(zerofill) => {
498+
format_type_with_optional_length(f, "BYTE", zerofill, true)
499+
}
500+
DataType::Short(zerofill) => {
501+
format_type_with_optional_length(f, "SHORT", zerofill, false)
502+
}
503+
DataType::UnsignedShort(zerofill) => {
504+
format_type_with_optional_length(f, "SHORT", zerofill, true)
505+
}
486506
DataType::Integer(zerofill) => {
487507
format_type_with_optional_length(f, "INTEGER", zerofill, false)
488508
}

src/keywords.rs

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ define_keywords!(
140140
BUCKETS,
141141
BY,
142142
BYPASSRLS,
143+
BYTE,
143144
BYTEA,
144145
BYTES,
145146
CACHE,
@@ -717,6 +718,7 @@ define_keywords!(
717718
SETS,
718719
SETTINGS,
719720
SHARE,
721+
SHORT,
720722
SHOW,
721723
SIMILAR,
722724
SKIP,

src/parser/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -8272,6 +8272,22 @@ impl<'a> Parser<'a> {
82728272
Keyword::INT64 => Ok(DataType::Int64),
82738273
Keyword::INT128 => Ok(DataType::Int128),
82748274
Keyword::INT256 => Ok(DataType::Int256),
8275+
Keyword::BYTE => {
8276+
let optional_precision = self.parse_optional_precision();
8277+
if self.parse_keyword(Keyword::UNSIGNED) {
8278+
Ok(DataType::UnsignedByte(optional_precision?))
8279+
} else {
8280+
Ok(DataType::Byte(optional_precision?))
8281+
}
8282+
}
8283+
Keyword::SHORT => {
8284+
let optional_precision = self.parse_optional_precision();
8285+
if self.parse_keyword(Keyword::UNSIGNED) {
8286+
Ok(DataType::UnsignedShort(optional_precision?))
8287+
} else {
8288+
Ok(DataType::Short(optional_precision?))
8289+
}
8290+
}
82758291
Keyword::INTEGER => {
82768292
let optional_precision = self.parse_optional_precision();
82778293
if self.parse_keyword(Keyword::UNSIGNED) {

tests/sqlparser_common.rs

+58
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,54 @@ fn parse_cast() {
24052405
expr_from_projection(only(&select.projection))
24062406
);
24072407

2408+
let sql = "SELECT CAST(id AS BYTE) FROM customer";
2409+
let select = verified_only_select(sql);
2410+
assert_eq!(
2411+
&Expr::Cast {
2412+
kind: CastKind::Cast,
2413+
expr: Box::new(Expr::Identifier(Ident::new("id"))),
2414+
data_type: DataType::Byte(None),
2415+
format: None,
2416+
},
2417+
expr_from_projection(only(&select.projection))
2418+
);
2419+
2420+
let sql = "SELECT CAST(id AS BYTE(6)) FROM customer";
2421+
let select = verified_only_select(sql);
2422+
assert_eq!(
2423+
&Expr::Cast {
2424+
kind: CastKind::Cast,
2425+
expr: Box::new(Expr::Identifier(Ident::new("id"))),
2426+
data_type: DataType::Byte(Some(6)),
2427+
format: None,
2428+
},
2429+
expr_from_projection(only(&select.projection))
2430+
);
2431+
2432+
let sql = "SELECT CAST(id AS SHORT) FROM customer";
2433+
let select = verified_only_select(sql);
2434+
assert_eq!(
2435+
&Expr::Cast {
2436+
kind: CastKind::Cast,
2437+
expr: Box::new(Expr::Identifier(Ident::new("id"))),
2438+
data_type: DataType::Short(None),
2439+
format: None,
2440+
},
2441+
expr_from_projection(only(&select.projection))
2442+
);
2443+
2444+
let sql = "SELECT CAST(id AS SHORT(9)) FROM customer";
2445+
let select = verified_only_select(sql);
2446+
assert_eq!(
2447+
&Expr::Cast {
2448+
kind: CastKind::Cast,
2449+
expr: Box::new(Expr::Identifier(Ident::new("id"))),
2450+
data_type: DataType::Short(Some(9)),
2451+
format: None,
2452+
},
2453+
expr_from_projection(only(&select.projection))
2454+
);
2455+
24082456
let sql = "SELECT CAST(id AS LONG) FROM customer";
24092457
let select = verified_only_select(sql);
24102458
assert_eq!(
@@ -2439,6 +2487,16 @@ fn parse_cast() {
24392487
"SELECT CAST(id AS BIGINT) FROM customer",
24402488
);
24412489

2490+
one_statement_parses_to(
2491+
"SELECT CAST(id AS BYTE(6) UNSIGNED) FROM customer",
2492+
"SELECT CAST(id AS BYTE(6) UNSIGNED) FROM customer",
2493+
);
2494+
2495+
one_statement_parses_to(
2496+
"SELECT CAST(id AS SHORT(9) UNSIGNED) FROM customer",
2497+
"SELECT CAST(id AS SHORT(9) UNSIGNED) FROM customer",
2498+
);
2499+
24422500
one_statement_parses_to(
24432501
"SELECT CAST(id AS LONG(20) UNSIGNED) FROM customer",
24442502
"SELECT CAST(id AS LONG(20) UNSIGNED) FROM customer",

0 commit comments

Comments
 (0)