1#[cfg(feature = "vectordata")]
26use std::sync::Arc;
27
28use crate::ast::PortType;
29
30#[cfg(feature = "vectordata")]
31pub use self::types::{PredicateForms, PredicateFragment, RecordForms, RecordFragment};
32#[cfg(feature = "vectordata")]
33pub use self::wires::Shared;
34
35#[derive(Debug, Clone)]
37pub enum DatasetValue {
38 #[cfg(feature = "vectordata")]
42 AdmittedRows(Arc<vectordata::ground_truth::OrdinalSet>),
43 #[cfg(feature = "vectordata")]
45 PredicateForms(Arc<PredicateForms>),
46 #[cfg(feature = "vectordata")]
48 RecordForms(Arc<RecordForms>),
49 #[cfg(feature = "vectordata")]
51 Predicate(PredicateFragment),
52 #[cfg(feature = "vectordata")]
54 Record(RecordFragment),
55}
56
57impl DatasetValue {
58 pub fn port_type(&self) -> PortType {
60 match self {
61 #[cfg(feature = "vectordata")]
62 DatasetValue::AdmittedRows(_) => PortType::AdmittedRows,
63 #[cfg(feature = "vectordata")]
64 DatasetValue::PredicateForms(_) | DatasetValue::RecordForms(_) => PortType::Forms,
65 #[cfg(feature = "vectordata")]
66 DatasetValue::Predicate(_) | DatasetValue::Record(_) => PortType::Fragment,
67 #[cfg(not(feature = "vectordata"))]
68 _ => match *self {},
69 }
70 }
71
72 pub fn display(&self) -> String {
76 match self {
77 #[cfg(feature = "vectordata")]
78 DatasetValue::AdmittedRows(set) => format!("{set:?}"),
79 #[cfg(feature = "vectordata")]
80 DatasetValue::PredicateForms(forms) => format!(
81 "forms of {}: {} over {} predicates",
82 forms.origin,
83 forms.census.forms().len(),
84 forms.census.count()
85 ),
86 #[cfg(feature = "vectordata")]
87 DatasetValue::RecordForms(forms) => format!(
88 "forms of {}: 1 over {} fields",
89 forms.origin,
90 forms.layout.names().len()
91 ),
92 #[cfg(feature = "vectordata")]
93 DatasetValue::Predicate(fragment) => format!(
94 "predicate {} of {}, form {}",
95 fragment.ordinal, fragment.forms.origin, fragment.form
96 ),
97 #[cfg(feature = "vectordata")]
98 DatasetValue::Record(fragment) => {
99 format!("record {} of {}", fragment.ordinal, fragment.forms.origin)
100 }
101 #[cfg(not(feature = "vectordata"))]
102 _ => match *self {},
103 }
104 }
105}
106
107impl PartialEq for DatasetValue {
112 fn eq(&self, other: &Self) -> bool {
113 match (self, other) {
114 #[cfg(feature = "vectordata")]
115 (DatasetValue::AdmittedRows(a), DatasetValue::AdmittedRows(b)) => {
116 Arc::ptr_eq(a, b) || a == b
117 }
118 #[cfg(feature = "vectordata")]
119 (DatasetValue::PredicateForms(a), DatasetValue::PredicateForms(b)) => Arc::ptr_eq(a, b),
120 #[cfg(feature = "vectordata")]
121 (DatasetValue::RecordForms(a), DatasetValue::RecordForms(b)) => Arc::ptr_eq(a, b),
122 #[cfg(feature = "vectordata")]
123 (DatasetValue::Predicate(a), DatasetValue::Predicate(b)) => {
124 Arc::ptr_eq(&a.forms, &b.forms) && a.form == b.form && a.ordinal == b.ordinal
125 }
126 #[cfg(feature = "vectordata")]
127 (DatasetValue::Record(a), DatasetValue::Record(b)) => {
128 Arc::ptr_eq(&a.forms, &b.forms) && a.ordinal == b.ordinal
129 }
130 #[cfg(feature = "vectordata")]
131 _ => false,
132 #[cfg(not(feature = "vectordata"))]
133 _ => match *self {},
134 }
135 }
136}
137
138#[cfg(feature = "vectordata")]
139mod types {
140 use std::sync::Arc;
141
142 use vectordata::binding::{Binder, Layout};
143 use vectordata::predicate_forms::FormCensus;
144 use vectordata::records::RecordFacet;
145
146 #[non_exhaustive]
159 pub struct PredicateForms {
160 pub census: FormCensus,
164 pub layout: Layout,
167 pub predicates: RecordFacet,
170 pub origin: String,
172 }
173
174 impl PredicateForms {
175 pub(crate) fn new(
176 census: FormCensus,
177 layout: Layout,
178 predicates: RecordFacet,
179 origin: String,
180 ) -> Self {
181 Self {
182 census,
183 layout,
184 predicates,
185 origin,
186 }
187 }
188 }
189
190 impl std::fmt::Debug for PredicateForms {
191 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
192 f.debug_struct("PredicateForms")
193 .field("origin", &self.origin)
194 .field("forms", &self.census.forms().len())
195 .field("predicates", &self.census.count())
196 .finish()
197 }
198 }
199
200 #[non_exhaustive]
204 pub struct RecordForms {
205 pub layout: Layout,
207 pub binder: Binder,
209 pub records: RecordFacet,
211 pub origin: String,
213 }
214
215 impl RecordForms {
216 pub(crate) fn new(layout: Layout, records: RecordFacet, origin: String) -> Self {
217 Self {
218 binder: Binder::all(&layout),
219 layout,
220 records,
221 origin,
222 }
223 }
224 }
225
226 impl std::fmt::Debug for RecordForms {
227 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
228 f.debug_struct("RecordForms")
229 .field("origin", &self.origin)
230 .field("fields", &self.layout.names())
231 .finish()
232 }
233 }
234
235 #[derive(Debug, Clone)]
243 #[non_exhaustive]
244 pub struct PredicateFragment {
245 pub forms: Arc<PredicateForms>,
247 pub form: u32,
249 pub ordinal: u64,
251 }
252
253 impl PredicateFragment {
254 pub(crate) fn new(forms: Arc<PredicateForms>, form: u32, ordinal: u64) -> Self {
255 Self {
256 forms,
257 form,
258 ordinal,
259 }
260 }
261 }
262
263 #[derive(Debug, Clone)]
269 #[non_exhaustive]
270 pub struct RecordFragment {
271 pub forms: Arc<RecordForms>,
273 pub ordinal: u64,
275 }
276
277 impl RecordFragment {
278 pub(crate) fn new(forms: Arc<RecordForms>, ordinal: u64) -> Self {
279 Self { forms, ordinal }
280 }
281 }
282}
283
284#[cfg(feature = "vectordata")]
296pub mod vectordata {
297 pub mod formats {
299 pub mod anode {
301 pub use ::vectordata::formats::anode::{ANode, decode, encode};
302 }
303 pub mod mnode {
305 pub use ::vectordata::formats::mnode::{MNode, MValue, TypeTag};
306 pub mod scan {
308 pub use ::vectordata::formats::mnode::scan::Field;
309 }
310 }
311 pub mod pnode {
314 pub use ::vectordata::formats::pnode::{
315 Comparand, ConjugateNode, ConjugateType, FieldRef, OpType, PNode, PredicateNode,
316 };
317 pub mod template {
320 pub use ::vectordata::formats::pnode::template::{
321 PredicateTemplate, parse_template,
322 };
323 }
324 }
325 }
326 pub mod binding {
328 pub use ::vectordata::binding::{
329 BindError, BindType, Binder, Condition, Layout, LayoutSource, Nullability,
330 PredicateBinder,
331 };
332 }
333 pub mod predicate_forms {
335 pub use ::vectordata::predicate_forms::{
336 Access, FormCensus, PredicateForm, PredicateStrata,
337 };
338 }
339 pub mod records {
341 pub use ::vectordata::records::{
342 RecordCodec, RecordError, RecordFacet, SharedBytes, codec_by_name, codec_names,
343 };
344 }
345 pub mod ground_truth {
347 pub use ::vectordata::ground_truth::OrdinalSet;
348 }
349}
350
351#[cfg(feature = "vectordata")]
352mod wires {
353 use std::sync::Arc;
354
355 use vectordata::ground_truth::OrdinalSet;
356
357 use super::{DatasetValue, PredicateForms, PredicateFragment, RecordForms, RecordFragment};
358 use crate::ast::{PortType, Value};
359 use crate::derive_support::{Wire, extract_mismatch};
360
361 #[derive(Debug)]
369 pub struct Shared<T>(pub Arc<T>);
370
371 impl<T> Clone for Shared<T> {
372 fn clone(&self) -> Self {
373 Shared(self.0.clone())
374 }
375 }
376
377 impl<T> std::ops::Deref for Shared<T> {
378 type Target = T;
379 fn deref(&self) -> &T {
380 &self.0
381 }
382 }
383
384 fn wrong_kind(wanted: &str, got: &DatasetValue) -> ! {
389 let got = match got {
390 DatasetValue::AdmittedRows(_) => "the rows a predicate admits",
391 DatasetValue::PredicateForms(_) => "the forms of a predicate facet",
392 DatasetValue::RecordForms(_) => "the forms of a record facet",
393 DatasetValue::Predicate(_) => "a predicate fragment",
394 DatasetValue::Record(_) => "a record fragment",
395 };
396 panic!("expected {wanted}, got {got}")
397 }
398
399 impl Wire for Shared<OrdinalSet> {
401 const PORT: PortType = PortType::AdmittedRows;
402 fn extract(v: &Value) -> Self {
403 match v {
404 Value::Dataset(DatasetValue::AdmittedRows(set)) => Shared(set.clone()),
405 other => extract_mismatch("Shared<OrdinalSet>", Self::PORT, other),
406 }
407 }
408 fn inject(self) -> Value {
409 Value::Dataset(DatasetValue::AdmittedRows(self.0))
410 }
411 }
412
413 impl Wire for Shared<PredicateForms> {
415 const PORT: PortType = PortType::Forms;
416 fn extract(v: &Value) -> Self {
417 match v {
418 Value::Dataset(DatasetValue::PredicateForms(forms)) => Shared(forms.clone()),
419 Value::Dataset(d @ DatasetValue::RecordForms(_)) => {
420 wrong_kind("the forms of a predicate facet", d)
421 }
422 other => extract_mismatch("Shared<PredicateForms>", Self::PORT, other),
423 }
424 }
425 fn inject(self) -> Value {
426 Value::Dataset(DatasetValue::PredicateForms(self.0))
427 }
428 }
429
430 impl Wire for Shared<RecordForms> {
432 const PORT: PortType = PortType::Forms;
433 fn extract(v: &Value) -> Self {
434 match v {
435 Value::Dataset(DatasetValue::RecordForms(forms)) => Shared(forms.clone()),
436 Value::Dataset(d @ DatasetValue::PredicateForms(_)) => {
437 wrong_kind("the forms of a record facet", d)
438 }
439 other => extract_mismatch("Shared<RecordForms>", Self::PORT, other),
440 }
441 }
442 fn inject(self) -> Value {
443 Value::Dataset(DatasetValue::RecordForms(self.0))
444 }
445 }
446
447 impl Wire for PredicateFragment {
449 const PORT: PortType = PortType::Fragment;
450 fn extract(v: &Value) -> Self {
451 match v {
452 Value::Dataset(DatasetValue::Predicate(fragment)) => fragment.clone(),
453 Value::Dataset(d @ DatasetValue::Record(_)) => {
454 wrong_kind("a predicate fragment", d)
455 }
456 other => extract_mismatch("PredicateFragment", Self::PORT, other),
457 }
458 }
459 fn inject(self) -> Value {
460 Value::Dataset(DatasetValue::Predicate(self))
461 }
462 }
463
464 impl Wire for RecordFragment {
466 const PORT: PortType = PortType::Fragment;
467 fn extract(v: &Value) -> Self {
468 match v {
469 Value::Dataset(DatasetValue::Record(fragment)) => fragment.clone(),
470 Value::Dataset(d @ DatasetValue::Predicate(_)) => {
471 wrong_kind("a record fragment", d)
472 }
473 other => extract_mismatch("RecordFragment", Self::PORT, other),
474 }
475 }
476 fn inject(self) -> Value {
477 Value::Dataset(DatasetValue::Record(self))
478 }
479 }
480}