Skip to content

Commit 48b0e4d

Browse files
authored
Support MySQL size variants for BLOB and TEXT columns (#1564)
1 parent a134910 commit 48b0e4d

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/ast/data_type.rs

+30
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ pub enum DataType {
7676
/// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#binary-large-object-string-type
7777
/// [Oracle]: https://docs.oracle.com/javadb/10.8.3.0/ref/rrefblob.html
7878
Blob(Option<u64>),
79+
/// [MySQL] blob with up to 2**8 bytes
80+
///
81+
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
82+
TinyBlob,
83+
/// [MySQL] blob with up to 2**24 bytes
84+
///
85+
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
86+
MediumBlob,
87+
/// [MySQL] blob with up to 2**32 bytes
88+
///
89+
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
90+
LongBlob,
7991
/// Variable-length binary data with optional length.
8092
///
8193
/// [bigquery]: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#bytes_type
@@ -275,6 +287,18 @@ pub enum DataType {
275287
Regclass,
276288
/// Text
277289
Text,
290+
/// [MySQL] text with up to 2**8 bytes
291+
///
292+
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
293+
TinyText,
294+
/// [MySQL] text with up to 2**24 bytes
295+
///
296+
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
297+
MediumText,
298+
/// [MySQL] text with up to 2**32 bytes
299+
///
300+
/// [MySQL]: https://dev.mysql.com/doc/refman/9.1/en/blob.html
301+
LongText,
278302
/// String with optional length.
279303
String(Option<u64>),
280304
/// A fixed-length string e.g [ClickHouse][1].
@@ -355,6 +379,9 @@ impl fmt::Display for DataType {
355379
format_type_with_optional_length(f, "VARBINARY", size, false)
356380
}
357381
DataType::Blob(size) => format_type_with_optional_length(f, "BLOB", size, false),
382+
DataType::TinyBlob => write!(f, "TINYBLOB"),
383+
DataType::MediumBlob => write!(f, "MEDIUMBLOB"),
384+
DataType::LongBlob => write!(f, "LONGBLOB"),
358385
DataType::Bytes(size) => format_type_with_optional_length(f, "BYTES", size, false),
359386
DataType::Numeric(info) => {
360387
write!(f, "NUMERIC{info}")
@@ -486,6 +513,9 @@ impl fmt::Display for DataType {
486513
DataType::JSONB => write!(f, "JSONB"),
487514
DataType::Regclass => write!(f, "REGCLASS"),
488515
DataType::Text => write!(f, "TEXT"),
516+
DataType::TinyText => write!(f, "TINYTEXT"),
517+
DataType::MediumText => write!(f, "MEDIUMTEXT"),
518+
DataType::LongText => write!(f, "LONGTEXT"),
489519
DataType::String(size) => format_type_with_optional_length(f, "STRING", size, false),
490520
DataType::Bytea => write!(f, "BYTEA"),
491521
DataType::Array(ty) => match ty {

src/keywords.rs

+6
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ define_keywords!(
453453
LOCKED,
454454
LOGIN,
455455
LOGS,
456+
LONGBLOB,
457+
LONGTEXT,
456458
LOWCARDINALITY,
457459
LOWER,
458460
LOW_PRIORITY,
@@ -471,7 +473,9 @@ define_keywords!(
471473
MAXVALUE,
472474
MAX_DATA_EXTENSION_TIME_IN_DAYS,
473475
MEASURES,
476+
MEDIUMBLOB,
474477
MEDIUMINT,
478+
MEDIUMTEXT,
475479
MEMBER,
476480
MERGE,
477481
METADATA,
@@ -765,7 +769,9 @@ define_keywords!(
765769
TIMEZONE_HOUR,
766770
TIMEZONE_MINUTE,
767771
TIMEZONE_REGION,
772+
TINYBLOB,
768773
TINYINT,
774+
TINYTEXT,
769775
TO,
770776
TOP,
771777
TOTALS,

src/parser/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -8129,6 +8129,9 @@ impl<'a> Parser<'a> {
81298129
Keyword::BINARY => Ok(DataType::Binary(self.parse_optional_precision()?)),
81308130
Keyword::VARBINARY => Ok(DataType::Varbinary(self.parse_optional_precision()?)),
81318131
Keyword::BLOB => Ok(DataType::Blob(self.parse_optional_precision()?)),
8132+
Keyword::TINYBLOB => Ok(DataType::TinyBlob),
8133+
Keyword::MEDIUMBLOB => Ok(DataType::MediumBlob),
8134+
Keyword::LONGBLOB => Ok(DataType::LongBlob),
81328135
Keyword::BYTES => Ok(DataType::Bytes(self.parse_optional_precision()?)),
81338136
Keyword::UUID => Ok(DataType::Uuid),
81348137
Keyword::DATE => Ok(DataType::Date),
@@ -8188,6 +8191,9 @@ impl<'a> Parser<'a> {
81888191
Ok(DataType::FixedString(character_length))
81898192
}
81908193
Keyword::TEXT => Ok(DataType::Text),
8194+
Keyword::TINYTEXT => Ok(DataType::TinyText),
8195+
Keyword::MEDIUMTEXT => Ok(DataType::MediumText),
8196+
Keyword::LONGTEXT => Ok(DataType::LongText),
81918197
Keyword::BYTEA => Ok(DataType::Bytea),
81928198
Keyword::NUMERIC => Ok(DataType::Numeric(
81938199
self.parse_exact_number_optional_precision_scale()?,

tests/sqlparser_mysql.rs

+17
Original file line numberDiff line numberDiff line change
@@ -3014,3 +3014,20 @@ fn parse_bitstring_literal() {
30143014
))]
30153015
);
30163016
}
3017+
3018+
#[test]
3019+
fn parse_longblob_type() {
3020+
let sql = "CREATE TABLE foo (bar LONGBLOB)";
3021+
let stmt = mysql_and_generic().verified_stmt(sql);
3022+
if let Statement::CreateTable(CreateTable { columns, .. }) = stmt {
3023+
assert_eq!(columns.len(), 1);
3024+
assert_eq!(columns[0].data_type, DataType::LongBlob);
3025+
} else {
3026+
unreachable!()
3027+
}
3028+
mysql_and_generic().verified_stmt("CREATE TABLE foo (bar TINYBLOB)");
3029+
mysql_and_generic().verified_stmt("CREATE TABLE foo (bar MEDIUMBLOB)");
3030+
mysql_and_generic().verified_stmt("CREATE TABLE foo (bar TINYTEXT)");
3031+
mysql_and_generic().verified_stmt("CREATE TABLE foo (bar MEDIUMTEXT)");
3032+
mysql_and_generic().verified_stmt("CREATE TABLE foo (bar LONGTEXT)");
3033+
}

0 commit comments

Comments
 (0)