use crate::batch_plan::_helpers::bounds::{integer_plan, time_plan};
use crate::batch_plan::constants::MAX_WINDOWS;
use crate::batch_plan::models::{WindowPlan, WindowSplit};
use crate::column_types::models::ColumnType;
use crate::project::models::BatchStep;
pub fn split_windows(split: &WindowSplit<'_>) -> Result<WindowPlan, String> {
let Some(from) = split.from else {
return Err(
"batch_size needs a start to split the cursor range: set cursor_start for the first load"
.to_owned(),
);
};
let plan = match (split.step, split.cursor_type) {
(BatchStep::Integer(step), Some(ColumnType::Int | ColumnType::SmallInt) | None) => {
integer_plan(split, from, step)?
}
(BatchStep::Integer(_), Some(other)) => {
return Err(format!(
"an integer batch needs an integer cursor, but the cursor is {other}; give a duration such as \"1d\""
));
}
(BatchStep::Duration(step), cursor_type) => time_plan(split, from, step, cursor_type)?,
(BatchStep::Whole, _) => {
return Err("batch_size = \"none\" reads in one piece; it has no windows".to_owned());
}
};
if plan.count > MAX_WINDOWS {
return Err(format!(
"batch_size splits the read into {} windows, more than {MAX_WINDOWS}; use a larger batch_size, or narrow the read with --start-cursor/--end-cursor",
plan.count
));
}
Ok(plan)
}