data-modelling-core 2.4.0

Core SDK library for model operations across platforms
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
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
//! Import module tests

use data_modelling_core::import::{
    avro::AvroImporter, json_schema::JSONSchemaImporter, odcs::ODCSImporter,
    protobuf::ProtobufImporter, sql::SQLImporter,
};

mod sql_import_tests {
    use super::*;

    #[test]
    fn test_parse_simple_table() {
        let importer = SQLImporter::new("postgres");
        let sql = "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL);";
        let result = importer.parse(sql).unwrap();

        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 1);

        let table = &result.tables[0];
        assert_eq!(table.name.as_deref(), Some("users"));
        assert_eq!(table.columns.len(), 2);

        let id_col = &table.columns[0];
        assert_eq!(id_col.name, "id");
        assert!(id_col.primary_key);

        let name_col = &table.columns[1];
        assert_eq!(name_col.name, "name");
        assert!(!name_col.nullable);
    }

    #[test]
    fn test_parse_multiple_tables() {
        let importer = SQLImporter::new("postgres");
        let sql = r#"
            CREATE TABLE users (id INT PRIMARY KEY, name TEXT);
            CREATE TABLE orders (id INT PRIMARY KEY, user_id INT, total DECIMAL);
        "#;
        let result = importer.parse(sql).unwrap();

        assert_eq!(result.tables.len(), 2);
        assert_eq!(result.tables[0].name.as_deref(), Some("users"));
        assert_eq!(result.tables[1].name.as_deref(), Some("orders"));
    }

    #[test]
    fn test_parse_with_schema_qualified_name() {
        let importer = SQLImporter::new("postgres");
        let sql = "CREATE TABLE public.users (id INT PRIMARY KEY);";
        let result = importer.parse(sql).unwrap();

        assert_eq!(result.tables.len(), 1);
        assert_eq!(result.tables[0].name.as_deref(), Some("users"));
    }

    #[test]
    fn test_parse_table_level_pk_constraint() {
        let importer = SQLImporter::new("postgres");
        let sql = "CREATE TABLE t (id INT, name TEXT, CONSTRAINT pk PRIMARY KEY (id));";
        let result = importer.parse(sql).unwrap();

        assert_eq!(result.tables.len(), 1);
        let id_col = &result.tables[0].columns[0];
        assert!(id_col.primary_key);
    }

    #[test]
    fn test_parse_mysql_dialect() {
        let importer = SQLImporter::new("mysql");
        let sql =
            "CREATE TABLE `users` (`id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(100));";
        let result = importer.parse(sql).unwrap();

        assert_eq!(result.tables.len(), 1);
        assert_eq!(result.tables[0].name.as_deref(), Some("users"));
    }

    #[test]
    fn test_parse_liquibase_formatted_sql() {
        let importer = SQLImporter::new("postgres");
        let sql = r#"
            --liquibase formatted sql
            --changeset user:1
            CREATE TABLE test (id INT PRIMARY KEY);
        "#;
        let result = importer.parse_liquibase(sql).unwrap();

        assert_eq!(result.tables.len(), 1);
        assert_eq!(result.tables[0].name.as_deref(), Some("test"));
    }

    #[test]
    fn test_parse_invalid_sql() {
        let importer = SQLImporter::new("postgres");
        let sql = "CREATE TABL users (id INT);"; // Typo: TABL instead of TABLE
        let result = importer.parse(sql).unwrap();

        // Should return errors rather than panic
        assert!(!result.errors.is_empty() || result.tables.is_empty());
    }
}

mod json_schema_import_tests {
    use super::*;

