use std::sync::Arc;
use arrow_array::RecordBatch;
use parquet::arrow::ArrowSchemaConverter;
use parquet::basic::{ConvertedType, LogicalType, Type as PhysicalType};
use parquet::schema::types::{SchemaDescriptor, Type, TypePtr};
pub(crate) fn json_schema(
batch: &RecordBatch,
json: &[String],
) -> parquet::errors::Result<SchemaDescriptor> {
let converted = ArrowSchemaConverter::new().convert(&batch.schema())?;
let root = converted.root_schema();
let fields = (root.get_fields().iter())
.map(|field| json_field(field, json))
.collect::<parquet::errors::Result<Vec<TypePtr>>>()?;
Ok(SchemaDescriptor::new(Arc::new(
Type::group_type_builder(root.name())
.with_fields(fields)
.build()?,
)))
}
fn json_field(field: &TypePtr, json: &[String]) -> parquet::errors::Result<TypePtr> {
let text = field.is_primitive() && field.get_physical_type() == PhysicalType::BYTE_ARRAY;
if !(text && json.iter().any(|name| name == field.name())) {
return Ok(field.clone());
}
Ok(Arc::new(
Type::primitive_type_builder(field.name(), PhysicalType::BYTE_ARRAY)
.with_repetition(field.get_basic_info().repetition())
.with_logical_type(Some(LogicalType::Json))
.with_converted_type(ConvertedType::JSON)
.build()?,
))
}