sqlload 0.60.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
//! A files batch loaded by Snowflake `COPY`ing the source's own files.

use crate::adapters::contract::constants::{BEGIN_STATEMENT, COMMIT_STATEMENT};
use crate::adapters::contract::main::staging_id::staging_id;
use crate::adapters::contract::models::{DirectBatch, DirectLoaded};
use crate::adapters::contract::models::{TableName, WriteMode, WriteStatementsRequest};
use crate::adapters::contract::types::SqlAdapter;
use crate::adapters::snowflake::_helpers::write::copy_columns::copy_columns;
use crate::adapters::snowflake::constants::{
    MAX_COPY_SQL_BYTES, MAX_FRACTION_DIGITS, MAX_SCRIPT_BYTES, STAGE_FOLDER, USER_STAGE,
};
use crate::adapters::snowflake::main::copy::_copy_rows::copy_rows;
use crate::adapters::snowflake::main::copy::_direct_check::direct_check;
use crate::adapters::snowflake::main::copy::_direct_copies::direct_copies;
use crate::adapters::snowflake::main::copy::_script_copy_rows::script_copy_rows;
use crate::adapters::snowflake::main::copy::_stage_covers::stage_covers;
use crate::adapters::snowflake::main::stage::_remove_staged::remove_staged;
use crate::adapters::snowflake::main::stage::_stage_parquet::stage_parquet;
use crate::adapters::snowflake::main::stage::_stage_reference::stage_reference;
use crate::adapters::snowflake::models::{
    CopyColumn, CopyPlace, CopyValue, DirectCopy, HashedScript, SnowflakeSql, SnowflakeWriter,
    StageRoute, StagedFile,
};
use crate::column_types::models::ColumnType;
use crate::file_store::main::read_file::read_file;
use crate::files_source::main::batch_refusals::batch_refusals;
use crate::files_source::models::FilesRead;
use crate::load_naming::main::metadata_role::metadata_role;
use crate::load_naming::models::MetadataRole;
use crate::loader::errors::LoaderError;
use crate::loader::models::TableSpec;
use crate::project::models::MaskMethod;
use crate::schema_plan::models::{DesiredColumn, LiveColumn};

/// Load `batch` by having Snowflake `COPY` its files itself: from the external stage the writer loads through where it covers them, otherwise, with `copy_to_internal_stage`, after uploading them unchanged to `@~` (removed once copied). Fails where the batch's own footers do not allow an exact load: files never fall back to SQLLoad. A batch hashing a column is copied into a temporary table and hashed from it: into the build table; into the staging table under a mode matching keys (a merge), so the commit checks the keys (`null_keys`, `on_duplicate_keys`) and the table's checks before it writes them; otherwise into the table itself. `None` when neither applies.
impl SnowflakeWriter {
    pub(in crate::adapters) async fn copy_files(
        &mut self,
        batch: &DirectBatch<'_>,
    ) -> Result<Option<DirectLoaded>, LoaderError> {
        let spec = batch.spec;
        let Some(spec_files) = spec.files.as_ref() else {
            return Ok(None);
        };
        let source = &spec_files.location;
        let covering = match &self.route {
            Some(StageRoute::External(stage)) if stage_covers(&stage.location, source) => source
                .prefix
                .strip_prefix(&stage.location.prefix)
                .map(|folder| (stage.clone(), folder.to_owned())),
            _ => None,
        };
        if covering.is_none() && !spec_files.copy_to_internal_stage {
            return Ok(None);
        }
        let refused = batch_refusals(batch.footers, batch.files, &spec_files.table.columns);
        if let Some(reason) = direct_check(
            spec,
            (batch.read, batch.files, &refused),
            covering.as_ref().map(|(stage, _)| stage.as_ref()),
        ) {
            return Err(LoaderError::Invalid(format!(
                "Snowflake cannot load this batch's files exactly by reading them itself, and they never go through SQLLoad instead: {reason}"
            )));
        }
        let place = match &covering {
            Some((stage, folder)) => CopyPlace {
                stage: stage_reference(stage),
                folder: folder.clone(),
                filename_prefix: source.prefix.clone(),
                uploaded: false,
            },
            None => {
                let folder = format!(
                    "{STAGE_FOLDER}/{}/{:05}/",
                    batch.target.load_id, batch.target.batch
                );
                CopyPlace {
                    stage: format!("@{USER_STAGE}"),
                    filename_prefix: format!("@{USER_STAGE}/{folder}"),
                    folder,
                    uploaded: true,
                }
            }
        };
        let loaded = match place.uploaded {
            true => match self.upload_unchanged(batch, &place.folder).await {
                Ok(()) => self.copy_from(batch, &place).await,
                Err(error) => Err(error),
            },
            false => self.copy_from(batch, &place).await,
        };
        if place.uploaded
            && let Err(error) = remove_staged(&self.destination, &place.stage, &place.folder).await
        {
            tracing::warn!(%error, load_id = %batch.target.load_id, "cannot remove a batch's uploaded files; the next load removes them after a day");
        }
        loaded
    }