    #[test]
    fn test_parse_simple_schema() {
        let importer = JSONSchemaImporter::new();
        let schema = r#"
        {
            "title": "User",
            "type": "object",
            "properties": {
                "id": { "type": "integer" },
                "name": { "type": "string" }
            },
            "required": ["id"]
        }
        "#;
        let result = importer.import(schema).unwrap();

        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 1);
        assert_eq!(result.tables[0].name.as_deref(), Some("User"));
        assert_eq!(result.tables[0].columns.len(), 2);
    }

    #[test]
    fn test_parse_schema_with_definitions() {
        let importer = JSONSchemaImporter::new();
        let schema = r#"
        {
            "definitions": {
                "User": {
                    "type": "object",
                    "properties": {
                        "id": { "type": "integer" }
                    }
                },
                "Order": {
                    "type": "object",
                    "properties": {
                        "id": { "type": "integer" }
                    }
                }
            }
        }
        "#;
        let result = importer.import(schema).unwrap();

        assert_eq!(result.tables.len(), 2);
    }

    #[test]
    fn test_parse_nested_object() {
        let importer = JSONSchemaImporter::new();
        let schema = r#"
        {
            "title": "Person",
            "type": "object",
            "properties": {
                "name": { "type": "string" },
                "address": {
                    "type": "object",
                    "properties": {
                        "street": { "type": "string" },
                        "city": { "type": "string" }
                    }
                }
            }
        }
        "#;
        let result = importer.import(schema).unwrap();

        let table = &result.tables[0];
        // Should have name, address.street, address.city
        assert!(table.columns.len() >= 3);
        assert!(table.columns.iter().any(|c| c.name == "name"));
        assert!(table.columns.iter().any(|c| c.name.contains("address")));
    }

    #[test]
    fn test_parse_array_type() {
        let importer = JSONSchemaImporter::new();
        let schema = r#"
        {
            "title": "Container",
            "type": "object",
            "properties": {
                "items": {
                    "type": "array",
                    "items": { "type": "string" }
                }
            }
        }
        "#;
        let result = importer.import(schema).unwrap();

        let table = &result.tables[0];
        let items_col = table.columns.iter().find(|c| c.name == "items").unwrap();
        assert!(items_col.data_type.contains("ARRAY"));
    }
}

mod avro_import_tests {
    use super::*;

    #[test]
    fn test_parse_simple_record() {
        let importer = AvroImporter::new();
        let schema = r#"
        {
            "type": "record",
            "name": "User",
            "fields": [
                { "name": "id", "type": "long" },
                { "name": "name", "type": "string" }
            ]
        }
        "#;
        let result = importer.import(schema).unwrap();

        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 1);
        assert_eq!(result.tables[0].name.as_deref(), Some("User"));
        assert_eq!(result.tables[0].columns.len(), 2);
    }

    #[test]
    fn test_parse_nullable_field() {
        let importer = AvroImporter::new();
        let schema = r#"
        {
            "type": "record",
            "name": "User",
            "fields": [
                { "name": "nickname", "type": ["null", "string"] }
            ]
        }
        "#;
        let result = importer.import(schema).unwrap();

        let nickname_col = &result.tables[0].columns[0];
        assert!(nickname_col.nullable);
    }

    #[test]
    fn test_parse_multiple_records() {
        let importer = AvroImporter::new();
        let schema = r#"
        [
            {
                "type": "record",
                "name": "User",
                "fields": [{ "name": "id", "type": "long" }]
            },
            {
                "type": "record",
                "name": "Order",
                "fields": [{ "name": "id", "type": "long" }]
            }
        ]
        "#;
        let result = importer.import(schema).unwrap();

        assert_eq!(result.tables.len(), 2);
    }

    #[test]
    fn test_parse_with_namespace() {
        let importer = AvroImporter::new();
        let schema = r#"
        {
            "type": "record",
            "namespace": "com.example",
            "name": "User",
            "fields": [{ "name": "id", "type": "long" }]
        }
        "#;
        let result = importer.import(schema).unwrap();

        assert_eq!(result.tables[0].name.as_deref(), Some("User"));
    }
}

mod protobuf_import_tests {
    use super::*;

