squonk-ast 2.0.0

Dialect-agnostic SQL abstract syntax tree for the squonk toolkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Moderately AI Inc.

//! The MSSQL / T-SQL dialect preset (ANSI-derived, deliberately conservative).
//!
//! Microsoft SQL Server's T-SQL diverges widely across its type, function, and statement
//! surface, and — unlike the five shipped oracle-compared presets — this workspace has **no
//! MSSQL oracle**, so over-acceptance cannot be measured. Conservatism is therefore the
//! honesty bar: this preset derives from [`FeatureSet::ANSI`], the strict standard baseline,
//! and enables only the T-SQL surface that already has a modelled, tested parser gate and
//! clear documentary evidence — in six cases the flag's *own* doc names T-SQL as the
//! motivating dialect. Every other axis keeps its ANSI value; a reader can predict from this
//! module exactly what MSSQL accepts beyond the standard, and unsupported T-SQL syntax is a
//! clean reject routed to a focused follow-up ticket, never a silent over-accept.
//!
//! # What this preset adds over ANSI
//!
//! Seven gates, each documented (in the flag's own doc) as T-SQL surface:
//!
//! - [`apply_join`](JoinSyntax::apply_join) — the `CROSS APPLY` / `OUTER APPLY`
//!   lateral-correlated join operators. This is the flag this preset exists to make real: it
//!   shipped staged (Lenient-only) before this preset gave it an engine home. The leading
//!   `CROSS`/`OUTER` keyword anchors the operator, so no reserved-word interplay is needed
//!   (the preceding factor's alias can never swallow it).
//! - [`table_hints`](TableExpressionSyntax::table_hints) — the `WITH ( <hint>, … )` table-hint
//!   tail (`FROM t WITH (NOLOCK)`, `WITH (INDEX(ix), FORCESEEK)`), a T-SQL locking / optimizer
//!   directive on a table factor. Reached only through the `WITH (` sequence at the
//!   table-factor tail, where `WITH` is never otherwise consumed on a base table, so it never
//!   contends with the leading-`WITH` CTE clause at statement start; when off (ANSI base) the
//!   trailing `WITH` is left unconsumed and the construct is a clean reject. The common
//!   documented hints ([`TableHintKeyword`](crate::ast::TableHintKeyword)) are typed for
//!   planner consumers; an unrecognized word is preserved verbatim
//!   ([`TableHint::Other`](crate::ast::TableHint)). Numeric index ids (`INDEX(0)`) and
//!   the legacy no-`WITH` parenthesized form (`FROM t (NOLOCK)`) are conservative deferrals.
//! - [`table_version`](TableExpressionSyntax::table_version) — the temporal-table
//!   `FOR SYSTEM_TIME {AS OF | FROM..TO | BETWEEN..AND | CONTAINED IN | ALL}` query modifier
//!   on a table factor, a typed [`TableVersion`](crate::ast::TableVersion) for planner
//!   consumers. It sits at the table-factor position (right after the table name, before the
//!   alias), so its `FOR SYSTEM_TIME` trigger never contends with the query-level `FOR XML` /
//!   `FOR JSON` tail or the `FOR` locking clause — those are read only after the whole
//!   `FROM`/`WHERE`, and the word after `FOR` (`SYSTEM_TIME` vs `XML`) partitions them.
//! - Bracket identifiers `[name]` — T-SQL's signature delimiter, modelled by the
//!   [`IdentifierQuote::Asymmetric`] `{ open: '[', close: ']' }` style whose own doc cites
//!   T-SQL. Listed in [`MSSQL_IDENTIFIER_QUOTES`] alongside the standard `"…"`. Enabling the
//!   `[` opener would contend with the `[`-punctuation expression grammar
//!   ([`ExpressionSyntax::subscript`] / [`array_constructor`](ExpressionSyntax::array_constructor)
//!   / [`collection_literals`](ExpressionSyntax::collection_literals), the
//!   [`LexicalConflict::BracketIdentifierVersusArraySyntax`](super::LexicalConflict) hazard),
//!   but all three are **off** in the ANSI base this preset keeps — and that is
//!   behaviour-accurate: T-SQL genuinely lacks `[]` array subscripting, so the bracket is
//!   unambiguously an identifier delimiter here. The lexical-consistency `const` assert below
//!   enforces the single claimant.
//! - [`named_at`](ParameterSyntax::named_at) — the `@name` parameter / local-variable sigil,
//!   whose own doc names T-SQL. Its `@name` trigger contends with
//!   [`SessionVariableSyntax::user_variables`] (MySQL's `@name` read), which stays off (ANSI's
//!   value) so `@name` has a single claimant — the
//!   [`AtNameParameterVersusUserVariable`](super::LexicalConflict) hazard the assert below
//!   rules out. The `@@name` system-variable form is disjoint (its second `@` is not an
//!   identifier byte) and stays off too.
//! - [`national_strings`](StringLiteralSyntax::national_strings) — `N'…'` national-character
//!   string constants, whose own doc names T-SQL. A pure lexical gate over the ANSI string
//!   surface; `backslash_escapes` stays off (its doc reads "MySQL default; not T-SQL").
//! - [`money_literals`](NumericLiteralSyntax::money_literals) — `$1234.56` / `$.5` money
//!   literals (the `$` currency sigil prefixes a decimal), whose own doc names T-SQL. Its
//!   `$`+digit trigger contends with [`ParameterSyntax::positional_dollar`] (PostgreSQL's
//!   `$1`), which stays off (ANSI's value) so `$`+digit has a single claimant — the
//!   [`MoneyVersusPositionalDollar`](super::LexicalConflict) hazard the assert below rules
//!   out.
//!
//! # The two lexical facts over ANSI
//!
//! - **Dual identifier quoting.** T-SQL quotes identifiers with the bracket `[…]` **and** the
//!   standard `"…"` (its default `QUOTED_IDENTIFIER ON`). [`MSSQL_IDENTIFIER_QUOTES`] lists
//!   both; T-SQL has no MySQL-style backtick, so — unlike SQLite/Databricks — no `` ` `` opener
//!   appears. `double_quoted_strings` stays off (ANSI's value) so `"x"` reads as an
//!   identifier, keeping `QUOTED_IDENTIFIER ON` behaviour and the preset lexically consistent.
//! - **Case folding.** T-SQL identifier resolution is case-insensitive (collation-dependent),
//!   so [`identifier_casing`](FeatureSet::identifier_casing) is [`Casing::Lower`] — the
//!   [`Casing`] doc names T-SQL as one of the two dialects [`Casing::Lower`] approximates
//!   ("case-preserving storage, case-insensitive comparison"). The interned text still renders
//!   exactly as written; the fold is identity-only and never affects acceptance. (The
//!   per-identifier-kind table-vs-column sensitivity split is a documented `Casing` limitation
//!   and a deliberate future extension, not modelled here.)
//!
//! # Deliberately deferred (conservative reject)
//!
//! `SELECT … INTO <table>` stays off: [`select_into`](SelectSyntax::select_into)'s own doc
//! models only PostgreSQL's `SELECT … INTO [TEMP] <table>` create-table form and does not cite
//! T-SQL, and with no MSSQL oracle the exact boundary (T-SQL has no `TEMP` keyword — it spells
//! temp tables `#name` instead) cannot be verified, so shipping the PG-shaped gate would be an
//! unmeasured guess. Likewise `TOP (n)`, `GO` batch separators,
//! `#temp` tables, the `OUTPUT` clause, and the T-SQL `MERGE` variants have no modelled gate;
//! several are already catalogued by the structural-extensibility spike. All are clean rejects
//! routed to follow-up tickets, never silent over-accepts.