    /// Upload `batch`'s files unchanged to `@~` under `folder`; nothing is decoded.
    async fn upload_unchanged(
        &mut self,
        batch: &DirectBatch<'_>,
        folder: &str,
    ) -> Result<(), LoaderError> {
        let source_folder = batch.source_folder.ok_or_else(|| {
            LoaderError::Invalid("copy_to_internal_stage needs the files' folder".to_owned())
        })?;
        let mut known = None;
        for data in batch.files {
            let body = read_file(source_folder, &data.file.relative_key)
                .await
                .map_err(|error| LoaderError::Invalid(error.to_string()))?;
            let key = format!("{folder}{}", data.file.relative_key);
            let file = StagedFile {
                stage: USER_STAGE,
                key: &key,
                body: &body,
            };
            known = Some(stage_parquet(&self.destination, known, file).await?);
        }
        Ok(())
    }

    /// The `COPY`s of `batch` from `place`: straight into the target, into the staging or build table, or through a temporary table that hashes (also used, hashing nothing, for a direct batch too large for one request).
    async fn copy_from(
        &mut self,
        batch: &DirectBatch<'_>,
        place: &CopyPlace,
    ) -> Result<Option<DirectLoaded>, LoaderError> {
        let spec = batch.spec;
        let read = batch.read;
        let files = batch.files;
        let Some(spec_files) = spec.files.as_ref() else {
            return Ok(None);
        };
        let source = &spec_files.location;
        let hashes = spec
            .masked
            .columns
            .values()
            .any(|method| *method == MaskMethod::Hash);
        let copy =
            |target: &'_ TableName, columns: &[CopyColumn]| -> Result<Vec<String>, LoaderError> {
                let request = DirectCopy {
                    target,
                    stage: &place.stage,
                    folder: &place.folder,
                    filename_prefix: &place.filename_prefix,
                    source,
                    files,
                    columns,
                    encoding: &spec_files.encoding,
                    inferred: &read.checks.inferred,
                    headers: &batch.footers.headers,
                };
                let batches = direct_copies(&request).map_err(LoaderError::Invalid)?;
                Ok(batches.into_iter().map(|batch| batch.sql).collect())
            };
        let literals = (batch.target.load_id, batch.loaded_at);
        let direct_copies = match (hashes, batch.target.direct) {
            (false, true) => Some(copy(
                &spec.destination,
                &filled_columns(spec, (read, batch.live), batch.write_columns, literals),
            )?),
            _ => None,
        };
        let oversized = direct_copies
            .iter()
            .flatten()
            .map(String::len)
            .sum::<usize>()
            > MAX_SCRIPT_BYTES;
        if hashes || oversized {
            let id = staging_id(batch.target.load_id, chrono::Utc::now().naive_utc());
            let keyed = batch
                .target
                .staging
                .cloned()
                .filter(|_| !batch.target.builds && batch.mode.staged_key().is_some());
            let carried = match keyed {
                Some(_) => staged_as_text(batch.target.columns),
                None => batch.write_columns.to_vec(),
            };
            let held = held_types(&carried);
            let (staging, create) = SnowflakeSql.render_create_staging_table(
                &spec.destination,
                &held,
                &format!("{id}_hashed"),
            );
            let create = create.replacen("TRANSIENT TABLE", "TEMPORARY TABLE", 1);
            let columns = filled_columns(spec, (read, batch.live), &carried, literals);
            let bare = staged_as_text(&carried);
            let hashed: Vec<String> = spec
                .masked
                .columns
                .iter()
                .filter(|(_, method)| **method == MaskMethod::Hash)
                .map(|(name, _)| name.clone())
                .collect();
            let build = batch
                .target
                .staging
                .cloned()
                .filter(|_| batch.target.builds)
                .or(keyed);
            let mode = match (&build, batch.mode) {
                (Some(_), _) => &WriteMode::Append,
                (None, WriteMode::Replace | WriteMode::Recreate) => {
                    return Err(LoaderError::Invalid(format!(
                        "{} replaces the table but has no build table to load into; use load_method external_stage or an internal stage",
                        spec.destination.table
                    )));
                }
                (None, mode) => mode,
            };
            let target = build.as_ref().unwrap_or(&spec.destination);
            let write_into =
                |table: &TableName, mode: &WriteMode, hashed: &[String], from: &TableName| {
                    SnowflakeSql.render_commit(
                        &WriteStatementsRequest {
                            table,
                            columns: &bare,
                            mode,
                            hashed,
                            built: None,
                        },
                        from,
                        (None, true),
                    )
                };
            let drop = |table: &TableName| {
                format!(
                    "DROP TABLE IF EXISTS {}",
                    SnowflakeSql.render_qualified_name(table)
                )
            };
            let copies = copy(&staging, &columns)?;
            if copies
                .iter()
                .any(|statement| statement.len() > MAX_COPY_SQL_BYTES)
            {
                return Err(LoaderError::Invalid(format!(
                    "a batch of {} has a statement over Snowflake's size limit; load fewer files per batch",
                    spec.destination.table
                )));
            }
            let (delta, create_delta) = SnowflakeSql.render_create_staging_table(
                &spec.destination,
                &held,
                &format!("{id}_delta"),
            );
            let rows = self
                .run_hashed(HashedScript {
                    create,
                    copies,
                    write: write_into(target, mode, &hashed, &staging),
                    drop_staging: drop(&staging),
                    create_delta,
                    hash_into_delta: write_into(&delta, &WriteMode::Append, &hashed, &staging),
                    finish: write_into(target, mode, &[], &delta),
                    drop_delta: drop(&delta),
                })
                .await?;
            return Ok(Some(match build {
                Some(_) => DirectLoaded::Staged(rows),
                None => DirectLoaded::Committed(rows),
            }));
        }
        if let Some(mut copies) = direct_copies {
            let rows = match copies.len() {
                1 => copy_rows(&self.destination, &copies.remove(0)).await?,
                _ => {
                    let mut script = vec![BEGIN_STATEMENT.to_owned()];
                    script.append(&mut copies);
                    script.push(COMMIT_STATEMENT.to_owned());
                    script_copy_rows(&self.destination, &script).await?
                }
            };
            return Ok(Some(DirectLoaded::Committed(rows)));
        }
        let Some(staging) = batch.target.staging.cloned() else {
            return Ok(None);
        };
        let staged = staged_as_text(batch.target.columns);
        let carried = match batch.target.builds {
            true => batch.write_columns,
            false => &staged,
        };
        let columns = filled_columns(spec, (read, batch.live), carried, literals);
        let mut rows: u64 = 0;
        for statement in copy(&staging, &columns)? {
            rows += copy_rows(&self.destination, &statement).await?;
        }
        Ok(Some(DirectLoaded::Staged(rows)))
    }

    /// Run a hashed batch's statements: in one request when they fit, otherwise each group of `COPY`s in a request of its own, hashed into the delta table, which one last request writes from; the delta is dropped afterwards either way.
    async fn run_hashed(&self, script: HashedScript) -> Result<u64, LoaderError> {
        let whole: usize = [script.create.len(), script.drop_staging.len()]
            .into_iter()
            .chain(script.copies.iter().chain(&script.write).map(String::len))
            .sum();
        if whole <= MAX_SCRIPT_BYTES {
            let mut single = vec![script.create];
            single.extend(script.copies);
            single.extend(script.write);
            single.push(script.drop_staging);
            return Ok(script_copy_rows(&self.destination, &single).await?);
        }
        let requests = hashed_requests(&script);
        let loaded = async {
            script_copy_rows(
                &self.destination,
                std::slice::from_ref(&script.create_delta),
            )
            .await?;
            let mut rows: u64 = 0;
            for request in &requests {
                rows += script_copy_rows(&self.destination, request).await?;
            }
            script_copy_rows(&self.destination, &script.finish).await?;
            Ok::<u64, LoaderError>(rows)
        }
        .await;
        if let Err(error) =
            script_copy_rows(&self.destination, std::slice::from_ref(&script.drop_delta)).await
        {
            tracing::warn!(%error, "cannot drop a split batch's delta table; its masked columns are already hashed, and the next load drops it after a day");
        }
        loaded
    }
}

