Skip to content

Commit 5bdb563

Browse files
Merge branch 'main' into extended-create-func
2 parents 848e117 + 1114d6a commit 5bdb563

File tree

6 files changed

+987
-53
lines changed

6 files changed

+987
-53
lines changed

src/ast/ddl.rs

Lines changed: 237 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
//! (commonly referred to as Data Definition Language, or DDL)
2020
2121
#[cfg(not(feature = "std"))]
22-
use alloc::{boxed::Box, format, string::String, vec, vec::Vec};
22+
use alloc::{
23+
boxed::Box,
24+
format,
25+
string::{String, ToString},
26+
vec,
27+
vec::Vec,
28+
};
2329
use core::fmt::{self, Display, Write};
2430

2531
#[cfg(feature = "serde")]
@@ -3960,3 +3966,233 @@ impl Spanned for DropFunction {
39603966
Span::empty()
39613967
}
39623968
}
3969+
3970+
/// CREATE OPERATOR statement
3971+
/// See <https://www.postgresql.org/docs/current/sql-createoperator.html>
3972+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
3973+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
3974+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
3975+
pub struct CreateOperator {
3976+
/// Operator name (can be schema-qualified)
3977+
pub name: ObjectName,
3978+
/// FUNCTION or PROCEDURE parameter (function name)
3979+
pub function: ObjectName,
3980+
/// Whether PROCEDURE keyword was used (vs FUNCTION)
3981+
pub is_procedure: bool,
3982+
/// LEFTARG parameter (left operand type)
3983+
pub left_arg: Option<DataType>,
3984+
/// RIGHTARG parameter (right operand type)
3985+
pub right_arg: Option<DataType>,
3986+
/// COMMUTATOR parameter (commutator operator)
3987+
pub commutator: Option<ObjectName>,
3988+
/// NEGATOR parameter (negator operator)
3989+
pub negator: Option<ObjectName>,
3990+
/// RESTRICT parameter (restriction selectivity function)
3991+
pub restrict: Option<ObjectName>,
3992+
/// JOIN parameter (join selectivity function)
3993+
pub join: Option<ObjectName>,
3994+
/// HASHES flag
3995+
pub hashes: bool,
3996+
/// MERGES flag
3997+
pub merges: bool,
3998+
}
3999+
4000+
/// CREATE OPERATOR FAMILY statement
4001+
/// See <https://www.postgresql.org/docs/current/sql-createopfamily.html>
4002+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4003+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4004+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4005+
pub struct CreateOperatorFamily {
4006+
/// Operator family name (can be schema-qualified)
4007+
pub name: ObjectName,
4008+
/// Index method (btree, hash, gist, gin, etc.)
4009+
pub using: Ident,
4010+
}
4011+
4012+
/// CREATE OPERATOR CLASS statement
4013+
/// See <https://www.postgresql.org/docs/current/sql-createopclass.html>
4014+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4015+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4016+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4017+
pub struct CreateOperatorClass {
4018+
/// Operator class name (can be schema-qualified)
4019+
pub name: ObjectName,
4020+
/// Whether this is the default operator class for the type
4021+
pub default: bool,
4022+
/// The data type
4023+
pub for_type: DataType,
4024+
/// Index method (btree, hash, gist, gin, etc.)
4025+
pub using: Ident,
4026+
/// Optional operator family name
4027+
pub family: Option<ObjectName>,
4028+
/// List of operator class items (operators, functions, storage)
4029+
pub items: Vec<OperatorClassItem>,
4030+
}
4031+
4032+
impl fmt::Display for CreateOperator {
4033+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4034+
write!(f, "CREATE OPERATOR {} (", self.name)?;
4035+
4036+
let function_keyword = if self.is_procedure {
4037+
"PROCEDURE"
4038+
} else {
4039+
"FUNCTION"
4040+
};
4041+
let mut params = vec![format!("{} = {}", function_keyword, self.function)];
4042+
4043+
if let Some(left_arg) = &self.left_arg {
4044+
params.push(format!("LEFTARG = {}", left_arg));
4045+
}
4046+
if let Some(right_arg) = &self.right_arg {
4047+
params.push(format!("RIGHTARG = {}", right_arg));
4048+
}
4049+
if let Some(commutator) = &self.commutator {
4050+
params.push(format!("COMMUTATOR = {}", commutator));
4051+
}
4052+
if let Some(negator) = &self.negator {
4053+
params.push(format!("NEGATOR = {}", negator));
4054+
}
4055+
if let Some(restrict) = &self.restrict {
4056+
params.push(format!("RESTRICT = {}", restrict));
4057+
}
4058+
if let Some(join) = &self.join {
4059+
params.push(format!("JOIN = {}", join));
4060+
}
4061+
if self.hashes {
4062+
params.push("HASHES".to_string());
4063+
}
4064+
if self.merges {
4065+
params.push("MERGES".to_string());
4066+
}
4067+
4068+
write!(f, "{}", params.join(", "))?;
4069+
write!(f, ")")
4070+
}
4071+
}
4072+
4073+
impl fmt::Display for CreateOperatorFamily {
4074+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4075+
write!(
4076+
f,
4077+
"CREATE OPERATOR FAMILY {} USING {}",
4078+
self.name, self.using
4079+
)
4080+
}
4081+
}
4082+
4083+
impl fmt::Display for CreateOperatorClass {
4084+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4085+
write!(f, "CREATE OPERATOR CLASS {}", self.name)?;
4086+
if self.default {
4087+
write!(f, " DEFAULT")?;
4088+
}
4089+
write!(f, " FOR TYPE {} USING {}", self.for_type, self.using)?;
4090+
if let Some(family) = &self.family {
4091+
write!(f, " FAMILY {}", family)?;
4092+
}
4093+
write!(f, " AS {}", display_comma_separated(&self.items))
4094+
}
4095+
}
4096+
4097+
/// Operator argument types for CREATE OPERATOR CLASS
4098+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4099+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4100+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4101+
pub struct OperatorArgTypes {
4102+
pub left: DataType,
4103+
pub right: DataType,
4104+
}
4105+
4106+
impl fmt::Display for OperatorArgTypes {
4107+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4108+
write!(f, "{}, {}", self.left, self.right)
4109+
}
4110+
}
4111+
4112+
/// An item in a CREATE OPERATOR CLASS statement
4113+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4114+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4115+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4116+
pub enum OperatorClassItem {
4117+
/// OPERATOR clause
4118+
Operator {
4119+
strategy_number: u32,
4120+
operator_name: ObjectName,
4121+
/// Optional operator argument types
4122+
op_types: Option<OperatorArgTypes>,
4123+
/// FOR SEARCH or FOR ORDER BY
4124+
purpose: Option<OperatorPurpose>,
4125+
},
4126+
/// FUNCTION clause
4127+
Function {
4128+
support_number: u32,
4129+
/// Optional function argument types for the operator class
4130+
op_types: Option<Vec<DataType>>,
4131+
function_name: ObjectName,
4132+
/// Function argument types
4133+
argument_types: Vec<DataType>,
4134+
},
4135+
/// STORAGE clause
4136+
Storage { storage_type: DataType },
4137+
}
4138+
4139+
/// Purpose of an operator in an operator class
4140+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4141+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4142+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4143+
pub enum OperatorPurpose {
4144+
ForSearch,
4145+
ForOrderBy { sort_family: ObjectName },
4146+
}
4147+
4148+
impl fmt::Display for OperatorClassItem {
4149+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4150+
match self {
4151+
OperatorClassItem::Operator {
4152+
strategy_number,
4153+
operator_name,
4154+
op_types,
4155+
purpose,
4156+
} => {
4157+
write!(f, "OPERATOR {strategy_number} {operator_name}")?;
4158+
if let Some(types) = op_types {
4159+
write!(f, " ({types})")?;
4160+
}
4161+
if let Some(purpose) = purpose {
4162+
write!(f, " {purpose}")?;
4163+
}
4164+
Ok(())
4165+
}
4166+
OperatorClassItem::Function {
4167+
support_number,
4168+
op_types,
4169+
function_name,
4170+
argument_types,
4171+
} => {
4172+
write!(f, "FUNCTION {support_number}")?;
4173+
if let Some(types) = op_types {
4174+
write!(f, " ({})", display_comma_separated(types))?;
4175+
}
4176+
write!(f, " {function_name}")?;
4177+
if !argument_types.is_empty() {
4178+
write!(f, "({})", display_comma_separated(argument_types))?;
4179+
}
4180+
Ok(())
4181+
}
4182+
OperatorClassItem::Storage { storage_type } => {
4183+
write!(f, "STORAGE {storage_type}")
4184+
}
4185+
}
4186+
}
4187+
}
4188+
4189+
impl fmt::Display for OperatorPurpose {
4190+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4191+
match self {
4192+
OperatorPurpose::ForSearch => write!(f, "FOR SEARCH"),
4193+
OperatorPurpose::ForOrderBy { sort_family } => {
4194+
write!(f, "FOR ORDER BY {sort_family}")
4195+
}
4196+
}
4197+
}
4198+
}

