proof_of_sql/base/database/
arrow_schema_utility.rs

1//! This module provides utility functions for working with Arrow schemas in the context of Proof of SQL.
2//! It includes functionality to convert Arrow schemas to PoSQL-compatible formats.
3
4use alloc::sync::Arc;
5use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
6
7/// Converts an Arrow schema to a PoSQL-compatible schema.
8///
9/// This function takes an Arrow `SchemaRef` and returns a new `SchemaRef` where
10/// floating-point data types (Float16, Float32, Float64) are converted to Decimal256(75, 30).
11/// Other data types remain unchanged.
12///
13/// # Arguments
14///
15/// * `schema` - The input Arrow schema to convert.
16///
17/// # Returns
18///
19/// A new `SchemaRef` with PoSQL-compatible data types.
20#[must_use]
21pub fn get_posql_compatible_schema(schema: &SchemaRef) -> SchemaRef {
22    let new_fields: Vec<Field> = schema
23        .fields()
24        .iter()
25        .map(|field| {
26            let new_data_type = match field.data_type() {
27                DataType::Float16 | DataType::Float32 | DataType::Float64 => {
28                    DataType::Decimal256(20, 10)
29                }
30                _ => field.data_type().clone(),
31            };
32            Field::new(field.name(), new_data_type, field.is_nullable())
33        })
34        .collect();
35
36    Arc::new(Schema::new(new_fields))
37}