1use std::fmt::Debug;
5use std::ops::Deref;
6use std::sync::Arc;
7
8use vortex_array::ArrayContext;
9use vortex_array::DeserializeMetadata;
10use vortex_array::SerializeMetadata;
11use vortex_dtype::DType;
12use vortex_error::VortexResult;
13use vortex_error::vortex_bail;
14use vortex_session::VortexSession;
15
16use crate::IntoLayout;
17use crate::Layout;
18use crate::LayoutChildType;
19use crate::LayoutEncoding;
20use crate::LayoutEncodingRef;
21use crate::LayoutId;
22use crate::LayoutReaderRef;
23use crate::LayoutRef;
24use crate::children::LayoutChildren;
25use crate::segments::SegmentId;
26use crate::segments::SegmentSource;
27
28pub trait VTable: 'static + Sized + Send + Sync + Debug {
29 type Layout: 'static + Send + Sync + Clone + Debug + Deref<Target = dyn Layout> + IntoLayout;
30 type Encoding: 'static + Send + Sync + Deref<Target = dyn LayoutEncoding>;
31 type Metadata: SerializeMetadata + DeserializeMetadata + Debug;
32
33 fn id(encoding: &Self::Encoding) -> LayoutId;
35
36 fn encoding(layout: &Self::Layout) -> LayoutEncodingRef;
38
39 fn row_count(layout: &Self::Layout) -> u64;
41
42 fn dtype(layout: &Self::Layout) -> &DType;
44
45 fn metadata(layout: &Self::Layout) -> Self::Metadata;
47
48 fn segment_ids(layout: &Self::Layout) -> Vec<SegmentId>;
50
51 fn nchildren(layout: &Self::Layout) -> usize;
53
54 fn child(layout: &Self::Layout, idx: usize) -> VortexResult<LayoutRef>;
56
57 fn child_type(layout: &Self::Layout, idx: usize) -> LayoutChildType;
59
60 fn new_reader(
62 layout: &Self::Layout,
63 name: Arc<str>,
64 segment_source: Arc<dyn SegmentSource>,
65 session: &VortexSession,
66 ) -> VortexResult<LayoutReaderRef>;
67
68 fn build(
70 encoding: &Self::Encoding,
71 dtype: &DType,
72 row_count: u64,
73 metadata: &<Self::Metadata as DeserializeMetadata>::Output,
74 segment_ids: Vec<SegmentId>,
75 children: &dyn LayoutChildren,
76 ctx: &ArrayContext,
77 ) -> VortexResult<Self::Layout>;
78
79 fn with_children(_layout: &mut Self::Layout, _children: Vec<LayoutRef>) -> VortexResult<()> {
84 vortex_bail!("with_children not implemented for this layout")
85 }
86}
87
88#[macro_export]
89macro_rules! vtable {
90 ($V:ident) => {
91 $crate::aliases::paste::paste! {
92 #[derive(Debug)]
93 pub struct [<$V VTable>];
94
95 impl AsRef<dyn $crate::Layout> for [<$V Layout>] {
96 fn as_ref(&self) -> &dyn $crate::Layout {
97 unsafe { &*(self as *const [<$V Layout>] as *const $crate::LayoutAdapter<[<$V VTable>]>) }
101 }
102 }
103
104 impl std::ops::Deref for [<$V Layout>] {
105 type Target = dyn $crate::Layout;
106
107 fn deref(&self) -> &Self::Target {
108 unsafe { &*(self as *const [<$V Layout>] as *const $crate::LayoutAdapter<[<$V VTable>]>) }
112 }
113 }
114
115 impl $crate::IntoLayout for [<$V Layout>] {
116 fn into_layout(self) -> $crate::LayoutRef {
117 std::sync::Arc::new(unsafe { std::mem::transmute::<[<$V Layout>], $crate::LayoutAdapter::<[<$V VTable>]>>(self) })
121 }
122 }
123
124 impl From<[<$V Layout>]> for $crate::LayoutRef {
125 fn from(value: [<$V Layout>]) -> $crate::LayoutRef {
126 use $crate::IntoLayout;
127 value.into_layout()
128 }
129 }
130
131 impl AsRef<dyn $crate::LayoutEncoding> for [<$V LayoutEncoding>] {
132 fn as_ref(&self) -> &dyn $crate::LayoutEncoding {
133 unsafe { &*(self as *const [<$V LayoutEncoding>] as *const $crate::LayoutEncodingAdapter<[<$V VTable>]>) }
137 }
138 }
139
140 impl std::ops::Deref for [<$V LayoutEncoding>] {
141 type Target = dyn $crate::LayoutEncoding;
142
143 fn deref(&self) -> &Self::Target {
144 unsafe { &*(self as *const [<$V LayoutEncoding>] as *const $crate::LayoutEncodingAdapter<[<$V VTable>]>) }
148 }
149 }
150 }
151 };
152}