use crate::adapters::contract::types::SqlAdapter;
use crate::column_types::models::ColumnType;
use crate::schema_plan::models::DesiredColumn;
pub fn newest_by_problem(
adapter: &dyn SqlAdapter,
columns: &[DesiredColumn],
name: &str,
) -> Option<String> {
let written = columns
.iter()
.find(|column| column.name.eq_ignore_ascii_case(name));
match written.map(|column| &column.column_type) {
None => Some(format!(
"newest_by column {name} is not written to the table (not a column of the source, excluded, not in the enforced contract, or ignored by on_schema_change)"
)),
Some(column_type @ (ColumnType::Json | ColumnType::Array(_))) => Some(format!(
"newest_by column {name} is {column_type}, which has no order to say which row is newer; name a column of another type"
)),
Some(column_type @ ColumnType::Native(native)) => {
let (family, parameterised) = native_family(native);
(!adapter.orders_native(&family, parameterised)).then(|| {
format!(
"newest_by column {name} is {column_type}, which {} can't order to say which row is newer; pin a portable type for it in columns, or name another column",
adapter.warehouse()
)
})
}
Some(_) => None,
}
}
fn native_family(native: &str) -> (String, bool) {
let mut upper = native.trim().to_ascii_uppercase();
while let Some(inner) = ["NULLABLE(", "LOWCARDINALITY("]
.iter()
.find_map(|wrapper| upper.strip_prefix(wrapper))
{
upper = inner.strip_suffix(')').unwrap_or(inner).trim().to_owned();
}
let end = upper
.find(|c: char| !(c.is_ascii_alphanumeric() || c == '_'))
.unwrap_or(upper.len());
let parameterised = upper[end..].trim_start().starts_with(['(', '<']);
(upper[..end].to_owned(), parameterised)
}