    #[test]
    fn test_parse_simple_message() {
        let importer = ProtobufImporter::new();
        let proto = r#"
            syntax = "proto3";

            message User {
                int64 id = 1;
                string name = 2;
            }
        "#;
        let result = importer.import(proto).unwrap();

        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 1);
        assert_eq!(result.tables[0].name.as_deref(), Some("User"));
        assert_eq!(result.tables[0].columns.len(), 2);
    }

    #[test]
    fn test_parse_multiple_messages() {
        let importer = ProtobufImporter::new();
        let proto = r#"
            syntax = "proto3";

            message User {
                int64 id = 1;
            }

            message Order {
                int64 id = 1;
            }
        "#;
        let result = importer.import(proto).unwrap();

        assert_eq!(result.tables.len(), 2);
    }

    #[test]
    fn test_parse_optional_fields() {
        let importer = ProtobufImporter::new();
        let proto = r#"
            syntax = "proto3";

            message User {
                optional string nickname = 1;
            }
        "#;
        let result = importer.import(proto).unwrap();

        let nickname_col = &result.tables[0].columns[0];
        assert!(nickname_col.nullable);
    }

    #[test]
    fn test_parse_repeated_fields() {
        let importer = ProtobufImporter::new();
        let proto = r#"
            syntax = "proto3";

            message Container {
                repeated string items = 1;
            }
        "#;
        let result = importer.import(proto).unwrap();

        let items_col = &result.tables[0].columns[0];
        // Repeated fields should be marked as nullable
        assert!(items_col.nullable);
    }
}

// DataFlow import tests removed - DataFlow format has been migrated to Domain schema
// Use migrate_dataflow_to_domain() for DataFlow → Domain migration

mod odcl_field_preservation_tests {
    use super::*;
    use std::fs;
    use std::path::PathBuf;

    fn get_test_fixture_path(filename: &str) -> PathBuf {
        let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        path.push("specs");
        path.push("003-odcs-field-preservation");
        path.push("test-fixtures");
        path.push(filename);
        path
    }

    #[test]
    fn test_odcl_import_preserves_description_field() {
        let mut importer = ODCSImporter::new();
        let yaml = r#"
dataContractSpecification: 1.2.1
id: test-contract
info:
  title: Test Contract
  version: 1.0.0
models:
  test_table:
    type: table
    fields:
      test_column:
        description: This is a test column description
        type: text
        required: true
"#;
        let result = importer.import(yaml).unwrap();

        assert_eq!(result.tables.len(), 1);
        let table = &result.tables[0];
        assert_eq!(table.columns.len(), 1);

        let column = &table.columns[0];
        assert_eq!(column.name, "test_column");
        assert_eq!(
            column.description,
            Some("This is a test column description".to_string())
        );
    }

    #[test]
    fn test_odcl_import_preserves_quality_array_with_nested_structures() {
        let mut importer = ODCSImporter::new();
        let yaml = r#"
dataContractSpecification: 1.2.1
id: test-contract
info:
  title: Test Contract
  version: 1.0.0
models:
  test_table:
    type: table
    fields:
      test_column:
        type: long
        required: true
        quality:
          - type: sql
            description: 95% of all values are expected to be between 10 and 499
            query: |
              SELECT quantile_cont(test_column, 0.95) AS percentile_95
              FROM test_table
            mustBeBetween: [10, 499]
"#;
        let result = importer.import(yaml).unwrap();

        assert_eq!(result.tables.len(), 1);
        let table = &result.tables[0];

        // Find the test_column (there might be additional columns created from quality rules)
        let column = table
            .columns
            .iter()
            .find(|c| c.name == "test_column")
            .expect("Should find test_column");

        // Verify quality array is preserved
        // Note: When required=true, a not_null quality rule may be added automatically
        assert!(column.quality.is_some());
        let quality = column.quality.as_ref().unwrap();
        assert!(
            !quality.is_empty(),
            "Quality array should have at least 1 rule"
        );

        // Find the SQL quality rule (there may be a not_null rule added automatically)
        let quality_rule = quality
            .iter()
            .find(|r| r.get("type").and_then(|v| v.as_str()) == Some("sql"))
            .expect("Should find SQL quality rule");
        assert_eq!(
            quality_rule.get("type").and_then(|v| v.as_str()),
            Some("sql")
        );
        assert_eq!(
            quality_rule.get("description").and_then(|v| v.as_str()),
            Some("95% of all values are expected to be between 10 and 499")
        );
        assert!(quality_rule.get("query").is_some());
        assert!(quality_rule.get("mustBeBetween").is_some());

        // Verify nested array structure
        if let Some(must_be_between) = quality_rule.get("mustBeBetween") {
            if let Some(arr) = must_be_between.as_array() {
                assert_eq!(arr.len(), 2);
                assert_eq!(arr[0].as_i64(), Some(10));
                assert_eq!(arr[1].as_i64(), Some(499));
            } else {
                panic!("mustBeBetween should be an array");
            }
        } else {
            panic!("mustBeBetween should be present");
        }
    }

