Support minimum display width for integer data types (#337)

* Support minimum display width for integers

* make fmt happy, updated docstrings
This commit is contained in:
Alex Vasilev 2021-08-29 14:13:10 +03:00 committed by GitHub
parent 9a5716d94b
commit 77d90d3b85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 36 deletions

View file

@ -41,14 +41,14 @@ pub enum DataType {
Decimal(Option<u64>, Option<u64>), Decimal(Option<u64>, Option<u64>),
/// Floating point with optional precision e.g. FLOAT(8) /// Floating point with optional precision e.g. FLOAT(8)
Float(Option<u64>), Float(Option<u64>),
/// Tiny integer /// Tiny integer with optional display width e.g. TINYINT or TINYINT(3)
TinyInt, TinyInt(Option<u64>),
/// Small integer /// Small integer with optional display width e.g. SMALLINT or SMALLINT(5)
SmallInt, SmallInt(Option<u64>),
/// Integer /// Integer with optional display width e.g. INT or INT(11)
Int, Int(Option<u64>),
/// Big integer /// Big integer with optional display width e.g. BIGINT or BIGINT(20)
BigInt, BigInt(Option<u64>),
/// Floating point e.g. REAL /// Floating point e.g. REAL
Real, Real,
/// Double e.g. DOUBLE PRECISION /// Double e.g. DOUBLE PRECISION
@ -97,10 +97,12 @@ impl fmt::Display for DataType {
} }
} }
DataType::Float(size) => format_type_with_optional_length(f, "FLOAT", size), DataType::Float(size) => format_type_with_optional_length(f, "FLOAT", size),
DataType::TinyInt => write!(f, "TINYINT"), DataType::TinyInt(zerofill) => format_type_with_optional_length(f, "TINYINT", zerofill),
DataType::SmallInt => write!(f, "SMALLINT"), DataType::SmallInt(zerofill) => {
DataType::Int => write!(f, "INT"), format_type_with_optional_length(f, "SMALLINT", zerofill)
DataType::BigInt => write!(f, "BIGINT"), }
DataType::Int(zerofill) => format_type_with_optional_length(f, "INT", zerofill),
DataType::BigInt(zerofill) => format_type_with_optional_length(f, "BIGINT", zerofill),
DataType::Real => write!(f, "REAL"), DataType::Real => write!(f, "REAL"),
DataType::Double => write!(f, "DOUBLE"), DataType::Double => write!(f, "DOUBLE"),
DataType::Boolean => write!(f, "BOOLEAN"), DataType::Boolean => write!(f, "BOOLEAN"),

View file

