Skip to content

Commit a362ed7

Browse files
author
Alex.Mo
committed
use OrderByExpr instead of IndexField
1 parent 982c3af commit a362ed7

File tree

5 files changed

+233
-188
lines changed

5 files changed

+233
-188
lines changed

src/ast/ddl.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ 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, IndexField, MySQLColumnPosition,
36-
ObjectName, OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag,
37-
Value,
35+
FunctionDeterminismSpecifier, FunctionParallel, Ident, MySQLColumnPosition, ObjectName,
36+
OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag, Value,
3837
};
3938
use crate::keywords::Keyword;
4039
use crate::tokenizer::Token;
@@ -833,8 +832,8 @@ pub enum TableConstraint {
833832
///
834833
/// [1]: IndexType
835834
index_type: Option<IndexType>,
836-
/// Index field list.
837-
index_fields: Vec<IndexField>,
835+
/// Index expr list.
836+
index_exprs: Vec<OrderByExpr>,
838837
index_options: Vec<IndexOption>,
839838
characteristics: Option<ConstraintCharacteristics>,
840839
/// Optional Postgres nulls handling: `[ NULLS [ NOT ] DISTINCT ]`
@@ -869,8 +868,8 @@ pub enum TableConstraint {
869868
///
870869
/// [1]: IndexType
871870
index_type: Option<IndexType>,
872-
/// Index field list that form the primary key.
873-
index_fields: Vec<IndexField>,
871+
/// Index expr list.
872+
index_exprs: Vec<OrderByExpr>,
874873
index_options: Vec<IndexOption>,
875874
characteristics: Option<ConstraintCharacteristics>,
876875
},
@@ -908,10 +907,8 @@ pub enum TableConstraint {
908907
///
909908
/// [1]: IndexType
910909
index_type: Option<IndexType>,
911-
/// [Index field list][1].
912-
///
913-
/// [1]: IndexField
914-
index_fields: Vec<IndexField>,
910+
/// Index expr list.
911+
index_exprs: Vec<OrderByExpr>,
915912
},
916913
/// MySQLs [fulltext][1] definition. Since the [`SPATIAL`][2] definition is exactly the same,
917914
/// and MySQL displays both the same way, it is part of this definition as well.
@@ -933,8 +930,8 @@ pub enum TableConstraint {
933930
index_type_display: KeyOrIndexDisplay,
934931
/// Optional index name.
935932
opt_index_name: Option<Ident>,
936-
/// Index field list.
937-
index_fields: Vec<IndexField>,
933+
/// Index expr list.
934+
index_exprs: Vec<OrderByExpr>,
938935
},
939936
}
940937