    #[test]
    fn test_odcl_import_preserves_ref_references() {
        let mut importer = ODCSImporter::new();
        let yaml = r#"
dataContractSpecification: 1.2.1
id: test-contract
info:
  title: Test Contract
  version: 1.0.0
models:
  test_table:
    type: table
    fields:
      order_id:
        $ref: '#/definitions/order_id'
        type: text
        required: true
definitions:
  order_id:
    type: text
    format: uuid
    description: An internal ID that identifies an order
"#;
        let result = importer.import(yaml).unwrap();

        assert_eq!(result.tables.len(), 1);
        let table = &result.tables[0];
        assert_eq!(table.columns.len(), 1);

        let column = &table.columns[0];
        assert_eq!(column.name, "order_id");
        // ref_path is now stored as relationships
        assert!(
            !column.relationships.is_empty(),
            "Column should have relationships from $ref"
        );
    }

    #[test]
    fn test_odcl_import_preserves_all_three_field_types_together() {
        let mut importer = ODCSImporter::new();
        let yaml = r#"
dataContractSpecification: 1.2.1
id: test-contract
info:
  title: Test Contract
  version: 1.0.0
models:
  test_table:
    type: table
    fields:
      complete_column:
        $ref: '#/definitions/order_id'
        description: This column has all three field types
        type: text
        required: true
        quality:
          - type: sql
            description: Validation rule
            query: SELECT COUNT(*) FROM test_table
            mustBeGreaterThan: 0
definitions:
  order_id:
    type: text
    format: uuid
    description: An internal ID
"#;
        let result = importer.import(yaml).unwrap();

        assert_eq!(result.tables.len(), 1);
        let table = &result.tables[0];

        // Find the complete_column (there might be additional columns created from quality rules)
        let column = table
            .columns
            .iter()
            .find(|c| c.name == "complete_column")
            .expect("Should find complete_column");

        // Verify description is preserved
        assert_eq!(
            column.description,
            Some("This column has all three field types".to_string())
        );

        // Verify $ref is preserved (now as relationships)
        assert!(
            !column.relationships.is_empty(),
            "Column should have relationships from $ref"
        );

        // Verify quality array is preserved with nested structures
        // Note: When required=true, a not_null quality rule may be added automatically
        assert!(column.quality.is_some());
        let quality = column.quality.as_ref().unwrap();
        assert!(
            !quality.is_empty(),
            "Quality array should have at least 1 rule"
        );

        // Find the SQL quality rule (there may be a not_null rule added automatically)
        let quality_rule = quality
            .iter()
            .find(|r| r.get("type").and_then(|v| v.as_str()) == Some("sql"))
            .expect("Should find SQL quality rule");
        assert_eq!(
            quality_rule.get("type").and_then(|v| v.as_str()),
            Some("sql")
        );
        assert_eq!(
            quality_rule.get("description").and_then(|v| v.as_str()),
            Some("Validation rule")
        );
        assert!(quality_rule.get("query").is_some());
        assert!(quality_rule.get("mustBeGreaterThan").is_some());
    }

