sqlload 0.67.0

SQL data loading tooling.
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
960
961
962
963
964
965
966
967
968
969
use crate::files_source::constants::LOADER_COLUMNS;
use crate::files_source::main::constant_columns::constant_columns;
use crate::files_source::main::path_template::path_template;
use crate::files_source::main::text_charset::text_charset;
use crate::files_source::models::{
    AfterLoad, Compression, CsvQuote, FilePattern, FilesOrder, OnEmpty, RecordShape, SourceFormat,
};
use crate::load_naming::constants::SQLLOAD_PREFIX;
use crate::project::_helpers::targets::{
    destination_kind_problems, is_files_target, stage_problems, target_problems,
};
use crate::project::_helpers::tombstones::tombstone_problems;
use crate::project::constants::{
    FILES_CONNECTION_KINDS, MAX_BATCH_CONCURRENCY, MAX_POSTGRES_NAME_BYTES, MIN_BATCH_INTERVAL,
    STAGE_NAME_PARTS, TABLE_PLACEHOLDER,
};
use crate::project::models::{
    CdcMode, ConnectionConfig, ConstantColumns, Deletes, ProjectConfig, RenameSetting,
    SourceConfig, SourceKind, TableSettings, WriteStrategy,
};
use crate::sheets_source::main::a1_range::a1_range;
use crate::store_paths::main::resolve_location::resolve_location;
use crate::store_paths::models::{Location, StoreKind};

/// Problems with names one section uses to refer to another.
pub(crate) fn reference_problems(project: &ProjectConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    if let Some(default_target) = &project.default_target
        && !project.targets.contains_key(default_target)
    {
        problems.push(format!(
            "default_target {default_target:?} is not a [targets] entry"
        ));
    }
    for (name, target) in &project.targets {
        problems.extend(target_problems(project, name, target));
    }
    for (name, connection) in &project.connections {
        let targeted = project
            .targets
            .values()
            .any(|target| target.connection == *name);
        problems.extend(stage_problems(name, connection, targeted));
        problems.extend(odbc_connection_problems(name, connection));
        if let ConnectionConfig::S3 {
            profile: Some(profile),
            ..
        } = connection
            && profile.is_empty()
        {
            problems.push(format!(
                "[connections.{name}] profile is empty; omit it to use AWS_PROFILE, else default"
            ));
        }
    }
    for (name, source) in &project.sources {
        problems.extend(source_problems(project, name, source));
    }
    problems
}

/// Why an `odbc` connection cannot connect: it needs exactly one of `connection_string` and `dsn`; `user` and `password` go with `dsn`; and `dsn` and `user` are written unbraced, as driver managers read them, so they cannot hold `;`, `{` or `}`.
fn odbc_connection_problems(name: &str, connection: &ConnectionConfig) -> Vec<String> {
    let ConnectionConfig::Odbc {
        connection_string,
        dsn,
        user,
        password,
    } = connection
    else {
        return Vec::new();
    };
    let place = format!("[connections.{name}]");
    let mut problems: Vec<String> = Vec::new();
    match (connection_string.is_some(), dsn.is_some()) {
        (false, false) => problems.push(format!("{place} needs connection_string, or dsn")),
        (true, true) => problems.push(format!(
            "{place} takes connection_string or dsn, not both"
        )),
        (true, false) if user.is_some() || password.is_some() => problems.push(format!(
            "{place} user and password go with dsn; with connection_string, put them in it (UID=...;PWD=...)"
        )),
        _ => {}
    }
    let unbraced = [dsn.as_deref(), user.as_deref()];
    if unbraced
        .into_iter()
        .flatten()
        .any(|value| value.contains([';', '{', '}']))
    {
        problems.push(format!(
            "{place} dsn and user cannot contain ';', '{{' or '}}'; use connection_string instead"
        ));
    }
    problems
}