@ -2008,10 +2008,12 @@ impl<'a> Parser<'a> {
let _ = self.parse_keyword(Keyword::PRECISION); let _ = self.parse_keyword(Keyword::PRECISION);
Ok(DataType::Double) Ok(DataType::Double)
} }
Keyword::TINYINT => Ok(DataType::TinyInt), Keyword::TINYINT => Ok(DataType::TinyInt(self.parse_optional_precision()?)),
Keyword::SMALLINT => Ok(DataType::SmallInt), Keyword::SMALLINT => Ok(DataType::SmallInt(self.parse_optional_precision()?)),
Keyword::INT | Keyword::INTEGER => Ok(DataType::Int), Keyword::INT | Keyword::INTEGER => {
Keyword::BIGINT => Ok(DataType::BigInt), Ok(DataType::Int(self.parse_optional_precision()?))
}
Keyword::BIGINT => Ok(DataType::BigInt(self.parse_optional_precision()?)),
Keyword::VARCHAR => Ok(DataType::Varchar(self.parse_optional_precision()?)), Keyword::VARCHAR => Ok(DataType::Varchar(self.parse_optional_precision()?)),
Keyword::CHAR | Keyword::CHARACTER => { Keyword::CHAR | Keyword::CHARACTER => {
if self.parse_keyword(Keyword::VARYING) { if self.parse_keyword(Keyword::VARYING) {

View file

@ -1013,7 +1013,7 @@ fn parse_cast() {
assert_eq!( assert_eq!(
&Expr::Cast { &Expr::Cast {
expr: Box::new(Expr::Identifier(Ident::new("id"))), expr: Box::new(Expr::Identifier(Ident::new("id"))),
data_type: DataType::BigInt data_type: DataType::BigInt(None)
}, },
expr_from_projection(only(&select.projection)) expr_from_projection(only(&select.projection))
); );
@ -1023,7 +1023,7 @@ fn parse_cast() {
assert_eq!( assert_eq!(
&Expr::Cast { &Expr::Cast {
expr: Box::new(Expr::Identifier(Ident::new("id"))), expr: Box::new(Expr::Identifier(Ident::new("id"))),
data_type: DataType::TinyInt data_type: DataType::TinyInt(None)
}, },
expr_from_projection(only(&select.projection)) expr_from_projection(only(&select.projection))
); );
@ -1053,7 +1053,7 @@ fn parse_try_cast() {
assert_eq!( assert_eq!(
&Expr::TryCast { &Expr::TryCast {
expr: Box::new(Expr::Identifier(Ident::new("id"))), expr: Box::new(Expr::Identifier(Ident::new("id"))),
data_type: DataType::BigInt data_type: DataType::BigInt(None)
}, },
expr_from_projection(only(&select.projection)) expr_from_projection(only(&select.projection))
); );
@ -1224,7 +1224,7 @@ fn parse_create_table() {
}, },
ColumnDef { ColumnDef {
name: "constrained".into(), name: "constrained".into(),
data_type: DataType::Int, data_type: DataType::Int(None),
collation: None, collation: None,
options: vec![ options: vec![
ColumnOptionDef { ColumnOptionDef {
@ -1251,7 +1251,7 @@ fn parse_create_table() {
}, },
ColumnDef { ColumnDef {
name: "ref".into(), name: "ref".into(),
data_type: DataType::Int, data_type: DataType::Int(None),
collation: None, collation: None,
options: vec![ColumnOptionDef { options: vec![ColumnOptionDef {
name: None, name: None,
@ -1265,7 +1265,7 @@ fn parse_create_table() {
}, },
ColumnDef { ColumnDef {
name: "ref2".into(), name: "ref2".into(),
data_type: DataType::Int, data_type: DataType::Int(None),
collation: None, collation: None,
options: vec![ColumnOptionDef { options: vec![ColumnOptionDef {
name: None, name: None,

View file

@ -16,6 +16,7 @@
#[macro_use] #[macro_use]
mod test_utils; mod test_utils;
use test_utils::*; use test_utils::*;
use sqlparser::ast::*; use sqlparser::ast::*;
@ -130,19 +131,19 @@ fn parse_create_table_auto_increment() {
assert_eq!( assert_eq!(
vec![ColumnDef { vec![ColumnDef {
name: Ident::new("bar"), name: Ident::new("bar"),
data_type: DataType::Int, data_type: DataType::Int(None),
collation: None, collation: None,
options: vec![ options: vec![
ColumnOptionDef { ColumnOptionDef {
name: None, name: None,
option: ColumnOption::Unique { is_primary: true } option: ColumnOption::Unique { is_primary: true },
}, },
ColumnOptionDef { ColumnOptionDef {
name: None, name: None,
option: ColumnOption::DialectSpecific(vec![Token::make_keyword( option: ColumnOption::DialectSpecific(vec![Token::make_keyword(
"AUTO_INCREMENT" "AUTO_INCREMENT"
)]) )]),
} },
], ],
}], }],
columns columns
@ -161,11 +162,11 @@ fn parse_quote_identifiers() {
assert_eq!( assert_eq!(
vec![ColumnDef { vec![ColumnDef {
name: Ident::with_quote('`', "BEGIN"), name: Ident::with_quote('`', "BEGIN"),
data_type: DataType::Int, data_type: DataType::Int(None),
collation: None, collation: None,
options: vec![ColumnOptionDef { options: vec![ColumnOptionDef {
name: None, name: None,
option: ColumnOption::Unique { is_primary: true } option: ColumnOption::Unique { is_primary: true },
}], }],
}], }],
columns columns
@ -175,6 +176,46 @@ fn parse_quote_identifiers() {
} }
} }
#[test]
fn parse_create_table_with_minimum_display_width() {
let sql = "CREATE TABLE foo (bar_tinyint TINYINT(3), bar_smallint SMALLINT(5), bar_int INT(11), bar_bigint BIGINT(20))";
match mysql().verified_stmt(sql) {
Statement::CreateTable { name, columns, .. } => {
assert_eq!(name.to_string(), "foo");
assert_eq!(
vec![
ColumnDef {
name: Ident::new("bar_tinyint"),
data_type: DataType::TinyInt(Some(3)),
collation: None,
options: vec![],
},
ColumnDef {
name: Ident::new("bar_smallint"),
data_type: DataType::SmallInt(Some(5)),
collation: None,
options: vec![],
},
ColumnDef {
name: Ident::new("bar_int"),
data_type: DataType::Int(Some(11)),
collation: None,
options: vec![],
},
ColumnDef {
name: Ident::new("bar_bigint"),
data_type: DataType::BigInt(Some(20)),
collation: None,
options: vec![],
}
],
columns
);
}
_ => unreachable!(),
}
}
fn mysql() -> TestedDialects { fn mysql() -> TestedDialects {
TestedDialects { TestedDialects {
dialects: vec![Box::new(MySqlDialect {})], dialects: vec![Box::new(MySqlDialect {})],

View file

@ -54,7 +54,7 @@ fn parse_create_table_with_defaults() {
vec![ vec![
ColumnDef { ColumnDef {
name: "customer_id".into(), name: "customer_id".into(),
data_type: DataType::Int, data_type: DataType::Int(None),
collation: None, collation: None,
options: vec![ColumnOptionDef { options: vec![ColumnOptionDef {
name: None, name: None,
@ -65,7 +65,7 @@ fn parse_create_table_with_defaults() {
}, },
ColumnDef { ColumnDef {
name: "store_id".into(), name: "store_id".into(),
data_type: DataType::SmallInt, data_type: DataType::SmallInt(None),
collation: None, collation: None,
options: vec![ColumnOptionDef { options: vec![ColumnOptionDef {
name: None, name: None,
@ -98,7 +98,7 @@ fn parse_create_table_with_defaults() {
}, },
ColumnDef { ColumnDef {
name: "address_id".into(), name: "address_id".into(),
data_type: DataType::SmallInt, data_type: DataType::SmallInt(None),
collation: None, collation: None,
options: vec![ColumnOptionDef { options: vec![ColumnOptionDef {
name: None, name: None,
@ -154,7 +154,7 @@ fn parse_create_table_with_defaults() {
}, },
ColumnDef { ColumnDef {
name: "active".into(), name: "active".into(),
data_type: DataType::Int, data_type: DataType::Int(None),
collation: None, collation: None,
options: vec![ColumnOptionDef { options: vec![ColumnOptionDef {
name: None, name: None,
@ -574,7 +574,7 @@ fn parse_prepare() {
.. ..
} => { } => {
assert_eq!(name, "a".into()); assert_eq!(name, "a".into());
assert_eq!(data_types, vec![DataType::Int, DataType::Text]); assert_eq!(data_types, vec![DataType::Int(None), DataType::Text]);
statement statement
} }

View file

@ -68,7 +68,7 @@ fn parse_create_table_auto_increment() {
assert_eq!( assert_eq!(
vec![ColumnDef { vec![ColumnDef {
name: "bar".into(), name: "bar".into(),
data_type: DataType::Int, data_type: DataType::Int(None),
collation: None, collation: None,
options: vec![ options: vec![
ColumnOptionDef { ColumnOptionDef {
@ -100,13 +100,13 @@ fn parse_create_sqlite_quote() {
vec![ vec![
ColumnDef { ColumnDef {
name: Ident::with_quote('"', "KEY"), name: Ident::with_quote('"', "KEY"),
data_type: DataType::Int, data_type: DataType::Int(None),
collation: None, collation: None,
options: vec![], options: vec![],
}, },
ColumnDef { ColumnDef {
name: Ident::with_quote('[', "INDEX"), name: Ident::with_quote('[', "INDEX"),
data_type: DataType::Int, data_type: DataType::Int(None),
collation: None, collation: None,
options: vec![], options: vec![],
}, },