    #[test]
    fn test_odcl_import_from_fixture_file() {
        let fixture_path = get_test_fixture_path("example.odcl.yaml");
        let yaml_content = fs::read_to_string(&fixture_path)
            .unwrap_or_else(|_| panic!("Failed to read fixture file: {:?}", fixture_path));

        let mut importer = ODCSImporter::new();
        let result = importer.import(&yaml_content).unwrap();

        // Verify we got tables
        assert!(!result.tables.is_empty());

        // The fixture has multiple models (orders, line_items). The ODCL importer currently
        // only returns the first model alphabetically. We search across all returned tables.

        // Verify description is preserved (find any column with description across all tables)
        let desc_column = result
            .tables
            .iter()
            .flat_map(|t| t.columns.iter())
            .find(|c| c.description.is_some() && !c.description.as_ref().unwrap().is_empty())
            .expect("Should find column with description");
        assert!(desc_column.description.is_some());

        // Verify quality array is preserved (find any column with quality rules across all tables)
        let quality_column = result
            .tables
            .iter()
            .flat_map(|t| t.columns.iter())
            .find(|c| c.quality.is_some() && !c.quality.as_ref().unwrap().is_empty())
            .expect("Should find column with quality");
        assert!(quality_column.quality.is_some());
        let quality = quality_column.quality.as_ref().unwrap();
        assert!(!quality.is_empty());

        // Verify $ref is preserved as relationships (find any column with relationships across all tables)
        let ref_column = result
            .tables
            .iter()
            .flat_map(|t| t.columns.iter())
            .find(|c| !c.relationships.is_empty())
            .expect("Should find column with relationships from $ref");
        assert!(!ref_column.relationships.is_empty());
        assert!(
            ref_column.relationships[0].to.starts_with("definitions/"),
            "Relationship 'to' should reference definitions"
        );
    }
}

mod databricks_sql_tests {
    use super::*;

