Skip to content

Commit d2092d6

Browse files
author
Alex.Mo
committed
introduce IndexExpr
1 parent 9991ea1 commit d2092d6

File tree

5 files changed

+185
-98
lines changed

5 files changed

+185
-98
lines changed

src/ast/ddl.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ use crate::ast::value::escape_single_quote_string;
3232
use crate::ast::{
3333
display_comma_separated, display_separated, CommentDef, CreateFunctionBody,
3434
CreateFunctionUsing, DataType, Expr, FunctionBehavior, FunctionCalledOnNull,
35-
FunctionDeterminismSpecifier, FunctionParallel, Ident, MySQLColumnPosition, ObjectName,
36-
OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag, Value,
35+
FunctionDeterminismSpecifier, FunctionParallel, Ident, IndexExpr, MySQLColumnPosition,
36+
ObjectName, OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag,
37+
Value,
3738
};
3839
use crate::keywords::Keyword;
3940
use crate::tokenizer::Token;
@@ -833,7 +834,7 @@ pub enum TableConstraint {
833834
/// [1]: IndexType
834835
index_type: Option<IndexType>,
835836
/// Index expr list.
836-
index_exprs: Vec<OrderByExpr>,
837+
index_exprs: Vec<IndexExpr>,
837838
index_options: Vec<IndexOption>,
838839
characteristics: Option<ConstraintCharacteristics>,
839840
/// Optional Postgres nulls handling: `[ NULLS [ NOT ] DISTINCT ]`
@@ -869,7 +870,7 @@ pub enum TableConstraint {
869870
/// [1]: IndexType
870871
index_type: Option<IndexType>,
871872
/// Index expr list.
872-
index_exprs: Vec<OrderByExpr>,
873+
index_exprs: Vec<IndexExpr>,
873874
index_options: Vec<IndexOption>,
874875
characteristics: Option<ConstraintCharacteristics>,
875876
},
@@ -908,7 +909,7 @@ pub enum TableConstraint {
908909
/// [1]: IndexType
909910
index_type: Option<IndexType>,
910911
/// Index expr list.
911-
index_exprs: Vec<OrderByExpr>,
912+
index_exprs: Vec<IndexExpr>,
912913
},
913914
/// MySQLs [fulltext][1] definition. Since the [`SPATIAL`][2] definition is exactly the same,
914915
/// and MySQL displays both the same way, it is part of this definition as well.
@@ -931,7 +932,7 @@ pub enum TableConstraint {
931932
/// Optional index name.
932933
opt_index_name: Option<Ident>,
933934
/// Index expr list.
934-
index_exprs: Vec<OrderByExpr>,
935+
index_exprs: Vec<IndexExpr>,
935936
},
936937
}
937938

src/ast/mod.rs

+31
Original file line numberDiff line numberDiff line change
@@ -8701,6 +8701,37 @@ pub enum CopyIntoSnowflakeKind {
87018701
Location,
87028702
}
87038703

8704+
/// Index Field
8705+
///
8706+
/// This structure used here [`MySQL` CREATE INDEX][1], [`PostgreSQL` CREATE INDEX][2], [`MySQL` CREATE TABLE][3].
8707+
///
8708+
/// [1]: https://dev.mysql.com/doc/refman/8.3/en/create-index.html
8709+
/// [2]: https://www.postgresql.org/docs/17/sql-createindex.html
8710+
/// [3]: https://dev.mysql.com/doc/refman/8.3/en/create-table.html
8711+
8712+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
8713+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8714+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8715+
pub struct IndexExpr {
8716+
pub expr: Expr,
8717+
pub collation: Option<ObjectName>,
8718+
pub operator_class: Option<Expr>,
8719+
pub order_options: OrderByOptions,
8720+
}
8721+
8722+
impl fmt::Display for IndexExpr {
8723+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8724+
write!(f, "{}", self.expr)?;
8725+
if let Some(collation) = &self.collation {
8726+
write!(f, "{collation}")?;
8727+
}
8728+
if let Some(operator) = &self.operator_class {
8729+
write!(f, "{operator}")?;
8730+
}
8731+
write!(f, "{}", self.order_options)
8732+
}
8733+
}
8734+
87048735
#[cfg(test)]
87058736
mod tests {
87068737
use super::*;

src/ast/spans.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use super::{
2727
CopySource, CreateIndex, CreateTable, CreateTableOptions, Cte, Delete, DoUpdate,
2828
ExceptSelectItem, ExcludeSelectItem, Expr, ExprWithAlias, Fetch, FromTable, Function,
2929
FunctionArg, FunctionArgExpr, FunctionArgumentClause, FunctionArgumentList, FunctionArguments,
30-
GroupByExpr, HavingBound, IlikeSelectItem, Insert, Interpolate, InterpolateExpr, Join,
31-
JoinConstraint, JoinOperator, JsonPath, JsonPathElem, LateralView, MatchRecognizePattern,
30+
GroupByExpr, HavingBound, IlikeSelectItem, IndexExpr, Insert, Interpolate, InterpolateExpr,
31+
Join, JoinConstraint, JoinOperator, JsonPath, JsonPathElem, LateralView, MatchRecognizePattern,
3232
Measure, NamedWindowDefinition, ObjectName, ObjectNamePart, Offset, OnConflict,
3333
OnConflictAction, OnInsert, OrderBy, OrderByExpr, OrderByKind, Partition, PivotValueSource,
3434
ProjectionSelect, Query, ReferentialAction, RenameSelectItem, ReplaceSelectElement,
@@ -2179,6 +2179,19 @@ impl Spanned for TableObject {
21792179
}
21802180
}
21812181

2182+
impl Spanned for IndexExpr {
2183+
fn span(&self) -> Span {
2184+
let IndexExpr {
2185+
expr,
2186+
collation: _,
2187+
operator_class: _,
2188+
order_options: _,
2189+
} = self;
2190+
2191+
union_spans(core::iter::once(expr.span()))
2192+
}
2193+
}
2194+
21822195
#[cfg(test)]
21832196
pub mod tests {
21842197
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};

0 commit comments

Comments
 (0)