sqlload 0.5.0

SQL data loading tooling. Pre-release, under development.
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
use crate::adapters::contract::models::WriteMode;
use crate::adapters::dispatch::main::dialect::sql_adapter::sql_adapter;
use crate::adapters::dispatch::main::route::sends_rows::sends_rows;
use crate::adapters::files::models::{PendingLoad, PendingPhase};
use crate::adapters::snowflake::models::PlannedStage;
use crate::batch_plan::constants::{BATCH_BYTES, MAX_BATCH_FILES};
use crate::column_types::models::ColumnType;
use crate::files_source::models::SourceFormat;
use crate::loader::_helpers::sources::source_readers;
use crate::loader::_helpers::table_plan::is_direct;
use crate::loader::constants::{PARALLEL_READERS, PLAN_DETAIL_COLUMN};
use crate::loader::models::{
    CommitRule, FilesOutput, PythonTable, SourceEnd, TablePlan, WholeReason,
};
use crate::project::models::BatchStep;
use crate::project::models::FilesFormat;
use crate::project::models::MaskMethod;
use crate::routing::models::Route;
use crate::run_log::main::describe_state::describe_state;
use crate::schema_plan::models::{ColumnOrigin, TableAction};

/// A section: a heading with an optional right-aligned note, then each item as a tree branch.
pub(crate) fn tree(heading: &str, note: &str, items: Vec<Vec<String>>) -> Vec<String> {
    if items.is_empty() {
        return Vec::new();
    }
    let mut lines = vec![aligned(heading, note)];
    let count = items.len();
    for (index, item) in items.into_iter().enumerate() {
        let last = index + 1 == count;
        let (branch, indent) = if last {
            ("└── ", "    ")
        } else {
            ("├── ", "│   ")
        };
        let detail_count = item.len().saturating_sub(1);
        for (line_index, line) in item.into_iter().enumerate() {
            match line_index {
                0 => lines.push(format!("{branch}{line}")),
                _ => {
                    let sub = if line_index == detail_count {
                        "└── "
                    } else {
                        "├── "
                    };
                    lines.push(format!("{indent}{sub}{line}"));
                }
            }
        }
    }
    lines.push(String::new());
    lines
}

fn aligned(left: &str, right: &str) -> String {
    match right.is_empty() {
        true => left.to_owned(),
        false => format!("{left:<width$}{right}", width = PLAN_DETAIL_COLUMN),
    }
}

/// `name` padded so `detail` starts in the plan's detail column (after a branch prefix).
fn row(name: &str, detail: &str) -> String {
    format!("{name:<width$}{detail}", width = PLAN_DETAIL_COLUMN - 4)
}

pub(crate) fn object_lines(table: &TablePlan) -> Vec<String> {
    let name = sql_adapter(table.spec.warehouse).render_display_name(&table.spec.destination);
    let mut lines = vec![match &table.schema.action {
        TableAction::Recreate => row(
            &name,
            &format!(
                "recreate table ({} columns, --reload)",
                table.schema.write_columns.len()
            ),
        ),
        TableAction::Create if table.spec.python.is_some() => row(&name, &python_create(table)),
        TableAction::Create => row(
            &name,
            &format!(
                "create table ({} columns, from source catalog)",
                table.schema.write_columns.len()
            ),
        ),
        _ if table.spec.python.is_some() => row(&name, "alter table at load"),
        _ => row(&name, "alter table"),
    }];
    if let TableAction::Alter(changes) = &table.schema.action {
        lines.extend(changes.iter().map(ToString::to_string));
    }
    lines.extend(
        table
            .spec
            .excluded_columns
            .iter()
            .map(|(column, rule)| format!("excluded  {column} ({rule})")),
    );
    lines.extend(
        table
            .spec
            .masked
            .columns
            .iter()
            .map(|(column, method)| format!("masked  {column} ({method})")),
    );
    lines
}

pub(crate) fn stage_object_lines(stage: &PlannedStage) -> Vec<String> {
    vec![row(&stage.name, &format!("create stage on {}", stage.url))]
}

