|
| 1 | +//! Align supplied columns to their authoritative schema without rendering adapter SQL. |
| 2 | +
|
| 3 | +use std::collections::{BTreeSet, HashMap, HashSet}; |
| 4 | + |
| 5 | +use crate::planner_fixtures::_helpers::schema_order::supplied_in_schema_order; |
| 6 | +use crate::planner_fixtures::models::{Alignment, AlignmentInput, RequiredColumn}; |
| 7 | + |
| 8 | +/// Resolve missing columns, diagnostics and star-prefix ordering for one fixture. |
| 9 | +pub fn align_columns(label: &str, input: AlignmentInput<'_>) -> Alignment { |
| 10 | + let missing = missing_columns(&input); |
| 11 | + if missing.is_empty() && !input.typed_nulls { |
| 12 | + return Alignment::default(); |
| 13 | + } |
| 14 | + let metadata: HashMap<&str, usize> = input |
| 15 | + .metadata |
| 16 | + .iter() |
| 17 | + .enumerate() |
| 18 | + .map(|(index, column)| (column.folded_name.as_str(), index)) |
| 19 | + .collect(); |
| 20 | + let mut unknown: Vec<&str> = Vec::new(); |
| 21 | + let mut non_nullable: Vec<&str> = Vec::new(); |
| 22 | + let mut readers: BTreeSet<&str> = BTreeSet::new(); |
| 23 | + let mut completed: Vec<usize> = Vec::new(); |
| 24 | + for required in missing { |
| 25 | + match metadata.get(required.folded_name.as_str()).copied() { |
| 26 | + Some(index) if input.metadata[index].sql_type.is_some() => { |
| 27 | + if input.metadata[index].nullable == Some(false) { |
| 28 | + non_nullable.push(required.name.as_str()); |
| 29 | + } else { |
| 30 | + completed.push(index); |
| 31 | + } |
| 32 | + } |
| 33 | + _ => { |
| 34 | + unknown.push(required.name.as_str()); |
| 35 | + readers.extend(required.readers.iter().map(String::as_str)); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + let diagnostics = missing_diagnostics(label, &unknown, &non_nullable, readers); |
| 40 | + if !diagnostics.is_empty() { |
| 41 | + return Alignment { |
| 42 | + diagnostics, |
| 43 | + completed: None, |
| 44 | + }; |
| 45 | + } |
| 46 | + if input.star && !supplied_in_schema_order(&input) { |
| 47 | + return Alignment { |
| 48 | + diagnostics: vec![format!( |
| 49 | + "{label} supplies columns out of authoritative order for a SELECT * closure; provide the leading relation columns in schema order" |
| 50 | + )], |
| 51 | + completed: None, |
| 52 | + }; |
| 53 | + } |
| 54 | + completed.sort_unstable(); |
| 55 | + Alignment { |
| 56 | + diagnostics, |
| 57 | + completed: Some(completed), |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +fn missing_columns<'a>(input: &AlignmentInput<'a>) -> Vec<&'a RequiredColumn> { |
| 62 | + let supplied: HashSet<&str> = input |
| 63 | + .supplied |
| 64 | + .iter() |
| 65 | + .map(|column| column.folded_name.as_str()) |
| 66 | + .collect(); |
| 67 | + let mut missing: Vec<_> = input |
| 68 | + .required |
| 69 | + .iter() |
| 70 | + .filter(|column| !supplied.contains(column.folded_name.as_str())) |
| 71 | + .collect(); |
| 72 | + missing.sort_by(|left, right| left.name.cmp(&right.name)); |
| 73 | + missing |
| 74 | +} |
| 75 | + |
| 76 | +fn missing_diagnostics( |
| 77 | + label: &str, |
| 78 | + unknown: &[&str], |
| 79 | + non_nullable: &[&str], |
| 80 | + readers: BTreeSet<&str>, |
| 81 | +) -> Vec<String> { |
| 82 | + let mut diagnostics: Vec<String> = Vec::new(); |
| 83 | + if !unknown.is_empty() { |
| 84 | + diagnostics.push(format!( |
| 85 | + "{label} is missing required columns: {}; provide explicit values because their types are not authoritative (read by: {})", |
| 86 | + unknown.join(", "), readers.into_iter().collect::<Vec<_>>().join(", ") |
| 87 | + )); |
| 88 | + } |
| 89 | + if !non_nullable.is_empty() { |
| 90 | + diagnostics.push(format!( |
| 91 | + "{label} must provide required non-nullable columns: {}", |
| 92 | + non_nullable.join(", ") |
| 93 | + )); |
| 94 | + } |
| 95 | + diagnostics |
| 96 | +} |
0 commit comments