    #[test]
    fn test_databricks_identifier_basic() {
        let importer = SQLImporter::new("databricks");
        // Note: USING DELTA is not supported by sqlparser, so we test without it
        // The IDENTIFIER() preprocessing is what we're testing here
        let sql = "CREATE TABLE IDENTIFIER(:catalog || '.schema.example_table') (id STRING COMMENT 'Unique identifier', name STRING COMMENT 'Name of the record');";
        let result = importer.parse(sql).unwrap();

        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 1);
        assert_eq!(
            result.tables[0].name.as_deref(),
            Some("schema.example_table")
        );
        assert_eq!(result.tables[0].columns.len(), 2);
    }

    #[test]
    fn test_databricks_variables_in_types() {
        let importer = SQLImporter::new("databricks");
        let sql = r#"
            CREATE TABLE example (
                id STRING,
                metadata STRUCT<key: STRING, value: :value_type, timestamp: TIMESTAMP>,
                items ARRAY<:element_type>,
                nested ARRAY<STRUCT<field1: :nested_type, field2: STRING>>
            );
        "#;
        let result = importer.parse(sql).unwrap();

        if !result.errors.is_empty() {
            eprintln!("Parse errors: {:?}", result.errors);
        }
        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 1);
        assert_eq!(result.tables[0].columns.len(), 4);

        // Check that variables were replaced with STRING
        assert!(
            result.tables[0].columns[1]
                .data_type
                .contains("value: STRING")
        );
        // ARRAY<:element_type> becomes ARRAY<STRING> after variable replacement
        assert!(result.tables[0].columns[2].data_type.contains("ARRAY"));
        // Nested ARRAY<STRUCT<...>> should have variables replaced
        assert!(
            result.tables[0].columns[3]
                .data_type
                .contains("field1: STRING")
        );
    }

    #[test]
    fn test_databricks_metadata_variables() {
        let importer = SQLImporter::new("databricks");
        // Test COMMENT with variable - sqlparser supports this
        let sql =
            "CREATE TABLE example (id STRING, name STRING) COMMENT ':table_comment_variable';";
        let result = importer.parse(sql).unwrap();

        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 1);
        assert_eq!(result.tables[0].columns.len(), 2);

        // Note: TBLPROPERTIES is not supported by sqlparser, so we test COMMENT separately
        // TBLPROPERTIES would require preprocessing to remove before parsing
    }

    #[test]
    fn test_databricks_column_variables() {
        let importer = SQLImporter::new("databricks");
        let sql =
            "CREATE TABLE example (id :id_var STRING, name :name_var STRING, age :age_var INT);";
        let result = importer.parse(sql).unwrap();

        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 1);
        assert_eq!(result.tables[0].columns.len(), 3);
        assert_eq!(result.tables[0].columns[0].name, "id");
        assert_eq!(result.tables[0].columns[0].data_type, "STRING");
        assert_eq!(result.tables[0].columns[1].name, "name");
        assert_eq!(result.tables[0].columns[1].data_type, "STRING");
        assert_eq!(result.tables[0].columns[2].name, "age");
        assert_eq!(result.tables[0].columns[2].data_type, "INT");
    }

    #[test]
    fn test_databricks_views_and_tables() {
        let importer = SQLImporter::new("databricks");
        let sql = r#"
            CREATE TABLE table1 (id STRING, name STRING);
            CREATE VIEW view1 AS SELECT id, name FROM table1;
            CREATE TABLE table2 (value INT);
        "#;
        let result = importer.parse(sql).unwrap();

        assert!(result.errors.is_empty());
        // Should import both tables and views
        assert_eq!(result.tables.len(), 3);
        assert_eq!(result.tables[0].name.as_deref(), Some("table1"));
        assert_eq!(result.tables[1].name.as_deref(), Some("view1"));
        assert_eq!(result.tables[2].name.as_deref(), Some("table2"));
    }

    #[test]
    fn test_databricks_backward_compatibility() {
        // Verify existing dialects still work
        let postgres_importer = SQLImporter::new("postgres");
        let mysql_importer = SQLImporter::new("mysql");
        let sqlite_importer = SQLImporter::new("sqlite");
        let generic_importer = SQLImporter::new("generic");

        let sql = "CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(100));";

        assert!(postgres_importer.parse(sql).unwrap().errors.is_empty());
        assert!(mysql_importer.parse(sql).unwrap().errors.is_empty());
        assert!(sqlite_importer.parse(sql).unwrap().errors.is_empty());
        assert!(generic_importer.parse(sql).unwrap().errors.is_empty());
    }

    #[test]
    fn test_databricks_full_example() {
        // Full example from GitHub issue #13
        let importer = SQLImporter::new("databricks");
        let sql = r#"
            CREATE TABLE IF NOT EXISTS IDENTIFIER(:catalog_name || '.schema.example_table') (
                id STRING COMMENT 'Unique identifier for each record.',
                name STRING COMMENT 'Name of the record.',
                events ARRAY<STRUCT<
                    id: STRING,
                    name: STRING,
                    details: STRUCT<
                        name: STRING,
                        status: :variable_type,
                        timestamp: TIMESTAMP
                    >
                >>,
                metadata STRUCT<
                    key: STRING,
                    value: :value_type,
                    timestamp: TIMESTAMP
                >,
                items ARRAY<:element_type>
            );
        "#;
        let result = importer.parse(sql).unwrap();

        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 1);
        assert_eq!(
            result.tables[0].name.as_deref(),
            Some("schema.example_table")
        );
        assert!(result.tables[0].columns.len() >= 5);
    }

    #[test]
    fn test_databricks_mixed_sql() {
        // Test Databricks SQL mixed with standard SQL
        let importer = SQLImporter::new("databricks");
        let sql = r#"
            CREATE TABLE standard_table (id INT, name VARCHAR(100));
            CREATE TABLE IDENTIFIER(:catalog || '.schema.databricks_table') (id STRING, metadata STRUCT<key: STRING, value: :value_type>);
            CREATE VIEW standard_view AS SELECT * FROM standard_table;
        "#;
        let result = importer.parse(sql).unwrap();

        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 3);
    }

    #[test]
    fn test_databricks_multiline_comment_clauses() {
        // Test case based on issue #15 - multiline COMMENT clauses
        // Representative SQL without "bets" or "flutter" references
        let importer = SQLImporter::new("databricks");
        let sql = r#"
CREATE TABLE IF NOT EXISTS IDENTIFIER(:catalog_name || '.analytics.user_events') (
  event_id STRING COMMENT 'Unique identifier for each event.',
  event_type STRING COMMENT 'The type of event that occurred. This is a finite list which can be found at the bottom of this contract, under the quality section.',
  event_metadata ARRAY<STRUCT<
    id: STRING,
    name: STRING,
    priority: INT,
    category: STRING,
    source: STRING,
    event_details: STRUCT<
      name: STRING,
      field: STRING,
      timestamp: TIMESTAMP
    >
  >> COMMENT 'details associated with the events that have been triggered.',
  highest_priority INT COMMENT 'If there are multiple events that are completed at once, this value highlights the highest priority from the group of events.',
  created_at TIMESTAMP,
  updated_at TIMESTAMP
)
COMMENT 'User events table for analytics processing'
TBLPROPERTIES (
  'delta.autoOptimize.optimizeWrite' = 'true',
  'delta.autoOptimize.autoCompact' = 'true'
);
        "#;
        let result = importer.parse(sql);

        // Should parse successfully despite multiline COMMENT clauses
        // TBLPROPERTIES is removed during preprocessing, so parsing should succeed
        assert!(
            result.is_ok(),
            "Failed to parse SQL with multiline COMMENT clauses: {:?}",
            result.err()
        );
        let result = result.unwrap();
        assert!(
            result.errors.is_empty(),
            "Parse errors: {:?}",
            result.errors
        );
        assert_eq!(result.tables.len(), 1);
        assert_eq!(
            result.tables[0].name.as_deref(),
            Some("analytics.user_events")
        );
        assert!(result.tables[0].columns.len() >= 5);

        // Verify COMMENT clauses are extracted
        let event_id_col = result.tables[0]
            .columns
            .iter()
            .find(|c| c.name == "event_id")
            .expect("event_id column should exist");
        assert_eq!(
            event_id_col.description.as_deref(),
            Some("Unique identifier for each event.")
        );

        let highest_priority_col = result.tables[0]
            .columns
            .iter()
            .find(|c| c.name == "highest_priority")
            .expect("highest_priority column should exist");
        assert!(
            highest_priority_col
                .description
                .as_deref()
                .unwrap()
                .contains("highest priority")
        );
    }

    #[test]
    fn test_databricks_escaped_quotes_in_comments() {
        // Test escaped quotes in COMMENT clauses (e.g., customer\'s, aren\'t)
        let importer = SQLImporter::new("databricks");
        let sql = r#"
CREATE TABLE test (
  id STRING COMMENT 'Unique identifier.',
  name STRING COMMENT 'Annotations are values that users link to customer IDs to provide any additional information about the customer\'s profile.',
  description STRING COMMENT 'The time at which the record was updated by the UI, by either completing the task, dismissing the item or by the item hitting the system\'s expiry time.',
  metadata STRING COMMENT 'Extended metadata gives additional information, and will be populated whenever there\'s an event based off of a transaction or group of transactions. These fields aren\'t'
);
        "#;
        let result = importer.parse(sql).unwrap();

        assert!(result.errors.is_empty());
        assert_eq!(result.tables.len(), 1);
        assert_eq!(result.tables[0].columns.len(), 4);

        // Verify escaped quotes are handled correctly
        let name_col = result.tables[0]
            .columns
            .iter()
            .find(|c| c.name == "name")
            .expect("name column should exist");
        assert!(
            name_col
                .description
                .as_deref()
                .unwrap()
                .contains("customer's")
        );

        let desc_col = result.tables[0]
            .columns
            .iter()
            .find(|c| c.name == "description")
            .expect("description column should exist");
        assert!(
            desc_col
                .description
                .as_deref()
                .unwrap()
                .contains("system's")
        );

        let meta_col = result.tables[0]
            .columns
            .iter()
            .find(|c| c.name == "metadata")
            .expect("metadata column should exist");
        assert!(meta_col.description.as_deref().unwrap().contains("there's"));
        assert!(meta_col.description.as_deref().unwrap().contains("aren't"));
    }
}