fn source_problems(project: &ProjectConfig, name: &str, source: &SourceConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    let place = source_place(project, name);
    problems.extend(after_load_problems(project, (name, &place), source));
    problems.extend(tombstone_problems(&place, source));
    let (kinds, expected): (&[&str], &str) = match source.kind {
        SourceKind::Postgres | SourceKind::PostgresCdc => (&["postgres"], "postgres"),
        SourceKind::Snowflake => (&["snowflake"], "snowflake"),
        SourceKind::Odbc => (&["odbc"], "odbc"),
        SourceKind::BigQuery => (&["bigquery"], "bigquery"),
        SourceKind::Databricks => (&["databricks"], "databricks"),
        SourceKind::Files => (&["s3", "filesystem", "sftp"], "s3, filesystem or sftp"),
        SourceKind::Python => (&[], "any"),
        SourceKind::GoogleSheets => (&["google_sheets"], "google_sheets"),
    };
    match project.connections.get(&source.connection) {
        None if source.connection.is_empty() && source.kind == SourceKind::Python => {}
        None if source.connection.is_empty() => {
            problems.push(format!("{place} needs a connection"));
        }
        None => problems.push(format!(
            "{place} connection {:?} is not a [connections] entry",
            source.connection
        )),
        Some(connection) if source.kind == SourceKind::Python => {
            problems.extend(shared_warehouse_problems(project, name, source, connection));
        }
        Some(connection) if !kinds.contains(&connection.kind()) => {
            problems.push(format!(
                "{place} has kind = {:?} but connection {:?} is a {} connection (expected {expected})",
                source.kind.name(),
                source.connection,
                connection.kind()
            ));
        }
        Some(ConnectionConfig::Sftp { .. }) if source.after_load != AfterLoad::Keep => {
            problems.push(format!(
                "{place} after_load is not for an sftp connection: SQLLoad only reads from SFTP servers, so it cannot delete or move their files"
            ));
        }
        Some(ConnectionConfig::Sftp { .. }) if source.copy_to_internal_stage => {
            problems.push(format!(
                "{place} copy_to_internal_stage is not for an sftp connection: files on an SFTP server always pass through SQLLoad's reader"
            ));
        }
        Some(_) => {}
    }
    let only_target = (project.targets.len() == 1)
        .then(|| project.targets.keys().next())
        .flatten();
    match source
        .destination
        .as_ref()
        .or(project.default_target.as_ref())
        .or(only_target)
    {
        Some(target) if !project.targets.contains_key(target) => {
            problems.push(format!("{place} destination {target:?} is not a [targets] entry"));
        }
        Some(target) => problems.extend(destination_kind_problems(
            &place,
            source,
            is_files_target(project, target),
        )),
        None => problems.push(format!(
            "{place} needs a destination: set destination, or default_target when there are several targets"
        )),
    }
    problems.extend(kind_problems(&place, source));
    problems.extend(transform_problems(project, &place, source));
    problems.extend(rename_problems(project, &place, source));
    problems.extend(concurrency_problems(project, &place, source));
    if source.kind == SourceKind::PostgresCdc {
        problems.extend(cdc_name_problems(&place, name, source));
    }
    problems
}

/// A Python source reading a connection a warehouse target loads into, whose credentials Python code must never get (a files target's s3 or filesystem connection may be shared).
fn shared_warehouse_problems(
    project: &ProjectConfig,
    name: &str,
    source: &SourceConfig,
    connection: &ConnectionConfig,
) -> Vec<String> {
    if FILES_CONNECTION_KINDS.contains(&connection.kind()) {
        return Vec::new();
    }
    project
        .targets
        .iter()
        .filter(|(_, target)| target.connection == source.connection)
        .map(|(target, _)| {
            format!(
                "Python source {name} uses connection {}, which target {target} loads into; Python code never gets warehouse credentials — give the source its own connection",
                source.connection
            )
        })
        .collect()
}

/// How problems name a source: its TOML section, or the Python file declaring it.
fn source_place(project: &ProjectConfig, name: &str) -> String {
    match project.python_modules.get(name) {
        Some(module) => format!("Python source {name} ({module})"),
        None => format!("[sources.{name}]"),
    }
}

