This is an automated email from the ASF dual-hosted git repository. LucaCappelletti94 pushed a commit to branch dialect-registry in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git
commit 234136ab42ebf143cde5e58ec5c6dd95ea6f104c Author: LucaCappelletti94 <[email protected]> AuthorDate: Tue Sep 22 09:55:44 2026 +0200 Derive the built-in dialect list from a single registry --- src/dialect/mod.rs | 67 +++++++++++++++++++++++++++++++---------------- src/test_utils.rs | 24 +++++------------ tests/sqlparser_common.rs | 3 ++- 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index 7c4744c5..669babd6 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -1911,32 +1911,53 @@ impl dyn Dialect { } } -/// Returns the built in [`Dialect`] corresponding to `dialect_name`. +/// Invokes `$callback` with every built-in dialect, each given as its type and +/// the names [`dialect_from_str`] accepts for it. /// -/// See [`Dialect`] documentation for an example. -pub fn dialect_from_str(dialect_name: impl AsRef<str>) -> Option<Box<dyn Dialect>> { - let dialect_name = dialect_name.as_ref(); - match dialect_name.to_lowercase().as_str() { - "generic" => Some(Box::new(GenericDialect)), - "mysql" => Some(Box::new(MySqlDialect {})), - "postgresql" | "postgres" => Some(Box::new(PostgreSqlDialect {})), - "hive" => Some(Box::new(HiveDialect {})), - "sqlite" => Some(Box::new(SQLiteDialect {})), - "snowflake" => Some(Box::new(SnowflakeDialect)), - "redshift" => Some(Box::new(RedshiftSqlDialect {})), - "mssql" => Some(Box::new(MsSqlDialect {})), - "clickhouse" => Some(Box::new(ClickHouseDialect {})), - "bigquery" => Some(Box::new(BigQueryDialect)), - "ansi" => Some(Box::new(AnsiDialect {})), - "duckdb" => Some(Box::new(DuckDbDialect {})), - "databricks" => Some(Box::new(DatabricksDialect {})), - "spark" | "sparksql" => Some(Box::new(SparkSqlDialect {})), - "oracle" => Some(Box::new(OracleDialect {})), - "teradata" => Some(Box::new(TeradataDialect {})), - _ => None, - } +/// This is the single list of built-in dialects, so a dialect added here reaches +/// [`ALL_DIALECTS`], [`dialect_from_str`], the test helpers and the fuzz targets. +macro_rules! for_all_dialects { + ($callback:ident) => { + $callback! { + (GenericDialect, ["generic"]), + (PostgreSqlDialect, ["postgresql", "postgres"]), + (MsSqlDialect, ["mssql"]), + (AnsiDialect, ["ansi"]), + (SnowflakeDialect, ["snowflake"]), + (HiveDialect, ["hive"]), + (RedshiftSqlDialect, ["redshift"]), + (MySqlDialect, ["mysql"]), + (BigQueryDialect, ["bigquery"]), + (SQLiteDialect, ["sqlite"]), + (DuckDbDialect, ["duckdb"]), + (DatabricksDialect, ["databricks"]), + (SparkSqlDialect, ["spark", "sparksql"]), + (ClickHouseDialect, ["clickhouse"]), + (OracleDialect, ["oracle"]), + (TeradataDialect, ["teradata"]), + } + }; } +macro_rules! define_dialect_registry { + ($(($dialect:ident, [$($name:literal),+ $(,)?])),+ $(,)?) => { + /// Every built-in [`Dialect`]. + pub const ALL_DIALECTS: &[&dyn Dialect] = &[$(&$dialect {}),+]; + + /// Returns the built in [`Dialect`] corresponding to `dialect_name`. + /// + /// See [`Dialect`] documentation for an example. + pub fn dialect_from_str(dialect_name: impl AsRef<str>) -> Option<Box<dyn Dialect>> { + match dialect_name.as_ref().to_lowercase().as_str() { + $($($name)|+ => Some(Box::new($dialect {})),)+ + _ => None, + } + } + }; +} + +for_all_dialects!(define_dialect_registry); + #[cfg(test)] mod tests { use super::*; diff --git a/src/test_utils.rs b/src/test_utils.rs index c4d1d0db..573751d2 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -275,25 +275,15 @@ impl TestedDialects { } } +macro_rules! boxed_dialects { + ($(($dialect:ident, [$($name:literal),+ $(,)?])),+ $(,)?) => { + vec![$(Box::new($dialect {}) as Box<dyn Dialect>),+] + }; +} + /// Returns all available dialects. pub fn all_dialects() -> TestedDialects { - TestedDialects::new(vec![ - Box::new(GenericDialect {}), - Box::new(PostgreSqlDialect {}), - Box::new(MsSqlDialect {}), - Box::new(AnsiDialect {}), - Box::new(SnowflakeDialect {}), - Box::new(HiveDialect {}), - Box::new(RedshiftSqlDialect {}), - Box::new(MySqlDialect {}), - Box::new(BigQueryDialect {}), - Box::new(SQLiteDialect {}), - Box::new(DuckDbDialect {}), - Box::new(DatabricksDialect {}), - Box::new(ClickHouseDialect {}), - Box::new(OracleDialect {}), - Box::new(TeradataDialect {}), - ]) + TestedDialects::new(for_all_dialects!(boxed_dialects)) } // Returns all available dialects with the specified parser options diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index c4aa607d..bf8ed410 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -35,7 +35,7 @@ use sqlparser::ast::*; use sqlparser::dialect::{ AnsiDialect, BigQueryDialect, ClickHouseDialect, DatabricksDialect, Dialect, DuckDbDialect, GenericDialect, HiveDialect, MsSqlDialect, MySqlDialect, OracleDialect, PostgreSqlDialect, - RedshiftSqlDialect, SQLiteDialect, SnowflakeDialect, + RedshiftSqlDialect, SQLiteDialect, SnowflakeDialect, SparkSqlDialect, }; use sqlparser::keywords::{Keyword, ALL_KEYWORDS}; use sqlparser::parser::{Parser, ParserError, ParserOptions}; @@ -13597,6 +13597,7 @@ fn test_selective_aggregation() { Box::new(SQLiteDialect {}), Box::new(DuckDbDialect {}), Box::new(GenericDialect {}), + Box::new(SparkSqlDialect {}), ]; assert_eq!(testing_dialects.dialects.len(), expected_dialects.len()); expected_dialects --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