src/ast/mod.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ pub use self::ddl::{
6565
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
6666
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
6767
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,
68+
CreateExtension, CreateFunction, CreateIndex, CreateOperator, CreateOperatorClass,
69+
CreateOperatorFamily, CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial,
70+
DropBehavior, DropExtension, DropFunction, DropTrigger, GeneratedAs, GeneratedExpressionMode,
71+
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
72+
IdentityPropertyOrder, IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck,
73+
NullsDistinctOption, OperatorArgTypes, OperatorClassItem, OperatorPurpose, Owner, Partition,
7374
ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
7475
TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
7576
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
@@ -3347,6 +3348,21 @@ pub enum Statement {
33473348
/// See [Hive](https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=27362034#LanguageManualDDL-CreateDataConnectorCreateConnector)
33483349
CreateConnector(CreateConnector),
33493350
/// ```sql
3351+
/// CREATE OPERATOR
3352+
/// ```
3353+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createoperator.html)
3354+
CreateOperator(CreateOperator),
3355+
/// ```sql
3356+
/// CREATE OPERATOR FAMILY
3357+
/// ```
3358+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createopfamily.html)
3359+
CreateOperatorFamily(CreateOperatorFamily),
3360+
/// ```sql
3361+
/// CREATE OPERATOR CLASS
3362+
/// ```
3363+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createopclass.html)
3364+
CreateOperatorClass(CreateOperatorClass),
3365+
/// ```sql
33503366
/// ALTER TABLE
33513367
/// ```
33523368
AlterTable(AlterTable),
@@ -4901,6 +4917,11 @@ impl fmt::Display for Statement {
49014917
Ok(())
49024918
}
49034919
Statement::CreateConnector(create_connector) => create_connector.fmt(f),
4920+
Statement::CreateOperator(create_operator) => create_operator.fmt(f),
4921+
Statement::CreateOperatorFamily(create_operator_family) => {
4922+
create_operator_family.fmt(f)
4923+
}
4924+
Statement::CreateOperatorClass(create_operator_class) => create_operator_class.fmt(f),
49044925
Statement::AlterTable(alter_table) => write!(f, "{alter_table}"),
49054926
Statement::AlterIndex { name, operation } => {
49064927
write!(f, "ALTER INDEX {name} {operation}")

src/ast/spans.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
use crate::ast::{
1919
ddl::AlterSchema, query::SelectItemQualifiedWildcardKind, AlterSchemaOperation, AlterTable,
20-
ColumnOptions, CreateView, ExportData, Owner, TypedString,
20+
ColumnOptions, CreateOperator, CreateOperatorClass, CreateOperatorFamily, CreateView,
21+
ExportData, Owner, TypedString,
2122
};
2223
use core::iter;
2324

@@ -368,6 +369,11 @@ impl Spanned for Statement {
368369
Statement::CreateSecret { .. } => Span::empty(),
369370
Statement::CreateServer { .. } => Span::empty(),
370371
Statement::CreateConnector { .. } => Span::empty(),
372+
Statement::CreateOperator(create_operator) => create_operator.span(),
373+
Statement::CreateOperatorFamily(create_operator_family) => {
374+
create_operator_family.span()
375+
}
376+
Statement::CreateOperatorClass(create_operator_class) => create_operator_class.span(),
371377
Statement::AlterTable(alter_table) => alter_table.span(),
372378
Statement::AlterIndex { name, operation } => name.span().union(&operation.span()),
373379
Statement::AlterView {
@@ -2357,6 +2363,24 @@ impl Spanned for AlterTable {
23572363
}
23582364
}
23592365

2366+
impl Spanned for CreateOperator {
2367+
fn span(&self) -> Span {
2368+
Span::empty()
2369+
}
2370+
}
2371+
2372+
impl Spanned for CreateOperatorFamily {
2373+
fn span(&self) -> Span {
2374+
Span::empty()
2375+
}
2376+
}
2377+
2378+
impl Spanned for CreateOperatorClass {
2379+
fn span(&self) -> Span {
2380+
Span::empty()
2381+
}
2382+
}
2383+
23602384
#[cfg(test)]
23612385
pub mod tests {
23622386
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};

0 commit comments

Comments
 (0)