sqlglot-rust 0.9.27

A SQL parser, optimizer, and transpiler library inspired by Python's sqlglot
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
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
/// Tests for the Custom Dialect Plugin System (PQO-199).
///
/// Verifies:
/// - DialectPlugin trait implementation
/// - DialectRegistry registration and lookup
/// - DialectRef built-in and custom variants
/// - Plugin-aware transpile pipeline (transpile_ext)
/// - Custom function and type mapping
/// - Custom ILIKE support flag
/// - Custom quote style
/// - Full statement-level transform hooks
/// - resolve_dialect helper
use sqlglot_rust::ast::{DataType, Expr, QuoteStyle, Statement};
use sqlglot_rust::dialects::plugin::{
    DialectPlugin, DialectRef, DialectRegistry, register_dialect, resolve_dialect, transpile_ext,
    transpile_statements_ext,
};
use sqlglot_rust::Dialect;

// ═════════════════════════════════════════════════════════════════════════════
// Test dialect implementations
// ═════════════════════════════════════════════════════════════════════════════

/// A minimal custom dialect that only overrides function names.
struct FuncDialect;

impl DialectPlugin for FuncDialect {
    fn name(&self) -> &str {
        "funcdialect"
    }

    fn map_function_name(&self, name: &str) -> Option<String> {
        match name.to_uppercase().as_str() {
            "NOW" => Some("GET_CURRENT_TS".to_string()),
            "LENGTH" => Some("CHAR_COUNT".to_string()),
            _ => None,
        }
    }
}

/// A custom dialect that overrides data type mapping.
struct TypeDialect;

impl DialectPlugin for TypeDialect {
    fn name(&self) -> &str {
        "typedialect"
    }

    fn map_data_type(&self, dt: &DataType) -> Option<DataType> {
        match dt {
            DataType::Text => Some(DataType::Varchar(Some(65535))),
            DataType::Int => Some(DataType::BigInt),
            _ => None,
        }
    }
}

/// A custom dialect that uses backtick quoting and supports ILIKE.
struct QuoteDialect;

impl DialectPlugin for QuoteDialect {
    fn name(&self) -> &str {
        "quotedialect"
    }

    fn quote_style(&self) -> Option<QuoteStyle> {
        Some(QuoteStyle::Backtick)
    }

    fn supports_ilike(&self) -> Option<bool> {
        Some(true)
    }
}

/// A custom dialect that provides expression-level transforms.
struct ExprDialect;

impl DialectPlugin for ExprDialect {
    fn name(&self) -> &str {
        "exprdialect"
    }

    fn transform_expr(&self, expr: &Expr) -> Option<Expr> {
        // Replace any Number literal "0" with "FALSE"
        if let Expr::Number(n) = expr
            && n == "0"
        {
            return Some(Expr::Boolean(false));
        }
        None
    }
}

/// A custom dialect that provides a full statement-level transform.
struct StmtDialect;

impl DialectPlugin for StmtDialect {
    fn name(&self) -> &str {
        "stmtdialect"
    }

    fn transform_statement(&self, statement: &Statement) -> Option<Statement> {
        // If the statement is a SELECT with a single column "x", add a WHERE TRUE
        if let Statement::Select(sel) = statement
            && sel.columns.len() == 1
            && sel.where_clause.is_none()
        {
            let mut new_sel = sel.clone();
            new_sel.where_clause = Some(Expr::Boolean(true));
            return Some(Statement::Select(new_sel));
        }
        None
    }
}

/// A custom dialect with both function and type mappings.
struct FullDialect;

impl DialectPlugin for FullDialect {
    fn name(&self) -> &str {
        "fulldialect"
    }

    fn quote_style(&self) -> Option<QuoteStyle> {
        Some(QuoteStyle::Bracket)
    }

    fn supports_ilike(&self) -> Option<bool> {
        Some(false)
    }

    fn map_function_name(&self, name: &str) -> Option<String> {
        match name.to_uppercase().as_str() {
            "COALESCE" => Some("NVL2".to_string()),
            _ => None,
        }
    }

    fn map_data_type(&self, dt: &DataType) -> Option<DataType> {
        match dt {
            DataType::Boolean => Some(DataType::Int),
            _ => None,
        }
    }
}

