sqlload 0.58.0

SQL data loading tooling.
//! The columns a direct `COPY` of a files table fills.

use crate::adapters::snowflake::models::{CopyColumn, CopyValue};
use crate::column_types::models::ColumnType;
use crate::files_source::constants::{
    SOURCE_FILE_COLUMN, SOURCE_ROW_COLUMN, SOURCE_VERSION_COLUMN,
};
use crate::files_source::models::{FilesRead, StoredRead};
use crate::loader::models::TableSpec;

/// The columns a direct `COPY` of a files table fills, in the order the files source reads them, each under its name in the table, from the files (by their name for it), its path, or the file's name, version and row number.
pub(crate) fn copy_columns(spec: &TableSpec, read: &FilesRead) -> Vec<CopyColumn> {
    read.columns
        .iter()
        .map(|column| CopyColumn {
            name: spec.source_table.loaded_as(&column.name).to_owned(),
            source: column.name.clone(),
            column_type: column.column_type.clone(),
            value: match column.name.as_str() {
                SOURCE_FILE_COLUMN => CopyValue::SourceFile,
                SOURCE_VERSION_COLUMN => CopyValue::SourceVersion,
                SOURCE_ROW_COLUMN => CopyValue::SourceRow,
                name if partition_column(spec, name) => CopyValue::Partition,
                name => CopyValue::File {
                    file_type: file_type(spec, name),
                    stored: stored_read(spec, name),
                    short: false,
                },
            },
        })
        .collect()
}

/// Whether `name` is one of the files source's partition columns.
pub(crate) fn partition_column(spec: &TableSpec, name: &str) -> bool {
    spec.files
        .iter()
        .flat_map(|files| files.partition_types.iter())
        .any(|(partition, _)| partition == name)
}

/// How a stage read returns column `name` of the known files.
fn stored_read(spec: &TableSpec, name: &str) -> StoredRead {
    spec.files
        .iter()
        .flat_map(|files| files.table.columns.iter())
        .find(|found| found.name == name)
        .map(|found| found.stored_read)
        .unwrap_or_default()
}

/// The type the known files hold column `name` as; a type with no portable equivalent is named as one the direct `COPY` never loads.
pub(crate) fn file_type(spec: &TableSpec, name: &str) -> Option<ColumnType> {
    let files = spec.files.as_ref()?;
    let found = files
        .table
        .columns
        .iter()
        .find(|found| found.name == name)?;
    Some(match &found.column_type {
        Ok(column_type) => column_type.clone(),
        Err(reason) => ColumnType::Native(format!("unsupported ({reason})")),
    })
}