use super::{
    AccessControlSyntax, AggregateCallSyntax, CallSyntax, CaretOperator, Casing,
    ColumnDefinitionSyntax, CommentSyntax, ConstraintSyntax, CreateTableClauseSyntax,
    DoubleAmpersand, ExistenceGuards, ExpressionSyntax, FeatureSet, GroupingSyntax,
    IdentifierQuote, IdentifierSyntax, IndexAlterSyntax, JoinSyntax, KeywordOperators, KeywordSet,
    MaintenanceSyntax, MutationSyntax, NullOrdering, NumericLiteralSyntax, OperatorSyntax,
    ParameterSyntax, PipeOperator, PredicateSyntax, QueryTailSyntax, RESERVED_BARE_ALIAS,
    RESERVED_COLUMN_NAME, RESERVED_FUNCTION_NAME, RESERVED_TYPE_NAME, STANDARD_BYTE_CLASSES,
    SelectSyntax, SessionVariableSyntax, ShowSyntax, StatementDdlGates, StringFuncForms,
    StringLiteralSyntax, TableExpressionSyntax, TableFactorSyntax, TargetSpelling,
    TransactionSyntax, TypeNameSyntax, UtilitySyntax, ViewSequenceClauseSyntax,
};
use crate::precedence::{STANDARD_BINDING_POWERS, STANDARD_SET_OPERATION_BINDING_POWERS};