/// Settings each kind requires or rejects.
fn kind_problems(place: &str, source: &SourceConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    let template = source.table_name_template();
    let cdc_only = source.publication.is_some()
        || source.slot.is_some()
        || source.cdc_mode != CdcMode::Merge
        || source.batch_interval.is_some();
    if cdc_only && source.kind != SourceKind::PostgresCdc {
        problems.push(format!(
            "{place} publication, slot, cdc_mode and batch_interval are for kind = \"postgres_cdc\""
        ));
    }
    if source.kind == SourceKind::PostgresCdc {
        problems.extend(cdc_problems(place, source));
    }
    problems.extend(constant_problems(place, source));
    let queries = source.tables.values().any(|table| table.sql.is_some());
    if !matches!(
        source.kind,
        SourceKind::Snowflake | SourceKind::Odbc | SourceKind::BigQuery | SourceKind::Databricks
    ) && queries
    {
        problems.push(format!(
            "{place} sql is for kind = \"snowflake\", \"odbc\", \"bigquery\" and \"databricks\""
        ));
    }
    if source.kind != SourceKind::BigQuery && source.project.is_some() {
        problems.push(format!("{place} project is for kind = \"bigquery\""));
    }
    if source.kind != SourceKind::Snowflake
        && (source.warehouse.is_some() || source.stage.is_some())
    {
        problems.push(format!(
            "{place} warehouse and stage are for kind = \"snowflake\""
        ));
    }
    if source.kind != SourceKind::Odbc
        && (source.max_text_size.is_some() || source.max_binary_size.is_some())
    {
        problems.push(format!(
            "{place} max_text_size and max_binary_size are for kind = \"odbc\""
        ));
    }
    if source.kind != SourceKind::Odbc
        && (source.long_column_threshold.is_some() || source.max_batch_memory.is_some())
    {
        problems.push(format!(
            "{place} long_column_threshold and max_batch_memory are for kind = \"odbc\""
        ));
    }
    if source.max_batch_memory.is_some_and(|size| size.0 == 0) {
        problems.push(format!("{place} max_batch_memory must be more than 0"));
    }
    if source.kind != SourceKind::Odbc
        && sheet_settings(source).any(|settings| settings.cursor_bounds.is_some())
    {
        problems.push(format!(
            "{place} cursor_bounds is for kind = \"odbc\", whose drivers may refuse a bound cursor value"
        ));
    }
    if source.kind == SourceKind::Snowflake {
        problems.extend(unload_stage_problems(place, source));
    }
    let selects = matches!(
        source.kind,
        SourceKind::Postgres
            | SourceKind::Snowflake
            | SourceKind::Odbc
            | SourceKind::BigQuery
            | SourceKind::Databricks
    );
    if !selects && sheet_settings(source).any(|settings| settings.row_filter.is_some()) {
        problems.push(format!(
            "{place} where is for kind = \"postgres\", \"snowflake\", \"odbc\", \"bigquery\" and \"databricks\", whose tables SQLLoad reads with a SELECT"
        ));
    }
    problems.extend(
        (source.tables.iter())
            .filter(|(_, table)| table.sql.is_some() && table.settings.row_filter.is_some())
            .map(|(name, _)| {
                format!("{place} [tables.{name}] where narrows a table SQLLoad selects from, and {name} is a query; put the condition in its sql")
            }),
    );
    let sheets_only = source.spreadsheet_id.is_some()
        || !source.ranges.is_empty()
        || sheet_settings(source)
            .any(|settings| settings.header.is_some() || settings.header_row.is_some());
    problems.extend(column_names_problems(place, source));
    if sheets_only && source.kind != SourceKind::GoogleSheets {
        problems.push(format!(
            "{place} spreadsheet_id, ranges, header and header_row are for kind = \"google_sheets\""
        ));
    }
    match source.kind {
        SourceKind::Postgres
        | SourceKind::PostgresCdc
        | SourceKind::Snowflake
        | SourceKind::Odbc
        | SourceKind::BigQuery
        | SourceKind::Databricks => {
            if source.include.is_empty() && !queries {
                let tables = match source.kind {
                    SourceKind::Snowflake
                    | SourceKind::Odbc
                    | SourceKind::BigQuery
                    | SourceKind::Databricks => {
                        "; list tables in include, or give a table a sql query"
                    }
                    _ => "",
                };
                problems.push(format!(
                    "{place} include is empty, so it selects no tables{tables}"
                ));
            }
            if !template.contains(TABLE_PLACEHOLDER) {
                problems.push(format!(
                    "{place} table_name {template:?} must contain {TABLE_PLACEHOLDER}, or every table gets the same name"
                ));
            }
            problems.extend(files_only_problems(place, source));
        }
        SourceKind::Files => {
            if source.path.is_none() {
                problems.push(format!("{place} kind = \"files\" needs a path"));
            }
            if !source.include.is_empty() || !source.exclude.is_empty() {
                problems.push(format!("{place} include and exclude are for database sources; a files source is one table"));
            }
            problems.extend(encoding_problems(place, source));
            problems.extend(path_value_problems(place, source));
        }
        SourceKind::GoogleSheets => {
            if source.ranges.len() != 1 && !template.contains(TABLE_PLACEHOLDER) {
                problems.push(format!(
                    "{place} table_name {template:?} must contain {TABLE_PLACEHOLDER}, or every table gets the same name"
                ));
            }
            problems.extend(files_only_problems(place, source));
            problems.extend(sheets_problems(place, source));
        }
        SourceKind::Python => {
            if source.tables.len() > 1 && !template.contains(TABLE_PLACEHOLDER) {
                problems.push(format!(
                    "{place} table_name {template:?} must contain {TABLE_PLACEHOLDER}, or every table gets the same name"
                ));
            }
        }
    }
    problems
}

