Skip to content

Commit 071b8a7

Browse files
Adressed PR comments
1 parent b3a86ab commit 071b8a7

File tree

5 files changed

+131
-39
lines changed

5 files changed

+131
-39
lines changed

src/ast/ddl.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,9 +2041,6 @@ pub enum UserDefinedTypeRepresentation {
20412041
SqlDefinition {
20422042
options: Vec<UserDefinedTypeSqlDefinitionOption>,
20432043
},
2044-
/// When the representation of the type is not specified.
2045-
/// This is used in `CREATE TYPE <name>;` statements.
2046-
None,
20472044
}
20482045

20492046
impl fmt::Display for UserDefinedTypeRepresentation {
@@ -2061,7 +2058,6 @@ impl fmt::Display for UserDefinedTypeRepresentation {
20612058
Self::SqlDefinition { options } => {
20622059
write!(f, "({})", display_comma_separated(options))
20632060
}
2064-
Self::None => Ok(()),
20652061
}
20662062
}
20672063
}

src/ast/mod.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,21 @@ pub use self::dcl::{
5959
AlterRoleOperation, CreateRole, ResetConfig, RoleOption, SecondaryRoles, SetConfigValue, Use,
6060
};
6161
pub use self::ddl::{
62-
Alignment, AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterPolicyOperation,
63-
AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm, AlterTableLock,
64-
AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
65-
AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue, ClusteredBy, ColumnDef,
66-
ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy, ColumnPolicyProperty,
67-
ConstraintCharacteristics, CreateConnector, CreateDomain, CreateExtension, CreateFunction,
68-
CreateIndex, CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial,
69-
DropBehavior, DropExtension, DropFunction, DropTrigger, GeneratedAs, GeneratedExpressionMode,
70-
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
71-
IdentityPropertyOrder, IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck,
72-
NullsDistinctOption, Owner, Partition, ProcedureParam, ReferentialAction, RenameTableNameKind,
73-
ReplicaIdentity, TagsColumnOption, TriggerObjectKind, Truncate,
74-
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
75-
UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption,
76-
UserDefinedTypeStorage, ViewColumnDef,
62+
Alignment, AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation,
63+
AlterPolicyOperation, AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm,
64+
AlterTableLock, AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue,
65+
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
66+
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
67+
ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
68+
CreateExtension, CreateFunction, CreateIndex, CreateTable, CreateTrigger, CreateView,
69+
Deduplicate, DeferrableInitial, DropBehavior, DropExtension, DropFunction, DropTrigger,
70+
GeneratedAs, GeneratedExpressionMode, IdentityParameters, IdentityProperty,
71+
IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder, IndexColumn,
72+
IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption, Owner, Partition,
73+
ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
74+
TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
75+
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
76+
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef,
7777
};
7878
pub use self::dml::{Delete, Insert, Update};
7979
pub use self::operator::{BinaryOperator, UnaryOperator};
@@ -4109,7 +4109,7 @@ pub enum Statement {
41094109
/// ```
41104110
CreateType {
41114111
name: ObjectName,
4112-
representation: UserDefinedTypeRepresentation,
4112+
representation: Option<UserDefinedTypeRepresentation>,
41134113
},
41144114
/// ```sql
41154115
/// PRAGMA <schema-name>.<pragma-name> = <pragma-value>
@@ -5660,10 +5660,10 @@ impl fmt::Display for Statement {
56605660
representation,
56615661
} => {
56625662
write!(f, "CREATE TYPE {name}")?;
5663-
match representation {
5664-
UserDefinedTypeRepresentation::None => Ok(()),
5665-
repr => write!(f, " {repr}"),
5663+
if let Some(repr) = representation {
5664+
write!(f, " {repr}")?;
56665665
}
5666+
Ok(())
56675667
}
56685668
Statement::Pragma { name, value, is_eq } => {
56695669
write!(f, "PRAGMA {name}")?;

src/parser/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17371,14 +17371,14 @@ impl<'a> Parser<'a> {
1737117371
self.expect_token(&Token::RParen)?;
1737217372
return Ok(Statement::CreateType {
1737317373
name,
17374-
representation: UserDefinedTypeRepresentation::SqlDefinition { options },
17374+
representation: Some(UserDefinedTypeRepresentation::SqlDefinition { options }),
1737517375
});
1737617376
}
1737717377

1737817378
// CREATE TYPE name; - no representation
1737917379
return Ok(Statement::CreateType {
1738017380
name,
17381-
representation: UserDefinedTypeRepresentation::None,
17381+
representation: None,
1738217382
});
1738317383
}
1738417384

@@ -17400,15 +17400,14 @@ impl<'a> Parser<'a> {
1740017400
/// Parse remainder of `CREATE TYPE AS (attributes)` statement (composite type)
1740117401
///
1740217402
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createtype.html)
17403-
fn parse_create_type_composite(
17404-
&mut self,
17405-
name: ObjectName,
17406-
) -> Result<Statement, ParserError> {
17403+
fn parse_create_type_composite(&mut self, name: ObjectName) -> Result<Statement, ParserError> {
1740717404
if self.consume_token(&Token::RParen) {
1740817405
// Empty composite type
1740917406
return Ok(Statement::CreateType {
1741017407
name,
17411-
representation: UserDefinedTypeRepresentation::Composite { attributes: vec![] },
17408+
representation: Some(UserDefinedTypeRepresentation::Composite {
17409+
attributes: vec![],
17410+
}),
1741217411
});
1741317412
}
1741417413

@@ -17435,7 +17434,7 @@ impl<'a> Parser<'a> {
1743517434

1743617435
Ok(Statement::CreateType {
1743717436
name,
17438-
representation: UserDefinedTypeRepresentation::Composite { attributes },
17437+
representation: Some(UserDefinedTypeRepresentation::Composite { attributes }),
1743917438
})
1744017439
}
1744117440

@@ -17449,7 +17448,7 @@ impl<'a> Parser<'a> {
1744917448

1745017449
Ok(Statement::CreateType {
1745117450
name,
17452-
representation: UserDefinedTypeRepresentation::Enum { labels },
17451+
representation: Some(UserDefinedTypeRepresentation::Enum { labels }),
1745317452
})
1745417453
}
1745517454

@@ -17463,7 +17462,7 @@ impl<'a> Parser<'a> {
1746317462

1746417463
Ok(Statement::CreateType {
1746517464
name,
17466-
representation: UserDefinedTypeRepresentation::Range { options },
17465+
representation: Some(UserDefinedTypeRepresentation::Range { options }),
1746717466
})
1746817467
}
1746917468

tests/sqlparser_common.rs

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11717,7 +11717,7 @@ fn parse_create_type() {
1171711717
representation,
1171811718
} => {
1171911719
assert_eq!(name.to_string(), "mytype");
11720-
assert_eq!(representation, UserDefinedTypeRepresentation::None);
11720+
assert!(representation.is_none());
1172111721
}
1172211722
_ => unreachable!(),
1172311723
}
@@ -11731,7 +11731,7 @@ fn parse_create_type() {
1173111731
} => {
1173211732
assert_eq!(name.to_string(), "address");
1173311733
match representation {
11734-
UserDefinedTypeRepresentation::Composite { attributes } => {
11734+
Some(UserDefinedTypeRepresentation::Composite { attributes }) => {
1173511735
assert_eq!(attributes.len(), 2);
1173611736
assert_eq!(attributes[0].name, Ident::new("street"));
1173711737
assert_eq!(
@@ -11766,7 +11766,7 @@ fn parse_create_type() {
1176611766
} => {
1176711767
assert_eq!(name.to_string(), "mood");
1176811768
match representation {
11769-
UserDefinedTypeRepresentation::Enum { labels } => {
11769+
Some(UserDefinedTypeRepresentation::Enum { labels }) => {
1177011770
assert_eq!(labels.len(), 2);
1177111771
assert_eq!(labels[0], Ident::with_quote('\'', "happy"));
1177211772
assert_eq!(labels[1], Ident::with_quote('\'', "sad"));
@@ -11785,7 +11785,7 @@ fn parse_create_type() {
1178511785
} => {
1178611786
assert_eq!(name.to_string(), "int4range");
1178711787
match representation {
11788-
UserDefinedTypeRepresentation::Range { options } => {
11788+
Some(UserDefinedTypeRepresentation::Range { options }) => {
1178911789
assert_eq!(options.len(), 2);
1179011790
assert!(matches!(
1179111791
options[0],
@@ -11804,6 +11804,103 @@ fn parse_create_type() {
1180411804

1180511805
verified_stmt("CREATE TYPE textrange AS RANGE (SUBTYPE = TEXT, COLLATION = \"en_US\", MULTIRANGE_TYPE_NAME = textmultirange)");
1180611806

11807+
// Test RANGE type with SUBTYPE_OPCLASS - verify AST structure
11808+
match verified_stmt(
11809+
"CREATE TYPE int4range AS RANGE (SUBTYPE = INTEGER, SUBTYPE_OPCLASS = int4_ops)",
11810+
) {
11811+
Statement::CreateType {
11812+
name,
11813+
representation,
11814+
} => {
11815+
assert_eq!(name.to_string(), "int4range");
11816+
match representation {
11817+
Some(UserDefinedTypeRepresentation::Range { options }) => {
11818+
assert_eq!(options.len(), 2);
11819+
assert!(matches!(
11820+
options[0],
11821+
UserDefinedTypeRangeOption::Subtype(DataType::Integer(_))
11822+
));
11823+
match &options[1] {
11824+
UserDefinedTypeRangeOption::SubtypeOpClass(name) => {
11825+
assert_eq!(name.to_string(), "int4_ops");
11826+
}
11827+
_ => unreachable!("Expected SubtypeOpClass"),
11828+
}
11829+
}
11830+
_ => unreachable!(),
11831+
}
11832+
}
11833+
_ => unreachable!(),
11834+
}
11835+
11836+
// Test RANGE type with SUBTYPE_DIFF - verify AST structure
11837+
match verified_stmt(
11838+
"CREATE TYPE int4range AS RANGE (SUBTYPE = INTEGER, SUBTYPE_DIFF = int4range_subdiff)",
11839+
) {
11840+
Statement::CreateType {
11841+
name,
11842+
representation,
11843+
} => {
11844+
assert_eq!(name.to_string(), "int4range");
11845+
match representation {
11846+
Some(UserDefinedTypeRepresentation::Range { options }) => {
11847+
assert_eq!(options.len(), 2);
11848+
assert!(matches!(
11849+
options[0],
11850+
UserDefinedTypeRangeOption::Subtype(DataType::Integer(_))
11851+
));
11852+
match &options[1] {
11853+
UserDefinedTypeRangeOption::SubtypeDiff(name) => {
11854+
assert_eq!(name.to_string(), "int4range_subdiff");
11855+
}
11856+
_ => unreachable!("Expected SubtypeDiff"),
11857+
}
11858+
}
11859+
_ => unreachable!(),
11860+
}
11861+
}
11862+
_ => unreachable!(),
11863+
}
11864+
11865+
// Test RANGE type with all options including SUBTYPE_OPCLASS and SUBTYPE_DIFF
11866+
match verified_stmt(
11867+
"CREATE TYPE int4range AS RANGE (SUBTYPE = INTEGER, SUBTYPE_OPCLASS = int4_ops, CANONICAL = int4range_canonical, SUBTYPE_DIFF = int4range_subdiff, MULTIRANGE_TYPE_NAME = int4multirange)",
11868+
) {
11869+
Statement::CreateType {
11870+
name,
11871+
representation,
11872+
} => {
11873+
assert_eq!(name.to_string(), "int4range");
11874+
match representation {
11875+
Some(UserDefinedTypeRepresentation::Range { options }) => {
11876+
assert_eq!(options.len(), 5);
11877+
assert!(matches!(
11878+
options[0],
11879+
UserDefinedTypeRangeOption::Subtype(DataType::Integer(_))
11880+
));
11881+
assert!(matches!(
11882+
options[1],
11883+
UserDefinedTypeRangeOption::SubtypeOpClass(_)
11884+
));
11885+
assert!(matches!(
11886+
options[2],
11887+
UserDefinedTypeRangeOption::Canonical(_)
11888+
));
11889+
assert!(matches!(
11890+
options[3],
11891+
UserDefinedTypeRangeOption::SubtypeDiff(_)
11892+
));
11893+
assert!(matches!(
11894+
options[4],
11895+
UserDefinedTypeRangeOption::MultirangeTypeName(_)
11896+
));
11897+
}
11898+
_ => unreachable!(),
11899+
}
11900+
}
11901+
_ => unreachable!(),
11902+
}
11903+
1180711904
// Test SQL definition type - verify AST
1180811905
match verified_stmt(
1180911906
"CREATE TYPE mytype (INPUT = in_fn, OUTPUT = out_fn, INTERNALLENGTH = 16, PASSEDBYVALUE)",
@@ -11814,7 +11911,7 @@ fn parse_create_type() {
1181411911
} => {
1181511912
assert_eq!(name.to_string(), "mytype");
1181611913
match representation {
11817-
UserDefinedTypeRepresentation::SqlDefinition { options } => {
11914+
Some(UserDefinedTypeRepresentation::SqlDefinition { options }) => {
1181811915
assert_eq!(options.len(), 4);
1181911916
assert!(matches!(
1182011917
options[0],

tests/sqlparser_postgres.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6223,7 +6223,7 @@ fn parse_create_type_as_enum() {
62236223
match statement {
62246224
Statement::CreateType {
62256225
name,
6226-
representation: UserDefinedTypeRepresentation::Enum { labels },
6226+
representation: Some(UserDefinedTypeRepresentation::Enum { labels }),
62276227
} => {
62286228
assert_eq!("public.my_type", name.to_string());
62296229
assert_eq!(

0 commit comments

Comments
 (0)