/// MSSQL / T-SQL identifier quoting: the bracket `[…]` **and** the SQL standard `"…"`, both at
/// once. The two openers are distinct bytes, so their order is immaterial. T-SQL has no
/// MySQL-style backtick, so — unlike the SQLite/Lenient bracket sets — no `` ` `` opener
/// appears. `"` stays a quote here (and `double_quoted_strings` is correspondingly off in
/// [`StringLiteralSyntax::ANSI`], which this preset keeps, matching T-SQL's default
/// `QUOTED_IDENTIFIER ON`), so `"x"` is an identifier, never a string.
pub const MSSQL_IDENTIFIER_QUOTES: &[IdentifierQuote] = &[
    IdentifierQuote::Symmetric('"'),
    IdentifierQuote::Asymmetric {
        open: '[',
        close: ']',
    },
];

impl StringLiteralSyntax {
    /// MSSQL string surface: the ANSI baseline plus `N'…'` national-character string constants.
    /// `backslash_escapes` stays off (its doc reads "MySQL default; not T-SQL"). Every other
    /// string knob is conservatively ANSI.
    pub const MSSQL: Self = Self {
        national_strings: true,
        escape_strings: false,
        dollar_quoted_strings: false,
        double_quoted_strings: false,
        backslash_escapes: false,
        unicode_strings: false,
        bit_string_literals: false,
        blob_literals: false,
        charset_introducers: false,
        same_line_adjacent_concat: false,
    };
}

impl NumericLiteralSyntax {
    /// MSSQL numeric surface: the ANSI baseline plus `$1234.56` / `$.5` money literals. Every
    /// other numeric knob is conservatively ANSI.
    pub const MSSQL: Self = Self {
        money_literals: true,
        hex_integers: false,
        octal_integers: false,
        binary_integers: false,
        underscore_separators: false,
        radix_leading_underscore: false,
        reject_trailing_junk: false,
    };
}

impl ParameterSyntax {
    /// MSSQL parameter surface: the ANSI baseline plus the `@name` parameter / local-variable
    /// sigil. `positional_dollar` stays off (ANSI's value) so `$`+digit belongs solely to
    /// [`NumericLiteralSyntax::money_literals`], and `user_variables` stays off (in
    /// [`SessionVariableSyntax::ANSI`]) so `@name` belongs solely to this — both enforced by
    /// the lexical assert below.
    pub const MSSQL: Self = Self {
        named_at: true,
        positional_dollar: false,
        positional_dollar_large: false,
        anonymous_question: false,
        named_colon: false,
        named_dollar: false,
        numbered_question: false,
    };
}

impl TableExpressionSyntax {
    /// MSSQL table-expression surface: the ANSI baseline plus the `WITH (...)` table-hint
    /// tail and the temporal-table `FOR SYSTEM_TIME` modifier (all five forms). The
    /// `CROSS APPLY` / `OUTER APPLY` join operators ride [`JoinSyntax`]; every other table
    /// knob is conservatively ANSI.
    pub const MSSQL: Self = Self {
        // `WITH (NOLOCK)` / `WITH (INDEX(ix), FORCESEEK)` table hints — see the module docs.
        table_hints: true,
        // `FOR SYSTEM_TIME {AS OF | FROM..TO | BETWEEN..AND | CONTAINED IN | ALL}` — the
        // temporal-table query modifier.
        table_version: true,
        only: false,
        table_sample: false,
        parenthesized_joins: true,
        table_alias_column_lists: true,
        join_using_alias: false,
        index_hints: false,
        partition_selection: false,
        base_table_alias_column_lists: true,
        string_literal_aliases: false,
        aliased_parenthesized_join: true,
        bare_table_alias_is_bare_label: false,
        table_json_path: false,
        indexed_by: false,
        prefix_colon_alias: false,
    };
}

