Skip to content

Commit

Permalink
Distinguish between INT and INTEGER types (sqlparser-rs#525)
Browse files Browse the repository at this point in the history
* support integer

* fmt

* Update src/ast/data_type.rs

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
  • Loading branch information
2 people authored and mobuchowski committed Aug 3, 2022
1 parent 266393e commit faf4a8d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/ast/data_type.rs
Expand Up @@ -55,8 +55,12 @@ pub enum DataType {
UnsignedSmallInt(Option<u64>),
/// Integer with optional display width e.g. INT or INT(11)
Int(Option<u64>),
/// Integer with optional display width e.g. INTEGER or INTEGER(11)
Integer(Option<u64>),
/// Unsigned integer with optional display width e.g. INT UNSIGNED or INT(11) UNSIGNED
UnsignedInt(Option<u64>),
/// Unsigned integer with optional display width e.g. INTGER UNSIGNED or INTEGER(11) UNSIGNED
UnsignedInteger(Option<u64>),
/// Big integer with optional display width e.g. BIGINT or BIGINT(20)
BigInt(Option<u64>),
/// Unsigned big integer with optional display width e.g. BIGINT UNSIGNED or BIGINT(20) UNSIGNED
Expand Down Expand Up @@ -134,6 +138,12 @@ impl fmt::Display for DataType {
DataType::UnsignedInt(zerofill) => {
format_type_with_optional_length(f, "INT", zerofill, true)
}
DataType::Integer(zerofill) => {
format_type_with_optional_length(f, "INTEGER", zerofill, false)
}
DataType::UnsignedInteger(zerofill) => {
format_type_with_optional_length(f, "INTEGER", zerofill, true)
}
DataType::BigInt(zerofill) => {
format_type_with_optional_length(f, "BIGINT", zerofill, false)
}
Expand Down
10 changes: 9 additions & 1 deletion src/parser.rs
Expand Up @@ -2885,14 +2885,22 @@ impl<'a> Parser<'a> {
Ok(DataType::SmallInt(optional_precision?))
}
}
Keyword::INT | Keyword::INTEGER => {
Keyword::INT => {
let optional_precision = self.parse_optional_precision();
if self.parse_keyword(Keyword::UNSIGNED) {
Ok(DataType::UnsignedInt(optional_precision?))
} else {
Ok(DataType::Int(optional_precision?))
}
}
Keyword::INTEGER => {
let optional_precision = self.parse_optional_precision();
if self.parse_keyword(Keyword::UNSIGNED) {
Ok(DataType::UnsignedInteger(optional_precision?))
} else {
Ok(DataType::Integer(optional_precision?))
}
}
Keyword::BIGINT => {
let optional_precision = self.parse_optional_precision();
if self.parse_keyword(Keyword::UNSIGNED) {
Expand Down
8 changes: 4 additions & 4 deletions tests/sqlparser_postgres.rs
Expand Up @@ -35,7 +35,7 @@ fn parse_create_table_with_defaults() {
activebool boolean DEFAULT true NOT NULL,
create_date date DEFAULT now()::text NOT NULL,
last_update timestamp without time zone DEFAULT now() NOT NULL,
active integer NOT NULL
active int NOT NULL
) WITH (fillfactor = 20, user_catalog_table = true, autovacuum_vacuum_threshold = 100)";
match pg_and_generic().one_statement_parses_to(sql, "") {
Statement::CreateTable {
Expand All @@ -55,7 +55,7 @@ fn parse_create_table_with_defaults() {
vec![
ColumnDef {
name: "customer_id".into(),
data_type: DataType::Int(None),
data_type: DataType::Integer(None),
collation: None,
options: vec![ColumnOptionDef {
name: None,
Expand Down Expand Up @@ -201,10 +201,10 @@ fn parse_create_table_from_pg_dump() {
create_date1 date DEFAULT 'now'::TEXT::date NOT NULL,
last_update timestamp without time zone DEFAULT now(),
release_year public.year,
active integer
active int
)";
pg().one_statement_parses_to(sql, "CREATE TABLE public.customer (\
customer_id INT DEFAULT nextval(CAST('public.customer_customer_id_seq' AS REGCLASS)) NOT NULL, \
customer_id INTEGER DEFAULT nextval(CAST('public.customer_customer_id_seq' AS REGCLASS)) NOT NULL, \
store_id SMALLINT NOT NULL, \
first_name CHARACTER VARYING(45) NOT NULL, \
last_name CHARACTER VARYING(45) NOT NULL, \
Expand Down

0 comments on commit faf4a8d

Please sign in to comment.