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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
// Copyright (C) 2026 Gabriel Shea
// Licensed under either of the Apache License, Version 2.0 or the MIT
// license, at your option.
//! Boolean capability flags with conservative defaults.
//!
//! [`DialectCapabilities`] tells the rendering layer what syntax features
//! the platform supports. Defaults are conservative (`false` / ANSI-safe),
//! so unknown platforms produce safe SQL.
use crate::data_type::DataType;
use crate::evolution::EvolutionAnswers;
use crate::merge_shape::MergeShape;
/// **The widest exact decimal one engine's column holds.**
///
/// Every engine that records one refuses a precision of zero, so the fewest
/// digits is always one and is not a field.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DecimalLimits {
/// The most digits a column may declare.
pub max_precision: u16,
/// The most digits after the decimal point.
pub max_scale: u16,
/// Whether the digits after the point must be no more than the digits in
/// all. Some engines let a column hold only a fraction smaller than a
/// thousandth, with more scale than precision; most refuse it.
pub scale_within_precision: bool,
/// The most digits before the decimal point, where the engine bounds
/// them apart from the digits in all; `None` where the digits in all are
/// the only bound. `BigQuery` holds 76 digits, but never more than 38 of
/// them before the point.
pub max_integer_digits: Option<u16>,
}
impl DecimalLimits {
/// Why `decimal(precision, scale)` does not fit, in a few plain words, or
/// `None` when it does.
///
/// Parameters:
/// - `precision`: the declared digits in all;
/// - `scale`: the declared digits after the point.
#[must_use]
pub fn misfit(&self, precision: u16, scale: u16) -> Option<String> {
let most = self.max_precision;
if precision == 0 || precision > most {
return Some(format!(
"its decimals hold 1 to {most} digits, and this asks for {precision}"
));
}
if scale > self.max_scale {
return Some(format!(
"its decimals hold at most {} digits after the point, and this asks for {scale}",
self.max_scale
));
}
if self.scale_within_precision && scale > precision {
return Some(format!(
"its decimals hold no more digits after the point than in all, and this asks for \
{scale} of {precision}"
));
}
let before_point = precision.saturating_sub(scale);
if let Some(most) = self.max_integer_digits
&& before_point > most
{
return Some(format!(
"its decimals hold at most {most} digits before the point, and this asks for \
{before_point}"
));
}
None
}
/// The nearest `decimal(precision, scale)` these limits hold, for a
/// refusal to suggest in place of the one declared.
///
/// Parameters:
/// - `precision`: the declared digits in all;
/// - `scale`: the declared digits after the point.
#[must_use]
pub fn nearest(&self, precision: u16, scale: u16) -> (u16, u16) {
let mut scale = scale.min(self.max_scale);
let mut precision = precision.clamp(1, self.max_precision);
if self.scale_within_precision {
scale = scale.min(precision);
}
if let Some(most) = self.max_integer_digits {
precision = precision.min(scale + most).max(1);
}
if self.scale_within_precision {
scale = scale.min(precision);
}
(precision, scale)
}
}
/// Level 6 — Boolean capability flags with conservative defaults.
///
/// These flags tell the rendering layer what syntax features the platform supports.
/// Defaults are `false` / conservative, so unknown platforms produce safe ANSI SQL.
pub trait DialectCapabilities {
// ═══════════════════════════════════════════════════════════════
// DML Capabilities
// ═══════════════════════════════════════════════════════════════
/// **How this engine performs the upsert Razeri's generated load is.**
///
/// See [`MergeShape`]. The default is a refusal, because the honest answer
/// for an engine nobody has measured is that Razeri does not know how to
/// load it — not that it spells its upsert the way `MySQL` does.
fn merge_shape(&self) -> MergeShape {
MergeShape::None
}
/// **What this engine keeps when a table that already holds rows changes
/// shape.**
///
/// See [`EvolutionAnswers`]. The default answers nothing, which Razeri
/// reads as *nobody has measured this engine* — so a change Razeri cannot
/// otherwise prove keeps every value is reported as one that does not, and
/// an operator gets a warning they can read instead of a surprise.
fn evolution_answers(&self) -> EvolutionAnswers {
EvolutionAnswers::none()
}
/// Whether the platform supports INSERT ... ON CONFLICT (PostgreSQL-style upsert).
fn supports_on_conflict(&self) -> bool {
false
}
/// Whether the platform supports INSERT OVERWRITE.
fn supports_insert_overwrite(&self) -> bool {
false
}
/// Whether the platform supports UPDATE ... FROM.
fn supports_update_from(&self) -> bool {
false
}
/// Whether the platform supports RETURNING / OUTPUT clauses.
fn supports_returning(&self) -> bool {
false
}
// ═══════════════════════════════════════════════════════════════
// Query Capabilities
// ═══════════════════════════════════════════════════════════════
/// Whether the platform supports Common Table Expressions (WITH).
fn supports_cte(&self) -> bool {
true
}
/// Whether the platform supports recursive CTEs.
fn supports_recursive_cte(&self) -> bool {
true
}
/// Whether the platform supports window functions.
fn supports_window_functions(&self) -> bool {
true
}
/// Whether the platform supports named windows (WINDOW clause).
fn supports_named_windows(&self) -> bool {
true
}
/// Whether the platform supports QUALIFY clause.
fn supports_qualify(&self) -> bool {
false
}
/// Whether the platform supports GROUPS frame type in window functions.
fn supports_groups_frame(&self) -> bool {
false
}
/// Whether the platform supports NULLS FIRST/LAST in ORDER BY.
fn supports_nulls_ordering(&self) -> bool {
true
}
// ═══════════════════════════════════════════════════════════════
// DDL Capabilities
// ═══════════════════════════════════════════════════════════════
/// Whether the platform supports IDENTITY columns.
fn supports_identity(&self) -> bool {
false
}
/// Whether the platform supports sequences (CREATE SEQUENCE).
fn supports_sequences(&self) -> bool {
false
}
/// Whether the platform supports schema namespacing.
fn supports_schemas(&self) -> bool {
true
}
/// **Can a routine created in one schema be called from a routine created
/// in another, with nothing else done?**
///
/// True nearly everywhere, and true by default: a schema on most engines is
/// a namespace, and naming an object in another namespace is all that
/// reaching it takes.
///
/// Answer `false` where a schema is also an owner and reaching across one
/// costs a privilege granted per object. On Oracle a schema IS a user; a
/// routine runs with its owner's privileges, so a function created in
/// `acme_core` calling one created in `acme_raw` compiles with errors —
/// `ORA-60535: invalid identifier`, measured — however exactly it spells
/// the name.
///
/// What Razeri does with the answer is put a copy of the key function in
/// **every namespace that calls it** instead of one per domain, so each
/// namespace stands on its own and no deploy owes a grant. It is one
/// derivation either way: the copies are the same builder over the same
/// vocabulary, not two spellings free to drift. The engine-level hash
/// beneath them keeps its single home — see
/// [`ReplicationRenderer::sid_prerequisite_home`](crate::ReplicationRenderer::sid_prerequisite_home)
/// — because that one is Razeri's own object and installs its own grant.
fn routine_is_callable_across_schemas(&self) -> bool {
true
}
/// Whether the platform supports bulk load (COPY, BULK INSERT, etc.).
fn supports_bulk_load(&self) -> bool {
false
}
/// Whether the platform supports stored procedures.
fn supports_stored_procedures(&self) -> bool {
false
}
/// Whether the platform **stores functions at all** — a routine the schema
/// creates and later calls by name, in any spelling.
///
/// Nearly every engine razeri reaches has one: `CREATE FUNCTION` on
/// `PostgreSQL`, `MySQL`, `MariaDB`, Snowflake, `SQL Server` and Databricks,
/// a `MACRO` on `DuckDB`, a lambda `CREATE FUNCTION` on `ClickHouse`.
///
/// **`SQLite` answers `false`, and it is the reason this question is asked
/// out loud.** `SQLite` has no `CREATE FUNCTION` statement in any spelling
/// and no `CREATE PROCEDURE` either — a function there is registered by the
/// program that opens the database, so nothing written into a schema script
/// can create one. Inheriting the `PostgreSQL` form wrote
/// `CREATE OR REPLACE FUNCTION … LANGUAGE SQL RETURN …` into `schema.sql`,
/// which `SQLite` answers with `near "CREATE": syntax error` part way
/// through the script, so the build refuses in front of the script instead.
///
/// The default is `true`, matching [`Self::supports_table_functions`] and
/// for the same reason: refusing is the destructive answer, so a refusal
/// fires only where an engine is KNOWN to have no form, never because a
/// dialect forgot to answer. That reverses the conservative-`false` default
/// this trait uses for flags that *add* syntax — here `false` is the
/// unusual claim, and it is made deliberately, one dialect at a time.
fn supports_stored_functions(&self) -> bool {
true
}
/// Whether the platform has a **table-valued function** — a routine a query
/// selects FROM, returning columns rather than one value.
///
/// Most engines have one in some spelling: `RETURNS TABLE(…)` on
/// `PostgreSQL`, Snowflake and Databricks, an inline TVF on SQL Server, a
/// table `MACRO` on `DuckDB`, a parameterized view on `ClickHouse`.
///
/// **`MySQL` and `MariaDB` answer `false`, and they are the reason this
/// question exists.** A stored function there returns exactly one value;
/// there is no clause that returns a relation, in any spelling. Rendering
/// `RETURNS TABLE` for them produced `ERROR 1064 … syntax error` in the
/// middle of a schema script (measured on `MySQL` 8.4 and `MariaDB` 11.4),
/// so the build refuses in front of the script instead.
///
/// The default is `true`: a refusal fires only where an engine is KNOWN to
/// have no form, never because a dialect forgot to answer.
fn supports_table_functions(&self) -> bool {
true
}
/// Whether the platform supports tasks / scheduled jobs.
fn supports_tasks(&self) -> bool {
false
}
/// Whether the platform supports native table partitioning.
fn supports_partitioning(&self) -> bool {
true
}
/// Whether the platform supports materialized views.
fn supports_materialized_views(&self) -> bool {
true
}
/// Whether the platform supports generated/computed columns.
fn supports_generated_columns(&self) -> bool {
true
}
/// Whether the platform supports JSONB or native JSON type.
fn supports_json(&self) -> bool {
true
}
// ═══════════════════════════════════════════════════════════════
// Type Capabilities
// ═══════════════════════════════════════════════════════════════
/// Whether the platform supports unsigned integers natively.
fn supports_unsigned_integers(&self) -> bool {
false
}
/// Whether the platform supports arrays natively.
fn supports_arrays(&self) -> bool {
false
}
/// **Whether this engine has a type of its own for a nested type, or
/// stores it as something else.**
///
/// A nested column is an array, a map or a record. Some engines have all
/// three; some have one; some have none and hold the value as JSON or as
/// text. Razeri never refuses the column — the design is portable and it is
/// the storage that degrades — but it does say so once, naming the engine,
/// the columns and what each became.
///
/// The default is `false`, which is the honest answer for an engine nobody
/// has answered for: the reader is told what the column became rather than
/// left to assume the type survived.
///
/// Parameters:
/// - `data_type`: the nested type in question.
fn renders_nested_type_natively(&self, data_type: &DataType) -> bool {
let _ = data_type;
false
}
/// **Whether this engine can tell two values of a nested type apart.**
///
/// Change detection asks whether an arriving value differs from the
/// standing one. For a scalar every engine answers with `<>`. For a nested
/// value they do not agree: some compare arrays, maps and records directly,
/// some refuse the comparison outright, and some hold the value as text and
/// compare the text. A column this returns `false` for is left out of
/// change detection and named in a note, rather than compared with an
/// operator the engine will reject.
///
/// The default is `false` — an unanswered engine leaves the column
/// unwatched and says so, which is the failure that can be read.
///
/// Parameters:
/// - `data_type`: the nested type in question.
fn compares_nested_values(&self, data_type: &DataType) -> bool {
let _ = data_type;
false
}
/// **Whether this engine can collapse rows on a nested column** — group by
/// it, or select distinct over it.
///
/// A load reads its staged rows through one collapsed form so that a row
/// stated twice, identically, is one arriving row rather than a conflict.
/// The plain spelling of that collapse is `SELECT DISTINCT` over every
/// column, and grouping is what `DISTINCT` is: an engine that will not
/// group a value of a nested type will not take the column in a `DISTINCT`
/// either.
///
/// It is a separate question from [`Self::compares_nested_values`], and the
/// two engines that answer them differently are the reason it is asked
/// separately: `BigQuery` compares two records with `=` and refuses to group
/// on one.
///
/// Where the answer is `false` the collapse is written as a `GROUP BY` over
/// the other columns with [`crate::SqlRenderer::render_any_value`] around
/// this one.
///
/// The default is `true`: `SELECT DISTINCT` over every column is the plain
/// spelling, and an engine says otherwise only where somebody has found
/// that it refuses.
///
/// Asked only of a nested type — an array, a map or a record. The answer
/// for anything else is not read.
///
/// Parameters:
/// - `data_type`: the nested type in question.
fn groups_nested_values(&self, data_type: &DataType) -> bool {
let _ = data_type;
true
}
/// Whether the platform supports native BOOLEAN type (vs BIT).
fn supports_boolean(&self) -> bool {
true
}
// ═══════════════════════════════════════════════════════════════
// Limits & Defaults
// ═══════════════════════════════════════════════════════════════
/// The default schema name for this platform.
fn default_schema(&self) -> &'static str {
"public"
}
/// Maximum identifier length for this platform.
fn max_identifier_length(&self) -> usize {
63
}
/// Maximum number of columns in a table (approximate).
fn max_columns(&self) -> usize {
10000
}
/// Maximum query parameters (approximate, 0 = unlimited).
fn max_parameters(&self) -> usize {
0
}
/// **The widest exact decimal a column on this platform holds**, where it
/// has been measured against the engine.
///
/// A type over it is refused by name rather than rendered: the engine
/// would refuse the `CREATE TABLE` anyway, later and in its own words.
///
/// The default is `None`, which means *not measured here*, not *no
/// limit*. An engine with no limit at all says so in its own doc comment
/// and still answers `None`.
fn decimal_limits(&self) -> Option<DecimalLimits> {
None
}
// ═══════════════════════════════════════════════════════════════
// Drop Capabilities
// ═══════════════════════════════════════════════════════════════
/// Whether DROP supports CASCADE modifier.
fn supports_cascade_drop(&self) -> bool {
true
}
/// Whether DROP FUNCTION requires parameter types for overloaded functions.
fn supports_function_overloading(&self) -> bool {
false
}
/// Whether DROP supports SYNC modifier (`ClickHouse`).
fn supports_sync_drop(&self) -> bool {
false
}
/// Whether DROP supports FORCE modifier (Databricks).
fn supports_force_drop(&self) -> bool {
false
}
// ═══════════════════════════════════════════════════════════════
// DML Extended Capabilities
// ═══════════════════════════════════════════════════════════════
/// Whether the platform supports compound assignment (e.g., `col += value`).
fn supports_compound_assignment(&self) -> bool {
false
}
/// Whether the platform supports EXCLUDE clause in window frame specs.
fn supports_window_frame_exclude(&self) -> bool {
false
}
/// Whether the platform supports MERGE NOT MATCHED BY SOURCE.
fn supports_merge_not_matched_by_source(&self) -> bool {
false
}
/// Keyword emitted for MERGE NOT MATCHED branch.
fn merge_not_matched_keyword(&self) -> &'static str {
"WHEN NOT MATCHED"
}
/// Whether the platform supports UPDATE SET * / INSERT * in MERGE.
fn supports_merge_star_syntax(&self) -> bool {
false
}
/// Whether an UPDATE is spelled `ALTER TABLE … UPDATE` (`ClickHouse`).
///
/// **There is a second declaration of the same fact** on
/// [`crate::SqlRenderer::uses_alter_table_update`], and that one is the
/// declaration Razeri's generators consult. A dialect that overrides one
/// must override both and keep them agreeing; the duplication is named
/// here so a reader finds it before an ambiguous-method error does.
fn uses_alter_table_update(&self) -> bool {
false
}
/// Whether the platform supports multi-table INSERT (Snowflake).
fn supports_multi_table_insert(&self) -> bool {
false
}
// ═══════════════════════════════════════════════════════════════
// DDL / Namespace Capabilities
// ═══════════════════════════════════════════════════════════════
/// The SQL keyword for the schema-level namespace.
///
/// Most platforms use `SCHEMA` (`CREATE SCHEMA IF NOT EXISTS ...`).
/// `ClickHouse` uses `DATABASE` (`CREATE DATABASE IF NOT EXISTS ...`).
fn schema_namespace_keyword(&self) -> &'static str {
"SCHEMA"
}
// ═══════════════════════════════════════════════════════════════
// Comment Capabilities
// ═══════════════════════════════════════════════════════════════
/// Whether the platform supports `#` as a single-line comment delimiter.
///
/// Default: `false`. A dialect whose engine accepts `#` as a single-line
/// comment delimiter overrides this.
fn supports_hash_comments(&self) -> bool {
false
}
// ═══════════════════════════════════════════════════════════════
// Script Formatting
// ═══════════════════════════════════════════════════════════════
/// Batch separator keyword required between DDL statements in a script.
///
/// SQL Server / T-SQL requires `GO` between `CREATE FUNCTION`, `CREATE VIEW`,
/// and `CREATE PROCEDURE` statements. Other platforms return `None` and use
/// double-newlines between statements.
fn batch_separator(&self) -> Option<&'static str> {
None
}
// ═══════════════════════════════════════════════════════════════
// Transaction Capabilities
// ═══════════════════════════════════════════════════════════════
/// Whether several statements in a script can be committed or discarded
/// together as one unit.
///
/// A generated load is a sequence of writes that only make sense finished.
/// An SCD-2 load, for instance, closes the standing version into `_history`
/// and then re-opens `_current` on the new attributes; a process that dies
/// between the two leaves a closed version that `_current` still holds open,
/// and because `_history` is keyed on (surrogate, version start), the next
/// load writes the same key again and the engine refuses it. Every load
/// after the crash fails until somebody deletes that row by hand.
///
/// Wrapping the load in one transaction is what makes the crash a no-op
/// instead. This flag says whether the platform offers that at all. It
/// defaults to `true` because a transaction spanning several statements is
/// core SQL and nearly every engine has it; a platform that does not
/// overrides it, and the load script then says plainly that the operator
/// owns the boundary.
fn supports_multi_statement_transactions(&self) -> bool {
true
}
/// How this platform opens a transaction.
///
/// The ANSI spelling, which `MySQL` and `MariaDB` also use. `DuckDB`,
/// `SQLite`, `SQL Server` and `Snowflake` say `BEGIN TRANSACTION`;
/// `PostgreSQL` and `Citus` say `BEGIN`.
fn begin_transaction_keyword(&self) -> &'static str {
"START TRANSACTION"
}
/// How this platform commits one.
///
/// `COMMIT` everywhere razeri reaches except `SQL Server`, which spells it
/// `COMMIT TRANSACTION`.
fn commit_transaction_keyword(&self) -> &'static str {
"COMMIT"
}
/// Whether emptying a table with `TRUNCATE` ends the open transaction.
///
/// `MySQL` and `MariaDB` treat `TRUNCATE TABLE` as schema change, and a
/// schema change commits everything before it and cannot be rolled back.
/// A generated load clears its staging table that way, so on those two
/// platforms the transaction around the load does not reach back past the
/// most recent staging clear. Everywhere else `TRUNCATE` is an ordinary
/// statement inside the transaction.
fn truncate_commits_implicitly(&self) -> bool {
false
}
// ═══════════════════════════════════════════════════════════════
// Storage / Partition / Compression Capabilities
// ═══════════════════════════════════════════════════════════════
/// Whether the platform supports a server-side partition function definable
/// at design time (`T-SQL` `PARTITION FUNCTION`, `PostgreSQL` `PARTITION BY`,
/// `MySQL` `PARTITION BY`, `ClickHouse` `PARTITION BY`).
///
/// `Snowflake` / `DuckDB` return `false` because micro-partitioning is automatic
/// and not user-definable.
fn supports_partition_function(&self) -> bool {
false
}
/// Whether the platform supports adding/removing partition boundaries on a
/// live table (`T-SQL` `ALTER PARTITION FUNCTION SPLIT/MERGE RANGE`,
/// `PostgreSQL` `ATTACH/DETACH PARTITION`, `MySQL` `REORGANIZE PARTITION`).
fn supports_partition_split_merge(&self) -> bool {
false
}
/// Whether the platform supports a columnstore index (`T-SQL` clustered/non-clustered
/// columnstore; `ClickHouse` via `MergeTree` engine; not natively in `PostgreSQL` / `MySQL`).
fn supports_columnstore_index(&self) -> bool {
false
}
/// Whether the platform realizes a columnstore as a **table storage engine**
/// rather than an index (`MariaDB` `ENGINE=ColumnStore`). This is deliberately
/// distinct from [`Self::supports_columnstore_index`]: a design that asks for
/// `index: { kind: clustered_columnstore }` is honoured on such a platform by
/// selecting the engine, but it must NOT inherit the `T-SQL`
/// index machinery — the composite-PK demotion to `UNIQUE NONCLUSTERED` keys
/// off `supports_columnstore_index`, and an engine-columnstore keeps its
/// ordinary primary key. So the two capabilities are separate axes: "supports
/// columnstore at all" is the disjunction of them.
fn columnstore_is_table_engine(&self) -> bool {
false
}
/// Whether the platform has a clustering-key concept distinct from primary key
/// (`Snowflake` `CLUSTER BY`, Delta Liquid Clustering / `Z-ORDER`, `ClickHouse` `ORDER BY`).
fn supports_clustering_key(&self) -> bool {
false
}
/// Whether the platform supports user-controllable table compression
/// (`T-SQL` `DATA_COMPRESSION = PAGE/ROW/COLUMNSTORE`, `MySQL` `ROW_FORMAT=COMPRESSED`,
/// `ClickHouse` `CODEC(ZSTD)`, Delta `delta.compressionCodec`).
fn supports_table_compression(&self) -> bool {
false
}
/// Whether the platform requires every column referenced by the partition
/// key to also appear in the table's PRIMARY KEY (or any UNIQUE
/// constraint).
///
/// `PostgreSQL` enforces this with `SQLSTATE 0A000` ("unique constraint on
/// partitioned table must include all partitioning columns"). `T-SQL`,
/// `MySQL`, `ClickHouse`, `Snowflake`, and `DuckDB` do not impose this
/// restriction.
fn partition_requires_pk_includes_field(&self) -> bool {
false
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::merge_shape::MergeRestrictions;
struct DefaultDialect;
impl DialectCapabilities for DefaultDialect {}
/// Assert default DML capability flags are conservative.
/// Parameters:
/// - d: Dialect capabilities implementation under test.
fn assert_default_dml_capabilities(d: &impl DialectCapabilities) {
assert_eq!(d.merge_shape(), MergeShape::None);
assert!(!d.supports_on_conflict());
assert!(!d.supports_insert_overwrite());
assert!(!d.supports_update_from());
assert!(!d.supports_returning());
}
/// Assert default query capability flags and ANSI-safe behavior.
/// Parameters:
/// - d: Dialect capabilities implementation under test.
fn assert_default_query_capabilities(d: &impl DialectCapabilities) {
assert!(d.supports_cte());
assert!(d.supports_recursive_cte());
assert!(d.supports_window_functions());
assert!(d.supports_named_windows());
assert!(!d.supports_qualify());
assert!(!d.supports_groups_frame());
assert!(d.supports_nulls_ordering());
}
/// Assert default DDL capability and type flags are conservative.
/// Parameters:
/// - d: Dialect capabilities implementation under test.
fn assert_default_ddl_and_type_capabilities(d: &impl DialectCapabilities) {
assert!(!d.supports_identity());
assert!(!d.supports_sequences());
assert!(d.supports_schemas());
assert!(!d.supports_bulk_load());
assert!(!d.supports_stored_procedures());
// The two routine-form questions default the other way on purpose:
// answering `false` REFUSES a build, so it is never the answer an
// unknown platform falls into.
assert!(d.supports_stored_functions());
assert!(d.supports_table_functions());
assert!(!d.supports_tasks());
assert!(d.supports_partitioning());
assert!(d.supports_materialized_views());
assert!(d.supports_generated_columns());
assert!(d.supports_json());
assert!(!d.supports_unsigned_integers());
assert!(!d.supports_arrays());
assert!(d.supports_boolean());
}
/// Assert default platform limits and storage capability flags.
/// Parameters:
/// - d: Dialect capabilities implementation under test.
fn assert_default_limits_and_storage_capabilities(d: &impl DialectCapabilities) {
assert_eq!(d.default_schema(), "public");
assert_eq!(d.max_identifier_length(), 63);
assert_eq!(d.max_columns(), 10000);
assert_eq!(d.max_parameters(), 0);
assert!(!d.supports_partition_function());
assert!(!d.supports_partition_split_merge());
assert!(!d.supports_columnstore_index());
assert!(!d.supports_clustering_key());
assert!(!d.supports_table_compression());
}
#[test]
fn defaults_are_conservative() {
let d = DefaultDialect;
assert_default_dml_capabilities(&d);
assert_default_query_capabilities(&d);
assert_default_ddl_and_type_capabilities(&d);
assert_default_limits_and_storage_capabilities(&d);
}
#[test]
fn overrides_work() {
struct PostgresLike;
impl DialectCapabilities for PostgresLike {
fn merge_shape(&self) -> MergeShape {
MergeShape::Merge(MergeRestrictions::none())
}
fn supports_on_conflict(&self) -> bool {
true
}
fn supports_returning(&self) -> bool {
true
}
fn supports_identity(&self) -> bool {
true
}
fn supports_sequences(&self) -> bool {
true
}
fn supports_arrays(&self) -> bool {
true
}
fn max_columns(&self) -> usize {
1600
}
}
let d = PostgresLike;
assert!(d.merge_shape().merges());
assert!(d.supports_on_conflict());
assert!(d.supports_returning());
assert!(d.supports_identity());
assert!(d.supports_sequences());
assert!(d.supports_arrays());
assert_eq!(d.max_columns(), 1600);
// Untouched defaults
assert!(!d.supports_insert_overwrite());
assert!(d.supports_cte());
}
#[test]
fn new_capability_defaults_are_conservative() {
let d = DefaultDialect;
// Drop capabilities
assert!(d.supports_cascade_drop()); // true = ANSI default
assert!(!d.supports_function_overloading());
assert!(!d.supports_sync_drop());
assert!(!d.supports_force_drop());
// DML extended
assert!(!d.supports_compound_assignment());
assert!(!d.supports_window_frame_exclude());
assert!(!d.supports_merge_not_matched_by_source());
assert_eq!(d.merge_not_matched_keyword(), "WHEN NOT MATCHED");
assert!(!d.supports_merge_star_syntax());
assert!(!d.uses_alter_table_update());
assert!(!d.supports_multi_table_insert());
// DDL / Namespace
assert_eq!(d.schema_namespace_keyword(), "SCHEMA");
}
/// The default transaction answer is the ANSI one: a load can be wrapped,
/// in the standard spelling, and clearing a staging table stays inside the
/// wrap. Every platform that differs says so on its own dialect.
#[test]
fn the_default_load_wrap_is_the_ansi_one() {
let d = DefaultDialect;
assert!(d.supports_multi_statement_transactions());
assert_eq!(d.begin_transaction_keyword(), "START TRANSACTION");
assert_eq!(d.commit_transaction_keyword(), "COMMIT");
assert!(!d.truncate_commits_implicitly());
}
/// **A bound on the digits before the point is its own refusal**, and the
/// nearest type that fits keeps the scale and gives up whole digits.
#[test]
fn digits_before_the_point_have_their_own_bound() {
let bignumeric = DecimalLimits {
max_precision: 76,
max_scale: 38,
scale_within_precision: true,
max_integer_digits: Some(38),
};
assert_eq!(bignumeric.misfit(38, 0), None);
assert_eq!(bignumeric.misfit(76, 38), None);
assert_eq!(
bignumeric.misfit(39, 0).as_deref(),
Some("its decimals hold at most 38 digits before the point, and this asks for 39")
);
assert_eq!(bignumeric.nearest(39, 0), (38, 0));
assert_eq!(bignumeric.nearest(60, 10), (48, 10));
assert_eq!(bignumeric.nearest(99, 40), (76, 38));
for (precision, scale) in [(39, 0), (60, 10), (99, 40), (5, 6), (0, 0)] {
let (p, s) = bignumeric.nearest(precision, scale);
assert_eq!(bignumeric.misfit(p, s), None, "nearest({precision}, {scale}) fits");
}
let unbounded = DecimalLimits { max_integer_digits: None, ..bignumeric };
assert_eq!(unbounded.misfit(76, 0), None, "the digits in all are the only bound");
}
}