rdf_fusion_encoding/object_id/
array.rs

1use crate::TermEncoding;
2use crate::encoding::EncodingArray;
3use crate::object_id::ObjectIdEncoding;
4use datafusion::arrow::array::{Array, ArrayRef, UInt32Array};
5use datafusion::common::exec_err;
6use rdf_fusion_model::DFResult;
7
8/// Represents an Arrow array with an [ObjectIdEncoding].
9#[derive(Debug, Clone)]
10pub struct ObjectIdArray {
11    encoding: ObjectIdEncoding,
12    inner: ArrayRef,
13}
14
15impl ObjectIdArray {
16    /// Tries to create a new [ObjectIdArray] from a regular [ArrayRef].
17    ///
18    /// # Errors
19    ///
20    /// Returns an error if the data type of `value` is unexpected.
21    pub fn try_new(encoding: ObjectIdEncoding, array: ArrayRef) -> DFResult<Self> {
22        if array.data_type() != &encoding.data_type() {
23            return exec_err!("Expected array with ObjectIdEncoding, got {:?}", array);
24        }
25        Ok(Self::new_unchecked(encoding, array))
26    }
27
28    /// Creates a new [ObjectIdArray] without checking invariants.
29    pub fn new_unchecked(encoding: ObjectIdEncoding, inner: ArrayRef) -> Self {
30        Self { encoding, inner }
31    }
32
33    /// Returns a reference to the inner [UInt32Array].
34    #[allow(clippy::expect_used)]
35    pub fn object_ids(&self) -> &UInt32Array {
36        self.inner
37            .as_any()
38            .downcast_ref::<UInt32Array>()
39            .expect("Checked in constructor")
40    }
41}
42
43impl EncodingArray for ObjectIdArray {
44    type Encoding = ObjectIdEncoding;
45
46    fn encoding(&self) -> &Self::Encoding {
47        &self.encoding
48    }
49
50    fn array(&self) -> &ArrayRef {
51        &self.inner
52    }
53
54    fn into_array(self) -> ArrayRef {
55        self.inner
56    }
57}