/// Problems with a `snowflake` source's `stage`: an S3 `url` with its `storage_integration`, or an existing stage's `name`, not both.
fn unload_stage_problems(place: &str, source: &SourceConfig) -> Vec<String> {
    let Some(stage) = &source.stage else {
        return Vec::new();
    };
    let place = format!("{place} stage");
    let mut problems: Vec<String> = Vec::new();
    match (&stage.url, &stage.storage_integration, &stage.name) {
        (Some(url), Some(_), None) => match resolve_location(None, url) {
            Ok(Location {
                store: StoreKind::S3 { .. },
                ..
            }) => {}
            Ok(_) => problems.push(format!("{place} url {url:?} must be an s3:// URL")),
            Err(error) => problems.push(format!("{place} url {url:?}: {error}")),
        },
        (Some(_), None, None) => problems.push(format!(
            "{place} url needs the storage_integration Snowflake writes to it with"
        )),
        (None, None, Some(name))
            if name.split('.').filter(|part| !part.is_empty()).count() != STAGE_NAME_PARTS =>
        {
            problems.push(format!("{place} name {name:?} must be DATABASE.SCHEMA.STAGE"));
        }
        (None, None, Some(_)) => {}
        _ => problems.push(format!(
            "{place} takes either url and storage_integration (an S3 folder) or name (an existing stage)"
        )),
    }
    problems
}

/// The settings only a files source takes, set on a source of another kind.
fn files_only_problems(place: &str, source: &SourceConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    if source.path.is_some()
        || !source.partition_columns.is_empty()
        || source.path_columns.is_some()
        || source.unmatched_paths.is_some()
        || source.copy_to_internal_stage
        || source.after_load != AfterLoad::Keep
        || source.order != FilesOrder::default()
        || source.on_empty != OnEmpty::default()
    {
        problems.push(format!(
            "{place} path, partition_columns, path_columns, unmatched_paths, copy_to_internal_stage, after_load, order and on_empty are for kind = \"files\""
        ));
    }
    if source.format != SourceFormat::Parquet
        || source.compression.is_some()
        || source.csv.is_some()
        || source.xlsx.is_some()
        || source.xml.is_some()
        || source.members.is_some()
        || source.pattern.is_some()
        || source.shape != RecordShape::Columns
        || source.record_column.is_some()
    {
        problems.push(format!(
            "{place} format, compression, csv, xlsx, xml, members, pattern, shape and record_column are for kind = \"files\""
        ));
    }
    problems
}

/// Where `transform` does not fit: a source other than files or `google_sheets`, an empty name, or a project without `python_paths` to find the function on.
fn transform_problems(project: &ProjectConfig, place: &str, source: &SourceConfig) -> Vec<String> {
    let transforms: Vec<&String> = sheet_settings(source)
        .filter_map(|settings| settings.transform.as_ref())
        .collect();
    let mut problems: Vec<String> = Vec::new();
    if transforms.is_empty() {
        return problems;
    }
    if !matches!(source.kind, SourceKind::Files | SourceKind::GoogleSheets) {
        problems.push(format!(
            "{place} transform is for kind = \"files\" and kind = \"google_sheets\" tables; an sl.http job takes transform= on paginate or fan_out"
        ));
    }
    if transforms
        .iter()
        .any(|transform| transform.trim().is_empty())
    {
        problems.push(format!(
            "{place} transform is empty; give the name @sl.transform registered the function under"
        ));
    }
    if project.python_paths.is_empty() {
        problems.push(format!(
            "{place} transform needs python_paths, naming the folder that holds the @sl.transform function"
        ));
    }
    problems
}

/// Where a `rename` function does not fit: an empty name, or a project without `python_paths` to find it on. A Python source's tables find theirs where they are declared.
fn rename_problems(project: &ProjectConfig, place: &str, source: &SourceConfig) -> Vec<String> {
    let functions: Vec<&String> = sheet_settings(source)
        .filter_map(|settings| match &settings.rename {
            Some(RenameSetting::Function(function)) => Some(function),
            _ => None,
        })
        .collect();
    let mut problems: Vec<String> = Vec::new();
    if functions.iter().any(|function| function.trim().is_empty()) {
        problems.push(format!(
            "{place} rename is empty; give a map of source names to loaded names, or the name @sl.rename registered a function under"
        ));
    }
    if !functions.is_empty() && project.python_paths.is_empty() {
        problems.push(format!(
            "{place} rename names a function, which needs python_paths, naming the folder that holds the @sl.rename function"
        ));
    }
    problems
}

