1use vortex_dtype::field::Field;
2use vortex_dtype::DType;
3use vortex_error::{vortex_bail, vortex_err, VortexResult};
4
5use self::projection::Projection;
6
7pub mod projection;
8
9#[derive(Clone, Debug)]
10pub struct Schema(pub(crate) DType);
11
12impl Schema {
13 pub fn new(schema_dtype: DType) -> Self {
14 Self(schema_dtype)
15 }
16
17 pub fn project(&self, projection: Projection) -> VortexResult<Self> {
18 match projection {
19 Projection::All => Ok(self.clone()),
20 Projection::Flat(fields) => {
21 let DType::Struct(s, n) = &self.0 else {
22 vortex_bail!("Can't project non struct types")
23 };
24 s.project(fields.as_ref())
25 .map(|p| Self(DType::Struct(p, *n)))
26 }
27 }
28 }
29
30 pub fn dtype(&self) -> &DType {
31 &self.0
32 }
33
34 pub fn field_type(&self, field: &Field) -> VortexResult<DType> {
35 let DType::Struct(s, _) = &self.0 else {
36 vortex_bail!("Can't project non struct types")
37 };
38
39 let idx = match field {
40 Field::Name(name) => s.find_name(name),
41 Field::Index(i) => Some(*i),
42 };
43
44 idx.and_then(|idx| s.dtypes().get(idx).cloned())
45 .ok_or_else(|| vortex_err!("Couldn't find field {field}"))
46 }
47}
48
49impl From<Schema> for DType {
50 fn from(value: Schema) -> Self {
51 value.0
52 }
53}