// ═════════════════════════════════════════════════════════════════════════════
// Registry tests
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_register_and_lookup() {
    let registry = DialectRegistry::global();
    registry.register(FuncDialect);

    let plugin = registry.get("funcdialect");
    assert!(plugin.is_some());
    assert_eq!(plugin.unwrap().name(), "funcdialect");
}

#[test]
fn test_case_insensitive_lookup() {
    let registry = DialectRegistry::global();
    registry.register(FuncDialect);

    assert!(registry.get("FuncDialect").is_some());
    assert!(registry.get("FUNCDIALECT").is_some());
    assert!(registry.get("funcdialect").is_some());
}

#[test]
fn test_lookup_nonexistent() {
    let registry = DialectRegistry::global();
    assert!(registry.get("nonexistent_dialect_xyz").is_none());
}

#[test]
fn test_unregister() {
    let registry = DialectRegistry::global();

    // Use a unique name to avoid conflicts with other tests
    struct TempDialect;
    impl DialectPlugin for TempDialect {
        fn name(&self) -> &str {
            "tempdialect_unregister_test"
        }
    }

    registry.register(TempDialect);
    assert!(registry.get("tempdialect_unregister_test").is_some());

    let removed = registry.unregister("tempdialect_unregister_test");
    assert!(removed);
    assert!(registry.get("tempdialect_unregister_test").is_none());

    // Unregistering again returns false
    assert!(!registry.unregister("tempdialect_unregister_test"));
}

#[test]
fn test_registered_names() {
    let registry = DialectRegistry::global();

    struct NameTestDialect;
    impl DialectPlugin for NameTestDialect {
        fn name(&self) -> &str {
            "nametest_dialect"
        }
    }

    registry.register(NameTestDialect);
    let names = registry.registered_names();
    assert!(names.contains(&"nametest_dialect".to_string()));
}

#[test]
fn test_register_replaces_existing() {
    let registry = DialectRegistry::global();

    struct V1;
    impl DialectPlugin for V1 {
        fn name(&self) -> &str {
            "versioned_dialect"
        }
        fn map_function_name(&self, name: &str) -> Option<String> {
            if name == "F" {
                Some("V1".to_string())
            } else {
                None
            }
        }
    }
    struct V2;
    impl DialectPlugin for V2 {
        fn name(&self) -> &str {
            "versioned_dialect"
        }
        fn map_function_name(&self, name: &str) -> Option<String> {
            if name == "F" {
                Some("V2".to_string())
            } else {
                None
            }
        }
    }

    registry.register(V1);
    let p1 = registry.get("versioned_dialect").unwrap();
    assert_eq!(p1.map_function_name("F"), Some("V1".to_string()));

    registry.register(V2);
    let p2 = registry.get("versioned_dialect").unwrap();
    assert_eq!(p2.map_function_name("F"), Some("V2".to_string()));
}

// ═════════════════════════════════════════════════════════════════════════════
// DialectRef tests
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_dialect_ref_builtin() {
    let dr = DialectRef::from(Dialect::Postgres);
    assert_eq!(dr.as_builtin(), Some(Dialect::Postgres));
    assert!(dr.as_plugin().is_none());
}

#[test]
fn test_dialect_ref_custom() {
    register_dialect(FuncDialect);
    let dr = DialectRef::custom("funcdialect");
    assert!(dr.as_builtin().is_none());
    assert!(dr.as_plugin().is_some());
}

#[test]
fn test_dialect_ref_display() {
    let builtin = DialectRef::from(Dialect::Mysql);
    assert_eq!(format!("{builtin}"), "MySQL");

    let custom = DialectRef::custom("mydb");
    assert_eq!(format!("{custom}"), "Custom(mydb)");
}

#[test]
fn test_dialect_ref_equality() {
    assert_eq!(
        DialectRef::from(Dialect::Postgres),
        DialectRef::from(Dialect::Postgres)
    );
    assert_ne!(
        DialectRef::from(Dialect::Postgres),
        DialectRef::from(Dialect::Mysql)
    );
    assert_eq!(DialectRef::custom("x"), DialectRef::custom("x"));
    assert_ne!(
        DialectRef::custom("x"),
        DialectRef::from(Dialect::Postgres)
    );
}

// ═════════════════════════════════════════════════════════════════════════════
// Custom function mapping via transpile_ext
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_custom_function_mapping() {
    register_dialect(FuncDialect);

    let result = transpile_ext(
        "SELECT NOW()",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("funcdialect"),
    )
    .unwrap();

    assert_eq!(result, "SELECT GET_CURRENT_TS()");
}

