use crate::adapters::contract::main::_rebuilt_in_place::rebuilt_in_place;
use crate::adapters::contract::models::TableName;
use crate::adapters::contract::types::SqlAdapter;
use crate::adapters::duckdb::_helpers::type_names::{logical_type, portable_type};
use crate::adapters::duckdb::models::DuckDbSql;
use crate::column_types::constants::VARIANT_TYPE;
use crate::column_types::main::is_variant::is_variant;
use crate::column_types::models::{ColumnType, LiveColumnType, Warehouse, WarehouseType};
use crate::schema_plan::models::{ColumnChange, LiveColumn};
impl SqlAdapter for DuckDbSql {
fn merges_files(&self) -> bool {
true
}
fn warehouse(&self) -> Warehouse {
Warehouse::DuckDb
}
fn memory_note(&self) -> Option<&'static str> {
Some(
"DuckDB holds a commit's rows in memory until a row group fills, so a table of few, wide rows peaks at about 4x its largest commit's bytes, plus DuckDB's cache up to its memory_limit",
)
}
fn max_identifier_length(&self) -> Option<usize> {
Some(65536)
}
fn marks_deleted_rows(&self) -> bool {
true
}
fn keeps_nanoseconds(&self) -> bool {
false
}
fn render_loader_logical_type(&self, column_type: &ColumnType) -> WarehouseType {
logical_type(column_type)
}
fn live_type(&self, live: &LiveColumnType) -> ColumnType {
portable_type(live).unwrap_or_else(|| ColumnType::Native(live.data_type.clone()))
}
fn widens_in_place(&self, _live: &ColumnType, _target: &ColumnType) -> bool {
true
}
fn parses_variant_pins(&self) -> bool {
true
}
fn render_source_expression_cast(&self, name: &str, column_type: &ColumnType) -> String {
let column = self.render_identifier(name);
match column_type {
variant if is_variant(variant) => {
format!("CAST(CAST({column} AS JSON) AS {VARIANT_TYPE})")
}
ColumnType::Json | ColumnType::Uuid => format!(
"CAST({column} AS {})",
self.render_loader_logical_type(column_type).sql
),
_ => column,
}
}
fn render_change_columns(
&self,
table: &TableName,
changes: &[ColumnChange],
live: &[LiveColumn],
) -> Vec<String> {
rebuilt_in_place(self, table, changes, live)
}
fn render_schema_columns(&self, _table: &TableName) -> String {
"(SELECT table_schema, table_name, column_name FROM information_schema.columns WHERE table_catalog = current_database())".to_owned()
}
fn render_staging_filter(&self, suffix: &str) -> String {
format!(
"table_catalog = current_database() AND table_name ILIKE {}",
self.render_loader_value_literal(&format!("%{suffix}%"))
)
}
}