typed_arrow_dyn/
schema.rs

1//! Runtime Arrow Schema wrapper.
2
3use std::sync::Arc;
4
5use arrow_array::RecordBatch;
6use arrow_schema::{Schema, SchemaRef};
7
8use crate::{DynRowView, DynRowViews, DynViewError};
9
10/// A runtime Arrow schema wrapper used by the unified facade.
11#[derive(Debug, Clone)]
12pub struct DynSchema {
13    /// The underlying `arrow_schema::SchemaRef`.
14    pub schema: SchemaRef,
15}
16
17impl DynSchema {
18    /// Construct from owned `Schema`.
19    #[must_use]
20    pub fn new(schema: Schema) -> Self {
21        Self {
22            schema: Arc::new(schema),
23        }
24    }
25
26    /// Construct from an existing `SchemaRef`.
27    #[must_use]
28    pub fn from_ref(schema: SchemaRef) -> Self {
29        Self { schema }
30    }
31
32    /// Create a dynamic row view iterator over `batch`, validating shapes first.
33    ///
34    /// # Errors
35    /// Returns `DynViewError` if the batch schema does not match this schema.
36    pub fn iter_views<'a>(
37        &'a self,
38        batch: &'a RecordBatch,
39    ) -> Result<DynRowViews<'a>, DynViewError> {
40        crate::view::DynRowViews::new(batch, self.schema.as_ref())
41    }
42
43    /// Borrow a single row from `batch` at `row` as a dynamic view.
44    ///
45    /// # Errors
46    /// Returns `DynViewError` if the batch schema mismatches this schema or if the
47    /// requested row index is out of bounds.
48    pub fn view_at<'a>(
49        &'a self,
50        batch: &'a RecordBatch,
51        row: usize,
52    ) -> Result<DynRowView<'a>, DynViewError> {
53        crate::view::view_batch_row(self, batch, row)
54    }
55}