hedl-csv 2.0.0

HEDL to/from CSV conversion
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
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Schema inference tests for CSV parsing.
//!
//! These tests verify that automatic type inference from CSV headers
//! correctly identifies column types based on sampled data.

use hedl_core::Value;
use hedl_csv::{from_csv_with_config, FromCsvConfig};

// ==================== Basic Type Inference Tests ====================

#[test]
fn test_infer_all_integers() {
    let csv_data = r"id,age,count
1,30,100
2,25,200
3,35,150
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Record", &["age", "count"], config).unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    // All three columns should be inferred as Int
    assert_eq!(list.rows.len(), 3);

    // First row
    assert_eq!(list.rows[0].fields[0], Value::Int(1)); // id
    assert_eq!(list.rows[0].fields[1], Value::Int(30)); // age
    assert_eq!(list.rows[0].fields[2], Value::Int(100)); // count

    // Second row
    assert_eq!(list.rows[1].fields[0], Value::Int(2));
    assert_eq!(list.rows[1].fields[1], Value::Int(25));
    assert_eq!(list.rows[1].fields[2], Value::Int(200));
}

#[test]
fn test_infer_all_floats() {
    let csv_data = r"id,price,score
1,99.99,87.5
2,149.50,92.3
3,75.25,88.1
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Product", &["price", "score"], config).unwrap();
    let list = doc.get("products").unwrap().as_list().unwrap();

    assert_eq!(list.rows.len(), 3);

    // First row - id is int, price and score are floats
    assert_eq!(list.rows[0].fields[0], Value::Int(1)); // id
    assert_eq!(list.rows[0].fields[1], Value::Float(99.99)); // price
    assert_eq!(list.rows[0].fields[2], Value::Float(87.5)); // score
}

#[test]
fn test_infer_all_booleans() {
    let csv_data = r"id,active,verified
1,true,false
2,false,true
3,true,true
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "User", &["active", "verified"], config).unwrap();
    let list = doc.get("users").unwrap().as_list().unwrap();

    assert_eq!(list.rows.len(), 3);

    // First row
    assert_eq!(list.rows[0].fields[0], Value::Int(1)); // id
    assert_eq!(list.rows[0].fields[1], Value::Bool(true)); // active
    assert_eq!(list.rows[0].fields[2], Value::Bool(false)); // verified

    // Second row
    assert_eq!(list.rows[1].fields[0], Value::Int(2));
    assert_eq!(list.rows[1].fields[1], Value::Bool(false));
    assert_eq!(list.rows[1].fields[2], Value::Bool(true));
}

#[test]
fn test_infer_all_strings() {
    let csv_data = r"id,name,email
1,Alice,alice@example.com
2,Bob,bob@example.com
3,Charlie,charlie@example.com
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "User", &["name", "email"], config).unwrap();
    let list = doc.get("users").unwrap().as_list().unwrap();

    assert_eq!(list.rows.len(), 3);

    // First row
    assert_eq!(list.rows[0].fields[0], Value::Int(1)); // id (numeric)
    assert_eq!(
        list.rows[0].fields[1],
        Value::String("Alice".to_string().into())
    );
    assert_eq!(
        list.rows[0].fields[2],
        Value::String("alice@example.com".to_string().into())
    );
}

#[test]
fn test_infer_mixed_types() {
    let csv_data = r"id,name,age,score,active
1,Alice,30,95.5,true
2,Bob,25,87.3,false
3,Charlie,35,92.1,true
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(
        csv_data,
        "Person",
        &["name", "age", "score", "active"],
        config,
    )
    .unwrap();
    let list = doc.get("persons").unwrap().as_list().unwrap();

    assert_eq!(list.rows.len(), 3);

    // First row - verify each type is correctly inferred
    assert_eq!(list.rows[0].fields[0], Value::Int(1)); // id: Int
    assert_eq!(
        list.rows[0].fields[1],
        Value::String("Alice".to_string().into())
    ); // name: String
    assert_eq!(list.rows[0].fields[2], Value::Int(30)); // age: Int
    assert_eq!(list.rows[0].fields[3], Value::Float(95.5)); // score: Float
    assert_eq!(list.rows[0].fields[4], Value::Bool(true)); // active: Bool
}

// ==================== Null Handling Tests ====================