/// A `batch_concurrency` above 1 in a project that has not opted in with `microbatch_concurrency = true`, or above [`MAX_BATCH_CONCURRENCY`], as each window read at once holds a source connection.
fn concurrency_problems(
    project: &ProjectConfig,
    place: &str,
    source: &SourceConfig,
) -> Vec<String> {
    let most = sheet_settings(source)
        .filter_map(|settings| settings.batch_concurrency)
        .map(std::num::NonZeroUsize::get)
        .max()
        .unwrap_or(1);
    let gated = (most > 1 && !project.microbatch_concurrency).then(|| {
        format!(
            "{place} batch_concurrency reads several windows at once, which needs microbatch_concurrency = true at the top of the project"
        )
    });
    let capped = (most > MAX_BATCH_CONCURRENCY).then(|| {
        format!(
            "{place} batch_concurrency = {most} would hold {most} source connections; read at most {MAX_BATCH_CONCURRENCY} windows at once"
        )
    });
    gated.into_iter().chain(capped).collect()
}

/// A source's settings and each of its tables'.
fn sheet_settings(source: &SourceConfig) -> impl Iterator<Item = &TableSettings> {
    std::iter::once(&source.defaults).chain(source.tables.values().map(|table| &table.settings))
}

/// Where `column_names` does not fit: a source other than headerless CSV or Excel files or `google_sheets`, whole records, a table whose data has a header row, or names repeated ignoring case.
fn column_names_problems(place: &str, source: &SourceConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    let named = sheet_settings(source).any(|settings| settings.column_names.is_some());
    let fits = match source.kind {
        SourceKind::GoogleSheets => true,
        SourceKind::Files => {
            matches!(source.format, SourceFormat::Csv | SourceFormat::Xlsx)
                && source.shape != RecordShape::Record
        }
        _ => false,
    };
    if named && !fits {
        problems.push(format!(
            "{place} column_names is for columns of data without a header row: kind = \"files\" with format = \"csv\" or \"xlsx\" (not shape = \"record\"), or kind = \"google_sheets\""
        ));
        return problems;
    }
    let header = match source.kind {
        SourceKind::GoogleSheets => "header = false",
        _ if source.format == SourceFormat::Csv => "csv = { header = false }",
        _ => "xlsx = { header = false }",
    };
    if named && headed_tables_named(source) {
        problems.push(format!(
            "{place} column_names names the columns of data without a header row; set {header}, or leave the names to the header"
        ));
    }
    for names in sheet_settings(source).filter_map(|settings| settings.column_names.as_ref()) {
        for (position, name) in names.iter().enumerate() {
            if name.trim().is_empty() {
                problems.push(format!(
                    "{place} column_names has an empty name in place {}",
                    position + 1
                ));
            } else if names[..position]
                .iter()
                .any(|earlier| earlier.eq_ignore_ascii_case(name))
            {
                problems.push(format!(
                    "{place} column_names names {name} twice (ignoring case)"
                ));
            }
        }
    }
    problems
}

/// Whether a table that `column_names` names, as its own setting or the source's, reads data with a header row: a files source's `csv` or `xlsx` header, or a sheet table's `header` (a tab no table entry or range names takes the source's).
fn headed_tables_named(source: &SourceConfig) -> bool {
    if source.kind != SourceKind::GoogleSheets {
        return match source.format {
            SourceFormat::Csv => source.csv.as_ref().is_none_or(|csv| csv.header),
            _ => source.xlsx.as_ref().is_none_or(|xlsx| xlsx.header),
        };
    }
    let defaults = &source.defaults;
    let entries = source
        .tables
        .iter()
        .map(|(name, table)| (name, Some(&table.settings)));
    let ranged = source
        .ranges
        .keys()
        .filter(|name| !source.tables.contains_key(*name))
        .map(|name| (name, None));
    let unlisted = source.ranges.is_empty().then_some(defaults);
    entries
        .chain(ranged)
        .map(|(_, settings)| settings)
        .chain(unlisted.map(Some))
        .any(|settings| {
            let pick = |value: fn(&TableSettings) -> Option<bool>| {
                settings.and_then(value).or(value(defaults))
            };
            let named = settings
                .and_then(|settings| settings.column_names.as_ref())
                .or(defaults.column_names.as_ref())
                .is_some();
            named && pick(|settings| settings.header).unwrap_or(true)
        })
}

/// What a `google_sheets` source needs and cannot take: a spreadsheet, and no cursor, since a sheet has no column to read after.
fn sheets_problems(place: &str, source: &SourceConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    if source
        .spreadsheet_id
        .as_deref()
        .is_none_or(|id| id.trim().is_empty())
    {
        problems.push(format!("{place} kind = \"google_sheets\" needs a spreadsheet_id, the ID in the spreadsheet's URL"));
    }
    if !source.include.is_empty() || !source.exclude.is_empty() {
        problems.push(format!("{place} include and exclude are not for kind = \"google_sheets\"; name its tables in ranges"));
    }
    for settings in sheet_settings(source) {
        if settings.cursor_column.is_some()
            || settings.batch_size.is_some()
            || settings.write_strategy == Some(WriteStrategy::DeleteInsert)
        {
            problems.push(format!(
                "{place} a sheet has no cursor, so cursor_column, batch_size and write_strategy = \"delete_insert\" do not apply; use replace, merge on a unique_key, or append"
            ));
            break;
        }
    }
    if sheet_settings(source).any(|settings| settings.header_row == Some(0)) {
        problems.push(format!("{place} header_row counts from 1"));
    }
    problems
}

