Skip to main content

sql_dialect_fmt_syntax/
dialect.rs

1//! The SQL [`Dialect`] selector — the runtime seam for multi-dialect support.
2//!
3//! The engine (lossless lexer, never-fail parser, Doc-IR formatter) is dialect-agnostic; only the
4//! ~20% that differs between dialects — reserved keywords, lexer quoting/special tokens, which
5//! statements/operators the grammar accepts, and a few formatter rules — is gated on a [`Dialect`].
6//! Modeled on `sqlparser-rs`'s `Dialect` trait: rather than a trait object, a small `Copy` enum
7//! threads through the lexer, parser, and formatter, and the divergence points are expressed as
8//! `#[must_use]` predicate methods on it.
9//!
10//! Today every predicate returns the **Snowflake-correct** answer ([`Dialect::Snowflake`] is the
11//! [`Default`]), so adding the seam changes no behavior. The Databricks arms encode where the two
12//! dialects diverge and are refined in later phases.
13
14/// The SQL dialect a lex/parse/format request targets.
15///
16/// `#[non_exhaustive]` so further dialects can be added without it being a breaking change. Default
17/// is [`Dialect::Snowflake`].
18#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
19#[non_exhaustive]
20pub enum Dialect {
21    /// Snowflake SQL — the original and default dialect of this toolchain.
22    #[default]
23    Snowflake,
24    /// Databricks SQL / Spark SQL. Behavior is being filled in across later phases; today its
25    /// predicate answers describe the *intended* divergence but no Databricks-specific lexing,
26    /// parsing, or formatting is wired up yet.
27    Databricks,
28}
29
30impl Dialect {
31    /// Dollar quoting: `$$ ... $$` dollar-quoted bodies and `$1` / `$name` positional/variable
32    /// references. Snowflake only — Databricks has no `$$` body or `$n` reference.
33    #[must_use]
34    pub fn supports_dollar_quoting(self) -> bool {
35        matches!(self, Dialect::Snowflake)
36    }
37
38    /// The flow operator `->>` chaining statements into a pipeline. Snowflake only.
39    #[must_use]
40    pub fn supports_flow_operator(self) -> bool {
41        matches!(self, Dialect::Snowflake)
42    }
43
44    /// `COPY INTO <target> FROM <source>` bulk load/unload. Snowflake only.
45    #[must_use]
46    pub fn supports_copy_into(self) -> bool {
47        matches!(self, Dialect::Snowflake)
48    }
49
50    /// `// ...` line comments. Snowflake accepts them; Databricks/Spark uses `/` as an operator
51    /// and must not have a doubled slash consume the rest of the physical line.
52    #[must_use]
53    pub fn supports_double_slash_comments(self) -> bool {
54        matches!(self, Dialect::Snowflake)
55    }
56
57    /// `CREATE SEMANTIC VIEW ...` semantic-layer DDL. Snowflake only.
58    #[must_use]
59    pub fn supports_semantic_view(self) -> bool {
60        matches!(self, Dialect::Snowflake)
61    }
62
63    /// SQL scripting blocks: `BEGIN ... END`, declarations, control-flow statements, and the `:=`
64    /// assignment operator. Snowflake and Databricks both support compound SQL blocks.
65    #[must_use]
66    pub fn supports_scripting_blocks(self) -> bool {
67        matches!(self, Dialect::Snowflake | Dialect::Databricks)
68    }
69
70    /// Stage references: `@stage` / `@~` / `@%table` paths in `FROM`, `COPY`, and `PUT`/`GET`.
71    /// Snowflake only.
72    #[must_use]
73    pub fn supports_stage_refs(self) -> bool {
74        matches!(self, Dialect::Snowflake)
75    }
76
77    /// Backtick-quoted identifiers: `` `col` ``. Databricks only (Snowflake quotes with `"`).
78    #[must_use]
79    pub fn supports_backtick_identifiers(self) -> bool {
80        matches!(self, Dialect::Databricks)
81    }
82
83    /// Databricks/Spark prefixed string literals such as raw strings (`r'...'`) and hex binary
84    /// literals (`X'1A'`).
85    #[must_use]
86    pub fn supports_prefixed_strings(self) -> bool {
87        matches!(self, Dialect::Databricks)
88    }
89
90    /// Databricks/Spark null-safe equality operator: `<=>`.
91    #[must_use]
92    pub fn supports_null_safe_eq(self) -> bool {
93        matches!(self, Dialect::Databricks)
94    }
95
96    /// `LATERAL VIEW explode(...)` table-generating clause. Databricks only.
97    #[must_use]
98    pub fn supports_lateral_view(self) -> bool {
99        matches!(self, Dialect::Databricks)
100    }
101
102    /// Delta/Spark table DDL options: `USING`, `LOCATION`, `TBLPROPERTIES`, `OPTIONS`, and
103    /// `PARTITIONED BY`. Databricks only.
104    #[must_use]
105    pub fn supports_delta_table_options(self) -> bool {
106        matches!(self, Dialect::Databricks)
107    }
108
109    /// Higher-order-function lambdas: `x -> expr` and `(x, y) -> expr`. Databricks only for now.
110    #[must_use]
111    pub fn supports_lambda_expr(self) -> bool {
112        matches!(self, Dialect::Databricks)
113    }
114
115    /// Time travel via `VERSION AS OF` / `TIMESTAMP AS OF`. Databricks only (Snowflake uses
116    /// `AT` / `BEFORE`).
117    #[must_use]
118    pub fn supports_as_of_travel(self) -> bool {
119        matches!(self, Dialect::Databricks)
120    }
121
122    /// Databricks/Spark query distribution clauses: `DISTRIBUTE BY`, `SORT BY`, and `CLUSTER BY`.
123    #[must_use]
124    pub fn supports_databricks_query_clauses(self) -> bool {
125        matches!(self, Dialect::Databricks)
126    }
127
128    /// Delta/Spark maintenance + cache statements — `VACUUM`, `OPTIMIZE … ZORDER BY`,
129    /// `INSERT OVERWRITE`, `CACHE`/`UNCACHE`/`REFRESH`, `DESCRIBE HISTORY`, and the
130    /// `WHEN NOT MATCHED BY SOURCE`/`INSERT *` MERGE extensions. Databricks only. The leading words
131    /// (`VACUUM`, `OPTIMIZE`, `CACHE`, …) are recognized **contextually** at statement start, so they
132    /// stay ordinary identifiers under Snowflake (and elsewhere under Databricks), and Snowflake
133    /// output is byte-identical.
134    #[must_use]
135    pub fn supports_delta_commands(self) -> bool {
136        matches!(self, Dialect::Databricks)
137    }
138}
139
140#[cfg(test)]
141mod tests {
142    use super::Dialect;
143
144    #[test]
145    fn default_is_snowflake() {
146        assert_eq!(Dialect::default(), Dialect::Snowflake);
147    }
148
149    #[test]
150    fn snowflake_only_predicates() {
151        let s = Dialect::Snowflake;
152        assert!(s.supports_dollar_quoting());
153        assert!(s.supports_flow_operator());
154        assert!(s.supports_copy_into());
155        assert!(s.supports_double_slash_comments());
156        assert!(s.supports_semantic_view());
157        assert!(s.supports_scripting_blocks());
158        assert!(s.supports_stage_refs());
159        assert!(!s.supports_backtick_identifiers());
160        assert!(!s.supports_prefixed_strings());
161        assert!(!s.supports_null_safe_eq());
162        assert!(!s.supports_lateral_view());
163        assert!(!s.supports_delta_table_options());
164        assert!(!s.supports_lambda_expr());
165        assert!(!s.supports_as_of_travel());
166        assert!(!s.supports_databricks_query_clauses());
167        assert!(!s.supports_delta_commands());
168    }
169
170    #[test]
171    fn databricks_only_predicates() {
172        let d = Dialect::Databricks;
173        assert!(!d.supports_dollar_quoting());
174        assert!(!d.supports_flow_operator());
175        assert!(!d.supports_copy_into());
176        assert!(!d.supports_double_slash_comments());
177        assert!(!d.supports_semantic_view());
178        assert!(d.supports_scripting_blocks());
179        assert!(!d.supports_stage_refs());
180        assert!(d.supports_backtick_identifiers());
181        assert!(d.supports_prefixed_strings());
182        assert!(d.supports_null_safe_eq());
183        assert!(d.supports_lateral_view());
184        assert!(d.supports_delta_table_options());
185        assert!(d.supports_lambda_expr());
186        assert!(d.supports_as_of_travel());
187        assert!(d.supports_databricks_query_clauses());
188        assert!(d.supports_delta_commands());
189    }
190}