#[test]
fn test_inference_with_nulls() {
    let csv_data = r"id,optional_age,optional_name
1,30,Alice
2,,Bob
3,25,
4,,
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(
        csv_data,
        "Record",
        &["optional_age", "optional_name"],
        config,
    )
    .unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    assert_eq!(list.rows.len(), 4);

    // Row 1: has values
    assert_eq!(list.rows[0].fields[1], Value::Int(30));
    assert_eq!(
        list.rows[0].fields[2],
        Value::String("Alice".to_string().into())
    );

    // Row 2: missing age
    assert_eq!(list.rows[1].fields[1], Value::Null);
    assert_eq!(
        list.rows[1].fields[2],
        Value::String("Bob".to_string().into())
    );

    // Row 3: missing name
    assert_eq!(list.rows[2].fields[1], Value::Int(25));
    assert_eq!(list.rows[2].fields[2], Value::Null);

    // Row 4: all optional fields missing
    assert_eq!(list.rows[3].fields[1], Value::Null);
    assert_eq!(list.rows[3].fields[2], Value::Null);
}

#[test]
fn test_inference_all_nulls_column() {
    let csv_data = r"id,always_null,value
1,,100
2,,200
3,,300
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Record", &["always_null", "value"], config).unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    assert_eq!(list.rows.len(), 3);

    // Column with all nulls should still be Null type
    assert_eq!(list.rows[0].fields[1], Value::Null);
    assert_eq!(list.rows[1].fields[1], Value::Null);
    assert_eq!(list.rows[2].fields[1], Value::Null);

    // Value column should be Int
    assert_eq!(list.rows[0].fields[2], Value::Int(100));
}

// ==================== Edge Cases ====================

#[test]
fn test_inference_integers_vs_floats() {
    // Mix of integers and floats should infer as Float
    let csv_data = r"id,value
1,10
2,20.5
3,30
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Record", &["value"], config).unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    // Should infer as Float because not all values are integers
    assert_eq!(list.rows[0].fields[1], Value::Float(10.0));
    assert_eq!(list.rows[1].fields[1], Value::Float(20.5));
    assert_eq!(list.rows[2].fields[1], Value::Float(30.0));
}

#[test]
fn test_inference_string_with_number_looking_values() {
    // Column with mix of numbers and non-numbers should be String
    let csv_data = r"id,code
1,123
2,ABC
3,456
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Item", &["code"], config).unwrap();
    let list = doc.get("items").unwrap().as_list().unwrap();

    // Column inferred as String, but per-value parsing still happens for
    // values that look like numbers (uses parse_csv_value which does int inference)
    assert_eq!(list.rows[0].fields[1], Value::Int(123)); // Parses as int
    assert_eq!(
        list.rows[1].fields[1],
        Value::String("ABC".to_string().into())
    );
    assert_eq!(list.rows[2].fields[1], Value::Int(456)); // Parses as int
}

#[test]
fn test_inference_bool_with_mixed_values() {
    // Column with true/false and other values should be String
    let csv_data = r"id,flag
1,true
2,maybe
3,false
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Record", &["flag"], config).unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    // Column inferred as String, but per-value parsing still happens
    // (parse_csv_value converts "true"/"false" to bools)
    assert_eq!(list.rows[0].fields[1], Value::Bool(true));
    assert_eq!(
        list.rows[1].fields[1],
        Value::String("maybe".to_string().into())
    );
    assert_eq!(list.rows[2].fields[1], Value::Bool(false));
}

// ==================== Sample Size Tests ====================

#[test]
fn test_inference_small_sample_size() {
    let csv_data = r"id,value
1,10
2,20
3,30
4,not_a_number
5,50
";

    // Sample only first 3 rows - should infer as Int
    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 3,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Record", &["value"], config).unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    // First 3 rows parse as Int based on inference
    assert_eq!(list.rows[0].fields[1], Value::Int(10));
    assert_eq!(list.rows[1].fields[1], Value::Int(20));
    assert_eq!(list.rows[2].fields[1], Value::Int(30));

    // Row 4 has non-integer but still uses inferred Int type, falls back to String
    assert_eq!(
        list.rows[3].fields[1],
        Value::String("not_a_number".to_string().into())
    );

    // Row 5 is Int
    assert_eq!(list.rows[4].fields[1], Value::Int(50));
}