/// A files source's `path_columns` and `constant_columns` that do not parse, and a column any two of them and `partition_columns` both fill.
fn path_value_problems(place: &str, source: &SourceConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    let mut names: Vec<(String, &str)> = source
        .partition_columns
        .keys()
        .map(|name| (name.clone(), "partition_columns"))
        .collect();
    match source
        .path_columns
        .as_deref()
        .map(|text| path_template(text, &[]))
    {
        None if source.unmatched_paths.is_some() => problems.push(format!(
            "{place} unmatched_paths is for path_columns, which is not set"
        )),
        None => {}
        Some(Ok(template)) => names.extend(
            template
                .captures
                .into_iter()
                .map(|(name, _)| (name, "path_columns")),
        ),
        Some(Err(problem)) => problems.push(format!("{place} {problem}")),
    }
    if let Some(record) = &source.files_encoding().record {
        names.push((record.clone(), "record_column"));
    }
    problems.extend(clashes(place, &names, 0));
    let filled = names.len();
    for constants in
        sheet_settings(source).filter_map(|settings| settings.constant_columns.as_ref())
    {
        let mut named = names.clone();
        named.extend(
            constant_columns(&constants.0, &[])
                .into_iter()
                .flatten()
                .map(|(name, _, _)| (name, "constant_columns")),
        );
        problems.extend(clashes(place, &named, filled));
    }
    problems
}

/// A problem for each of `names` from `from` on that an earlier one also fills, ignoring case.
fn clashes(place: &str, names: &[(String, &str)], from: usize) -> Vec<String> {
    (from..names.len())
        .filter_map(|index| clash(place, &names[..=index]))
        .collect()
}

/// The problem when an earlier one of `names` also fills the last one's column, ignoring case.
fn clash(place: &str, names: &[(String, &str)]) -> Option<String> {
    let ((name, setting), earlier) = names.split_last()?;
    let (_, first) = earlier
        .iter()
        .find(|(seen, _)| seen.eq_ignore_ascii_case(name))?;
    Some(format!(
        "{place} column {name} is in both {first} and {setting}; give it one source"
    ))
}

/// Problems with the `constant_columns` of a source and of each of its tables: a value that does not parse, a name SQLLoad keeps for its own columns, and any on a `postgres_cdc` source, whose changes are applied by key as they are captured.
fn constant_problems(place: &str, source: &SourceConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    let maps: Vec<&ConstantColumns> = sheet_settings(source)
        .filter_map(|settings| settings.constant_columns.as_ref())
        .collect();
    if source.kind == SourceKind::PostgresCdc && !maps.is_empty() {
        problems.push(format!(
            "{place} constant_columns is not supported for kind = \"postgres_cdc\""
        ));
    }
    for constants in maps {
        if let Err(problem) = constant_columns(&constants.0, &[]) {
            problems.push(format!("{place} {problem}"));
        }
        problems.extend(
            constants
                .0
                .keys()
                .filter(|name| name.to_ascii_lowercase().starts_with(SQLLOAD_PREFIX))
                .map(|name| {
                    format!("{place} constant column {name}: names starting with {SQLLOAD_PREFIX} are SQLLoad's own")
                }),
        );
    }
    problems
}

/// A ZIP files source's `after_load` while another files source over the same connection reads ZIP archives under an overlapping `path`, which may still need an archive this one moves or deletes; where `move_to` may go is checked once `path` is resolved.
fn after_load_problems(
    project: &ProjectConfig,
    (name, place): (&str, &str),
    source: &SourceConfig,
) -> Vec<String> {
    if source.kind != SourceKind::Files
        || source.after_load == AfterLoad::Keep
        || source.compression != Some(Compression::Zip)
    {
        return Vec::new();
    }
    let folder = |source: &SourceConfig| {
        let path = source.path.as_deref().unwrap_or_default();
        let path = path.strip_prefix("./").unwrap_or(path);
        path.trim_matches('/').to_owned()
    };
    let inside = |inner: &str, outer: &str| {
        outer.is_empty() || inner == outer || inner.starts_with(&format!("{outer}/"))
    };
    let mine = folder(source);
    project
        .sources
        .iter()
        .filter(|(other_name, other)| {
            other_name.as_str() != name
                && other.kind == SourceKind::Files
                && other.connection == source.connection
                && other.compression == Some(Compression::Zip)
        })
        .filter(|(_, other)| {
            let theirs = folder(other);
            inside(&mine, &theirs) || inside(&theirs, &mine)
        })
        .map(|(other_name, other)| {
            format!(
                "{place} after_load moves or deletes each ZIP archive once its members load, and [sources.{other_name}] reads ZIP archives in {:?} on the same connection, which may still need them; remove after_load, or give the sources folders apart",
                other.path.as_deref().unwrap_or_default()
            )
        })
        .collect()
}