pub(crate) fn load_lines(table: &TablePlan) -> Vec<String> {
    let spec = &table.spec;
    let strategy = match &table.mode {
        WriteMode::Merge { unique_key } => format!("merge (unique_key: {})", unique_key.join(", ")),
        WriteMode::DeleteInsert { cursor_column, .. } => {
            format!("delete_insert (cursor: {cursor_column})")
        }
        WriteMode::Append => "append".to_owned(),
        WriteMode::Replace => "replace".to_owned(),
        WriteMode::Recreate => "recreate from the whole source (--reload)".to_owned(),
    };
    let origin = format!("  ({})", spec.strategy_origin);
    let source = match (&spec.files, &spec.python) {
        (Some(files), _) => format!(
            "source  files  {}  {}  {}",
            spec.source_name, files.location, files.encoding
        ),
        (None, Some(python)) => format!("source  python  {} ({})", python.module, spec.source_name),
        (None, None) => format!(
            "source  postgres  {}  {}",
            spec.source_name,
            spec.source_table.qualified_name()
        ),
    };
    let mut lines = vec![
        row(&spec.destination.table, &format!("{strategy}{origin}")),
        source,
    ];
    lines.extend(
        spec.python
            .as_ref()
            .map(|python| python_columns_line(table, python)),
    );
    lines.extend(
        spec.files
            .as_ref()
            .zip(table.files.as_ref())
            .map(|(source, read)| {
                let skipped = match source.table.skipped {
                    0 => String::new(),
                    count => format!(
                        ", {count} skipped (names that do not fit {})",
                        source.encoding
                    ),
                };
                format!(
                    "files   {} to read of {} listed{skipped}",
                    read.files.len(),
                    source.table.files.len()
                )
            }),
    );
    lines.extend(
        spec.files
            .iter()
            .flat_map(|source| source.notes.iter())
            .map(|note| format!("note    {note}")),
    );
    lines.extend(table.output.as_ref().map(files_line));
    lines.extend(table.output.iter().map(|output| {
        format!(
            "footers {} read from the newest partition ({} files listed, max_footer_reads {})",
            output.footers_read, output.listed, output.max_footer_reads
        )
    }));
    if table.output.is_some() {
        lines.extend(table.schema.action.summary().into_iter().map(
            |change| match change.as_str() {
                "create" => format!(
                    "schema  first files, {} columns",
                    table.schema.write_columns.len()
                ),
                "recreate" => {
                    "schema  every file rewritten with the current columns (--reload)".to_owned()
                }
                "unchanged" => "schema  as the newest files".to_owned(),
                _ => format!("schema  new files {change}"),
            },
        ));
    }
    if reload_moves_columns(table) {
        lines.push("schema  changed columns move to the end of the column order".to_owned());
    }
    lines.extend(table.route.as_ref().map(route_line));
    if let Some(reason) = table
        .route
        .as_ref()
        .filter(|_| table.stage.is_some() && spec.files.is_some())
        .map(|route| route.reason.clone())
    {
        lines.push(format!("why     {reason}"));
    }
    if table.route.as_ref().is_some_and(|route| {
        is_direct(Some(route))
            && spec
                .masked
                .columns
                .values()
                .any(|method| *method == MaskMethod::Hash)
    }) {
        lines.push(
            "note    hashed columns reach Snowflake in plain text in a temporary table, gone with its session; mask = \"hmac\" keeps them out, through SQLLoad (much slower)"
                .to_owned(),
        );
    }
    lines.push(batch_line(table));
    if matches!(table.commits, CommitRule::Windows(_))
        && spec.python.is_none()
        && spec.files.is_none()
    {
        lines.push(
            "note    the windows are read in one REPEATABLE READ snapshot, held open on the source for the whole table load (mind idle_in_transaction_session_timeout and vacuum)"
                .to_owned(),
        );
    }
    if source_readers(table) > 1 {
        lines.push(format!(
            "note    read over up to {PARALLEL_READERS} connections in one snapshot, each a range of the table's pages, when it is a plain table of 128 MiB or more on Postgres 14+"
        ));
    }
    lines.extend(
        table
            .cursor_note
            .iter()
            .map(|note| format!("cursor  {note}")),
    );
    lines.extend(table.logged.as_ref().map(|logged| {
        format!(
            "state   logged: {}",
            describe_state(logged.current.as_ref())
        )
    }));
    lines.extend(
        table
            .output
            .iter()
            .flat_map(|output| output.pending.iter())
            .map(pending_line),
    );
    lines.extend(
        table
            .output
            .iter()
            .flat_map(|output| output.notes.iter())
            .map(|note| format!("note    {note}")),
    );
    lines
}