/// `script`'s `COPY`s grouped into requests within `MAX_SCRIPT_BYTES`: each creates the temporary table, runs its `COPY`s, hashes them into the delta table and drops the temporary table.
fn hashed_requests(script: &HashedScript) -> Vec<Vec<String>> {
    let fixed: usize = [script.create.len(), script.drop_staging.len()]
        .into_iter()
        .chain(script.hash_into_delta.iter().map(String::len))
        .sum();
    let mut requests: Vec<Vec<String>> = Vec::new();
    let mut size = usize::MAX;
    for statement in &script.copies {
        if size.saturating_add(statement.len()) > MAX_SCRIPT_BYTES {
            requests.push(vec![script.create.clone()]);
            size = fixed;
        }
        size += statement.len();
        if let Some(request) = requests.last_mut() {
            request.push(statement.clone());
        }
    }
    for request in &mut requests {
        request.extend(script.hash_into_delta.iter().cloned());
        request.push(script.drop_staging.clone());
    }
    requests
}

/// The columns a temporary or delta table holds a direct `COPY`'s values in: their own types, JSON as `VARIANT`, so writing them on casts nothing.
fn held_types(columns: &[DesiredColumn]) -> Vec<DesiredColumn> {
    columns
        .iter()
        .cloned()
        .map(|column| match column.column_type {
            ColumnType::Json => DesiredColumn {
                column_type: ColumnType::Native("VARIANT".to_owned()),
                ..column
            },
            _ => column,
        })
        .collect()
}