#[test]
fn test_inference_large_sample_size() {
    let csv_data = r"id,value
1,10
2,20
3,30
4,not_a_number
5,50
";

    // Sample all rows - should infer as String
    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Record", &["value"], config).unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    // Column inferred as String (due to "not_a_number"), but values that look
    // like numbers still parse as numbers via parse_csv_value
    assert_eq!(list.rows[0].fields[1], Value::Int(10));
    assert_eq!(list.rows[1].fields[1], Value::Int(20));
    assert_eq!(list.rows[2].fields[1], Value::Int(30));
    assert_eq!(
        list.rows[3].fields[1],
        Value::String("not_a_number".to_string().into())
    );
    assert_eq!(list.rows[4].fields[1], Value::Int(50));
}

// ==================== Comparison with Non-Inference ====================

#[test]
fn test_schema_inference_vs_no_inference() {
    let csv_data = r"id,age,name
1,30,Alice
2,25,Bob
";

    // Without inference
    let config_no_infer = FromCsvConfig {
        infer_schema: false,
        ..Default::default()
    };

    let doc_no_infer =
        from_csv_with_config(csv_data, "Person", &["age", "name"], config_no_infer).unwrap();
    let list_no_infer = doc_no_infer.get("persons").unwrap().as_list().unwrap();

    // With inference
    let config_infer = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc_infer =
        from_csv_with_config(csv_data, "Person", &["age", "name"], config_infer).unwrap();
    let list_infer = doc_infer.get("persons").unwrap().as_list().unwrap();

    // Both should have same structure
    assert_eq!(list_no_infer.rows.len(), list_infer.rows.len());

    // Without inference: per-value inference happens
    // With inference: column-wide inference happens
    // Both should produce Int for age in this case
    assert!(matches!(list_no_infer.rows[0].fields[1], Value::Int(30)));
    assert!(matches!(list_infer.rows[0].fields[1], Value::Int(30)));
}

// ==================== Complex Scenarios ====================

#[test]
fn test_inference_with_scientific_notation() {
    let csv_data = r"id,measurement
1,1.5e10
2,2.3e-5
3,1000000
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Data", &["measurement"], config).unwrap();
    let list = doc.get("datas").unwrap().as_list().unwrap();

    // Should infer as Float
    assert!(matches!(list.rows[0].fields[1], Value::Float(_)));
    assert!(matches!(list.rows[1].fields[1], Value::Float(_)));
    // Last one is also Float because column is inferred as Float
    assert_eq!(list.rows[2].fields[1], Value::Float(1000000.0));
}

#[test]
fn test_inference_empty_csv() {
    let csv_data = "id,value\n";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Record", &["value"], config).unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    assert_eq!(list.rows.len(), 0);
}

#[test]
fn test_inference_single_row() {
    let csv_data = r"id,value
1,42
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Record", &["value"], config).unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    assert_eq!(list.rows.len(), 1);
    assert_eq!(list.rows[0].fields[1], Value::Int(42));
}

// ==================== Whitespace Handling ====================

#[test]
fn test_inference_with_whitespace() {
    let csv_data = r"id,value
1,  42
2,  100
3,  75
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        trim: true, // Trimming enabled
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Record", &["value"], config).unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    // Should trim and infer as Int
    assert_eq!(list.rows[0].fields[1], Value::Int(42));
    assert_eq!(list.rows[1].fields[1], Value::Int(100));
    assert_eq!(list.rows[2].fields[1], Value::Int(75));
}

// ==================== Delimiter Tests ====================

#[test]
fn test_inference_with_tab_delimiter() {
    let csv_data = "id\tage\tactive\n1\t30\ttrue\n2\t25\tfalse\n";

    let config = FromCsvConfig {
        delimiter: b'\t',
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "User", &["age", "active"], config).unwrap();
    let list = doc.get("users").unwrap().as_list().unwrap();

    assert_eq!(list.rows.len(), 2);
    assert_eq!(list.rows[0].fields[1], Value::Int(30));
    assert_eq!(list.rows[0].fields[2], Value::Bool(true));
}

// ==================== Fallback Behavior ====================

#[test]
fn test_inference_fallback_to_string() {
    // When type inference fails during parsing, should fallback to string
    let csv_data = r"id,value
1,10
2,20
3,30
";

    let config = FromCsvConfig {
        infer_schema: true,
        sample_rows: 100,
        ..Default::default()
    };

    let doc = from_csv_with_config(csv_data, "Record", &["value"], config).unwrap();
    let list = doc.get("records").unwrap().as_list().unwrap();

    // All values should be Int
    assert_eq!(list.rows[0].fields[1], Value::Int(10));
    assert_eq!(list.rows[1].fields[1], Value::Int(20));
    assert_eq!(list.rows[2].fields[1], Value::Int(30));
}