use std::sync::Arc;
use arrow::datatypes::{DataType, Field, Schema};
use prost::Message;
use crate::StorageError;
use crate::StorageResult;
use crate::lancefmt::pb::file;
pub(crate) const PLAIN_ENCODING: i32 = 1;
const FSL_CHILD_NULLABLE: &str = "genegraph-storage:fsl-child-nullable";
pub(crate) fn logical_type_name(dt: &DataType) -> StorageResult<String> {
match dt {
DataType::Float64 => Ok("double".to_string()),
DataType::Float32 => Ok("float".to_string()),
DataType::UInt8 => Ok("uint8".to_string()),
DataType::UInt32 => Ok("uint32".to_string()),
DataType::UInt64 => Ok("uint64".to_string()),
DataType::Int64 => Ok("int64".to_string()),
DataType::FixedSizeList(child, n) => match child.data_type() {
DataType::Float64 => Ok(format!("fixed_size_list:double:{n}")),
DataType::Float32 => Ok(format!("fixed_size_list:float:{n}")),
other => Err(StorageError::UnsupportedFormat(format!(
"unsupported fixed_size_list item type for lancefmt: {other:?}"
))),
},
other => Err(StorageError::UnsupportedFormat(format!(
"unsupported Arrow type for lancefmt: {other:?}"
))),
}
}
fn parse_logical_type(name: &str) -> StorageResult<DataType> {
match name {
"double" => Ok(DataType::Float64),
"float" => Ok(DataType::Float32),
"uint8" => Ok(DataType::UInt8),
"uint32" => Ok(DataType::UInt32),
"uint64" => Ok(DataType::UInt64),
"int64" => Ok(DataType::Int64),
_ => {
let fsl = match name.strip_prefix("fixed_size_list:double:") {
Some(rest) => Some((rest, DataType::Float64)),
None => name
.strip_prefix("fixed_size_list:float:")
.map(|rest| (rest, DataType::Float32)),
};
if let Some((rest, item)) = fsl {
let dim: i32 = rest.parse().map_err(|_| {
StorageError::UnsupportedFormat(format!("bad fixed_size_list dim: {name}"))
})?;
return Ok(DataType::FixedSizeList(
Arc::new(Field::new("item", item, true)),
dim,
));
}
Err(StorageError::UnsupportedFormat(format!(
"unsupported Lance logical type: {name}"
)))
}
}
}
pub(crate) fn to_lance_schema(schema: &Schema) -> StorageResult<(Vec<file::Field>, SchemaMeta)> {
let mut fields = Vec::with_capacity(schema.fields().len());
for (id, f) in schema.fields().iter().enumerate() {
if f.is_nullable() {
return Err(StorageError::UnsupportedFormat(format!(
"lancefmt writer requires non-nullable top-level fields; '{}' is nullable",
f.name()
)));
}
if f.metadata().contains_key(FSL_CHILD_NULLABLE) {
return Err(StorageError::Invalid(format!(
"reserved field metadata key: {FSL_CHILD_NULLABLE}"
)));
}
let mut metadata = SchemaMeta::new();
if let DataType::FixedSizeList(child, _) = f.data_type() {
metadata.insert(
FSL_CHILD_NULLABLE.to_string(),
child.is_nullable().to_string().into_bytes(),
);
}
fields.push(file::Field {
r#type: 0,
name: f.name().clone(),
id: id as i32,
parent_id: -1,
logical_type: logical_type_name(f.data_type())?,
nullable: false,
metadata,
unenforced_primary_key: false,
unenforced_primary_key_position: 0,
unenforced_clustering_key: false,
unenforced_clustering_key_position: 0,
encoding: PLAIN_ENCODING,
dictionary: None,
extension_name: String::new(),
});
}
let meta: SchemaMeta = schema
.metadata()
.iter()
.map(|(k, v)| (k.clone(), v.clone().into_bytes()))
.collect();
Ok((fields, meta))
}
pub(crate) type SchemaMeta = std::collections::HashMap<String, Vec<u8>>;
pub(crate) fn from_lance_fields(
fields: &[file::Field],
metadata: &SchemaMeta,
) -> StorageResult<Schema> {
let mut arrow_fields = Vec::with_capacity(fields.len());
for f in fields {
if f.parent_id != -1 {
return Err(StorageError::UnsupportedFormat(format!(
"lancefmt reader only supports top-level fields; '{}' has parent_id {}",
f.name, f.parent_id
)));
}
let mut dt = parse_logical_type(&f.logical_type)?;
if let Some(value) = f.metadata.get(FSL_CHILD_NULLABLE) {
let nullable = match value.as_slice() {
b"true" => true,
b"false" => false,
_ => {
return Err(StorageError::Invalid(format!(
"invalid {FSL_CHILD_NULLABLE} on field '{}'",
f.name
)));
}
};
let DataType::FixedSizeList(child, dim) = dt else {
return Err(StorageError::Invalid(format!(
"{FSL_CHILD_NULLABLE} requires a fixed-size list on field '{}'",
f.name
)));
};
dt = DataType::FixedSizeList(
Arc::new(child.as_ref().clone().with_nullable(nullable)),
dim,
);
}
arrow_fields.push(Field::new(&f.name, dt, f.nullable));
}
let meta = metadata
.iter()
.map(|(k, v)| {
let s = String::from_utf8(v.clone())
.map_err(|_| StorageError::Invalid("schema metadata is not UTF-8".into()))?;
Ok((k.clone(), s))
})
.collect::<StorageResult<std::collections::HashMap<_, _>>>()?;
Ok(Schema::new_with_metadata(arrow_fields, meta))
}
pub(crate) fn encode_schema_global(
fields: &[file::Field],
meta: &SchemaMeta,
num_rows: u64,
) -> Vec<u8> {
let descriptor = file::FileDescriptor {
schema: Some(file::Schema {
fields: fields.to_vec(),
metadata: meta.clone(),
}),
length: num_rows,
};
descriptor.encode_to_vec()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_malformed_child_nullability() {
let field = file::Field {
name: "vector".into(),
parent_id: -1,
logical_type: "fixed_size_list:double:8".into(),
metadata: [(FSL_CHILD_NULLABLE.into(), b"invalid".to_vec())].into(),
..Default::default()
};
assert!(matches!(
from_lance_fields(&[field], &SchemaMeta::new()),
Err(StorageError::Invalid(message)) if message.contains(FSL_CHILD_NULLABLE)
));
}
#[test]
fn rejects_child_nullability_on_scalar_fields() {
let field = file::Field {
name: "value".into(),
parent_id: -1,
logical_type: "double".into(),
metadata: [(FSL_CHILD_NULLABLE.into(), b"false".to_vec())].into(),
..Default::default()
};
assert!(matches!(
from_lance_fields(&[field], &SchemaMeta::new()),
Err(StorageError::Invalid(message)) if message.contains("fixed-size list")
));
}
#[test]
fn rejects_user_child_nullability_stamp() {
let field = Field::new(
"vector",
DataType::FixedSizeList(Arc::new(Field::new("item", DataType::Float64, false)), 8),
false,
)
.with_metadata([(FSL_CHILD_NULLABLE.to_string(), "true".to_string())]);
assert!(matches!(
to_lance_schema(&Schema::new(vec![field])),
Err(StorageError::Invalid(message)) if message.contains("reserved field metadata")
));
}
}