#[test]
fn test_custom_function_mapping_length() {
    register_dialect(FuncDialect);

    let result = transpile_ext(
        "SELECT LENGTH(name) FROM users",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("funcdialect"),
    )
    .unwrap();

    assert_eq!(result, "SELECT CHAR_COUNT(name) FROM users");
}

#[test]
fn test_custom_function_passthrough() {
    register_dialect(FuncDialect);

    // Functions not in the custom map should pass through unchanged
    let result = transpile_ext(
        "SELECT UPPER(name) FROM users",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("funcdialect"),
    )
    .unwrap();

    assert_eq!(result, "SELECT UPPER(name) FROM users");
}

// ═════════════════════════════════════════════════════════════════════════════
// Custom type mapping via transpile_ext
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_custom_type_mapping() {
    register_dialect(TypeDialect);

    let result = transpile_ext(
        "SELECT CAST(x AS TEXT)",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("typedialect"),
    )
    .unwrap();

    assert_eq!(result, "SELECT CAST(x AS VARCHAR(65535))");
}

#[test]
fn test_custom_type_mapping_int_to_bigint() {
    register_dialect(TypeDialect);

    let result = transpile_ext(
        "SELECT CAST(x AS INT)",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("typedialect"),
    )
    .unwrap();

    assert_eq!(result, "SELECT CAST(x AS BIGINT)");
}

// ═════════════════════════════════════════════════════════════════════════════
// Custom quote style
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_custom_quote_style() {
    register_dialect(QuoteDialect);

    let dr = DialectRef::custom("quotedialect");
    assert_eq!(dr.quote_style(), QuoteStyle::Backtick);
}

#[test]
fn test_custom_supports_ilike() {
    register_dialect(QuoteDialect);

    let dr = DialectRef::custom("quotedialect");
    assert!(dr.supports_ilike());
}

// ═════════════════════════════════════════════════════════════════════════════
// Built-in → built-in via transpile_ext (passthrough)
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_builtin_to_builtin_transpile_ext() {
    let result = transpile_ext(
        "SELECT NOW()",
        &DialectRef::from(Dialect::Postgres),
        &DialectRef::from(Dialect::Tsql),
    )
    .unwrap();

    assert_eq!(result, "SELECT GETDATE()");
}

#[test]
fn test_builtin_identity_transpile_ext() {
    let sql = "SELECT a, b FROM t WHERE a > 1";
    let result = transpile_ext(
        sql,
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::from(Dialect::Ansi),
    )
    .unwrap();

    assert_eq!(result, sql);
}

// ═════════════════════════════════════════════════════════════════════════════
// transpile_statements_ext
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_transpile_statements_ext() {
    register_dialect(FuncDialect);

    let result = transpile_statements_ext(
        "SELECT NOW(); SELECT LENGTH(x)",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("funcdialect"),
    )
    .unwrap();

    assert_eq!(result.len(), 2);
    assert_eq!(result[0], "SELECT GET_CURRENT_TS()");
    assert_eq!(result[1], "SELECT CHAR_COUNT(x)");
}

// ═════════════════════════════════════════════════════════════════════════════
// Expression-level transform hook
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_custom_expr_transform() {
    register_dialect(ExprDialect);

    let result = transpile_ext(
        "SELECT 0 FROM t",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("exprdialect"),
    )
    .unwrap();

    assert_eq!(result, "SELECT FALSE FROM t");
}

// ═════════════════════════════════════════════════════════════════════════════
// Statement-level transform hook
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_custom_statement_transform() {
    register_dialect(StmtDialect);

    let result = transpile_ext(
        "SELECT x FROM t",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("stmtdialect"),
    )
    .unwrap();

    assert_eq!(result, "SELECT x FROM t WHERE TRUE");
}

#[test]
fn test_custom_statement_transform_multi_column_fallthrough() {
    register_dialect(StmtDialect);

    // More than one column → statement transform returns None → falls through
    let result = transpile_ext(
        "SELECT x, y FROM t",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("stmtdialect"),
    )
    .unwrap();

    // Should not add WHERE TRUE (multi-column fallthrough)
    assert_eq!(result, "SELECT x, y FROM t");
}