impl JoinSyntax {
    /// The `MSSQL` preset for join syntax.
    pub const MSSQL: Self = Self {
        apply_join: true,
        stacked_join_qualifiers: true,
        full_outer_join: true,
        natural_cross_join: false,
        straight_join: false,
        asof_join: false,
        positional_join: false,
        semi_anti_join: false,
        sided_semi_anti_join: false,
        recursive_search_cycle: false,
        recursive_union_rejects_order_limit: false,
        recursive_using_key: false,
    };
}

impl QueryTailSyntax {
    /// MSSQL query-tail surface: the ANSI baseline plus the `FOR XML`/`FOR JSON`
    /// result-shaping tail. MSSQL has no query-tail row-locking clause (it spells
    /// isolation with `WITH (…)` table hints, [`TableExpressionSyntax::MSSQL`]), so
    /// `locking_clauses` stays off and the `FOR` lead is unambiguously the
    /// result-shaping clause here.
    pub const MSSQL: Self = Self {
        for_xml_json_clause: true,
        fetch_first: true,
        limit_offset_comma: false,
        locking_clauses: false,
        key_lock_strengths: false,
        stacked_locking_clauses: false,
        using_sample: false,
        leading_offset: true,
        limit_expressions: true,
        limit_percent: false,
        with_ties_requires_order_by: false,
        pipe_syntax: false,
        limit_by_clause: false,
        settings_clause: false,
        format_clause: false,
    };
}

impl TableFactorSyntax {
    /// The `MSSQL` preset for table factor syntax.
    pub const MSSQL: Self = Self {
        // SQL Server's `OPENJSON(<json> [, <path>]) [WITH (…)]` rowset-function table factor —
        // the sole engine with this exact form (documented; no differential oracle here, so the
        // gate follows the grammar on this conservative preset, the `apply_join` precedent).
        open_json: true,
        lateral: false,
        table_functions: false,
        rows_from: false,
        unnest: false,
        unnest_with_offset: false,
        table_function_ordinality: false,
        special_function_table_source: true,
        pivot: false,
        unpivot: false,
        show_ref: false,
        from_values: false,
        json_table: false,
        xml_table: false,
        table_expr_factor: false,
        pivot_value_sources: false,
        match_recognize: false,
    };
}