/// How the load commits: by cursor window, in cursor order, by size, by files batch, or as one commit; with a hint to set `batch_size` for a large first load that would be one commit.
fn batch_line(table: &TablePlan) -> String {
    let spec = &table.spec;
    let at_end = matches!(table.mode, WriteMode::Replace | WriteMode::Recreate);
    let described = match (&table.commits, at_end) {
        (CommitRule::Windows(windows), _) => {
            let step = match spec.batch {
                Some(BatchStep::Duration(step)) => humantime::format_duration(step).to_string(),
                Some(BatchStep::Integer(step)) => step.to_string(),
                Some(BatchStep::Whole) | None => String::new(),
            };
            let end = match &spec.source_end {
                Some(SourceEnd::Maximum { maximum, .. }) => {
                    format!(", up to the source's maximum {maximum} (read at plan)")
                }
                Some(SourceEnd::Empty) => ", none: the source has no cursor values".to_owned(),
                None => String::new(),
            };
            format!(
                "{} cursor windows of {step}, read and committed one at a time{end}",
                windows.count
            )
        }
        (CommitRule::FileGroups(groups), _) => {
            let parquet = spec
                .files
                .as_ref()
                .is_some_and(|files| files.encoding.format == SourceFormat::Parquet);
            let drift = match parquet {
                true => "; each batch's footers are combined, so later batches may add columns",
                false => "",
            };
            format!(
                "{} batches of whole partitions (up to {MAX_BATCH_FILES} files or about 1 GB each){drift}",
                groups.len()
            )
        }
        (_, true) if sends_rows(table.stage.as_ref()) => {
            "about 1 GB at a time into a staging table, copied and swapped in once at the end"
                .to_owned()
        }
        (_, true) => "about 1 GB at a time into a new table, swapped in once at the end".to_owned(),
        (CommitRule::Ordered { column, .. }, false) => format!(
            "read in {column} order (an index leads with it, NULLs first), committing about every 1 GB between distinct values{}",
            match table.mode {
                WriteMode::DeleteInsert { .. } => ", each commit replacing its own cursor range",
                _ => "",
            }
        ),
        (CommitRule::Size, false) => "committing about every 1 GB".to_owned(),
        (CommitRule::Whole(reason), false) => {
            let estimated = u64::try_from(spec.source_table.estimated_bytes).unwrap_or_default();
            let large = !table.exists && (spec.python.is_some() || estimated > BATCH_BYTES as u64);
            let why = match reason {
                WholeReason::Chosen => "batch_size = \"none\"",
                WholeReason::TextCursor => {
                    "a text cursor, which the source and destination may sort differently"
                }
                WholeReason::AppendWithoutCursor => {
                    "append without a cursor: a rerun after a partial commit would duplicate it"
                }
                WholeReason::Unordered | WholeReason::Blocked => {
                    "committing part of an unordered cursor read could skip rows"
                }
            };
            let hint = match (large, reason) {
                (true, WholeReason::Unordered) => {
                    "; for a large first load, set batch_size (such as batch_size = \"1d\") to commit it window by window"
                }
                _ => "",
            };
            format!("one commit for the whole read ({why}){hint}")
        }
    };
    let logged = match table.logged {
        Some(_) => ", and only when ctx.state has changed",
        None => "",
    };
    format!("batch   {described}{logged}")
}

fn pending_line(load: &PendingLoad) -> String {
    let (stopped, repair) = match load.phase {
        PendingPhase::Writing => ("while writing", "deletes its"),
        PendingPhase::Replacing => ("while deleting the files it replaced", "finishes deleting"),
    };
    format!(
        "cleanup load {} stopped {stopped}; load {repair} {} files first",
        load.load_id,
        load.deletes.len()
    )
}

fn files_line(output: &FilesOutput) -> String {
    let partition = match &output.path.partition {
        Some(partition) => format!("  partitions {partition}"),
        None => String::new(),
    };
    let format = match output.format {
        FilesFormat::Parquet => "parquet (zstd)",
    };
    format!("files   {}  {format}{partition}", output.path.folder)
}

/// Whether a Snowflake `--reload` re-adds any column, as its build table starts `LIKE` the table to keep the grants.
fn reload_moves_columns(table: &TablePlan) -> bool {
    let warehouse = table.spec.warehouse;
    let Some(live) = &table.live else {
        return false;
    };
    if table.mode != WriteMode::Recreate || !sql_adapter(warehouse).builds_like_target() {
        return false;
    }
    let sql = |column_type: &ColumnType| {
        sql_adapter(warehouse)
            .render_loader_logical_type(column_type)
            .sql
    };
    for column in &table.schema.write_columns {
        let name = sql_adapter(warehouse).stored_name(&column.name);
        let known = live.iter().find(|known| known.name == name);
        if known.is_none_or(|known| sql(&known.column_type) != sql(&column.column_type)) {
            return true;
        }
    }
    false
}

fn route_line(route: &Route) -> String {
    let steps: Vec<String> = route.steps.iter().map(ToString::to_string).collect();
    format!("route  {}", steps.join(" -> "))
}

pub(crate) fn blocked_lines(table: &TablePlan) -> Vec<String> {
    let Some(blocked) = &table.schema.blocked else {
        return Vec::new();
    };
    let mut lines = vec![row(&table.spec.destination.table, "cannot load")];
    lines.extend(blocked.reasons.iter().cloned());
    lines.extend(
        blocked
            .fix_flag
            .iter()
            .map(|flag| format!("fix  sqlload load {} {flag}", table.spec.destination.table)),
    );
    lines
}

pub(crate) fn warning_lines(table: &TablePlan) -> Vec<Vec<String>> {
    table
        .schema
        .warnings
        .iter()
        .map(|warning| vec![format!("{}  {warning}", table.spec.destination.table)])
        .collect()
}

/// A Python table's object line before its first load.
fn python_create(table: &TablePlan) -> String {
    let declared = table
        .schema
        .write_columns
        .iter()
        .filter(|column| column.origin != ColumnOrigin::Metadata)
        .count();
    match declared {
        0 => "create table on first load (columns inferred from the data)".to_owned(),
        count => format!(
            "create table on first load ({count} declared columns, the rest inferred from the data)"
        ),
    }
}

/// Where a Python table's columns come from.
fn python_columns_line(table: &TablePlan, python: &PythonTable) -> String {
    let declared = match python.declared.len() {
        0 => String::new(),
        count => format!(", {count} declared"),
    };
    match table.exists {
        true => format!("columns inferred at load and checked against the live table{declared}"),
        false => format!("columns inferred on first load{declared}"),
    }
}