use std::sync::Arc;
use polars_core::schema::{Schema, SchemaRef};
use polars_plan::plans::hive::HivePartitionsDf;
pub fn resolve_projections(
final_output_schema: &Schema,
file_schema: &SchemaRef,
hive_parts: &mut Option<HivePartitionsDf>,
row_index_name: Option<&str>,
include_file_paths: Option<&str>,
) -> (SchemaRef, SchemaRef) {
if let Some(hive_parts) = hive_parts.as_mut() {
*hive_parts = hive_parts.filter_columns(final_output_schema)
}
let hive_schema = hive_parts.as_ref().map(|x| x.schema().as_ref());
let projected_file_schema: Schema = file_schema
.iter()
.filter_map(|(name, dtype)| {
let in_final = final_output_schema.contains(name);
let in_hive = hive_schema.is_some_and(|x| x.contains(name));
let is_row_index_col = row_index_name.is_some_and(|x| name == x);
let is_file_path_col = include_file_paths.is_some_and(|x| name == x);
(in_final && !(in_hive || is_file_path_col || is_row_index_col))
.then(|| (name.clone(), dtype.clone()))
})
.collect();
let mut full_file_schema = file_schema.clone();
if row_index_name.is_some_and(|x| full_file_schema.contains(x)) {
Arc::make_mut(&mut full_file_schema).shift_remove(row_index_name.unwrap());
}
if include_file_paths.is_some_and(|x| full_file_schema.contains(x)) {
Arc::make_mut(&mut full_file_schema).shift_remove(include_file_paths.unwrap());
}
(Arc::new(projected_file_schema), full_file_schema)
}