impl FeatureSet {
    /// MSSQL / T-SQL as ANSI-derived dialect data (see the module docs for the full derivation
    /// rationale and the conservatism bar).
    pub const MSSQL: Self = Self {
        // T-SQL identifier resolution is case-insensitive (collation-dependent); `Casing::Lower`
        // is the closest single fit (the `Casing` doc names T-SQL). Identity only — the interned
        // text still renders exactly as written, so this never affects acceptance.
        identifier_casing: Casing::Lower,
        // The lexical delta over ANSI: bracket `[…]` *and* double-quote identifier quoting.
        identifier_quotes: MSSQL_IDENTIFIER_QUOTES,
        default_null_ordering: NullOrdering::NullsLast,
        // No reserved-set delta over ANSI — MSSQL adds no keyword reservation here.
        reserved_column_name: RESERVED_COLUMN_NAME,
        reserved_function_name: RESERVED_FUNCTION_NAME,
        reserved_type_name: RESERVED_TYPE_NAME,
        reserved_bare_alias: RESERVED_BARE_ALIAS,
        reserved_as_label: KeywordSet::EMPTY,
        catalog_qualified_names: true,
        byte_classes: STANDARD_BYTE_CLASSES,
        binding_powers: STANDARD_BINDING_POWERS,
        set_operation_powers: STANDARD_SET_OPERATION_BINDING_POWERS,
        // `N'…'` national-character strings.
        string_literals: StringLiteralSyntax::MSSQL,
        // `$1234.56` money literals.
        numeric_literals: NumericLiteralSyntax::MSSQL,
        // `@name` parameters / local variables.
        parameters: ParameterSyntax::MSSQL,
        session_variables: SessionVariableSyntax::ANSI,
        identifier_syntax: IdentifierSyntax::ANSI,
        // `CROSS APPLY` / `OUTER APPLY` — the capstone this preset exposes.
        table_expressions: TableExpressionSyntax::MSSQL,
        join_syntax: JoinSyntax::MSSQL,
        table_factor_syntax: TableFactorSyntax::MSSQL,
        expression_syntax: ExpressionSyntax::ANSI,
        operator_syntax: OperatorSyntax::ANSI,
        call_syntax: CallSyntax::ANSI,
        string_func_forms: StringFuncForms::ANSI,
        aggregate_call_syntax: AggregateCallSyntax::ANSI,
        predicate_syntax: PredicateSyntax::ANSI,
        pipe_operator: PipeOperator::StringConcat,
        double_ampersand: DoubleAmpersand::Unsupported,
        keyword_operators: KeywordOperators::Unsupported,
        caret_operator: CaretOperator::Unsupported,
        hash_bitwise_xor: false,
        comment_syntax: CommentSyntax::ANSI,
        mutation_syntax: MutationSyntax::ANSI,
        statement_ddl_gates: StatementDdlGates::ANSI,
        view_sequence_clause_syntax: ViewSequenceClauseSyntax::ANSI,
        create_table_clause_syntax: CreateTableClauseSyntax::ANSI,
        column_definition_syntax: ColumnDefinitionSyntax::ANSI,
        constraint_syntax: ConstraintSyntax::ANSI,
        index_alter_syntax: IndexAlterSyntax::ANSI,
        existence_guards: ExistenceGuards::ANSI,
        // `SELECT … INTO <table>` stays off (deferred — see the module docs); every other
        // SELECT knob is conservatively ANSI.
        select_syntax: SelectSyntax::ANSI,
        query_tail_syntax: QueryTailSyntax::MSSQL,
        grouping_syntax: GroupingSyntax::ANSI,
        utility_syntax: UtilitySyntax::ANSI,
        transaction_syntax: TransactionSyntax::ANSI,
        show_syntax: ShowSyntax::ANSI,
        maintenance_syntax: MaintenanceSyntax::ANSI,
        access_control_syntax: AccessControlSyntax::ANSI,
        type_name_syntax: TypeNameSyntax::ANSI,
        // No MSSQL-specific Tier-1 output spelling yet; render the portable ANSI canonical type
        // names (a `TargetSpelling::Mssql` is render work a later ticket owns).
        target_spelling: TargetSpelling::Ansi,
    };
}

/// Prefer [`FeatureSet::MSSQL`] for struct update.
pub const MSSQL: FeatureSet = FeatureSet::MSSQL;

