Skip to main content

squonk_ast/dialect/
quiltdb.rs

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 Moderately AI Inc.
3
4//! The QuiltDB dialect preset.
5//!
6//! This preset starts from the PostgreSQL grammar and adds angle-bracket and composite
7//! types (`ARRAY<T>`, `STRUCT<...>`, `MAP(K, V)`), collection constructors including
8//! `MAP { ... }`, `* REPLACE`, mutation modifier shapes, multi-row `MERGE INSERT`, joined
9//! `UPDATE`/`DELETE`, compact and alter-column identity forms, sequence `CACHE`,
10//! `DROP PRIMARY KEY`, the front-position
11//! `COMMENT IF EXISTS ON ...` guard, and colocation-group DDL.
12//!
13//! Its PostgreSQL-compatible query surface rejects wildcard `EXCLUDE`/`RENAME`,
14//! `ILIKE`, `IS [NOT] DISTINCT FROM`, `NATURAL CROSS JOIN`, `TABLESAMPLE`, and
15//! `INTERSECT ALL`/`EXCEPT ALL`. Alternative DDL spellings such as `MODIFY COLUMN`,
16//! `CHANGE COLUMN`, `SET TBLPROPERTIES`, table-scoped `DROP INDEX`, and trailing
17//! index `USING` remain outside the grammar.
18
19use super::{
20    AccessControlSyntax, AggregateCallSyntax, CallSyntax, CaretOperator, Casing,
21    ColumnDefinitionSyntax, CommentSyntax, ConstraintSyntax, CreateTableClauseSyntax,
22    DoubleAmpersand, ExistenceGuards, ExpressionSyntax, FeatureSet, GroupingSyntax,
23    IdentifierSyntax, IndexAlterSyntax, JoinSyntax, KeywordOperators, KeywordSet,
24    MaintenanceSyntax, MutationSyntax, NullOrdering, NumericLiteralSyntax, OperatorSyntax,
25    POSTGRES_BYTE_CLASSES, ParameterSyntax, PipeOperator, PredicateSyntax, QueryTailSyntax,
26    RESERVED_BARE_ALIAS, RESERVED_COLUMN_NAME, RESERVED_FUNCTION_NAME, RESERVED_TYPE_NAME,
27    STANDARD_IDENTIFIER_QUOTES, SelectSyntax, SessionVariableSyntax, ShowSyntax, StatementDdlGates,
28    StringFuncForms, StringLiteralSyntax, TableExpressionSyntax, TableFactorSyntax, TargetSpelling,
29    TransactionSyntax, TypeNameSyntax, UtilitySyntax, ViewSequenceClauseSyntax,
30};
31use crate::precedence::STANDARD_SET_OPERATION_BINDING_POWERS;
32
33impl AccessControlSyntax {
34    /// Access-control syntax enabled by this preset.
35    pub const QUILTDB: Self = Self {
36        // Preserve user/role statement structure for downstream validation. The richer
37        // PostgreSQL GRANT/REVOKE route remains selected.
38        user_role_management: true,
39        alter_role_rename: true,
40        access_control: true,
41        access_control_extended_objects: true,
42        access_control_account_grants: false,
43    };
44}
45
46impl ExpressionSyntax {
47    /// Expression syntax enabled by this preset.
48    pub const QUILTDB: Self = Self {
49        struct_constructor: true,
50        collection_literals: true,
51        typecast_operator: true,
52        subscript: true,
53        slice_step: false,
54        collate: true,
55        at_time_zone: true,
56        semi_structured_access: false,
57        array_constructor: true,
58        multidim_array_literals: true,
59        row_constructor: true,
60        field_selection: true,
61        field_wildcard: true,
62        typed_string_literals: true,
63        typed_interval_literal: true,
64        relaxed_interval_syntax: false,
65        mysql_interval_operator: false,
66        positional_column: false,
67        lambda_keyword: false,
68    };
69}
70
71impl SelectSyntax {
72    /// Query syntax enabled by this preset.
73    pub const QUILTDB: Self = Self {
74        // Enables wildcard projection modifiers; individual spellings are gated below.
75        wildcard_modifiers: false,
76        wildcard_replace: true,
77        intersect_all: false,
78        except_all: false,
79        distinct_on: true,
80        select_into: true,
81        empty_target_list: true,
82        qualify: false,
83        alias_string_literals: false,
84        bare_alias_string_literals: false,
85        union_by_name: false,
86        qualified_wildcard_alias: true,
87        from_first: false,
88        explicit_table: true,
89        parenthesized_query_operands: true,
90        values_rows_require_equal_arity: false,
91        values_row_constructor: true,
92        as_alias_rejects_reserved: false,
93        trailing_comma: false,
94        prefix_colon_alias: false,
95        lateral_view_clause: false,
96        connect_by_clause: false,
97    };
98}
99
100impl PredicateSyntax {
101    /// Predicate syntax enabled by this preset.
102    pub const QUILTDB: Self = Self {
103        is_distinct_from: false,
104        ilike: false,
105        like: true,
106        similar_to: true,
107        overlaps_period_predicate: true,
108        unparenthesized_in_list: false,
109        pattern_match_quantifier: true,
110        between_symmetric: true,
111        is_normalized: true,
112        empty_in_list: false,
113        null_test_two_word_postfix: false,
114    };
115}
116
117impl MutationSyntax {
118    /// Mutation syntax enabled by this preset.
119    pub const QUILTDB: Self = Self {
120        insert_ignore: true,
121        insert_overwrite: true,
122        merge_insert_multirow: true,
123        // Preserve modifier and joined-target structure for downstream validation.
124        replace_into: true,
125        update_delete_tails: true,
126        joined_update_delete: true,
127        or_conflict_action: true,
128        returning: true,
129        on_conflict: true,
130        on_duplicate_key_update: false,
131        multi_column_assignment: true,
132        update_tuple_value_row_arity: false,
133        where_current_of: true,
134        merge: true,
135        insert_set: false,
136        insert_column_matching: false,
137        delete_using: true,
138        update_from: true,
139        delete_using_target_alias: true,
140        cte_before_insert: true,
141        cte_before_merge: true,
142        data_modifying_ctes: true,
143        merge_when_not_matched_by: true,
144        merge_insert_default_values: true,
145        merge_insert_overriding: true,
146        merge_update_set_star: false,
147        merge_insert_star_by_name: false,
148        merge_error_action: false,
149        update_set_qualified_column: true,
150    };
151}
152
153impl IndexAlterSyntax {
154    /// Index and extended `ALTER` syntax enabled by this preset.
155    pub const QUILTDB: Self = Self {
156        drop_primary_key: true,
157        alter_column_add_identity: true,
158        rename_constraint: true,
159        alter_table_set_options: true,
160        index_storage_parameters: true,
161        drop_behavior: true,
162        index_drop_on_table: false,
163        index_concurrently: true,
164        index_using_method: true,
165        partial_index: true,
166        index_if_not_exists: true,
167        index_nulls_order: true,
168        alter_table_extended: true,
169        alter_nested_column_paths: false,
170        alter_existence_guards: true,
171        alter_column_set_data_type: true,
172        routine_arg_types: true,
173        routine_arg_defaults: true,
174        routine_arg_modes: true,
175        routine_language_string: true,
176        alter_table_multiple_actions: true,
177    };
178}
179
180impl ColumnDefinitionSyntax {
181    /// Column-definition syntax enabled by this preset.
182    pub const QUILTDB: Self = Self {
183        compact_identity_columns: true,
184        generated_column_shorthand: false,
185        column_conflict_resolution_clause: false,
186        typeless_column_definitions: false,
187        typeless_generated_columns: false,
188        joined_autoincrement_attribute: false,
189        inline_primary_key_ordering: false,
190        named_column_collate_constraint: false,
191        identity_columns: true,
192        default_expression_requires_parens: false,
193        column_default_requires_b_expr: true,
194        column_collation: true,
195        column_storage: true,
196    };
197}
198
199impl StatementDdlGates {
200    /// Statement-level DDL productions enabled by this preset.
201    pub const QUILTDB: Self = Self {
202        colocation_groups: true,
203        create_trigger: false,
204        create_macro: false,
205        create_secret: false,
206        create_type: false,
207        create_virtual_table: false,
208        create_sequence: true,
209        extension_ddl: true,
210        transform_ddl: true,
211        alter_system: true,
212        tablespace_ddl: false,
213        logfile_group_ddl: false,
214        schemas: true,
215        schema_elements: true,
216        databases: true,
217        drop_database: false,
218        materialized_views: true,
219        routines: true,
220        or_replace: true,
221        create_or_replace_table: false,
222        compound_statements: false,
223        alter_database: false,
224        alter_database_options: false,
225        server_definition: false,
226        alter_instance: false,
227        spatial_reference_system: false,
228        resource_group: false,
229        alter_sequence: false,
230        alter_object_set_schema: false,
231    };
232}
233impl ViewSequenceClauseSyntax {
234    /// View/sequence clause surface for the `QUILTDB` preset.
235    pub const QUILTDB: Self = Self {
236        materialized_view_to: true,
237        create_sequence_cache: true,
238        temporary_views: true,
239        recursive_views: false,
240        view_definition_options: false,
241    };
242}
243
244impl TypeNameSyntax {
245    /// Type-name syntax enabled by this preset.
246    pub const QUILTDB: Self = Self {
247        extended_scalar_type_names: true,
248        enum_type: true,
249        set_type: true,
250        numeric_modifiers: true,
251        angle_bracket_types: true,
252        composite_types: true,
253        nullable_type: true,
254        low_cardinality_type: true,
255        fixed_string_type: true,
256        datetime64_type: true,
257        bit_width_integer_names: true,
258        liberal_type_names: true,
259        string_type_modifiers: true,
260        integer_display_width: false,
261        varchar_requires_length: false,
262        zoned_temporal_types: true,
263        empty_type_parens: false,
264        character_set_annotation: false,
265        signed_type_modifier: true,
266        nested_type: false,
267    };
268}
269
270impl TableExpressionSyntax {
271    /// Table-expression syntax enabled by this preset.
272    pub const QUILTDB: Self = Self {
273        table_sample: false,
274        only: true,
275        parenthesized_joins: true,
276        table_alias_column_lists: true,
277        join_using_alias: true,
278        index_hints: false,
279        table_hints: false,
280        partition_selection: false,
281        base_table_alias_column_lists: true,
282        string_literal_aliases: false,
283        aliased_parenthesized_join: true,
284        bare_table_alias_is_bare_label: false,
285        table_version: false,
286        table_json_path: false,
287        indexed_by: false,
288        prefix_colon_alias: false,
289    };
290}
291
292impl UtilitySyntax {
293    /// Utility-statement syntax enabled by this preset.
294    pub const QUILTDB: Self = Self {
295        comment_if_exists: true,
296        copy: true,
297        copy_into: false,
298        stage_references: false,
299        comment_on: true,
300        pragma: false,
301        attach: false,
302        kill: false,
303        handler_statements: false,
304        plugin_component_statements: false,
305        shutdown: false,
306        restart: false,
307        clone: false,
308        import_table: false,
309        help_statement: false,
310        binlog: false,
311        key_cache_statements: false,
312        use_statement: false,
313        use_qualified_name: false,
314        use_string_literal_name: false,
315        prepared_statements: true,
316        prepare_typed_parameters: true,
317        prepared_statements_from: false,
318        call: false,
319        call_bare_name: false,
320        load_extension: true,
321        load_bare_name: false,
322        load_data: false,
323        reset_scope: false,
324        detach_if_exists: false,
325        do_statement: true,
326        do_expression_list: false,
327        lock_tables: false,
328        lock_instance: false,
329        rename_statement: false,
330        signal_diagnostics: false,
331        export_import_database: false,
332        update_extensions: false,
333        flush: false,
334        purge_binary_logs: false,
335        replication_statements: false,
336    };
337}
338impl TransactionSyntax {
339    /// Transaction-control surface for the `QUILTDB` preset (split from UtilitySyntax).
340    pub const QUILTDB: Self = Self {
341        start_transaction: true,
342        start_transaction_block_optional: false,
343        transaction_work_keyword: true,
344        begin_transaction_keyword: true,
345        commit_transaction_keyword: true,
346        rollback_transaction_keyword: true,
347        transaction_name: false,
348        begin_transaction_modes: true,
349        transaction_savepoints: true,
350        set_transaction: true,
351        transaction_isolation_mode: true,
352        transaction_access_mode: true,
353        transaction_deferrable_mode: true,
354        start_transaction_isolation_mode: true,
355        start_transaction_deferrable_mode: true,
356        start_transaction_consistent_snapshot: false,
357        transaction_multiple_modes: true,
358        transaction_modes_require_commas: false,
359        transaction_modes_reject_duplicates: false,
360        abort_transaction_alias: true,
361        end_transaction_alias: true,
362        transaction_release: false,
363        transaction_chain: true,
364        release_savepoint_keyword_optional: true,
365        begin_transaction_mode: false,
366        xa_transactions: false,
367    };
368}
369
370impl FeatureSet {
371    /// The complete QuiltDB feature set.
372    pub const QUILTDB: Self = Self {
373        access_control_syntax: AccessControlSyntax::QUILTDB,
374        expression_syntax: ExpressionSyntax::QUILTDB,
375        index_alter_syntax: IndexAlterSyntax::QUILTDB,
376        column_definition_syntax: ColumnDefinitionSyntax::QUILTDB,
377        statement_ddl_gates: StatementDdlGates::QUILTDB,
378        view_sequence_clause_syntax: ViewSequenceClauseSyntax::QUILTDB,
379        table_expressions: TableExpressionSyntax::QUILTDB,
380        mutation_syntax: MutationSyntax::QUILTDB,
381        predicate_syntax: PredicateSyntax::QUILTDB,
382        select_syntax: SelectSyntax::QUILTDB,
383        type_name_syntax: TypeNameSyntax::QUILTDB,
384        utility_syntax: UtilitySyntax::QUILTDB,
385        transaction_syntax: TransactionSyntax::QUILTDB,
386        identifier_casing: Casing::Lower,
387        identifier_quotes: STANDARD_IDENTIFIER_QUOTES,
388        default_null_ordering: NullOrdering::NullsLast,
389        reserved_column_name: RESERVED_COLUMN_NAME,
390        reserved_function_name: RESERVED_FUNCTION_NAME,
391        reserved_type_name: RESERVED_TYPE_NAME,
392        reserved_bare_alias: RESERVED_BARE_ALIAS,
393        reserved_as_label: KeywordSet::EMPTY,
394        catalog_qualified_names: true,
395        byte_classes: POSTGRES_BYTE_CLASSES,
396        binding_powers: FeatureSet::POSTGRES.binding_powers, // shared with Postgres (byte-identical; avoid dual SoT)
397        set_operation_powers: STANDARD_SET_OPERATION_BINDING_POWERS,
398        string_literals: StringLiteralSyntax::POSTGRES,
399        numeric_literals: NumericLiteralSyntax::POSTGRES,
400        parameters: ParameterSyntax::POSTGRES,
401        session_variables: SessionVariableSyntax::ANSI,
402        identifier_syntax: IdentifierSyntax::POSTGRES,
403        join_syntax: JoinSyntax::POSTGRES,
404        table_factor_syntax: TableFactorSyntax::POSTGRES,
405        operator_syntax: OperatorSyntax::POSTGRES,
406        call_syntax: CallSyntax::POSTGRES,
407        string_func_forms: StringFuncForms::POSTGRES,
408        aggregate_call_syntax: AggregateCallSyntax::POSTGRES,
409        pipe_operator: PipeOperator::StringConcat,
410        double_ampersand: DoubleAmpersand::Unsupported,
411        keyword_operators: KeywordOperators::Unsupported,
412        caret_operator: CaretOperator::Exponent,
413        hash_bitwise_xor: true,
414        comment_syntax: CommentSyntax::POSTGRES,
415        create_table_clause_syntax: CreateTableClauseSyntax::POSTGRES,
416        constraint_syntax: ConstraintSyntax::POSTGRES,
417        existence_guards: ExistenceGuards::POSTGRES,
418        query_tail_syntax: QueryTailSyntax::POSTGRES,
419        grouping_syntax: GroupingSyntax::POSTGRES,
420        show_syntax: ShowSyntax::POSTGRES,
421        maintenance_syntax: MaintenanceSyntax::POSTGRES,
422        target_spelling: TargetSpelling::Postgres,
423    };
424}
425
426/// Prefer [`FeatureSet::QUILTDB`] for struct update.
427pub const QUILTDB: FeatureSet = FeatureSet::QUILTDB;
428
429const _: () = assert!(FeatureSet::QUILTDB.is_lexically_consistent());
430const _: () = assert!(FeatureSet::QUILTDB.has_satisfied_feature_dependencies());
431const _: () = assert!(FeatureSet::QUILTDB.has_no_grammar_conflict());