use crate::adapters::contract::main::_rebuilt_in_place::rebuilt_in_place;
use crate::adapters::contract::models::{SessionRows, TableName};
use crate::adapters::contract::types::SqlAdapter;
use crate::adapters::duckdb::_helpers::type_names::{logical_type, portable_type};
use crate::adapters::duckdb::constants::DESCRIBE_TABLE_SQL;
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 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 deletes_superseded_rows(&self) -> 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}%"))
)
}
fn session_rows(&self) -> SessionRows {
SessionRows::LocalParquet
}
fn render_describe_table(&self, table: &TableName, _database: Option<&str>) -> Option<String> {
let mut parts = DESCRIBE_TABLE_SQL.split('?');
let (head, middle, tail) = (parts.next()?, parts.next()?, parts.next()?);
Some(format!(
"{head}{}{middle}{}{tail}",
self.render_loader_value_literal(&table.schema),
self.render_loader_value_literal(&table.table)
))
}
fn render_insert_local_parquet(
&self,
table: &TableName,
names: &[String],
file: &str,
) -> Option<String> {
let list: Vec<String> = names
.iter()
.map(|name| self.render_identifier(name))
.collect();
Some(format!(
"INSERT INTO {} ({list}) SELECT {list} FROM read_parquet({})",
self.render_qualified_name(table),
self.render_loader_value_literal(file),
list = list.join(", ")
))
}
}