// Compile-time proof the MSSQL preset claims no shared tokenizer trigger twice. Beyond ANSI it
// adds three triggers — the `[` bracket identifier opener, the `@` named-at sigil, and the `$`
// money sigil — each with a single claimant: no enabled expression grammar lexes `[` as
// punctuation (`subscript`/`array_constructor`/`collection_literals` stay off, so T-SQL's
// bracket is unambiguously an identifier delimiter), `user_variables` stays off so `@name`
// belongs solely to `named_at`, and `positional_dollar` stays off so `$`+digit belongs solely
// to `money_literals`. `"` stays the sole identifier quote it already was (`double_quoted_strings`
// off). Kept as a ratchet so a future MSSQL delta that *does* add a contending trigger fails the
// build here.
const _: () = assert!(FeatureSet::MSSQL.is_lexically_consistent());
// The two sibling self-consistency registries are ratcheted the same way, so the
// parse-entry `debug_assert!` folds all three to dead code for this preset: no refinement
// flag rides an unset base, and no two features contend for one parser-position head.
const _: () = assert!(FeatureSet::MSSQL.has_satisfied_feature_dependencies());
const _: () = assert!(FeatureSet::MSSQL.has_no_grammar_conflict());

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mssql_is_ansi_plus_the_six_gates_and_two_lexical_facts() {
        // The preset is ANSI with a documented, closed set of divergent axes: the two lexical
        // facts (case-folding, dual identifier quoting) and the five enabled sub-presets that
        // carry the six gates (the `join_syntax` sub-preset carries `apply_join`, and the
        // `table_expressions` sub-preset carries `table_hints` and `table_version`). Asserting
        // the whole rest equals ANSI keeps the "ANSI-derived, every delta documented" claim
        // honest against a future stray edit. Bind to locals so the const reads are not flagged by
        // clippy's `assertions_on_constants`.
        let ansi = FeatureSet::ANSI;
        let mssql = FeatureSet::MSSQL;

        // The two lexical facts.
        assert_eq!(mssql.identifier_casing, Casing::Lower);
        assert_ne!(mssql.identifier_casing, ansi.identifier_casing);
        assert_eq!(mssql.identifier_quotes, MSSQL_IDENTIFIER_QUOTES);
        assert_ne!(mssql.identifier_quotes, ansi.identifier_quotes);
        // `"` stays an identifier quote, so MSSQL must keep `double_quoted_strings` off (the
        // `QUOTED_IDENTIFIER ON` behaviour the preset depends on for lexical consistency).
        assert!(!mssql.string_literals.double_quoted_strings);
        // The single-claimant interplays the enabled sigils depend on.
        assert!(!mssql.session_variables.user_variables);
        assert!(!mssql.parameters.positional_dollar);
        // T-SQL has no backtick identifier quote.
        assert!(
            !mssql
                .identifier_quotes
                .iter()
                .any(|quote| quote.open() == '`')
        );

        // The five divergent sub-presets.
        assert_eq!(mssql.string_literals, StringLiteralSyntax::MSSQL);
        assert_ne!(mssql.string_literals, ansi.string_literals);
        assert_eq!(mssql.numeric_literals, NumericLiteralSyntax::MSSQL);
        assert_ne!(mssql.numeric_literals, ansi.numeric_literals);
        assert_eq!(mssql.parameters, ParameterSyntax::MSSQL);
        assert_ne!(mssql.parameters, ansi.parameters);
        assert_eq!(mssql.table_expressions, TableExpressionSyntax::MSSQL);
        assert_ne!(mssql.table_expressions, ansi.table_expressions);

        // No reserved-set delta: every position is inherited verbatim from ANSI.
        assert_eq!(mssql.reserved_column_name, ansi.reserved_column_name);
        assert_eq!(mssql.reserved_function_name, ansi.reserved_function_name);
        assert_eq!(mssql.reserved_type_name, ansi.reserved_type_name);
        assert_eq!(mssql.reserved_bare_alias, ansi.reserved_bare_alias);
        assert_eq!(mssql.reserved_as_label, KeywordSet::EMPTY);

        // Everything else is inherited verbatim from ANSI — including SELECT (SELECT INTO is
        // deferred, so it stays off).
        assert_eq!(mssql.select_syntax, ansi.select_syntax);
        assert_eq!(mssql.expression_syntax, ansi.expression_syntax);
        assert_eq!(mssql.session_variables, ansi.session_variables);
        assert_eq!(mssql.identifier_syntax, ansi.identifier_syntax);
        assert_eq!(mssql.operator_syntax, ansi.operator_syntax);
        assert_eq!(mssql.call_syntax, ansi.call_syntax);
        assert_eq!(mssql.predicate_syntax, ansi.predicate_syntax);
        assert_eq!(mssql.mutation_syntax, ansi.mutation_syntax);
        assert_eq!(mssql.statement_ddl_gates, ansi.statement_ddl_gates);
        assert_eq!(
            mssql.create_table_clause_syntax,
            ansi.create_table_clause_syntax
        );
        assert_eq!(
            mssql.column_definition_syntax,
            ansi.column_definition_syntax
        );
        assert_eq!(mssql.constraint_syntax, ansi.constraint_syntax);
        assert_eq!(mssql.index_alter_syntax, ansi.index_alter_syntax);
        assert_eq!(mssql.existence_guards, ansi.existence_guards);
        assert_eq!(mssql.utility_syntax, ansi.utility_syntax);
        assert_eq!(mssql.type_name_syntax, ansi.type_name_syntax);
        assert_eq!(mssql.byte_classes, ansi.byte_classes);
        assert_eq!(mssql.binding_powers, ansi.binding_powers);
        assert_eq!(mssql.target_spelling, ansi.target_spelling);
        assert_eq!(mssql.default_null_ordering, ansi.default_null_ordering);
    }

    #[test]
    fn mssql_enables_exactly_the_six_staged_gates() {
        // The capstone: CROSS/OUTER APPLY, `WITH (...)` table hints, `FOR SYSTEM_TIME` table
        // versioning, bracket identifiers, `@name` parameters, `N'…'` national strings, and
        // `$…` money literals are on, and each is off in the ANSI base it derives from.
        // Forcing the flags back off recovers the ANSI sub-presets verbatim.
        let ansi = FeatureSet::ANSI;
        let mssql = FeatureSet::MSSQL;

        assert!(mssql.join_syntax.apply_join);
        assert!(!ansi.join_syntax.apply_join);
        assert!(mssql.table_expressions.table_hints);
        assert!(!ansi.table_expressions.table_hints);
        assert!(mssql.parameters.named_at && !ansi.parameters.named_at);
        assert!(mssql.string_literals.national_strings && !ansi.string_literals.national_strings);
        assert!(mssql.numeric_literals.money_literals && !ansi.numeric_literals.money_literals);
        // The bracket opener is the sixth gate (an identifier-quote delta, not a bool).
        assert!(
            mssql
                .identifier_quotes
                .iter()
                .any(|quote| quote.open() == '[')
        );
        assert!(
            !ansi
                .identifier_quotes
                .iter()
                .any(|quote| quote.open() == '[')
        );

        assert_eq!(
            TableExpressionSyntax {
                table_hints: false,
                table_version: false,
                ..mssql.table_expressions
            },
            ansi.table_expressions,
        );
        assert_eq!(
            JoinSyntax {
                apply_join: false,
                ..mssql.join_syntax
            },
            ansi.join_syntax,
        );
        assert_eq!(
            ParameterSyntax {
                named_at: false,
                ..mssql.parameters
            },
            ansi.parameters,
        );
        assert_eq!(
            StringLiteralSyntax {
                national_strings: false,
                ..mssql.string_literals
            },
            ansi.string_literals,
        );
        assert_eq!(
            NumericLiteralSyntax {
                money_literals: false,
                ..mssql.numeric_literals
            },
            ansi.numeric_literals,
        );
    }

    #[test]
    fn mssql_is_lexically_consistent_and_dependency_clean() {
        // Both self-consistency registries must be clean: the `[` bracket quote, the `@` named-at
        // sigil, and the `$` money sigil each have a single claimant (array/subscript grammar off,
        // `user_variables` off, `positional_dollar` off, `double_quoted_strings` off), and none of
        // the six enabled gates rides an unset base flag.
        let mssql = FeatureSet::MSSQL;
        assert_eq!(mssql.lexical_conflict(), None);
        assert!(mssql.is_lexically_consistent());
        assert_eq!(mssql.feature_dependencies(), None);
        assert!(mssql.has_satisfied_feature_dependencies());
        assert_eq!(mssql.grammar_conflict(), None);
        assert!(mssql.has_no_grammar_conflict());
    }
    #[test]
    fn mssql_closed_delta_axes_match_documented_set() {
        crate::dialect::closed_delta::assert_closed_delta(
            &FeatureSet::ANSI,
            &FeatureSet::MSSQL,
            &[
                "identifier_casing",
                "identifier_quotes",
                "string_literals",
                "numeric_literals",
                "parameters",
                "table_expressions",
                "join_syntax",
                "table_factor_syntax",
                "query_tail_syntax",
            ],
        );
    }
}