quack_rs/arrow/converted.rs
1// SPDX-License-Identifier: MIT
2// Copyright 2026 Tom F. <https://github.com/tomtom215/>
3// My way of giving something small back to the open source community
4// and encouraging more Rust development!
5
6//! `ArrowConvertedSchema` — an Arrow schema translated into `DuckDB`'s own type descriptors.
7
8use libduckdb_sys::{duckdb_arrow_converted_schema, duckdb_destroy_arrow_converted_schema};
9
10use super::import_layout::Shape;
11use super::{ArrowConvertedSchema, ArrowSchema};
12
13impl ArrowConvertedSchema {
14 /// Takes ownership of a raw converted schema, recording the column count
15 /// and the shape of `schema`, the Arrow schema it was built from.
16 ///
17 /// # Safety
18 ///
19 /// - `raw` must be a non-null handle the caller is responsible for
20 /// destroying, and nobody else may destroy it.
21 /// - `raw` must have been built from `schema` (by
22 /// `duckdb_schema_from_arrow`). A different schema defeats the bounds and
23 /// layout checks in [`data_chunk_from_arrow`][super::data_chunk_from_arrow].
24 #[must_use]
25 pub unsafe fn from_raw(raw: duckdb_arrow_converted_schema, schema: &ArrowSchema) -> Self {
26 let column_count = schema.child_count();
27 let shapes = (0..column_count)
28 .filter_map(|i| schema.child(i).map(Shape::of))
29 .collect();
30 Self {
31 raw,
32 column_count,
33 shapes,
34 }
35 }
36
37 /// The shape of each column's Arrow schema.
38 pub(super) fn shapes(&self) -> &[Shape] {
39 &self.shapes
40 }
41
42 /// The raw handle, still owned by this value.
43 #[inline]
44 #[must_use]
45 pub const fn as_raw(&self) -> duckdb_arrow_converted_schema {
46 self.raw
47 }
48
49 /// How many columns this schema describes.
50 #[inline]
51 #[must_use]
52 pub const fn column_count(&self) -> usize {
53 self.column_count
54 }
55}
56
57impl Drop for ArrowConvertedSchema {
58 #[cfg_attr(test, mutants::skip)] // frees a DuckDB handle; nothing observable without a runtime
59 fn drop(&mut self) {
60 if self.raw.is_null() {
61 return;
62 }
63 // SAFETY: `self.raw` was owned by this value and is destroyed once;
64 // DuckDB nulls it.
65 unsafe { duckdb_destroy_arrow_converted_schema(&raw mut self.raw) };
66 }
67}
68
69impl core::fmt::Debug for ArrowConvertedSchema {
70 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
71 f.debug_struct("ArrowConvertedSchema")
72 .field("raw", &self.raw)
73 .field("column_count", &self.column_count)
74 .field("shapes", &self.shapes)
75 .finish()
76 }
77}