From 5e1d9f8d6e9e3904425f03806ac80df8e61ae276 Mon Sep 17 00:00:00 2001 From: Augusto Fotino <75763288+AugustoFKL@users.noreply.github.com> Date: Wed, 30 Nov 2022 14:25:59 -0300 Subject: [PATCH] Derive `PartialOrd`, `Ord`, and `Copy` whenever possible (#717) This allow other projects to use our structures inside others that need those. --- src/ast/data_type.rs | 10 ++-- src/ast/ddl.rs | 18 +++--- src/ast/mod.rs | 114 ++++++++++++++++++------------------ src/ast/operator.rs | 4 +- src/ast/query.rs | 46 +++++++-------- src/ast/value.rs | 6 +- src/parser.rs | 2 +- src/tokenizer.rs | 6 +- tests/sqlparser_mysql.rs | 2 +- tests/sqlparser_postgres.rs | 4 +- 10 files changed, 106 insertions(+), 106 deletions(-) diff --git a/src/ast/data_type.rs b/src/ast/data_type.rs index 12a95a4fc..1353eca90 100644 --- a/src/ast/data_type.rs +++ b/src/ast/data_type.rs @@ -22,7 +22,7 @@ use crate::ast::ObjectName; use super::value::escape_single_quote_string; /// SQL data types -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum DataType { /// Fixed-length character type e.g. CHARACTER(10) @@ -335,7 +335,7 @@ fn format_datetime_precision_and_tz( /// /// This is more related to a display information than real differences between each variant. To /// guarantee compatibility with the input query we must maintain its exact information. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum TimezoneInfo { /// No information about time zone. E.g., TIMESTAMP @@ -382,7 +382,7 @@ impl fmt::Display for TimezoneInfo { /// following the 2016 [standard]. /// /// [standard]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#exact-numeric-type -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ExactNumberInfo { /// No additional information e.g. `DECIMAL` @@ -412,7 +412,7 @@ impl fmt::Display for ExactNumberInfo { /// Information about [character length][1], including length and possibly unit. /// /// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#character-length -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct CharacterLength { /// Default (if VARYING) or maximum (if not VARYING) length @@ -434,7 +434,7 @@ impl fmt::Display for CharacterLength { /// Possible units for characters, initially based on 2016 ANSI [standard][1]. /// /// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#char-length-units -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum CharLengthUnits { /// CHARACTERS unit diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index 3c2bc3427..00bf83aba 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -25,7 +25,7 @@ use crate::ast::{display_comma_separated, display_separated, DataType, Expr, Ide use crate::tokenizer::Token; /// An `ALTER TABLE` (`Statement::AlterTable`) operation -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum AlterTableOperation { /// `ADD ` @@ -201,7 +201,7 @@ impl fmt::Display for AlterTableOperation { } /// An `ALTER COLUMN` (`Statement::AlterTable`) operation -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum AlterColumnOperation { /// `SET NOT NULL` @@ -244,7 +244,7 @@ impl fmt::Display for AlterColumnOperation { /// A table-level constraint, specified in a `CREATE TABLE` or an /// `ALTER TABLE ADD ` statement. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum TableConstraint { /// `[ CONSTRAINT ] { PRIMARY KEY | UNIQUE } ()` @@ -407,7 +407,7 @@ impl fmt::Display for TableConstraint { /// statements of `MySQL` [(1)]. /// /// [1]: https://dev.mysql.com/doc/refman/8.0/en/create-table.html -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum KeyOrIndexDisplay { /// Nothing to display @@ -442,7 +442,7 @@ impl fmt::Display for KeyOrIndexDisplay { /// [1]: https://dev.mysql.com/doc/refman/8.0/en/create-table.html /// [2]: https://dev.mysql.com/doc/refman/8.0/en/create-index.html /// [3]: https://www.postgresql.org/docs/14/sql-createindex.html -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum IndexType { BTree, @@ -460,7 +460,7 @@ impl fmt::Display for IndexType { } /// SQL column definition -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ColumnDef { pub name: Ident, @@ -495,7 +495,7 @@ impl fmt::Display for ColumnDef { /// For maximum flexibility, we don't distinguish between constraint and /// non-constraint options, lumping them all together under the umbrella of /// "column options," and we allow any column option to be named. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ColumnOptionDef { pub name: Option, @@ -510,7 +510,7 @@ impl fmt::Display for ColumnOptionDef { /// `ColumnOption`s are modifiers that follow a column definition in a `CREATE /// TABLE` statement. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ColumnOption { /// `NULL` @@ -597,7 +597,7 @@ fn display_constraint_name(name: &'_ Option) -> impl fmt::Display + '_ { /// { RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT }` /// /// Used in foreign key constraints in `ON UPDATE` and `ON DELETE` options. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ReferentialAction { Restrict, diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 7fa320d3f..226dc8b43 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -82,7 +82,7 @@ where } /// An identifier, decomposed into its value or character data and the quote style. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Ident { /// The value of the identifier without quotes. @@ -142,7 +142,7 @@ impl fmt::Display for Ident { } /// A name of a table, view, custom type, etc., possibly multi-part, i.e. db.schema.obj -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ObjectName(pub Vec); @@ -152,7 +152,7 @@ impl fmt::Display for ObjectName { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] /// Represents an Array Expression, either /// `ARRAY[..]`, or `[..]` @@ -176,7 +176,7 @@ impl fmt::Display for Array { } /// JsonOperator -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum JsonOperator { /// -> keeps the value as json @@ -218,7 +218,7 @@ impl fmt::Display for JsonOperator { /// The parser does not distinguish between expressions of different types /// (e.g. boolean vs string), so the caller must handle expressions of /// inappropriate type, like `WHERE 1` or `SELECT 1=1`, as necessary. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Expr { /// Identifier e.g. table name or column name @@ -858,7 +858,7 @@ impl fmt::Display for Expr { } /// A window specification (i.e. `OVER (PARTITION BY .. ORDER BY .. etc.)`) -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct WindowSpec { pub partition_by: Vec, @@ -903,7 +903,7 @@ impl fmt::Display for WindowSpec { /// /// Note: The parser does not validate the specified bounds; the caller should /// reject invalid bounds like `ROWS UNBOUNDED FOLLOWING` before execution. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct WindowFrame { pub units: WindowFrameUnits, @@ -928,7 +928,7 @@ impl Default for WindowFrame { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum WindowFrameUnits { Rows, @@ -947,7 +947,7 @@ impl fmt::Display for WindowFrameUnits { } /// Specifies [WindowFrame]'s `start_bound` and `end_bound` -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum WindowFrameBound { /// `CURRENT ROW` @@ -970,7 +970,7 @@ impl fmt::Display for WindowFrameBound { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum AddDropSync { ADD, @@ -988,7 +988,7 @@ impl fmt::Display for AddDropSync { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ShowCreateObject { Event, @@ -1012,7 +1012,7 @@ impl fmt::Display for ShowCreateObject { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum CommentObject { Column, @@ -1028,7 +1028,7 @@ impl fmt::Display for CommentObject { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Password { Password(Expr), @@ -1037,7 +1037,7 @@ pub enum Password { /// A top-level statement (SELECT, INSERT, CREATE, etc.) #[allow(clippy::large_enum_variant)] -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Statement { /// Analyze (Hive) @@ -2591,12 +2591,12 @@ impl fmt::Display for Statement { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] /// Can use to describe options in create sequence or table column type identity /// [ INCREMENT [ BY ] increment ] /// [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ] /// [ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum SequenceOptions { IncrementBy(Expr, bool), MinValue(MinMaxValue), @@ -2657,10 +2657,10 @@ impl fmt::Display for SequenceOptions { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] /// Can use to describe options in create sequence or table column type identity /// [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum MinMaxValue { // clause is not specified Empty, @@ -2670,7 +2670,7 @@ pub enum MinMaxValue { Some(Expr), } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[non_exhaustive] pub enum OnInsert { @@ -2680,13 +2680,13 @@ pub enum OnInsert { OnConflict(OnConflict), } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct OnConflict { pub conflict_target: Vec, pub action: OnConflictAction, } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum OnConflictAction { DoNothing, @@ -2724,7 +2724,7 @@ impl fmt::Display for OnConflictAction { } /// Privileges granted in a GRANT statement or revoked in a REVOKE statement. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Privileges { /// All privileges applicable to the object type @@ -2760,7 +2760,7 @@ impl fmt::Display for Privileges { } /// Specific direction for FETCH statement -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum FetchDirection { Count { limit: Value }, @@ -2823,7 +2823,7 @@ impl fmt::Display for FetchDirection { } /// A privilege on a database object (table, sequence, etc.). -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Action { Connect, @@ -2872,7 +2872,7 @@ impl fmt::Display for Action { } /// Objects on which privileges are granted in a GRANT statement. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum GrantObjects { /// Grant privileges on `ALL SEQUENCES IN SCHEMA [, ...]` @@ -2918,7 +2918,7 @@ impl fmt::Display for GrantObjects { } /// SQL assignment `foo = expr` as used in SQLUpdate -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Assignment { pub id: Vec, @@ -2931,7 +2931,7 @@ impl fmt::Display for Assignment { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum FunctionArgExpr { Expr(Expr), @@ -2951,7 +2951,7 @@ impl fmt::Display for FunctionArgExpr { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum FunctionArg { Named { name: Ident, arg: FunctionArgExpr }, @@ -2967,7 +2967,7 @@ impl fmt::Display for FunctionArg { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum CloseCursor { All, @@ -2984,7 +2984,7 @@ impl fmt::Display for CloseCursor { } /// A function call -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Function { pub name: ObjectName, @@ -2997,7 +2997,7 @@ pub struct Function { pub special: bool, } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum AnalyzeFormat { TEXT, @@ -3038,7 +3038,7 @@ impl fmt::Display for Function { } /// External table's available file format -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum FileFormat { TEXTFILE, @@ -3067,7 +3067,7 @@ impl fmt::Display for FileFormat { /// A `LISTAGG` invocation `LISTAGG( [ DISTINCT ] [, ] [ON OVERFLOW ] ) ) /// [ WITHIN GROUP (ORDER BY [, ...] ) ]` -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ListAgg { pub distinct: bool, @@ -3104,7 +3104,7 @@ impl fmt::Display for ListAgg { } /// The `ON OVERFLOW` clause of a LISTAGG invocation -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ListAggOnOverflow { /// `ON OVERFLOW ERROR` @@ -3141,7 +3141,7 @@ impl fmt::Display for ListAggOnOverflow { /// An `ARRAY_AGG` invocation `ARRAY_AGG( [ DISTINCT ] [ORDER BY ] [LIMIT ] )` /// Or `ARRAY_AGG( [ DISTINCT ] ) [ WITHIN GROUP ( ORDER BY ) ]` /// ORDER BY position is defined differently for BigQuery, Postgres and Snowflake. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct ArrayAgg { pub distinct: bool, @@ -3177,7 +3177,7 @@ impl fmt::Display for ArrayAgg { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ObjectType { Table, @@ -3201,7 +3201,7 @@ impl fmt::Display for ObjectType { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum KillType { Connection, @@ -3221,7 +3221,7 @@ impl fmt::Display for KillType { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum HiveDistributionStyle { PARTITIONED { @@ -3240,7 +3240,7 @@ pub enum HiveDistributionStyle { NONE, } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum HiveRowFormat { SERDE { class: String }, @@ -3248,7 +3248,7 @@ pub enum HiveRowFormat { } #[allow(clippy::large_enum_variant)] -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[allow(clippy::large_enum_variant)] pub enum HiveIOFormat { @@ -3261,7 +3261,7 @@ pub enum HiveIOFormat { }, } -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash, Default)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct HiveFormat { pub row_format: Option, @@ -3269,7 +3269,7 @@ pub struct HiveFormat { pub location: Option, } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct SqlOption { pub name: Ident, @@ -3282,7 +3282,7 @@ impl fmt::Display for SqlOption { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum TransactionMode { AccessMode(TransactionAccessMode), @@ -3299,7 +3299,7 @@ impl fmt::Display for TransactionMode { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum TransactionAccessMode { ReadOnly, @@ -3316,7 +3316,7 @@ impl fmt::Display for TransactionAccessMode { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum TransactionIsolationLevel { ReadUncommitted, @@ -3337,7 +3337,7 @@ impl fmt::Display for TransactionIsolationLevel { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ShowStatementFilter { Like(String), @@ -3359,7 +3359,7 @@ impl fmt::Display for ShowStatementFilter { /// Sqlite specific syntax /// /// https://sqlite.org/lang_conflict.html -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum SqliteOnConflict { Rollback, @@ -3382,7 +3382,7 @@ impl fmt::Display for SqliteOnConflict { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum CopyTarget { Stdin, @@ -3413,7 +3413,7 @@ impl fmt::Display for CopyTarget { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum OnCommit { DeleteRows, @@ -3424,7 +3424,7 @@ pub enum OnCommit { /// An option in `COPY` statement. /// /// -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum CopyOption { /// FORMAT format_name @@ -3477,7 +3477,7 @@ impl fmt::Display for CopyOption { /// An option in `COPY` statement before PostgreSQL version 9.0. /// /// -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum CopyLegacyOption { /// BINARY @@ -3505,7 +3505,7 @@ impl fmt::Display for CopyLegacyOption { /// A `CSV` option in `COPY` statement before PostgreSQL version 9.0. /// /// -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum CopyLegacyCsvOption { /// HEADER @@ -3536,7 +3536,7 @@ impl fmt::Display for CopyLegacyCsvOption { } /// -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum MergeClause { MatchedUpdate { @@ -3597,7 +3597,7 @@ impl fmt::Display for MergeClause { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum DiscardObject { ALL, @@ -3618,7 +3618,7 @@ impl fmt::Display for DiscardObject { } /// Optional context modifier for statements that can be or `LOCAL`, or `SESSION`. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum ContextModifier { /// No context defined. Each dialect defines the default in this scenario. @@ -3645,7 +3645,7 @@ impl fmt::Display for ContextModifier { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum CreateFunctionUsing { Jar(String), @@ -3667,7 +3667,7 @@ impl fmt::Display for CreateFunctionUsing { /// Schema possible naming variants ([1]). /// /// [1]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#schema-definition -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum SchemaName { /// Only schema name specified: ``. diff --git a/src/ast/operator.rs b/src/ast/operator.rs index 1c96ebbcb..f22839474 100644 --- a/src/ast/operator.rs +++ b/src/ast/operator.rs @@ -20,7 +20,7 @@ use serde::{Deserialize, Serialize}; use super::display_separated; /// Unary operators -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum UnaryOperator { Plus, @@ -57,7 +57,7 @@ impl fmt::Display for UnaryOperator { } /// Binary operators -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum BinaryOperator { Plus, diff --git a/src/ast/query.rs b/src/ast/query.rs index 4f3d79cdf..34b4ed7f5 100644 --- a/src/ast/query.rs +++ b/src/ast/query.rs @@ -20,7 +20,7 @@ use crate::ast::*; /// The most complete variant of a `SELECT` query expression, optionally /// including `WITH`, `UNION` / other set operations, and `ORDER BY`. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Query { /// WITH (common table expressions, or CTEs) @@ -67,7 +67,7 @@ impl fmt::Display for Query { /// A node in a tree, representing a "query body" expression, roughly: /// `SELECT ... [ {UNION|EXCEPT|INTERSECT} SELECT ...]` #[allow(clippy::large_enum_variant)] -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum SetExpr { /// Restricted SELECT .. FROM .. HAVING (no ORDER BY or set operations) @@ -114,7 +114,7 @@ impl fmt::Display for SetExpr { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum SetOperator { Union, @@ -135,7 +135,7 @@ impl fmt::Display for SetOperator { /// A quantifier for [SetOperator]. // TODO: Restrict parsing specific SetQuantifier in some specific dialects. // For example, BigQuery does not support `DISTINCT` for `EXCEPT` and `INTERSECT` -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum SetQuantifier { All, @@ -155,7 +155,7 @@ impl fmt::Display for SetQuantifier { /// A restricted variant of `SELECT` (without CTEs/`ORDER BY`), which may /// appear either as the only body item of a `Query`, or as an operand /// to a set operation like `UNION`. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Select { pub distinct: bool, @@ -239,7 +239,7 @@ impl fmt::Display for Select { } /// A hive LATERAL VIEW with potential column aliases -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct LateralView { /// LATERAL VIEW @@ -272,7 +272,7 @@ impl fmt::Display for LateralView { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct With { pub recursive: bool, @@ -294,7 +294,7 @@ impl fmt::Display for With { /// The names in the column list before `AS`, when specified, replace the names /// of the columns returned by the query. The parser does not validate that the /// number of columns in the query matches the number of columns in the query. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Cte { pub alias: TableAlias, @@ -313,7 +313,7 @@ impl fmt::Display for Cte { } /// One item of the comma-separated list following `SELECT` -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum SelectItem { /// Any expression, not followed by `[ AS ] alias` @@ -337,7 +337,7 @@ impl fmt::Display for SelectItem { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TableWithJoins { pub relation: TableFactor, @@ -355,7 +355,7 @@ impl fmt::Display for TableWithJoins { } /// A table name or a parenthesized subquery with an optional alias -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum TableFactor { Table { @@ -482,7 +482,7 @@ impl fmt::Display for TableFactor { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct TableAlias { pub name: Ident, @@ -499,7 +499,7 @@ impl fmt::Display for TableAlias { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Join { pub relation: TableFactor, @@ -565,7 +565,7 @@ impl fmt::Display for Join { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum JoinOperator { Inner(JoinConstraint), @@ -579,7 +579,7 @@ pub enum JoinOperator { OuterApply, } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum JoinConstraint { On(Expr), @@ -589,7 +589,7 @@ pub enum JoinConstraint { } /// An `ORDER BY` expression -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct OrderByExpr { pub expr: Expr, @@ -616,7 +616,7 @@ impl fmt::Display for OrderByExpr { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Offset { pub value: Expr, @@ -630,7 +630,7 @@ impl fmt::Display for Offset { } /// Stores the keyword after `OFFSET ` -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum OffsetRows { /// Omitting ROW/ROWS is non-standard MySQL quirk. @@ -649,7 +649,7 @@ impl fmt::Display for OffsetRows { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Fetch { pub with_ties: bool, @@ -669,7 +669,7 @@ impl fmt::Display for Fetch { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum LockType { Share, @@ -686,7 +686,7 @@ impl fmt::Display for LockType { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Top { /// SQL semantic equivalent of LIMIT but with same structure as FETCH. @@ -707,7 +707,7 @@ impl fmt::Display for Top { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Values(pub Vec>); @@ -724,7 +724,7 @@ impl fmt::Display for Values { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct SelectInto { pub temporary: bool, diff --git a/src/ast/value.rs b/src/ast/value.rs index 33660a628..49c0b5d7a 100644 --- a/src/ast/value.rs +++ b/src/ast/value.rs @@ -20,7 +20,7 @@ use bigdecimal::BigDecimal; use serde::{Deserialize, Serialize}; /// Primitive SQL values such as number and string -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Value { /// Numeric literal @@ -66,7 +66,7 @@ impl fmt::Display for Value { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum DateTimeField { Year, @@ -192,7 +192,7 @@ pub fn escape_escaped_string(s: &str) -> EscapeEscapedStringLiteral<'_> { EscapeEscapedStringLiteral(s) } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum TrimWhereField { Both, diff --git a/src/parser.rs b/src/parser.rs index c875f653b..deb6d6bf9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2281,7 +2281,7 @@ impl<'a> Parser<'a> { let file_format = if let Some(ff) = &hive_formats.storage { match ff { - HiveIOFormat::FileFormat { format } => Some(format.clone()), + HiveIOFormat::FileFormat { format } => Some(*format), _ => None, } } else { diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 5a9afdbef..e34ccf1f9 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -36,7 +36,7 @@ use crate::dialect::{Dialect, MySqlDialect}; use crate::keywords::{Keyword, ALL_KEYWORDS, ALL_KEYWORDS_INDEX}; /// SQL Token enumeration -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Token { /// An end-of-file marker, not a real token @@ -240,7 +240,7 @@ impl Token { } /// A keyword (like SELECT) or an optionally quoted SQL identifier -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Word { /// The value of the token, without the enclosing quotes, and with the @@ -278,7 +278,7 @@ impl Word { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum Whitespace { Space, diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index 960dbf5ae..1d8c6a05e 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -211,7 +211,7 @@ fn parse_show_create() { assert_eq!( mysql_and_generic().verified_stmt(format!("SHOW CREATE {} myident", obj_type).as_str()), Statement::ShowCreate { - obj_type: obj_type.clone(), + obj_type: *obj_type, obj_name: obj_name.clone(), } ); diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 5cc333935..d7e07a762 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -1271,7 +1271,7 @@ fn parse_pg_unary_ops() { let select = pg().verified_only_select(&format!("SELECT {}a", &str_op)); assert_eq!( SelectItem::UnnamedExpr(Expr::UnaryOp { - op: op.clone(), + op: *op, expr: Box::new(Expr::Identifier(Ident::new("a"))), }), select.projection[0] @@ -1287,7 +1287,7 @@ fn parse_pg_postfix_factorial() { let select = pg().verified_only_select(&format!("SELECT a{}", &str_op)); assert_eq!( SelectItem::UnnamedExpr(Expr::UnaryOp { - op: op.clone(), + op: *op, expr: Box::new(Expr::Identifier(Ident::new("a"))), }), select.projection[0]