/// Settings of the xlsx and xml formats and of ZIP archives that do not fit the source.
fn container_problems(place: &str, source: &SourceConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    let zip = source.compression == Some(Compression::Zip);
    if source.xlsx.is_some() && source.format != SourceFormat::Xlsx {
        problems.push(format!("{place} xlsx is for format = \"xlsx\""));
    }
    if let Some(xlsx) = &source.xlsx {
        if let Some(range) = &xlsx.range {
            if xlsx.sheet.is_some() || xlsx.skip_rows != 0 {
                problems.push(format!(
                    "{place} xlsx.range names the sheet and the first row itself; drop xlsx.sheet and xlsx.skip_rows"
                ));
            }
            match (a1_range(range), xlsx.header_row) {
                (Err(problem), _) => problems.push(format!("{place} xlsx.{problem}")),
                (Ok(range), Some(header_row)) => {
                    let rows = range.last_row.map(|last| last - range.first_row + 1);
                    if rows.is_some_and(|rows| header_row > rows) {
                        problems.push(format!(
                            "{place} xlsx.header_row {header_row} is past the range's {} rows",
                            rows.unwrap_or_default()
                        ));
                    }
                }
                (Ok(_), None) => {}
            }
        }
        if xlsx.header_row == Some(0) {
            problems.push(format!("{place} xlsx.header_row counts from 1"));
        }
    }
    match (&source.xml, source.format) {
        (Some(_), format) if format != SourceFormat::Xml => {
            problems.push(format!("{place} xml is for format = \"xml\""));
        }
        (Some(xml), _) if !xml.record_path.trim_matches('/').is_empty() => {}
        (_, SourceFormat::Xml) => problems.push(format!(
            "{place} format = \"xml\" needs xml.record_path, the element names from the root to each row's element, such as \"orders/order\""
        )),
        _ => {}
    }
    if let Some(Err(error)) = source.pattern.as_deref().map(FilePattern::new) {
        problems.push(format!("{place} pattern: {error}"));
    }
    match (&source.members, zip) {
        (Some(_), false) => problems.push(format!("{place} members is for compression = \"zip\"")),
        (Some(pattern), true) => {
            if let Err(error) = globset::Glob::new(pattern) {
                problems.push(format!("{place} members {pattern:?}: {error}"));
            }
        }
        (None, _) => {}
    }
    let loader_only = zip || matches!(source.format, SourceFormat::Xlsx | SourceFormat::Xml);
    if source.copy_to_internal_stage && loader_only {
        problems.push(format!(
            "{place} copy_to_internal_stage is for files Snowflake reads itself; SQLLoad reads xlsx and xml files and ZIP archives' members"
        ));
    }
    problems
}