@@ -946,7 +943,7 @@ impl fmt::Display for TableConstraint {
946943
index_name,
947944
index_type_display,
948945
index_type,
949-
index_fields,
946+
index_exprs,
950947
index_options,
951948
characteristics,
952949
nulls_distinct,
@@ -957,7 +954,7 @@ impl fmt::Display for TableConstraint {
957954
display_constraint_name(name),
958955
display_option_spaced(index_name),
959956
display_option(" USING ", "", index_type),
960-
display_comma_separated(index_fields),
957+
display_comma_separated(index_exprs),
961958
)?;
962959

963960
if !index_options.is_empty() {
@@ -971,7 +968,7 @@ impl fmt::Display for TableConstraint {
971968
name,
972969
index_name,
973970
index_type,
974-
index_fields,
971+
index_exprs,
975972
index_options,
976973
characteristics,
977974
} => {
@@ -981,7 +978,7 @@ impl fmt::Display for TableConstraint {
981978
display_constraint_name(name),
982979
display_option_spaced(index_name),
983980
display_option(" USING ", "", index_type),
984-
display_comma_separated(index_fields),
981+
display_comma_separated(index_exprs),
985982
)?;
986983

987984
if !index_options.is_empty() {
@@ -1028,7 +1025,7 @@ impl fmt::Display for TableConstraint {
10281025
display_as_key,
10291026
name,
10301027
index_type,
1031-
index_fields,
1028+
index_exprs,
10321029
} => {
10331030
write!(f, "{}", if *display_as_key { "KEY" } else { "INDEX" })?;
10341031
if let Some(name) = name {
@@ -1038,15 +1035,15 @@ impl fmt::Display for TableConstraint {
10381035
write!(f, " USING {index_type}")?;
10391036
}
10401037

1041-
write!(f, " ({})", display_comma_separated(index_fields))?;
1038+
write!(f, " ({})", display_comma_separated(index_exprs))?;
10421039

10431040
Ok(())
10441041
}
10451042
Self::FulltextOrSpatial {
10461043
fulltext,
10471044
index_type_display,
10481045
opt_index_name,
1049-
index_fields,
1046+
index_exprs,
10501047
} => {
10511048
if *fulltext {
10521049
write!(f, "FULLTEXT")?;
@@ -1060,7 +1057,7 @@ impl fmt::Display for TableConstraint {
10601057
write!(f, " {name}")?;
10611058
}
10621059

1063-
write!(f, " ({})", display_comma_separated(index_fields))?;
1060+
write!(f, " ({})", display_comma_separated(index_exprs))?;
10641061

10651062
Ok(())
10661063
}

src/ast/mod.rs

+11-55
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,16 @@ pub enum Expr {
10491049
/// [Databricks](https://docs.databricks.com/en/sql/language-manual/sql-ref-lambda-functions.html)
10501050
/// [DuckDb](https://duckdb.org/docs/sql/functions/lambda.html)
10511051
Lambda(LambdaFunction),
1052+
/// A ColumnPrefix used in MySQL indexes.
1053+
/// ```sql
1054+
/// CREATE INDEX ON tbl (col(10));
1055+
/// ```
1056+
///
1057+
/// [MySQL](https://dev.mysql.com/doc/refman/8.0/en/create-index.html)
1058+
ColumnPrefix {
1059+
column: Ident,
1060+
length: u64,
1061+
},
10521062
}
10531063

10541064
/// The contents inside the `[` and `]` in a subscript expression.
@@ -1817,6 +1827,7 @@ impl fmt::Display for Expr {
18171827
}
18181828
Expr::Prior(expr) => write!(f, "PRIOR {expr}"),
18191829
Expr::Lambda(lambda) => write!(f, "{lambda}"),
1830+
Expr::ColumnPrefix { column, length } => write!(f, "{column}({length})"),
18201831
}
18211832
}
18221833
}
@@ -8647,61 +8658,6 @@ pub enum CopyIntoSnowflakeKind {
86478658
Location,
86488659
}
86498660

8650-
/// Index Field
8651-
///
8652-
/// This structure used here [`MySQL` CREATE INDEX][1], [`PostgreSQL` CREATE INDEX][2], [`MySQL` CREATE TABLE][3].
8653-
///
8654-
/// [1]: https://dev.mysql.com/doc/refman/8.3/en/create-index.html
8655-
/// [2]: https://www.postgresql.org/docs/17/sql-createindex.html
8656-
/// [3]: https://dev.mysql.com/doc/refman/8.3/en/create-table.html
8657-
8658-
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
8659-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8660-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8661-
pub struct IndexField {
8662-
pub expr: IndexExpr,
8663-
/// Optional `ASC` or `DESC`
8664-
pub asc: Option<bool>,
8665-
}
8666-
8667-
impl fmt::Display for IndexField {
8668-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8669-
write!(f, "{}", self.expr)?;
8670-
match self.asc {
8671-
Some(true) => write!(f, " ASC")?,
8672-
Some(false) => write!(f, " DESC")?,
8673-
None => (),
8674-
}
8675-
Ok(())
8676-
}
8677-
}
8678-
8679-
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
8680-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8681-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8682-
pub enum IndexExpr {
8683-
Column(Ident),
8684-
/// Mysql specific syntax
8685-
///
8686-
/// See [Mysql documentation](https://dev.mysql.com/doc/refman/8.3/en/create-index.html)
8687-
/// for more details.
8688-
ColumnPrefix {
8689-
column: Ident,
8690-
length: u64,
8691-
},
8692-
Functional(Box<Expr>),
8693-
}
8694-
8695-
impl fmt::Display for IndexExpr {
8696-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8697-
match self {
8698-
IndexExpr::Column(column) => write!(f, "{}", column),
8699-
IndexExpr::ColumnPrefix { column, length } => write!(f, "{column}({length})"),
8700-
IndexExpr::Functional(expr) => write!(f, "({expr})"),
8701-
}
8702-
}
8703-
}
8704-
87058661
#[cfg(test)]
87068662
mod tests {
87078663
use super::*;

src/ast/spans.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -628,27 +628,29 @@ impl Spanned for TableConstraint {
628628
index_name,
629629
index_type_display: _,
630630
index_type: _,
631-
index_fields: _,
631+
index_exprs,
632632
index_options: _,
633633
characteristics,
634634
nulls_distinct: _,
635635
} => union_spans(
636636
name.iter()
637637
.map(|i| i.span)
638638
.chain(index_name.iter().map(|i| i.span))
639+
.chain(index_exprs.iter().map(|i| i.span()))
639640
.chain(characteristics.iter().map(|i| i.span())),
640641
),
641642
TableConstraint::PrimaryKey {
642643
name,
643644
index_name,
644645
index_type: _,
645-
index_fields: _,
646+
index_exprs,
646647
index_options: _,
647648
characteristics,
648649
} => union_spans(
649650
name.iter()
650651
.map(|i| i.span)
651652
.chain(index_name.iter().map(|i| i.span))
653+
.chain(index_exprs.iter().map(|i| i.span()))
652654
.chain(characteristics.iter().map(|i| i.span())),
653655
),
654656
TableConstraint::ForeignKey {
@@ -676,14 +678,23 @@ impl Spanned for TableConstraint {
676678
display_as_key: _,
677679
name,
678680
index_type: _,
679-
index_fields: _,
680-
} => union_spans(name.iter().map(|i| i.span)),
681+
index_exprs,
682+
} => union_spans(
683+
name.iter()
684+
.map(|i| i.span)
685+
.chain(index_exprs.iter().map(|i| i.span())),
686+
),
681687
TableConstraint::FulltextOrSpatial {
682688
fulltext: _,
683689
index_type_display: _,
684690
opt_index_name,
685-
index_fields: _,
686-
} => union_spans(opt_index_name.iter().map(|i| i.span)),
691+
index_exprs,
692+
} => union_spans(
693+
opt_index_name
694+
.iter()
695+
.map(|i| i.span)
696+
.chain(index_exprs.iter().map(|i| i.span())),
697+
),
687698
}
688699
}
689700
}
@@ -1468,6 +1479,7 @@ impl Spanned for Expr {
14681479
Expr::OuterJoin(expr) => expr.span(),
14691480
Expr::Prior(expr) => expr.span(),
14701481
Expr::Lambda(_) => Span::empty(),
1482+
Expr::ColumnPrefix { .. } => Span::empty(),
14711483
}
14721484
}
14731485
}

0 commit comments

Comments
 (0)