/// The columns a staging table holds, as the staged rows carry them: JSON and UUIDs as text; also the columns that write a table of `held_types` on with no cast.
fn staged_as_text(columns: &[DesiredColumn]) -> Vec<DesiredColumn> {
    columns
        .iter()
        .cloned()
        .map(|column| match column.column_type {
            ColumnType::Json | ColumnType::Uuid => DesiredColumn {
                column_type: ColumnType::Text,
                ..column
            },
            _ => column,
        })
        .collect()
}

/// What each of `columns` is filled with by a direct `COPY`: the files' columns (cut to microseconds where the `live` column holds fewer digits), then this load's metadata as literals, and NULL for a live column the files lack.
fn filled_columns(
    spec: &TableSpec,
    (read, live): (&FilesRead, Option<&[LiveColumn]>),
    columns: &[DesiredColumn],
    (load_id, loaded_at): (&str, &str),
) -> Vec<CopyColumn> {
    let from_files = copy_columns(spec, read);
    let mut filled: Vec<CopyColumn> = Vec::with_capacity(columns.len());
    for column in columns {
        let found = from_files.iter().find(|found| found.name == column.name);
        let short = live.into_iter().flatten().any(|live| {
            live.name == SnowflakeSql.stored_name(&column.name)
                && live
                    .fraction_digits
                    .is_some_and(|digits| digits < i64::from(MAX_FRACTION_DIGITS))
        });
        let value = match (found, metadata_role(spec.compat, &column.name)) {
            (
                Some(CopyColumn {
                    value:
                        CopyValue::File {
                            file_type, stored, ..
                        },
                    ..
                }),
                _,
            ) => CopyValue::File {
                file_type: file_type.clone(),
                stored: *stored,
                short,
            },
            (Some(found), _) => found.value.clone(),
            (None, Some(MetadataRole::LoadId)) => {
                CopyValue::Literal(SnowflakeSql.render_loader_value_literal(load_id))
            }
            (None, Some(MetadataRole::LoadedAt)) => CopyValue::Literal(format!(
                "CAST({} AS TIMESTAMP_TZ(6))",
                SnowflakeSql.render_loader_value_literal(loaded_at)
            )),
            (None, _) => CopyValue::Literal("NULL".to_owned()),
        };
        filled.push(CopyColumn {
            name: column.name.clone(),
            source: found.map_or_else(|| column.name.clone(), |found| found.source.clone()),
            column_type: column.column_type.clone(),
            value,
        });
    }
    filled
}