/// Settings that do not fit a files source's format.
fn encoding_problems(place: &str, source: &SourceConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    match (source.shape, source.format, &source.record_column) {
        (RecordShape::Record, SourceFormat::Parquet, _) => problems.push(format!(
            "{place} shape = \"record\" is for ndjson, json and csv files; Parquet files already have typed columns"
        )),
        (RecordShape::Record, format @ (SourceFormat::Xlsx | SourceFormat::Xml), _) => {
            problems.push(format!(
                "{place} shape = \"record\" is for ndjson, json and csv files, not {format}"
            ));
        }
        (RecordShape::Columns, _, Some(_)) => problems.push(format!(
            "{place} record_column is for shape = \"record\", which is not set"
        )),
        (_, _, Some(name)) if name.trim().is_empty() => {
            problems.push(format!("{place} record_column must not be empty"));
        }
        (_, _, Some(name))
            if LOADER_COLUMNS
                .iter()
                .any(|loader| loader.eq_ignore_ascii_case(name)) =>
        {
            problems.push(format!(
                "{place} record_column {name} is a column SQLLoad adds itself; choose another name"
            ));
        }
        _ => {}
    }
    if let Some(record) = &source.files_encoding().record {
        for (table, overrides) in &source.tables {
            let pinned: Vec<&str> = overrides
                .columns
                .keys()
                .map(String::as_str)
                .filter(|name| name != record)
                .collect();
            if !pinned.is_empty() {
                problems.push(format!(
                    "{place} shape = \"record\" loads each record into one json column, {record}, so [tables.{table}] columns cannot pin {}",
                    pinned.join(", ")
                ));
            }
        }
    }
    problems.extend(container_problems(place, source));
    if source.format == SourceFormat::Parquet && source.compression.is_some() {
        problems.push(format!(
            "{place} compression is for ndjson, json, csv, xlsx and xml files; Parquet files compress inside the file, and SQLLoad does not read them from ZIP archives"
        ));
    }
    match (&source.csv, source.format) {
        (None, _) => {}
        (Some(csv), SourceFormat::Csv) => {
            let quote = csv.quote.character();
            let characters = [
                ("delimiter", Some(csv.delimiter)),
                ("quote", quote),
                ("escape", csv.escape),
            ];
            for (setting, character) in characters {
                if let Some(character) = character
                    && (!character.is_ascii() || character == '\n' || character == '\r')
                {
                    problems.push(format!(
                        "{place} csv {setting} must be one ASCII character other than a line break, not {character:?}"
                    ));
                }
            }
            if quote == Some(csv.delimiter) || csv.escape == Some(csv.delimiter) {
                problems.push(format!(
                    "{place} csv delimiter must differ from quote and escape"
                ));
            }
            match (csv.quote, csv.escape) {
                (CsvQuote::Off(true), _) => problems.push(format!(
                    "{place} csv quote = true names no character; write it, such as '\"', or false to read every quote as a plain character"
                )),
                (CsvQuote::Off(false), Some(_)) => problems.push(format!(
                    "{place} csv escape works inside quoted fields, and quote = false has none; remove escape"
                )),
                _ => {}
            }
            if let Err(problem) = text_charset(&csv.encoding) {
                problems.push(format!("{place} {problem}"));
            }
        }
        (Some(_), format) => problems.push(format!(
            "{place} csv is for format = \"csv\", not {format:?}",
            format = format.to_string()
        )),
    }
    problems
}

/// Settings a `postgres_cdc` source cannot take: the change stream decides how rows are written, so no strategy or cursor; `deletes = "hard"` needs `merge`.
fn cdc_problems(place: &str, source: &SourceConfig) -> Vec<String> {
    let mut problems: Vec<String> = Vec::new();
    let settings = std::iter::once(&source.defaults)
        .chain(source.tables.values().map(|table| &table.settings));
    for settings in settings {
        if settings.write_strategy.is_some()
            || settings.cursor_column.is_some()
            || settings.batch_size.is_some()
            || settings.state.is_some()
            || settings.checks.is_some()
            || settings.on_duplicate_keys.is_some()
            || settings.null_keys.is_some()
            || settings.replace_key.is_some()
            || settings.newest_by.is_some()
        {
            problems.push(format!(
                "{place} kind = \"postgres_cdc\" writes every captured change, so it takes no write_strategy, cursor_column, batch_size, state, checks, on_duplicate_keys, null_keys, replace_key or newest_by; choose cdc_mode = \"merge\" or \"changelog\""
            ));
            break;
        }
    }
    if source
        .batch_interval
        .is_some_and(|interval| interval < MIN_BATCH_INTERVAL)
    {
        problems.push(format!(
            "{place} batch_interval must be at least 1s, or `load --follow` would write a batch for almost every change"
        ));
    }
    let hard = std::iter::once(&source.defaults)
        .chain(source.tables.values().map(|table| &table.settings))
        .any(|settings| settings.deletes == Some(Deletes::Hard));
    if hard && source.cdc_mode == CdcMode::Changelog {
        problems.push(format!(
            "{place} deletes = \"hard\" applies to cdc_mode = \"merge\"; a changelog keeps every delete as a row"
        ));
    }
    problems
}

/// A `postgres_cdc` source's slot and publication names, as set or by default (`sqlload_<source>`), must be plain Postgres names.
fn cdc_name_problems(place: &str, name: &str, source: &SourceConfig) -> Vec<String> {
    let default_name = format!("sqlload_{}", name.to_lowercase());
    let mut problems: Vec<String> = Vec::new();
    for (setting, chosen) in [("slot", &source.slot), ("publication", &source.publication)] {
        let effective = chosen.as_deref().unwrap_or(&default_name);
        let valid = !effective.is_empty()
            && effective.len() <= MAX_POSTGRES_NAME_BYTES
            && effective
                .chars()
                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_');
        if !valid {
            problems.push(format!(
                "{place} {setting} {effective:?} must be lower-case letters, digits and underscores, at most {MAX_POSTGRES_NAME_BYTES} characters; set {setting} = \"...\""
            ));
        }
    }
    problems
}