Skip to content

Commit e951cd5

Browse files
authored
#625 adding mediumint support (#630)
1 parent 46b7c03 commit e951cd5

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

src/ast/data_type.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ pub enum DataType {
5353
SmallInt(Option<u64>),
5454
/// Unsigned small integer with optional display width e.g. SMALLINT UNSIGNED or SMALLINT(5) UNSIGNED
5555
UnsignedSmallInt(Option<u64>),
56+
/// MySQL medium integer ([1]) with optional display width e.g. MEDIUMINT or MEDIUMINT(5)
57+
///
58+
/// [1]: https://dev.mysql.com/doc/refman/8.0/en/integer-types.html
59+
MediumInt(Option<u64>),
60+
/// Unsigned medium integer ([1]) with optional display width e.g. MEDIUMINT UNSIGNED or MEDIUMINT(5) UNSIGNED
61+
///
62+
/// [1]: https://dev.mysql.com/doc/refman/8.0/en/integer-types.html
63+
UnsignedMediumInt(Option<u64>),
5664
/// Integer with optional display width e.g. INT or INT(11)
5765
Int(Option<u64>),
5866
/// Integer with optional display width e.g. INTEGER or INTEGER(11)
@@ -141,6 +149,12 @@ impl fmt::Display for DataType {
141149
DataType::UnsignedSmallInt(zerofill) => {
142150
format_type_with_optional_length(f, "SMALLINT", zerofill, true)
143151
}
152+
DataType::MediumInt(zerofill) => {
153+
format_type_with_optional_length(f, "MEDIUMINT", zerofill, false)
154+
}
155+
DataType::UnsignedMediumInt(zerofill) => {
156+
format_type_with_optional_length(f, "MEDIUMINT", zerofill, true)
157+
}
144158
DataType::Int(zerofill) => format_type_with_optional_length(f, "INT", zerofill, false),
145159
DataType::UnsignedInt(zerofill) => {
146160
format_type_with_optional_length(f, "INT", zerofill, true)

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ define_keywords!(
325325
MATCHED,
326326
MATERIALIZED,
327327
MAX,
328+
MEDIUMINT,
328329
MEMBER,
329330
MERGE,
330331
METADATA,

src/parser.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,6 +3360,14 @@ impl<'a> Parser<'a> {
33603360
Ok(DataType::SmallInt(optional_precision?))
33613361
}
33623362
}
3363+
Keyword::MEDIUMINT => {
3364+
let optional_precision = self.parse_optional_precision();
3365+
if self.parse_keyword(Keyword::UNSIGNED) {
3366+
Ok(DataType::UnsignedMediumInt(optional_precision?))
3367+
} else {
3368+
Ok(DataType::MediumInt(optional_precision?))
3369+
}
3370+
}
33633371
Keyword::INT => {
33643372
let optional_precision = self.parse_optional_precision();
33653373
if self.parse_keyword(Keyword::UNSIGNED) {

tests/sqlparser_common.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,11 @@ fn parse_cast() {
16231623
expr_from_projection(only(&select.projection))
16241624
);
16251625

1626+
one_statement_parses_to(
1627+
"SELECT CAST(id AS MEDIUMINT) FROM customer",
1628+
"SELECT CAST(id AS MEDIUMINT) FROM customer",
1629+
);
1630+
16261631
one_statement_parses_to(
16271632
"SELECT CAST(id AS BIGINT) FROM customer",
16281633
"SELECT CAST(id AS BIGINT) FROM customer",

tests/sqlparser_mysql.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ fn parse_escaped_string() {
552552

553553
#[test]
554554
fn parse_create_table_with_minimum_display_width() {
555-
let sql = "CREATE TABLE foo (bar_tinyint TINYINT(3), bar_smallint SMALLINT(5), bar_int INT(11), bar_bigint BIGINT(20))";
555+
let sql = "CREATE TABLE foo (bar_tinyint TINYINT(3), bar_smallint SMALLINT(5), bar_mediumint MEDIUMINT(6), bar_int INT(11), bar_bigint BIGINT(20))";
556556
match mysql().verified_stmt(sql) {
557557
Statement::CreateTable { name, columns, .. } => {
558558
assert_eq!(name.to_string(), "foo");
@@ -570,6 +570,12 @@ fn parse_create_table_with_minimum_display_width() {
570570
collation: None,
571571
options: vec![],
572572
},
573+
ColumnDef {
574+
name: Ident::new("bar_mediumint"),
575+
data_type: DataType::MediumInt(Some(6)),
576+
collation: None,
577+
options: vec![],
578+
},
573579
ColumnDef {
574580
name: Ident::new("bar_int"),
575581
data_type: DataType::Int(Some(11)),
@@ -592,7 +598,7 @@ fn parse_create_table_with_minimum_display_width() {
592598

593599
#[test]
594600
fn parse_create_table_unsigned() {
595-
let sql = "CREATE TABLE foo (bar_tinyint TINYINT(3) UNSIGNED, bar_smallint SMALLINT(5) UNSIGNED, bar_int INT(11) UNSIGNED, bar_bigint BIGINT(20) UNSIGNED)";
601+
let sql = "CREATE TABLE foo (bar_tinyint TINYINT(3) UNSIGNED, bar_smallint SMALLINT(5) UNSIGNED, bar_mediumint MEDIUMINT(13) UNSIGNED, bar_int INT(11) UNSIGNED, bar_bigint BIGINT(20) UNSIGNED)";
596602
match mysql().verified_stmt(sql) {
597603
Statement::CreateTable { name, columns, .. } => {
598604
assert_eq!(name.to_string(), "foo");
@@ -610,6 +616,12 @@ fn parse_create_table_unsigned() {
610616
collation: None,
611617
options: vec![],
612618
},
619+
ColumnDef {
620+
name: Ident::new("bar_mediumint"),
621+
data_type: DataType::UnsignedMediumInt(Some(13)),
622+
collation: None,
623+
options: vec![],
624+
},
613625
ColumnDef {
614626
name: Ident::new("bar_int"),
615627
data_type: DataType::UnsignedInt(Some(11)),

0 commit comments

Comments
 (0)