// ═════════════════════════════════════════════════════════════════════════════
// Full-featured custom dialect
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_full_dialect_function_mapping() {
    register_dialect(FullDialect);

    let result = transpile_ext(
        "SELECT COALESCE(a, b) FROM t",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("fulldialect"),
    )
    .unwrap();

    assert_eq!(result, "SELECT NVL2(a, b) FROM t");
}

#[test]
fn test_full_dialect_type_mapping() {
    register_dialect(FullDialect);

    let result = transpile_ext(
        "SELECT CAST(x AS BOOLEAN)",
        &DialectRef::from(Dialect::Ansi),
        &DialectRef::custom("fulldialect"),
    )
    .unwrap();

    assert_eq!(result, "SELECT CAST(x AS INT)");
}

// ═════════════════════════════════════════════════════════════════════════════
// resolve_dialect helper
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_resolve_builtin_dialect() {
    let dr = resolve_dialect("postgres");
    assert_eq!(dr, Some(DialectRef::from(Dialect::Postgres)));
}

#[test]
fn test_resolve_builtin_case_insensitive() {
    let dr = resolve_dialect("MYSQL");
    assert_eq!(dr, Some(DialectRef::from(Dialect::Mysql)));
}

#[test]
fn test_resolve_custom_dialect() {
    register_dialect(FuncDialect);
    let dr = resolve_dialect("funcdialect");
    assert_eq!(dr, Some(DialectRef::custom("funcdialect")));
}

#[test]
fn test_resolve_nonexistent() {
    let dr = resolve_dialect("no_such_dialect_123");
    assert!(dr.is_none());
}

// ═════════════════════════════════════════════════════════════════════════════
// register_dialect convenience function
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_register_dialect_convenience() {
    struct ConvDialect;
    impl DialectPlugin for ConvDialect {
        fn name(&self) -> &str {
            "convdialect"
        }
    }

    register_dialect(ConvDialect);
    assert!(DialectRegistry::global().get("convdialect").is_some());
}

// ═════════════════════════════════════════════════════════════════════════════
// DialectRef method tests
// ═════════════════════════════════════════════════════════════════════════════

#[test]
fn test_dialect_ref_map_function_builtin() {
    let dr = DialectRef::from(Dialect::Tsql);
    assert_eq!(dr.map_function_name("NOW"), "GETDATE");
}

#[test]
fn test_dialect_ref_map_function_custom() {
    register_dialect(FuncDialect);
    let dr = DialectRef::custom("funcdialect");
    assert_eq!(dr.map_function_name("NOW"), "GET_CURRENT_TS");
    // Unmapped function returns original
    assert_eq!(dr.map_function_name("UPPER"), "UPPER");
}

#[test]
fn test_dialect_ref_map_data_type_builtin() {
    let dr = DialectRef::from(Dialect::BigQuery);
    assert_eq!(dr.map_data_type(&DataType::Text), DataType::String);
}

#[test]
fn test_dialect_ref_map_data_type_custom() {
    register_dialect(TypeDialect);
    let dr = DialectRef::custom("typedialect");
    assert_eq!(
        dr.map_data_type(&DataType::Text),
        DataType::Varchar(Some(65535))
    );
    // Unmapped type returns original
    assert_eq!(dr.map_data_type(&DataType::Double), DataType::Double);
}

#[test]
fn test_dialect_ref_supports_ilike_builtin() {
    // Postgres supports ILIKE
    assert!(DialectRef::from(Dialect::Postgres).supports_ilike());
    // MySQL does not
    assert!(!DialectRef::from(Dialect::Mysql).supports_ilike());
}

#[test]
fn test_dialect_ref_quote_style_builtin() {
    assert_eq!(
        DialectRef::from(Dialect::Mysql).quote_style(),
        QuoteStyle::Backtick
    );
    assert_eq!(
        DialectRef::from(Dialect::Postgres).quote_style(),
        QuoteStyle::DoubleQuote
    );
    assert_eq!(
        DialectRef::from(Dialect::Tsql).quote_style(),
        QuoteStyle::Bracket
    );
}

#[test]
fn test_dialect_ref_custom_nonexistent_defaults() {
    // Custom dialect that isn't registered falls back to defaults
    let dr = DialectRef::custom("does_not_exist_abc");
    assert_eq!(dr.quote_style(), QuoteStyle::DoubleQuote);
    assert!(!dr.supports_ilike());
    assert_eq!(dr.map_function_name("FOO"), "FOO");
    assert_eq!(dr.map_data_type(&DataType::Text), DataType::Text);
}