1pub mod function;
41pub mod generator;
42#[allow(clippy::type_complexity)]
43pub mod injection;
44pub mod intervals;
45pub mod product;
46pub mod sql;
47pub mod value;
48
49use chrono;
50use itertools::Itertools;
51use paste::paste;
52use std::{
53 cmp,
54 collections::{BTreeMap, BTreeSet, HashSet},
55 convert::Infallible,
56 error, fmt, hash,
57 marker::Copy,
58 ops::{self, Deref, Index},
59 result,
60 sync::Arc,
61};
62
63use crate::{
64 builder::With,
65 hierarchy::{Hierarchy, Path},
66 namer,
67 types::{And, Or},
68 visitor::{self, Acceptor},
69};
70use injection::{Base, InjectInto, Injection};
71use intervals::{Bound, Intervals};
72
73pub use generator::Generator;
74pub use value::Value;
75
76#[derive(Debug)]
80pub enum Error {
81 NoSuperset(String),
82 InvalidConversion(String),
83 InvalidField(String),
84 Other(String),
85}
86
87impl Error {
88 pub fn no_superset(left: impl fmt::Display, right: impl fmt::Display) -> Error {
89 Error::NoSuperset(format!(
90 "No superset including {} and {} found",
91 left, right
92 ))
93 }
94 pub fn invalid_conversion(this: impl fmt::Display, that: impl fmt::Display) -> Error {
95 Error::InvalidConversion(format!("Cannot convert {} into {}", this, that))
96 }
97 pub fn invalid_field(field: impl fmt::Display) -> Error {
98 Error::InvalidField(format!("{} is missing", field))
99 }
100 pub fn other(desc: impl fmt::Display) -> Error {
101 Error::Other(format!("{}", desc))
102 }
103}
104
105impl fmt::Display for Error {
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 match self {
108 Error::NoSuperset(desc) => writeln!(f, "NoSuperset: {}", desc),
109 Error::InvalidConversion(desc) => writeln!(f, "InvalidConversion: {}", desc),
110 Error::InvalidField(desc) => writeln!(f, "InvalidField: {}", desc),
111 Error::Other(err) => writeln!(f, "{}", err),
112 }
113 }
114}
115
116impl error::Error for Error {}
117
118impl From<Infallible> for Error {
119 fn from(err: Infallible) -> Self {
120 Error::Other(err.to_string())
121 }
122}
123impl From<function::Error> for Error {
124 fn from(err: function::Error) -> Self {
125 Error::Other(err.to_string())
126 }
127}
128impl From<injection::Error> for Error {
129 fn from(err: injection::Error) -> Self {
130 Error::Other(err.to_string())
131 }
132}
133impl From<value::Error> for Error {
134 fn from(err: value::Error) -> Self {
135 Error::Other(err.to_string())
136 }
137}
138
139pub type Result<T> = result::Result<T, Error>;
140
141#[allow(unused_macros)]
144macro_rules! for_all_pairs {
146 (@inner $fun:expr, $left:expr, ($($right:expr),*)) => {
147 $($fun($left, $right);)*
148 };
149 (@expand $fun:expr, ($($left:expr),*), $right:tt) => {
150 $(
151 for_all_pairs!(@inner $fun, $left, $right);
152 )*
153 };
154 ($fun:expr, $($value:expr),*) => {
155 for_all_pairs!(@expand $fun, ($($value),*), ($($value),*));
156 };
157}
158
159macro_rules! for_all_variants {
161 ($data_type:expr, $variant:ident, $fun:expr, [$($Variant:ident),*], $default:expr) => {
162 match $data_type {
163 $(DataType::$Variant($variant) => $fun,)*
164 _ => $default,
165 }
166 };
167 ($data_type:expr, $variant:ident, $fun:expr, [$($Variant:ident),*]) => {
168 match $data_type {
169 $(DataType::$Variant($variant) => $fun,)*
170 }
171 };
172}
173
174macro_rules! for_all_variant_pairs {
176 ($left:expr, $right:expr, $left_variant:ident, $right_variant:ident, $fun:expr, [$($Variant:ident),*]) => {
177 match ($left, $right) {
178 $((DataType::$Variant($left_variant), DataType::$Variant($right_variant)) => $fun,)*
179 }
180 };
181 ($left:expr, $right:expr, $left_variant:ident, $right_variant:ident, $fun:expr, [$($Variant:ident),*], $default:expr) => {
182 match ($left, $right) {
183 $((DataType::$Variant($left_variant), DataType::$Variant($right_variant)) => $fun,)*
184 _ => $default,
185 }
186 };
187}
188
189pub trait DataTyped {
191 fn data_type(&self) -> DataType;
193
194 fn has_data_type(&self, data_type: &DataType) -> bool {
196 &self.data_type() == data_type
197 }
198 fn is_contained_by(&self, data_type: &DataType) -> bool {
200 self.data_type().is_subset_of(data_type)
201 }
202}
203
204impl<D: DataTyped> From<D> for DataType {
205 fn from(d: D) -> Self {
206 d.data_type()
207 }
208}
209
210pub trait Variant:
220 Into<DataType>
221 + TryFrom<DataType>
222 + From<Self::Element>
223 + Clone
224 + hash::Hash
225 + cmp::PartialEq
226 + cmp::PartialOrd
227 + fmt::Debug
228 + fmt::Display
229{
230 type Element: value::Variant;
231
232 fn contains(&self, element: &Self::Element) -> bool;
234
235 fn is_subset_of(&self, other: &Self) -> bool;
237
238 fn is_superset_of(&self, other: &Self) -> bool {
240 other.is_subset_of(self)
241 }
242
243 fn super_union(&self, other: &Self) -> Result<Self>;
247
248 fn super_intersection(&self, other: &Self) -> Result<Self>;
252
253 fn into_data_type(&self, other: &DataType) -> Result<DataType>
258 where
259 Self: InjectInto<DataType>,
260 {
261 Ok(self.inject_into(other)?.super_image(self)?)
262 }
263
264 fn minimal_subset(&self) -> Result<Self> {
266 Err(Error::other("Cannot build a minimal DataType"))
267 }
268
269 fn maximal_superset(&self) -> Result<Self> {
271 Err(Error::other("Cannot build a maximal DataType"))
272 }
273
274 fn into_variant(&self, variant: &DataType) -> Result<DataType>
277 where
278 Self: InjectInto<DataType>,
279 {
280 variant
281 .maximal_superset()
282 .and_then(|var| self.into_data_type(&var))
283 }
284
285 fn try_empty(&self) -> Result<Self> {
286 Err(Error::other("Cannot build an empty DataType"))
287 }
288}
289
290fn partial_cmp<V: Variant>(variant: &V, other: &V) -> Option<cmp::Ordering> {
294 match (variant.is_subset_of(other), other.is_subset_of(variant)) {
295 (true, true) => Some(cmp::Ordering::Equal),
296 (true, false) => Some(cmp::Ordering::Less),
297 (false, true) => Some(cmp::Ordering::Greater),
298 (false, false) => None,
299 }
300}
301
302pub trait Specific: Variant {}
305impl Specific for Unit {}
306impl Specific for Boolean {}
307impl Specific for Integer {}
308impl Specific for Enum {}
309impl Specific for Float {}
310impl Specific for Text {}
311impl Specific for Bytes {}
312impl Specific for Date {}
313impl Specific for Time {}
314impl Specific for DateTime {}
315impl Specific for Duration {}
316impl Specific for Id {}
317impl Specific for Struct {}
318impl Specific for Union {}
319impl Specific for Optional {}
320impl Specific for List {}
321impl Specific for Set {}
322impl Specific for Array {}
323impl Specific for Function {}
324
325pub trait Generic: Variant {}
326impl Generic for DataType {}
327
328pub trait Primitive: Specific {}
332impl Primitive for Unit {}
333impl Primitive for Boolean {}
334impl Primitive for Integer {}
335impl Primitive for Enum {}
336impl Primitive for Float {}
337impl Primitive for Text {}
338impl Primitive for Bytes {}
339impl Primitive for Date {}
340impl Primitive for Time {}
341impl Primitive for DateTime {}
342impl Primitive for Duration {}
343impl Primitive for Id {}
344
345pub trait Composite: Specific {}
346impl Composite for Struct {}
347impl Composite for Union {}
348impl Composite for Optional {}
349impl Composite for List {}
350impl Composite for Set {}
351impl Composite for Array {}
352impl Composite for Function {}
353
354#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd)]
356pub struct Unit;
357
358impl Default for Unit {
359 fn default() -> Self {
360 Unit
361 }
362}
363
364impl fmt::Display for Unit {
365 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
366 write!(f, "()")
367 }
368}
369
370impl Or<DataType> for Unit {
371 type Sum = Optional;
372 fn or(self, other: DataType) -> Self::Sum {
373 match other {
374 DataType::Null | DataType::Unit(_) => Optional::from_data_type(DataType::Null),
375 DataType::Optional(o) => o,
376 o => Optional::from_data_type(o),
377 }
378 }
379}
380
381impl InjectInto<DataType> for Unit {
382 type Injection = Base<Self, DataType>;
383 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
384 injection::From(self.clone()).into(other.clone())
385 }
386}
387
388impl From<value::Unit> for Unit {
389 fn from(_value: value::Unit) -> Self {
390 Unit
391 }
392}
393
394impl Variant for Unit {
395 type Element = value::Unit;
396
397 fn contains(&self, _element: &Self::Element) -> bool {
398 true
399 }
400
401 fn is_subset_of(&self, _other: &Self) -> bool {
402 true
403 }
404
405 fn super_union(&self, _other: &Self) -> Result<Self> {
406 Ok(Unit)
407 }
408
409 fn super_intersection(&self, _other: &Self) -> Result<Self> {
410 Ok(Unit)
411 }
412
413 fn is_superset_of(&self, other: &Self) -> bool {
414 other.is_subset_of(self)
415 }
416
417 fn minimal_subset(&self) -> Result<Self> {
418 Ok(Unit)
419 }
420
421 fn maximal_superset(&self) -> Result<Self> {
422 Ok(Unit)
423 }
424
425 fn try_empty(&self) -> Result<Self> {
426 Ok(Unit)
427 }
428}
429
430pub type Boolean = Intervals<bool>;
432
433impl Variant for Boolean {
434 type Element = value::Boolean;
435
436 fn contains(&self, element: &Self::Element) -> bool {
437 self.contains(&**element)
438 }
439
440 fn is_subset_of(&self, other: &Self) -> bool {
441 self.is_subset_of(other)
442 }
443
444 fn super_union(&self, other: &Self) -> Result<Self> {
445 Ok(self.clone().union(other.clone()))
446 }
447
448 fn super_intersection(&self, other: &Self) -> Result<Self> {
449 Ok(self.clone().intersection(other.clone()))
450 }
451
452 fn minimal_subset(&self) -> Result<Self> {
453 Ok(Self::empty())
454 }
455
456 fn maximal_superset(&self) -> Result<Self> {
457 Ok(Self::full())
458 }
459
460 fn try_empty(&self) -> Result<Self> {
461 Ok(Self::empty())
462 }
463}
464
465impl InjectInto<DataType> for Boolean {
466 type Injection = Base<Self, DataType>;
467 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
468 injection::From(self.clone()).into(other.clone())
469 }
470}
471
472impl From<value::Boolean> for Boolean {
473 fn from(value: value::Boolean) -> Self {
474 Boolean::from_value(*value)
475 }
476}
477
478pub type Integer = Intervals<i64>;
480
481impl Variant for Integer {
482 type Element = value::Integer;
483
484 fn contains(&self, element: &Self::Element) -> bool {
485 self.contains(&**element)
486 }
487
488 fn is_subset_of(&self, other: &Self) -> bool {
489 self.is_subset_of(other)
490 }
491
492 fn super_union(&self, other: &Self) -> Result<Self> {
493 Ok(self.clone().union(other.clone()))
494 }
495
496 fn super_intersection(&self, other: &Self) -> Result<Self> {
497 Ok(self.clone().intersection(other.clone()))
498 }
499
500 fn minimal_subset(&self) -> Result<Self> {
501 Ok(Self::empty())
502 }
503
504 fn maximal_superset(&self) -> Result<Self> {
505 Ok(Self::full())
506 }
507
508 fn try_empty(&self) -> Result<Self> {
509 Ok(Self::empty())
510 }
511}
512
513impl InjectInto<DataType> for Integer {
514 type Injection = Base<Self, DataType>;
515 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
516 injection::From(self.clone()).into(other.clone())
517 }
518}
519
520impl From<value::Integer> for Integer {
521 fn from(value: value::Integer) -> Self {
522 Integer::from_value(*value)
523 }
524}
525
526impl From<Boolean> for Integer {
527 fn from(_: Boolean) -> Self {
528 Integer::from_values(&[0, 1])
529 }
530}
531
532impl From<Enum> for Integer {
533 fn from(e: Enum) -> Self {
534 e.values.into_iter().map(|(_, i)| i).collect()
535 }
536}
537
538#[derive(Debug, Clone, Hash, PartialEq)]
540pub struct Enum {
541 values: Arc<[(String, i64)]>,
542}
543
544impl Enum {
545 pub fn new(values: Arc<[(String, i64)]>) -> Enum {
546 assert!(!values.is_empty()); let codes: BTreeSet<i64> = values.iter().map(|(_, i)| *i).collect();
548 assert!(values.len() == codes.len()); Enum { values }
550 }
551
552 pub fn values(&self) -> BTreeSet<(String, i64)> {
553 self.values.iter().cloned().collect()
554 }
555 pub fn value(&self) -> &(String, i64) {
556 self.values.iter().next().unwrap()
557 }
558
559 pub fn encode(&self, key: String) -> Result<i64> {
560 Ok(self
561 .values
562 .iter()
563 .find(|(k, _)| &key == k)
564 .ok_or(Error::invalid_field(key))?
565 .1)
566 }
567
568 pub fn decode(&self, value: i64) -> Result<String> {
569 Ok(self
570 .values
571 .iter()
572 .find(|(_, v)| &value == v)
573 .ok_or(Error::invalid_field(value))?
574 .0
575 .clone())
576 }
577}
578
579impl Deref for Enum {
581 type Target = [(String, i64)];
582
583 fn deref(&self) -> &Self::Target {
584 &*self.values
585 }
586}
587
588impl<S: Clone + Into<String>> From<&[S]> for Enum {
589 fn from(values: &[S]) -> Self {
590 Enum::new(
591 values
592 .iter()
593 .enumerate()
594 .map(|(i, s)| (s.clone().into(), i as i64))
595 .collect(),
596 )
597 }
598}
599
600impl<S: Into<String>> FromIterator<(S, i64)> for Enum {
601 fn from_iter<I: IntoIterator<Item = (S, i64)>>(iter: I) -> Self {
602 Enum::new(
603 iter.into_iter()
604 .map(|(s, i)| (s.into(), i as i64))
605 .collect(),
606 )
607 }
608}
609
610impl cmp::PartialOrd for Enum {
611 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
612 partial_cmp(self, other)
613 }
614}
615
616impl fmt::Display for Enum {
617 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
618 write!(
619 f,
620 "enum({})",
621 self.values
622 .iter()
623 .map(|(s, i)| format!(r#""{}" ({})"#, s, i))
624 .join(", ")
625 )
626 }
627}
628
629impl Variant for Enum {
630 type Element = value::Enum;
631
632 fn contains(&self, element: &Self::Element) -> bool {
633 if let Ok(key) = element.decode() {
634 self.values.contains(&(key, element.0))
635 } else {
636 false
637 }
638 }
639
640 fn is_subset_of(&self, other: &Self) -> bool {
641 self.values().is_subset(&other.values())
642 }
643
644 fn super_union(&self, other: &Self) -> Result<Self> {
645 Ok(self.values().union(&other.values()).cloned().collect())
646 }
647
648 fn super_intersection(&self, other: &Self) -> Result<Self> {
649 Ok(self
650 .values()
651 .intersection(&other.values())
652 .cloned()
653 .collect())
654 }
655
656 fn minimal_subset(&self) -> Result<Self> {
657 Ok(Self::new(Arc::new([])))
658 }
659}
660
661impl InjectInto<DataType> for Enum {
662 type Injection = Base<Self, DataType>;
663 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
664 injection::From(self.clone()).into(other.clone())
665 }
666}
667
668impl From<value::Enum> for Enum {
669 fn from(value: value::Enum) -> Self {
670 Enum::new(value.1.clone())
671 }
672}
673
674pub type Float = Intervals<f64>;
676
677impl Variant for Float {
678 type Element = value::Float;
679
680 fn contains(&self, element: &Self::Element) -> bool {
681 self.contains(&**element)
682 }
683
684 fn is_subset_of(&self, other: &Self) -> bool {
685 self.is_subset_of(other)
686 }
687
688 fn super_union(&self, other: &Self) -> Result<Self> {
689 Ok(self.clone().union(other.clone()))
690 }
691
692 fn super_intersection(&self, other: &Self) -> Result<Self> {
693 Ok(self.clone().intersection(other.clone()))
694 }
695
696 fn minimal_subset(&self) -> Result<Self> {
697 Ok(Self::empty())
698 }
699
700 fn maximal_superset(&self) -> Result<Self> {
701 Ok(Self::full())
702 }
703
704 fn try_empty(&self) -> Result<Self> {
705 Ok(Self::empty())
706 }
707}
708
709impl InjectInto<DataType> for Float {
710 type Injection = Base<Self, DataType>;
711 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
712 injection::From(self.clone()).into(other.clone())
713 }
714}
715
716impl From<value::Float> for Float {
717 fn from(value: value::Float) -> Self {
718 Float::from_value(*value)
719 }
720}
721
722pub type Text = Intervals<String>;
724
725impl Variant for Text {
726 type Element = value::Text;
727
728 fn contains(&self, element: &Self::Element) -> bool {
729 self.contains(&**element)
730 }
731
732 fn is_subset_of(&self, other: &Self) -> bool {
733 self.is_subset_of(other)
734 }
735
736 fn super_union(&self, other: &Self) -> Result<Self> {
737 Ok(self.clone().union(other.clone()))
738 }
739
740 fn super_intersection(&self, other: &Self) -> Result<Self> {
741 Ok(self.clone().intersection(other.clone()))
742 }
743
744 fn minimal_subset(&self) -> Result<Self> {
745 Ok(Self::empty())
746 }
747
748 fn maximal_superset(&self) -> Result<Self> {
749 Ok(Self::full())
750 }
751
752 fn try_empty(&self) -> Result<Self> {
753 Ok(Self::empty())
754 }
755}
756
757impl InjectInto<DataType> for Text {
758 type Injection = Base<Self, DataType>;
759 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
760 injection::From(self.clone()).into(other.clone())
761 }
762}
763
764impl From<value::Text> for Text {
765 fn from(value: value::Text) -> Self {
766 Text::from_value((*value).clone())
767 }
768}
769
770#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd)]
772pub struct Bytes;
773
774impl Default for Bytes {
775 fn default() -> Self {
776 Bytes
777 }
778}
779
780impl fmt::Display for Bytes {
781 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
782 write!(f, "bytes")
783 }
784}
785
786impl Variant for Bytes {
787 type Element = value::Bytes;
788
789 fn contains(&self, _: &Self::Element) -> bool {
790 true
791 }
792
793 fn is_subset_of(&self, _: &Self) -> bool {
794 true
795 }
796
797 fn super_union(&self, _: &Self) -> Result<Self> {
798 Ok(Bytes)
799 }
800
801 fn super_intersection(&self, _: &Self) -> Result<Self> {
802 Ok(Bytes)
803 }
804
805 fn minimal_subset(&self) -> Result<Self> {
806 Ok(Bytes)
807 }
808
809 fn maximal_superset(&self) -> Result<Self> {
810 Ok(Bytes)
811 }
812
813 fn try_empty(&self) -> Result<Self> {
814 Ok(Bytes)
815 }
816}
817
818impl InjectInto<DataType> for Bytes {
819 type Injection = Base<Self, DataType>;
820 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
821 injection::From(self.clone()).into(other.clone())
822 }
823}
824
825impl From<value::Bytes> for Bytes {
826 fn from(_value: value::Bytes) -> Self {
827 Bytes
828 }
829}
830
831#[derive(Debug, Clone, PartialEq, Eq)]
833pub struct Struct {
834 fields: Vec<(String, Arc<DataType>)>,
835}
836
837impl Struct {
838 pub fn new(fields: Vec<(String, Arc<DataType>)>) -> Struct {
840 let mut uniques = HashSet::new();
841 assert!(fields.iter().all(move |(f, _)| uniques.insert(f.clone())));
842 Struct { fields }
843 }
844 pub fn unit() -> Struct {
846 Struct::new(vec![])
847 }
848 pub fn from_field<S: Into<String>, T: Into<Arc<DataType>>>(s: S, t: T) -> Struct {
850 Struct::new(vec![(s.into(), t.into())])
851 }
852 pub fn from_data_type(data_type: DataType) -> Struct {
854 Struct::default().and(data_type)
855 }
856 pub fn from_data_types<T: Clone + Into<DataType>, A: AsRef<[T]>>(data_types: A) -> Struct {
858 data_types
859 .as_ref()
860 .iter()
861 .fold(Struct::default(), |s, t| s.and(t.clone().into()))
862 }
863 pub fn fields(&self) -> &[(String, Arc<DataType>)] {
865 self.fields.as_ref()
866 }
867 pub fn field(&self, name: &str) -> Result<&(String, Arc<DataType>)> {
869 self.fields
870 .iter()
871 .find(|(f, _)| f == name)
872 .map_or(Err(Error::invalid_field(name).into()), |f| Ok(f))
873 }
874 pub fn data_type(&self, name: &str) -> Arc<DataType> {
876 self.fields
877 .iter()
878 .find(|(f, _)| f == name)
879 .map_or(Arc::new(DataType::Any), |(_, t)| t.clone())
880 }
881 pub fn index_from_name(&self, name: &str) -> Result<usize> {
883 self.fields
884 .iter()
885 .position(|(s, _t)| s == name)
886 .map_or(Err(Error::InvalidField(name.to_string()).into()), |i| Ok(i))
887 }
888 pub fn field_from_index(&self, index: usize) -> &(String, Arc<DataType>) {
890 &self.fields[index]
891 }
892 pub fn from_schema_size(schema: Struct, size: &Integer) -> Struct {
894 schema
895 .fields
896 .into_iter()
897 .map(|(s, t)| (s, Arc::new(List::new(t, size.clone()).into())))
898 .collect()
899 }
900 pub fn hierarchy(&self) -> Hierarchy<&DataType> {
903 self.iter().fold(
904 self.iter()
905 .map(|(s, d)| (vec![s.clone()], d.as_ref()))
906 .collect(),
907 |h, (s, d)| h.with(d.hierarchy().prepend(&[s.clone()]).into_iter()),
908 )
909 }
910
911 pub fn all_values(&self) -> bool {
912 self.iter().all(|(_, dt)| dt.deref().all_values())
913 }
914}
915
916impl Default for Struct {
918 fn default() -> Self {
919 Struct::unit()
920 }
921}
922
923#[allow(clippy::derive_hash_xor_eq)]
924impl hash::Hash for Struct {
925 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
926 self.fields.iter().for_each(|(f, t)| {
927 Bound::hash(f, state);
928 DataType::hash(t, state);
929 });
930 }
931}
932
933impl Deref for Struct {
935 type Target = [(String, Arc<DataType>)];
936
937 fn deref(&self) -> &Self::Target {
938 &self.fields
939 }
940}
941
942impl<S: Into<String>, T: Into<Arc<DataType>>> And<(S, T)> for Struct {
944 type Product = Struct;
945 fn and(self, other: (S, T)) -> Self::Product {
946 let field: String = other.0.into();
947 let data_type: Arc<DataType> = other.1.into();
948 let (mut fields, push_other): (Vec<_>, _) =
950 self.fields.iter().fold((vec![], true), |(v, b), (f, t)| {
951 let mut v = v;
952 let mut b = b;
953 if &field != f {
954 v.push((f.clone(), t.clone()));
955 } else {
956 b = false;
957 v.push((
958 f.clone(),
959 Arc::new(
960 t.as_ref()
961 .clone()
962 .super_intersection(data_type.as_ref())
963 .unwrap(),
964 ),
965 ));
966 }
967 (v, b)
968 });
969 if push_other {
970 fields.push((field, data_type))
971 }
972 Struct::new(fields.into())
973 }
974}
975
976impl<T: Into<Arc<DataType>>> And<(T,)> for Struct {
977 type Product = Struct;
978 fn and(self, other: (T,)) -> Self::Product {
979 let field = namer::new_name_outside("", self.fields.iter().map(|(f, _t)| f));
980 let data_type: Arc<DataType> = other.0.into();
981 self.and((field, data_type))
982 }
983}
984
985impl And<Struct> for Struct {
986 type Product = Struct;
987 fn and(self, other: Struct) -> Self::Product {
988 self.super_intersection(&other).unwrap()
989 }
990}
991
992impl And<DataType> for Struct {
993 type Product = Struct;
994 fn and(self, other: DataType) -> Self::Product {
995 match other {
997 DataType::Struct(s) => self.super_intersection(&s).unwrap(), other => self.and((other,)),
999 }
1000 }
1001}
1002
1003impl<S: Into<String>, T: Into<Arc<DataType>>> From<(S, T)> for Struct {
1004 fn from(field: (S, T)) -> Self {
1005 Struct::from_field(field.0, field.1)
1006 }
1007}
1008
1009impl<S: Clone + Into<String>, T: Clone + Into<Arc<DataType>>> From<&[(S, T)]> for Struct {
1010 fn from(values: &[(S, T)]) -> Self {
1011 Struct::new(
1012 values
1013 .into_iter()
1014 .map(|(f, t)| (f.clone().into(), t.clone().into()))
1015 .collect(),
1016 )
1017 }
1018}
1019
1020impl From<Unit> for Struct {
1021 fn from(_value: Unit) -> Self {
1022 Struct::unit()
1023 }
1024}
1025
1026impl From<value::Struct> for Struct {
1027 fn from(value: value::Struct) -> Self {
1028 (*value)
1029 .iter()
1030 .map(|(s, v)| (s.clone(), v.data_type()))
1031 .collect()
1032 }
1033}
1034
1035impl<S: Into<String>, T: Into<Arc<DataType>>> FromIterator<(S, T)> for Struct {
1036 fn from_iter<I: IntoIterator<Item = (S, T)>>(iter: I) -> Self {
1037 iter.into_iter().fold(Struct::unit(), |s, f| s.and(f))
1038 }
1039}
1040
1041impl cmp::PartialOrd for Struct {
1042 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
1043 partial_cmp(self, other)
1044 }
1045}
1046
1047impl fmt::Display for Struct {
1048 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1049 write!(
1050 f,
1051 "struct{{{}}}",
1052 self.fields
1053 .iter()
1054 .map(|(f, t)| { format!("{}: {}", f, t).to_string() })
1055 .collect::<Vec<String>>()
1056 .join(", ")
1057 )
1058 }
1059}
1060
1061impl Variant for Struct {
1062 type Element = value::Struct;
1063
1064 fn contains(&self, element: &Self::Element) -> bool {
1065 self.fields.iter().all(|(s, d)| {
1066 element
1067 .value(s)
1068 .map_or(false, |v| d.as_ref().contains(v.as_ref()))
1069 })
1070 }
1071
1072 fn is_subset_of(&self, other: &Self) -> bool {
1075 other.fields.iter().all(
1076 |(other_field, other_data_type)| {
1077 self.data_type(other_field).is_subset_of(other_data_type)
1078 }, )
1080 }
1081
1082 fn super_union(&self, other: &Self) -> Result<Self> {
1083 let self_fields: Vec<String> = self
1084 .fields
1085 .iter()
1086 .map(|(f, _)| f.clone())
1087 .unique()
1088 .collect();
1089 let other_fields: Vec<String> = other
1090 .fields
1091 .iter()
1092 .map(|(f, _)| f.clone())
1093 .unique()
1094 .collect();
1095 self_fields
1096 .into_iter()
1097 .chain(other_fields.into_iter())
1098 .into_iter()
1099 .map(|f| {
1100 Ok((
1101 f.clone(),
1102 self.data_type(&f).super_union(&other.data_type(&f))?,
1103 ))
1104 })
1105 .collect()
1106 }
1107
1108 fn super_intersection(&self, other: &Self) -> Result<Self> {
1109 let self_fields: Vec<String> = self
1110 .fields
1111 .iter()
1112 .map(|(f, _)| f.clone())
1113 .unique()
1114 .collect();
1115 let other_fields: Vec<String> = other
1116 .fields
1117 .iter()
1118 .map(|(f, _)| f.clone())
1119 .unique()
1120 .collect();
1121 self_fields
1122 .into_iter()
1123 .chain(other_fields.into_iter())
1124 .into_iter()
1125 .map(|f| {
1126 Ok((
1127 f.clone(),
1128 self.data_type(&f)
1129 .super_intersection(&other.data_type(&f))?,
1130 ))
1131 })
1132 .collect()
1133 }
1134
1135 fn maximal_superset(&self) -> Result<Self> {
1136 Ok(Struct::new(vec![]))
1137 }
1138
1139 fn try_empty(&self) -> Result<Self> {
1140 Ok(Self::new(
1141 self.fields()
1142 .into_iter()
1143 .map(|(s, d)| {
1144 if let Ok(dd) = d.deref().try_empty() {
1145 Ok((s.to_string(), Arc::new(dd)))
1146 } else {
1147 Err(Error::other("other"))
1148 }
1149 })
1150 .collect::<Result<Vec<_>>>()?,
1151 ))
1152 }
1153}
1154
1155impl InjectInto<DataType> for Struct {
1156 type Injection = Base<Self, DataType>;
1157 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
1158 injection::From(self.clone()).into(other.clone())
1159 }
1160}
1161
1162impl<P: Path> Index<P> for Struct {
1164 type Output = DataType;
1165
1166 fn index(&self, index: P) -> &Self::Output {
1167 self.hierarchy()[index]
1168 }
1169}
1170
1171impl Index<usize> for Struct {
1172 type Output = Arc<DataType>;
1173
1174 fn index(&self, index: usize) -> &Self::Output {
1175 &self.field_from_index(index).1
1176 }
1177}
1178
1179#[derive(Debug, Clone, PartialEq)]
1181pub struct Union {
1182 fields: Vec<(String, Arc<DataType>)>,
1183}
1184
1185impl Union {
1186 pub fn new(fields: Vec<(String, Arc<DataType>)>) -> Union {
1187 let mut uniques = HashSet::new();
1188 assert!(fields.iter().all(move |(f, _)| uniques.insert(f.clone())));
1189 Union { fields }
1190 }
1191 pub fn null() -> Union {
1193 Union::new(vec![])
1194 }
1195 pub fn from_field<S: Into<String>, T: Into<Arc<DataType>>>(s: S, t: T) -> Union {
1197 Union::new(vec![(s.into(), t.into())])
1198 }
1199 pub fn from_data_type(data_type: DataType) -> Union {
1201 Union::default().or(data_type)
1202 }
1203 pub fn from_data_types(data_types: &[DataType]) -> Union {
1205 data_types
1206 .iter()
1207 .fold(Union::default(), |s, t| s.or(t.clone()))
1208 }
1209 pub fn fields(&self) -> &[(String, Arc<DataType>)] {
1211 self.fields.as_ref()
1212 }
1213 pub fn field(&self, name: &str) -> Result<&(String, Arc<DataType>)> {
1215 self.fields
1216 .iter()
1217 .find(|(f, _)| f == name)
1218 .map_or(Err(Error::InvalidField(name.to_string()).into()), |f| Ok(f))
1219 }
1220 pub fn data_type(&self, name: &str) -> Arc<DataType> {
1222 self.fields
1223 .iter()
1224 .find(|(f, _)| f == name)
1225 .map_or(Arc::new(DataType::Null), |(_, t)| t.clone())
1226 }
1227 pub fn index_from_name(&self, name: &str) -> Result<usize> {
1229 self.fields
1230 .iter()
1231 .position(|(s, _t)| s == name)
1232 .map_or(Err(Error::InvalidField(name.to_string()).into()), |i| Ok(i))
1233 }
1234 pub fn field_from_index(&self, index: usize) -> &(String, Arc<DataType>) {
1236 &self.fields[index]
1237 }
1238 pub fn hierarchy(&self) -> Hierarchy<&DataType> {
1241 self.iter().fold(
1242 self.iter()
1243 .map(|(s, d)| (vec![s.clone()], d.as_ref()))
1244 .collect(),
1245 |h, (s, d)| h.with(d.hierarchy().prepend(&[s.clone()]).into_iter()),
1246 )
1247 }
1248}
1249
1250impl Default for Union {
1252 fn default() -> Self {
1253 Union::null()
1254 }
1255}
1256
1257#[allow(clippy::derive_hash_xor_eq)]
1258impl hash::Hash for Union {
1259 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
1260 self.fields.iter().for_each(|(f, t)| {
1261 Bound::hash(f, state);
1262 DataType::hash(t, state);
1263 });
1264 }
1265}
1266
1267impl Deref for Union {
1269 type Target = [(String, Arc<DataType>)];
1270
1271 fn deref(&self) -> &Self::Target {
1272 &self.fields
1273 }
1274}
1275
1276impl<S: Into<String>, T: Into<Arc<DataType>>> Or<(S, T)> for Union {
1278 type Sum = Union;
1279 fn or(self, other: (S, T)) -> Self::Sum {
1280 let field: String = other.0.into();
1281 let data_type: Arc<DataType> = other.1.into();
1282 let mut fields: Vec<(String, Arc<DataType>)> = self
1284 .fields
1285 .iter()
1286 .filter_map(|(f, t)| (&field != f).then_some((f.clone(), t.clone())))
1287 .collect();
1288 fields.push((field, data_type));
1289 Union::new(fields.into())
1290 }
1291}
1292
1293impl<T: Into<Arc<DataType>>> Or<(T,)> for Union {
1294 type Sum = Union;
1295 fn or(self, other: (T,)) -> Self::Sum {
1296 let field = namer::new_name_outside("", self.fields.iter().map(|(f, _t)| f));
1297 let data_type: Arc<DataType> = other.0.into();
1298 self.or((field, data_type))
1299 }
1300}
1301
1302impl Or<Unit> for Union {
1303 type Sum = Optional;
1304 fn or(self, _other: Unit) -> Self::Sum {
1305 Optional::from_data_type(DataType::from(self))
1306 }
1307}
1308
1309impl Or<Optional> for Union {
1310 type Sum = Optional;
1311 fn or(self, other: Optional) -> Self::Sum {
1312 Optional::from_data_type(DataType::from(self.or(other.data_type().clone())))
1313 }
1314}
1315
1316impl Or<Union> for Union {
1317 type Sum = Union;
1318 fn or(self, other: Union) -> Self::Sum {
1319 let mut result = self;
1320 for field in other.fields() {
1321 result = result.or(field.clone())
1322 }
1323 result
1324 }
1325}
1326
1327impl Or<DataType> for Union {
1328 type Sum = Union;
1329 fn or(self, other: DataType) -> Self::Sum {
1330 match other {
1332 DataType::Null => self,
1333 DataType::Union(u) => self.or(u),
1334 other => self.or((other,)),
1335 }
1336 }
1337}
1338
1339impl<S: Into<String>, T: Into<Arc<DataType>>> From<(S, T)> for Union {
1340 fn from(field: (S, T)) -> Self {
1341 Union::from_field(field.0, field.1)
1342 }
1343}
1344
1345impl<S: Clone + Into<String>, T: Clone + Into<Arc<DataType>>> From<&[(S, T)]> for Union {
1346 fn from(values: &[(S, T)]) -> Self {
1347 Union::new(
1348 values
1349 .into_iter()
1350 .map(|(f, t)| (f.clone().into(), t.clone().into()))
1351 .collect(),
1352 )
1353 }
1354}
1355
1356impl From<value::Union> for Union {
1357 fn from(value: value::Union) -> Self {
1358 ((*value).0.clone(), (*value).1.data_type().clone()).into()
1359 }
1360}
1361
1362impl<S: Into<String>, T: Into<Arc<DataType>>> FromIterator<(S, T)> for Union {
1363 fn from_iter<I: IntoIterator<Item = (S, T)>>(iter: I) -> Self {
1364 iter.into_iter().fold(Union::null(), |s, f| s.or(f))
1365 }
1366}
1367
1368impl cmp::PartialOrd for Union {
1369 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
1370 partial_cmp(self, other)
1371 }
1372}
1373
1374impl fmt::Display for Union {
1375 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1376 write!(
1377 f,
1378 "union{{{}}}",
1379 self.fields
1380 .iter()
1381 .map(|(f, t)| { format!("{}: {}", f, t).to_string() })
1382 .collect::<Vec<String>>()
1383 .join(", ")
1384 )
1385 }
1386}
1387
1388impl Variant for Union {
1389 type Element = value::Union;
1390
1391 fn contains(&self, element: &Self::Element) -> bool {
1392 self.fields
1393 .iter()
1394 .any(|(s, d)| &element.0 == s && d.contains(&*element.1))
1395 }
1396
1397 fn is_subset_of(&self, other: &Self) -> bool {
1398 let fields: BTreeSet<String> = self.fields.iter().map(|(f, _)| f.to_string()).collect();
1399 let other_fields: BTreeSet<String> =
1400 other.fields.iter().map(|(f, _)| f.to_string()).collect();
1401 fields.is_subset(&other_fields)
1402 && self
1403 .fields
1404 .iter()
1405 .all(|(f, t)| t.is_subset_of(&other.data_type(f)))
1406 }
1407
1408 fn super_union(&self, other: &Self) -> Result<Self> {
1409 let self_fields: Vec<String> = self
1410 .fields
1411 .iter()
1412 .map(|(f, _)| f.clone())
1413 .unique()
1414 .collect();
1415 let other_fields: Vec<String> = other
1416 .fields
1417 .iter()
1418 .map(|(f, _)| f.clone())
1419 .unique()
1420 .collect();
1421 self_fields
1422 .into_iter()
1423 .chain(other_fields.into_iter())
1424 .into_iter()
1425 .map(|f| {
1426 Ok((
1427 f.clone(),
1428 self.data_type(&f).super_union(&other.data_type(&f))?,
1429 ))
1430 })
1431 .collect()
1432 }
1433
1434 fn super_intersection(&self, other: &Self) -> Result<Self> {
1435 let self_fields: Vec<String> = self
1436 .fields
1437 .iter()
1438 .map(|(f, _)| f.clone())
1439 .unique()
1440 .collect();
1441 let other_fields: Vec<String> = other
1442 .fields
1443 .iter()
1444 .map(|(f, _)| f.clone())
1445 .unique()
1446 .collect();
1447 self_fields
1448 .into_iter()
1449 .chain(other_fields.into_iter())
1450 .into_iter()
1451 .map(|f| {
1452 Ok((
1453 f.clone(),
1454 self.data_type(&f)
1455 .super_intersection(&other.data_type(&f))?,
1456 ))
1457 })
1458 .collect()
1459 }
1460
1461 fn minimal_subset(&self) -> Result<Self> {
1462 Ok(Union::new(vec![]))
1463 }
1464
1465 fn try_empty(&self) -> Result<Self> {
1466 Ok(Self::new(
1467 self.fields()
1468 .into_iter()
1469 .map(|(s, d)| {
1470 if let Ok(dd) = d.deref().try_empty() {
1471 Ok((s.to_string(), Arc::new(dd)))
1472 } else {
1473 Err(Error::other("other"))
1474 }
1475 })
1476 .collect::<Result<Vec<_>>>()?,
1477 ))
1478 }
1479}
1480
1481impl InjectInto<DataType> for Union {
1482 type Injection = Base<Self, DataType>;
1483 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
1484 injection::From(self.clone()).into(other.clone())
1485 }
1486}
1487
1488impl<P: Path> Index<P> for Union {
1490 type Output = DataType;
1491
1492 fn index(&self, index: P) -> &Self::Output {
1493 self.hierarchy()[index]
1494 }
1495}
1496
1497impl Index<usize> for Union {
1498 type Output = Arc<DataType>;
1499
1500 fn index(&self, index: usize) -> &Self::Output {
1501 &self.field_from_index(index).1
1502 }
1503}
1504
1505#[derive(Debug, Clone, PartialEq, Hash)]
1507pub struct Optional {
1508 data_type: Arc<DataType>,
1509}
1510
1511impl Optional {
1512 pub fn new(data_type: Arc<DataType>) -> Optional {
1513 Optional { data_type }
1514 }
1515
1516 pub fn data_type(&self) -> &DataType {
1517 self.data_type.as_ref()
1518 }
1519
1520 pub fn from_data_type<T: Into<DataType>>(data_type: T) -> Optional {
1521 let data_type = data_type.into();
1522 match data_type {
1523 DataType::Optional(o) => o,
1524 _ => Optional::new(Arc::new(data_type)),
1525 }
1526 }
1527}
1528
1529impl From<Arc<DataType>> for Optional {
1530 fn from(data_type: Arc<DataType>) -> Self {
1531 Optional::new(data_type)
1532 }
1533}
1534
1535impl From<DataType> for Optional {
1536 fn from(data_type: DataType) -> Self {
1537 Optional::from_data_type(data_type)
1538 }
1539}
1540
1541impl cmp::PartialOrd for Optional {
1542 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
1543 partial_cmp(self, other)
1544 }
1545}
1546
1547impl fmt::Display for Optional {
1548 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1549 write!(f, "option({})", self.data_type)
1550 }
1551}
1552
1553impl Or<Optional> for Optional {
1554 type Sum = Optional;
1555 fn or(self, other: Optional) -> Self::Sum {
1556 Optional::from_data_type(self.data_type().clone().or(other.data_type().clone()))
1557 }
1558}
1559
1560impl Or<DataType> for Optional {
1561 type Sum = Optional;
1562 fn or(self, other: DataType) -> Self::Sum {
1563 match other {
1564 DataType::Null | DataType::Unit(_) => self,
1565 DataType::Optional(o) => self.or(o),
1566 o => Optional::from_data_type(self.data_type().clone().or(o)),
1567 }
1568 }
1569}
1570
1571impl Variant for Optional {
1572 type Element = value::Optional;
1573
1574 fn contains(&self, element: &Self::Element) -> bool {
1575 element
1576 .as_ref()
1577 .map_or(true, |v| self.data_type.contains(v))
1578 }
1579
1580 fn is_subset_of(&self, other: &Self) -> bool {
1581 self.data_type <= other.data_type
1582 }
1583
1584 fn super_union(&self, other: &Self) -> Result<Self> {
1585 Ok(Optional::from(
1586 self.data_type().super_union(other.data_type())?,
1587 ))
1588 }
1589
1590 fn super_intersection(&self, other: &Self) -> Result<Self> {
1591 Ok(Optional::from(
1592 self.data_type().super_intersection(other.data_type())?,
1593 ))
1594 }
1595
1596 fn minimal_subset(&self) -> Result<Self> {
1597 Ok(Optional::from_data_type(DataType::Null))
1598 }
1599
1600 fn maximal_superset(&self) -> Result<Self> {
1601 Ok(Optional::from_data_type(DataType::Any))
1602 }
1603
1604 fn try_empty(&self) -> Result<Self> {
1605 Ok(Optional::from_data_type(self.data_type.try_empty()?))
1606 }
1607}
1608
1609impl InjectInto<DataType> for Optional {
1610 type Injection = Base<Self, DataType>;
1611 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
1612 injection::From(self.clone()).into(other.clone())
1613 }
1614}
1615
1616impl From<value::Optional> for Optional {
1617 fn from(value: value::Optional) -> Self {
1618 (*value).clone().map_or(Optional::from(DataType::Any), |o| {
1619 Optional::from(o.as_ref().data_type())
1620 })
1621 }
1622}
1623
1624#[derive(Debug, Clone, Hash, PartialEq)]
1627pub struct List {
1628 data_type: Arc<DataType>,
1629 size: Integer,
1630}
1631
1632impl List {
1633 pub fn new(data_type: Arc<DataType>, size: Integer) -> List {
1634 List {
1635 data_type,
1636 size: size.intersection(Integer::from_min(0)),
1637 }
1638 }
1639
1640 pub fn from_data_type_size(data_type: DataType, size: Integer) -> List {
1641 List::new(Arc::new(data_type), size)
1642 }
1643
1644 pub fn from_data_type(data_type: DataType) -> List {
1645 List::from_data_type_size(data_type, Integer::from_min(0))
1646 }
1647
1648 pub fn size(&self) -> &Integer {
1649 &self.size
1650 }
1651
1652 pub fn data_type(&self) -> &DataType {
1653 self.data_type.as_ref()
1654 }
1655}
1656
1657impl From<(Arc<DataType>, Integer)> for List {
1658 fn from(data_type_size: (Arc<DataType>, Integer)) -> Self {
1659 List::new(data_type_size.0, data_type_size.1)
1660 }
1661}
1662
1663impl From<(DataType, Integer)> for List {
1664 fn from(data_type_size: (DataType, Integer)) -> Self {
1665 List::new(Arc::new(data_type_size.0), data_type_size.1)
1666 }
1667}
1668
1669impl cmp::PartialOrd for List {
1670 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
1671 partial_cmp(self, other)
1672 }
1673}
1674
1675impl fmt::Display for List {
1676 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1677 write!(f, "list({}, size ∈ {})", self.data_type, self.size)
1678 }
1679}
1680
1681impl Variant for List {
1682 type Element = value::List;
1683
1684 fn contains(&self, element: &Self::Element) -> bool {
1685 self.size.contains(&(element.len() as i64))
1686 && element.iter().all(|v| self.data_type.contains(v))
1687 }
1688
1689 fn is_subset_of(&self, other: &Self) -> bool {
1690 self.data_type <= other.data_type && self.size <= other.size
1691 }
1692
1693 fn super_union(&self, other: &Self) -> Result<Self> {
1694 Ok(List::from((
1695 self.data_type().super_union(other.data_type())?,
1696 self.size().super_union(other.size())?,
1697 )))
1698 }
1699
1700 fn super_intersection(&self, other: &Self) -> Result<Self> {
1701 Ok(List::from((
1702 self.data_type().super_intersection(other.data_type())?,
1703 self.size().super_intersection(other.size())?,
1704 )))
1705 }
1706
1707 fn minimal_subset(&self) -> Result<Self> {
1708 Ok(List::from_data_type_size(DataType::Null, Integer::empty()))
1709 }
1710
1711 fn maximal_superset(&self) -> Result<Self> {
1712 Ok(List::from_data_type_size(
1713 DataType::Any,
1714 Integer::from_max(i64::MAX),
1715 ))
1716 }
1717
1718 fn try_empty(&self) -> Result<Self> {
1719 Ok(Self::new(self.data_type().try_empty()?.into(), 0.into()))
1720 }
1721}
1722
1723impl InjectInto<DataType> for List {
1724 type Injection = Base<Self, DataType>;
1725 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
1726 injection::From(self.clone()).into(other.clone())
1727 }
1728}
1729
1730impl From<value::List> for List {
1731 fn from(value: value::List) -> Self {
1732 List::from((
1733 (*value).iter().fold(DataType::Null, |s, d| {
1734 s.super_union(&d.data_type()).unwrap_or(DataType::Any)
1735 }),
1736 Integer::from_value((*value).len() as i64),
1737 ))
1738 }
1739}
1740
1741#[derive(Debug, Clone, Hash, PartialEq)]
1744pub struct Set {
1745 data_type: Arc<DataType>,
1746 size: Integer,
1747}
1748
1749impl Set {
1750 pub fn new(data_type: Arc<DataType>, size: Integer) -> Set {
1751 Set {
1752 data_type,
1753 size: size.intersection(Integer::from_min(0)),
1754 }
1755 }
1756
1757 pub fn from_data_type_size(data_type: DataType, size: Integer) -> Set {
1758 Set::new(Arc::new(data_type), size)
1759 }
1760
1761 pub fn from_data_type(data_type: DataType) -> Set {
1762 Set::from_data_type_size(data_type, Integer::from_max(i64::MAX))
1763 }
1764
1765 pub fn size(&self) -> &Integer {
1766 &self.size
1767 }
1768
1769 pub fn data_type(&self) -> &DataType {
1770 self.data_type.as_ref()
1771 }
1772}
1773
1774impl From<(Arc<DataType>, Integer)> for Set {
1775 fn from(data_type_size: (Arc<DataType>, Integer)) -> Self {
1776 Set::new(data_type_size.0, data_type_size.1)
1777 }
1778}
1779
1780impl From<(DataType, Integer)> for Set {
1781 fn from(data_type_size: (DataType, Integer)) -> Self {
1782 Set::new(Arc::new(data_type_size.0), data_type_size.1)
1783 }
1784}
1785
1786impl cmp::PartialOrd for Set {
1787 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
1788 partial_cmp(self, other)
1789 }
1790}
1791
1792impl fmt::Display for Set {
1793 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1794 write!(f, "set({}, size ∈ {})", self.data_type, self.size)
1795 }
1796}
1797
1798impl Variant for Set {
1799 type Element = value::Set;
1800
1801 fn contains(&self, element: &Self::Element) -> bool {
1802 self.size.contains(&(element.len() as i64))
1803 && element.iter().all(|v| self.data_type.contains(v))
1804 }
1805
1806 fn is_subset_of(&self, other: &Self) -> bool {
1807 self.data_type <= other.data_type && self.size <= other.size
1808 }
1809
1810 fn super_union(&self, other: &Self) -> Result<Self> {
1811 Ok(Set::from((
1812 self.data_type().super_union(other.data_type())?,
1813 self.size().super_union(other.size())?,
1814 )))
1815 }
1816
1817 fn super_intersection(&self, other: &Self) -> Result<Self> {
1818 Ok(Set::from((
1819 self.data_type().super_intersection(other.data_type())?,
1820 self.size().super_intersection(other.size())?,
1821 )))
1822 }
1823
1824 fn minimal_subset(&self) -> Result<Self> {
1825 Ok(Set::from_data_type_size(DataType::Null, Integer::empty()))
1826 }
1827
1828 fn maximal_superset(&self) -> Result<Self> {
1829 Ok(Set::from_data_type_size(
1830 DataType::Any,
1831 Integer::from_max(i64::MAX),
1832 ))
1833 }
1834
1835 fn try_empty(&self) -> Result<Self> {
1836 Ok(Self::new(self.data_type().try_empty()?.into(), 0.into()))
1837 }
1838}
1839
1840impl InjectInto<DataType> for Set {
1841 type Injection = Base<Self, DataType>;
1842 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
1843 injection::From(self.clone()).into(other.clone())
1844 }
1845}
1846
1847impl From<value::Set> for Set {
1848 fn from(value: value::Set) -> Self {
1849 Set::from((
1850 (*value).iter().fold(DataType::Null, |s, d| {
1851 s.super_union(&d.data_type()).unwrap_or(DataType::Any)
1852 }),
1853 Integer::from_value((*value).len() as i64),
1854 ))
1855 }
1856}
1857
1858#[derive(Debug, Clone, Hash, PartialEq)]
1860pub struct Array {
1861 data_type: Arc<DataType>,
1862 shape: Arc<[usize]>,
1863}
1864
1865impl Array {
1866 pub fn new(data_type: Arc<DataType>, shape: Arc<[usize]>) -> Array {
1868 Array { data_type, shape }
1869 }
1870
1871 pub fn from_data_type_shape<S: AsRef<[usize]>>(data_type: DataType, shape: S) -> Array {
1872 Array::new(Arc::new(data_type), Arc::from(shape.as_ref()))
1873 }
1874
1875 pub fn data_type(&self) -> &DataType {
1876 self.data_type.as_ref()
1877 }
1878
1879 pub fn shape(&self) -> &[usize] {
1880 self.shape.as_ref()
1881 }
1882}
1883
1884impl From<(Arc<DataType>, &[usize])> for Array {
1885 fn from(data_type_shape: (Arc<DataType>, &[usize])) -> Self {
1886 Array::new(data_type_shape.0, Arc::from(data_type_shape.1))
1887 }
1888}
1889
1890impl From<(DataType, &[usize])> for Array {
1891 fn from(data_type_shape: (DataType, &[usize])) -> Self {
1892 Array::new(Arc::new(data_type_shape.0), Arc::from(data_type_shape.1))
1893 }
1894}
1895
1896impl cmp::PartialOrd for Array {
1897 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
1898 partial_cmp(self, other)
1899 }
1900}
1901
1902impl fmt::Display for Array {
1903 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1904 write!(
1905 f,
1906 "array({}, shape<({}))",
1907 self.data_type,
1908 self.shape
1909 .iter()
1910 .map(|s| { format!("{}", s) })
1911 .collect::<Vec<String>>()
1912 .join(", ")
1913 )
1914 }
1915}
1916
1917impl Variant for Array {
1918 type Element = value::Array;
1919
1920 fn contains(&self, element: &Self::Element) -> bool {
1921 element.0.len() == self.shape.iter().product::<usize>()
1922 && element.1.iter().zip(self.shape.iter()).all(|(e, d)| e == d)
1923 && element.0.iter().all(|v| self.data_type.contains(v))
1924 }
1925
1926 fn is_subset_of(&self, other: &Self) -> bool {
1928 self.data_type <= other.data_type && self.shape == other.shape
1929 }
1930
1931 fn super_union(&self, other: &Self) -> Result<Self> {
1932 Ok(Array::from((
1933 self.data_type().super_union(other.data_type())?,
1934 self.shape(),
1935 )))
1936 }
1937
1938 fn super_intersection(&self, other: &Self) -> Result<Self> {
1939 Ok(Array::from((
1940 self.data_type().super_intersection(other.data_type())?,
1941 self.shape(),
1942 )))
1943 }
1944
1945 fn try_empty(&self) -> Result<Self> {
1946 Ok(Self::new(
1947 self.data_type().try_empty()?.into(),
1948 Arc::new([0 as usize]),
1949 ))
1950 }
1951}
1952
1953impl InjectInto<DataType> for Array {
1954 type Injection = Base<Self, DataType>;
1955 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
1956 injection::From(self.clone()).into(other.clone())
1957 }
1958}
1959
1960impl From<value::Array> for Array {
1961 fn from(value: value::Array) -> Self {
1962 Array::from((
1963 (*value).0.iter().fold(DataType::Null, |s, d| {
1964 s.super_union(&d.data_type()).unwrap_or(DataType::Any)
1965 }),
1966 (*value).1.as_ref(),
1967 ))
1968 }
1969}
1970
1971pub type Date = Intervals<chrono::NaiveDate>;
1973
1974impl Variant for Date {
1975 type Element = value::Date;
1976
1977 fn contains(&self, element: &Self::Element) -> bool {
1978 self.contains(&**element)
1979 }
1980
1981 fn is_subset_of(&self, other: &Self) -> bool {
1982 self.is_subset_of(other)
1983 }
1984
1985 fn super_union(&self, other: &Self) -> Result<Self> {
1986 Ok(self.clone().union(other.clone()))
1987 }
1988
1989 fn super_intersection(&self, other: &Self) -> Result<Self> {
1990 Ok(self.clone().intersection(other.clone()))
1991 }
1992
1993 fn minimal_subset(&self) -> Result<Self> {
1994 Ok(Self::empty())
1995 }
1996
1997 fn maximal_superset(&self) -> Result<Self> {
1998 Ok(Self::full())
1999 }
2000
2001 fn try_empty(&self) -> Result<Self> {
2002 Ok(Self::empty())
2003 }
2004}
2005
2006impl InjectInto<DataType> for Date {
2007 type Injection = Base<Self, DataType>;
2008 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
2009 injection::From(self.clone()).into(other.clone())
2010 }
2011}
2012
2013impl From<value::Date> for Date {
2014 fn from(value: value::Date) -> Self {
2015 Date::from_value(*value)
2016 }
2017}
2018
2019pub type Time = Intervals<chrono::NaiveTime>;
2021
2022impl Variant for Time {
2023 type Element = value::Time;
2024
2025 fn contains(&self, element: &Self::Element) -> bool {
2026 self.contains(&**element)
2027 }
2028
2029 fn is_subset_of(&self, other: &Self) -> bool {
2030 self.is_subset_of(other)
2031 }
2032
2033 fn super_union(&self, other: &Self) -> Result<Self> {
2034 Ok(self.clone().union(other.clone()))
2035 }
2036
2037 fn super_intersection(&self, other: &Self) -> Result<Self> {
2038 Ok(self.clone().intersection(other.clone()))
2039 }
2040
2041 fn minimal_subset(&self) -> Result<Self> {
2042 Ok(Self::empty())
2043 }
2044
2045 fn maximal_superset(&self) -> Result<Self> {
2046 Ok(Self::full())
2047 }
2048
2049 fn try_empty(&self) -> Result<Self> {
2050 Ok(Self::empty())
2051 }
2052}
2053
2054impl InjectInto<DataType> for Time {
2055 type Injection = Base<Self, DataType>;
2056 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
2057 injection::From(self.clone()).into(other.clone())
2058 }
2059}
2060
2061impl From<value::Time> for Time {
2062 fn from(value: value::Time) -> Self {
2063 Time::from_value(*value)
2064 }
2065}
2066
2067pub type DateTime = Intervals<chrono::NaiveDateTime>;
2069
2070impl Variant for DateTime {
2071 type Element = value::DateTime;
2072
2073 fn contains(&self, element: &Self::Element) -> bool {
2074 self.contains(&**element)
2075 }
2076
2077 fn is_subset_of(&self, other: &Self) -> bool {
2078 self.is_subset_of(other)
2079 }
2080
2081 fn super_union(&self, other: &Self) -> Result<Self> {
2082 Ok(self.clone().union(other.clone()))
2083 }
2084
2085 fn super_intersection(&self, other: &Self) -> Result<Self> {
2086 Ok(self.clone().intersection(other.clone()))
2087 }
2088
2089 fn minimal_subset(&self) -> Result<Self> {
2090 Ok(Self::empty())
2091 }
2092
2093 fn maximal_superset(&self) -> Result<Self> {
2094 Ok(Self::full())
2095 }
2096
2097 fn try_empty(&self) -> Result<Self> {
2098 Ok(Self::empty())
2099 }
2100}
2101
2102impl InjectInto<DataType> for DateTime {
2103 type Injection = Base<Self, DataType>;
2104 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
2105 injection::From(self.clone()).into(other.clone())
2106 }
2107}
2108
2109impl From<value::DateTime> for DateTime {
2110 fn from(value: value::DateTime) -> Self {
2111 DateTime::from_value(*value)
2112 }
2113}
2114
2115pub type Duration = Intervals<chrono::Duration>;
2117
2118impl Variant for Duration {
2119 type Element = value::Duration;
2120
2121 fn contains(&self, element: &Self::Element) -> bool {
2122 self.contains(&**element)
2123 }
2124
2125 fn is_subset_of(&self, other: &Self) -> bool {
2126 self.is_subset_of(other)
2127 }
2128
2129 fn super_union(&self, other: &Self) -> Result<Self> {
2130 Ok(self.clone().union(other.clone()))
2131 }
2132
2133 fn super_intersection(&self, other: &Self) -> Result<Self> {
2134 Ok(self.clone().intersection(other.clone()))
2135 }
2136
2137 fn minimal_subset(&self) -> Result<Self> {
2138 Ok(Self::empty())
2139 }
2140
2141 fn maximal_superset(&self) -> Result<Self> {
2142 Ok(Self::full())
2143 }
2144
2145 fn try_empty(&self) -> Result<Self> {
2146 Ok(Self::empty())
2147 }
2148}
2149
2150impl InjectInto<DataType> for Duration {
2151 type Injection = Base<Self, DataType>;
2152 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
2153 injection::From(self.clone()).into(other.clone())
2154 }
2155}
2156
2157impl From<value::Duration> for Duration {
2158 fn from(value: value::Duration) -> Self {
2159 Duration::from_value(*value)
2160 }
2161}
2162
2163#[derive(Default, Debug, Clone, Hash, PartialEq)]
2165pub struct Id {
2166 reference: Option<Arc<Id>>,
2168 unique: bool,
2170 attributes: BTreeMap<String, String>,
2172}
2173
2174impl Id {
2175 pub fn new(
2176 reference: Option<Arc<Id>>,
2177 unique: bool,
2178 attributes: BTreeMap<String, String>,
2179 ) -> Id {
2180 Id {
2181 reference,
2182 unique,
2183 attributes,
2184 }
2185 }
2186
2187 pub fn reference(&self) -> Option<&Id> {
2188 self.reference.as_deref()
2189 }
2190
2191 pub fn unique(&self) -> bool {
2192 self.unique
2193 }
2194
2195 pub fn attributes(&self) -> &BTreeMap<String, String> {
2196 &self.attributes
2197 }
2198}
2199
2200impl From<(Option<Arc<Id>>, bool)> for Id {
2201 fn from(ref_unique: (Option<Arc<Id>>, bool)) -> Self {
2202 let (reference, unique) = ref_unique;
2203 Id::new(reference, unique, BTreeMap::new())
2204 }
2205}
2206
2207impl From<(Option<Id>, bool)> for Id {
2208 fn from(ref_unique: (Option<Id>, bool)) -> Self {
2209 let (reference, unique) = ref_unique;
2210 Id::new(reference.map(Arc::new), unique, BTreeMap::new())
2211 }
2212}
2213
2214impl cmp::PartialOrd for Id {
2215 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
2216 partial_cmp(self, other)
2217 }
2218}
2219
2220impl fmt::Display for Id {
2221 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2222 write!(f, "id")
2223 }
2224}
2225
2226impl Variant for Id {
2227 type Element = value::Id;
2228
2229 fn contains(&self, _: &Self::Element) -> bool {
2230 true
2231 }
2232
2233 fn is_subset_of(&self, _: &Self) -> bool {
2234 true
2235 }
2236
2237 fn super_union(&self, other: &Self) -> Result<Self> {
2239 let attributes: BTreeMap<String, String> = self
2240 .attributes
2241 .iter()
2242 .filter(|(key, value)| other.attributes.get(*key).map_or(false, |v| v == *value))
2243 .map(|(key, value)| (key.clone(), value.clone()))
2244 .collect();
2245
2246 Ok(Id::new(
2247 if self.reference == other.reference {
2248 self.reference.clone()
2249 } else {
2250 None
2251 },
2252 false,
2253 attributes,
2254 ))
2255 }
2256 fn super_intersection(&self, other: &Self) -> Result<Self> {
2258 let attributes: BTreeMap<String, String> = self
2259 .attributes
2260 .iter()
2261 .filter(|(key, value)| other.attributes.get(*key).map_or(false, |v| v == *value))
2262 .map(|(key, value)| (key.clone(), value.clone()))
2263 .collect();
2264
2265 Ok(Id::new(
2266 if self.reference == other.reference {
2267 self.reference.clone()
2268 } else {
2269 None
2270 },
2271 self.unique && other.unique,
2272 attributes,
2273 ))
2274 }
2275
2276 fn maximal_superset(&self) -> Result<Self> {
2277 Ok(Id::new(None, false, self.attributes().clone()))
2278 }
2279}
2280
2281impl From<value::Id> for Id {
2282 fn from(_value: value::Id) -> Self {
2283 Id::default()
2284 }
2285}
2286
2287impl InjectInto<DataType> for Id {
2288 type Injection = Base<Self, DataType>;
2289 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
2290 injection::From(self.clone()).into(other.clone())
2291 }
2292}
2293
2294#[derive(Debug, Clone, Hash, PartialEq)]
2296pub struct Function {
2297 pub domain: Arc<DataType>,
2298 pub co_domain: Arc<DataType>,
2299}
2300
2301impl Function {
2302 pub fn new(from: Arc<DataType>, to: Arc<DataType>) -> Function {
2303 Function {
2304 domain: from,
2305 co_domain: to,
2306 }
2307 }
2308
2309 pub fn from_data_types(from: DataType, to: DataType) -> Function {
2310 Function::new(Arc::new(from), Arc::new(to))
2311 }
2312
2313 pub fn domain(&self) -> &DataType {
2314 self.domain.as_ref()
2315 }
2316
2317 pub fn co_domain(&self) -> &DataType {
2318 self.co_domain.as_ref()
2319 }
2320}
2321
2322impl From<(Arc<DataType>, Arc<DataType>)> for Function {
2323 fn from(dom_co_dom: (Arc<DataType>, Arc<DataType>)) -> Self {
2324 let (dom, co_dom) = dom_co_dom;
2325 Function::new(dom, co_dom)
2326 }
2327}
2328
2329impl From<(DataType, DataType)> for Function {
2330 fn from(dom_co_dom: (DataType, DataType)) -> Self {
2331 let (dom, co_dom) = dom_co_dom;
2332 Function::from_data_types(dom, co_dom)
2333 }
2334}
2335
2336impl cmp::PartialOrd for Function {
2337 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
2338 partial_cmp(self, other)
2339 }
2340}
2341
2342impl fmt::Display for Function {
2343 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2344 write!(f, "{} -> {}", self.domain, self.co_domain)
2345 }
2346}
2347
2348impl Variant for Function {
2349 type Element = value::Function;
2350
2351 fn contains(&self, element: &Self::Element) -> bool {
2352 element.domain() >= *self.domain && element.co_domain() <= *self.co_domain
2353 }
2354
2355 fn is_subset_of(&self, other: &Self) -> bool {
2356 self.domain >= other.domain && self.co_domain <= other.co_domain
2357 }
2358
2359 fn super_union(&self, other: &Self) -> Result<Self> {
2360 if self.domain() == other.domain() {
2361 Ok(Function::from((
2362 self.domain().clone(),
2363 self.co_domain().super_union(other.co_domain())?,
2364 )))
2365 } else {
2366 Err(Error::no_superset(self, other))
2367 }
2368 }
2369
2370 fn super_intersection(&self, other: &Self) -> Result<Self> {
2371 Ok(Function::from((
2372 self.domain().super_union(other.domain())?,
2373 self.co_domain().super_intersection(other.co_domain())?,
2374 )))
2375 }
2376
2377 fn minimal_subset(&self) -> Result<Self> {
2378 Ok(Function::from_data_types(DataType::Any, DataType::Null))
2379 }
2380
2381 fn maximal_superset(&self) -> Result<Self> {
2382 Ok(Function::from_data_types(DataType::Null, DataType::Any))
2383 }
2384}
2385
2386impl InjectInto<DataType> for Function {
2387 type Injection = Base<Self, DataType>;
2388 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
2389 injection::From(self.clone()).into(other.clone())
2390 }
2391}
2392
2393impl From<value::Function> for Function {
2394 fn from(value: value::Function) -> Self {
2395 Function::from(((*value).domain(), (*value).co_domain()))
2396 }
2397}
2398
2399#[allow(clippy::derive_hash_xor_eq)]
2400#[derive(Debug, Clone, Hash)]
2403pub enum DataType {
2404 Null,
2406 Unit(Unit),
2408 Boolean(Boolean),
2410 Integer(Integer),
2412 Enum(Enum),
2414 Float(Float),
2416 Text(Text),
2418 Bytes(Bytes),
2420 Struct(Struct),
2422 Union(Union),
2424 Optional(Optional),
2426 List(List),
2428 Set(Set),
2430 Array(Array),
2432 Date(Date),
2434 Time(Time),
2436 DateTime(DateTime),
2438 Duration(Duration),
2440 Id(Id),
2442 Function(Function),
2445 Any,
2447}
2448
2449impl DataType {
2450 pub fn default(&self) -> DataType {
2452 match self {
2453 DataType::Unit(_) => DataType::from(Unit::default()),
2454 DataType::Boolean(_) => DataType::from(Boolean::default()),
2455 DataType::Integer(_) => DataType::from(Integer::default()),
2456 DataType::Float(_) => DataType::from(Float::default()),
2457 DataType::Text(_) => DataType::from(Text::default()),
2458 DataType::Bytes(_) => DataType::from(Bytes::default()),
2459 DataType::Date(_) => DataType::from(Date::default()),
2460 DataType::Time(_) => DataType::from(Time::default()),
2461 DataType::DateTime(_) => DataType::from(DateTime::default()),
2462 DataType::Duration(_) => DataType::from(Duration::default()),
2463 DataType::Id(_) => DataType::from(Id::default()),
2464 _ => self.clone(),
2465 }
2466 }
2467
2468 pub fn into_common_super_variant(
2470 left: &DataType,
2471 right: &DataType,
2472 ) -> Result<(DataType, DataType)> {
2473 match (left.into_variant(right), right.into_variant(left)) {
2474 (Ok(l), Ok(r)) => {
2475 let l_into_left = left.maximal_superset().and_then(|t| l.into_data_type(&t));
2476 if l_into_left.map_or(false, |t| &t == left) {
2477 Ok((l, right.clone()))
2478 } else {
2479 Ok((left.clone(), r))
2480 }
2481 }
2482 (Ok(l), Err(_)) => Ok((l, right.clone())),
2483 (Err(_), Ok(r)) => Ok((left.clone(), r)),
2484 (Err(_), Err(_)) => Err(Error::other("No common variant")),
2485 }
2486 }
2487
2488 pub fn into_common_sub_variant(
2490 left: &DataType,
2491 right: &DataType,
2492 ) -> Result<(DataType, DataType)> {
2493 match (left.into_variant(right), right.into_variant(left)) {
2494 (Ok(l), Ok(r)) => {
2495 let l_into_left = left.minimal_subset().and_then(|t| l.into_data_type(&t));
2496 if l_into_left.map_or(false, |t| &t == left) {
2497 Ok((l, right.clone()))
2498 } else {
2499 Ok((left.clone(), r))
2500 }
2501 }
2502 _ => Err(Error::other("No common variant")),
2503 }
2504 }
2505 pub fn hierarchy(&self) -> Hierarchy<&DataType> {
2508 for_all_variants!(
2509 self,
2510 x,
2511 x.hierarchy(),
2512 [Struct, Union],
2513 Hierarchy::from([(Vec::<&str>::new(), self)])
2514 )
2515 }
2516
2517 pub fn fields(&self) -> &[(String, Arc<DataType>)] {
2518 match self {
2519 DataType::Struct(s) => s.fields(),
2520 DataType::Union(u) => u.fields(),
2521 _ => panic!(),
2522 }
2523 }
2524
2525 pub fn all_values(&self) -> bool {
2526 TryInto::<Vec<Value>>::try_into(self.clone()).is_ok()
2527 }
2528}
2529
2530impl Variant for DataType {
2531 type Element = value::Value;
2532
2533 fn contains(&self, element: &Self::Element) -> bool {
2534 match (self, element) {
2535 (DataType::Null, _) => false, (DataType::Unit(_), value::Value::Unit(_)) => true,
2538 (DataType::Boolean(s), value::Value::Boolean(e)) => s.contains(e),
2539 (DataType::Integer(s), value::Value::Integer(e)) => s.contains(e),
2540 (DataType::Enum(s), value::Value::Enum(e)) => s.contains(e),
2541 (DataType::Float(s), value::Value::Float(e)) => s.contains(e),
2542 (DataType::Text(s), value::Value::Text(e)) => s.contains(e),
2543 (DataType::Bytes(_), value::Value::Bytes(_)) => true,
2544 (DataType::Struct(s), value::Value::Struct(e)) => s.contains(e),
2545 (DataType::Union(s), value::Value::Union(e)) => s.contains(e),
2546 (DataType::Optional(s), value::Value::Optional(e)) => s.contains(e),
2547 (DataType::List(s), value::Value::List(e)) => s.contains(e),
2548 (DataType::Set(s), value::Value::Set(e)) => s.contains(e),
2549 (DataType::Array(s), value::Value::Array(e)) => s.contains(e),
2550 (DataType::Date(s), value::Value::Date(e)) => s.contains(e),
2551 (DataType::Time(s), value::Value::Time(e)) => s.contains(e),
2552 (DataType::DateTime(s), value::Value::DateTime(e)) => s.contains(e),
2553 (DataType::Duration(s), value::Value::Duration(e)) => s.contains(e),
2554 (DataType::Id(s), value::Value::Id(e)) => s.contains(e),
2555 (DataType::Function(s), value::Value::Function(e)) => s.contains(e),
2556 (DataType::Any, _) => true,
2557 (s, e) => s
2558 .clone()
2559 .into_data_type(&e.data_type())
2560 .map_or(false, |s| s.contains(e)),
2561 }
2562 }
2563
2564 fn is_subset_of(&self, other: &Self) -> bool {
2565 for_all_variant_pairs!(
2567 self,
2568 other,
2569 s,
2570 o,
2571 s.is_subset_of(o),
2572 [
2573 Boolean, Integer, Enum, Float, Text, Struct, Union, Optional, List, Set, Array,
2574 Date, Time, DateTime, Duration, Id, Function
2575 ],
2576 {
2577 match (self, other) {
2578 (DataType::Null, _) => true, (DataType::Unit(_), DataType::Unit(_))
2581 | (DataType::Unit(_), DataType::Optional(_)) => true,
2582 (DataType::Bytes(_), DataType::Bytes(_)) => true,
2583 (_, DataType::Any) => true,
2584 (DataType::Any, _) => false,
2585 (s, DataType::Text(o)) => {
2586 let o = DataType::Text(o.clone());
2587 s.clone()
2588 .into_data_type(&o)
2589 .map_or(false, |s| s.is_subset_of(&o))
2590 }
2591 (s, DataType::Null) => s
2592 .clone()
2593 .into_data_type(&DataType::Null)
2594 .map_or(false, |s| s.is_subset_of(&DataType::Null)),
2595 (DataType::Float(s), DataType::Integer(o)) => {
2596 let left = DataType::Float(s.clone());
2597 let right = DataType::Integer(o.clone());
2598 left.into_data_type(&right)
2599 .map_or(false, |left| left.is_subset_of(&right))
2600 }
2601 (DataType::Integer(s), DataType::Float(o)) => {
2602 let left = DataType::Integer(s.clone());
2603 let right = DataType::Float(o.clone());
2604 left.into_data_type(&right)
2605 .map_or(false, |left| left.is_subset_of(&right))
2606 }
2607 (DataType::Boolean(s), DataType::Float(o)) => {
2608 let left = DataType::Boolean(s.clone());
2609 let right = DataType::Float(o.clone());
2610 left.into_data_type(&right)
2611 .map_or(false, |left| left.is_subset_of(&right))
2612 }
2613 (DataType::Float(s), DataType::Boolean(o)) => {
2614 let left = DataType::Float(s.clone());
2615 let right = DataType::Boolean(o.clone());
2616 left.into_data_type(&right)
2617 .map_or(false, |left| left.is_subset_of(&right))
2618 }
2619 (DataType::Boolean(s), DataType::Integer(o)) => {
2620 let left = DataType::Boolean(s.clone());
2621 let right = DataType::Integer(o.clone());
2622 left.into_data_type(&right)
2623 .map_or(false, |left| left.is_subset_of(&right))
2624 }
2625 (DataType::Integer(s), DataType::Boolean(o)) => {
2626 let left = DataType::Integer(s.clone());
2627 let right = DataType::Boolean(o.clone());
2628 left.into_data_type(&right)
2629 .map_or(false, |left| left.is_subset_of(&right))
2630 }
2631 (DataType::Date(s), DataType::DateTime(o)) => {
2632 let left = DataType::Date(s.clone());
2633 let right = DataType::DateTime(o.clone());
2634 left.into_data_type(&right)
2635 .map_or(false, |left| left.is_subset_of(&right))
2636 }
2637 (DataType::DateTime(s), DataType::Date(o)) => {
2638 let left = DataType::DateTime(s.clone());
2639 let right = DataType::Date(o.clone());
2640 left.into_data_type(&right)
2641 .map_or(false, |left| left.is_subset_of(&right))
2642 }
2643 (s, DataType::Union(o)) => {
2644 let right = DataType::Union(o.clone());
2645 s.clone()
2646 .into_data_type(&right)
2647 .map_or(false, |left| left.is_subset_of(&right))
2648 }
2649 (s, DataType::Optional(o)) => {
2650 let right = DataType::Optional(o.clone());
2651 s.clone()
2652 .into_data_type(&right)
2653 .map_or(false, |left| left.is_subset_of(&right))
2654 }
2655 _ => false,
2657 }
2658 }
2659 )
2660 }
2661
2662 fn super_union(&self, other: &Self) -> Result<Self> {
2663 for_all_variant_pairs!(
2664 self,
2665 other,
2666 s,
2667 o,
2668 Ok(DataType::from(s.super_union(o)?)),
2669 [
2670 Boolean, Integer, Enum, Float, Text, Struct, Union, Optional, List, Set, Array,
2671 Date, Time, DateTime, Duration, Id, Function
2672 ],
2673 {
2674 match (self, other) {
2675 (DataType::Null, o) => Ok(o.clone()),
2676 (s, DataType::Null) => Ok(s.clone()),
2677 (DataType::Unit(_), DataType::Unit(_)) => Ok(DataType::from(Unit)),
2678 (DataType::Bytes(_), DataType::Bytes(_)) => Ok(DataType::from(Bytes)),
2679 (DataType::Any, _) => Ok(DataType::Any),
2680 (_, DataType::Any) => Ok(DataType::Any),
2681 (s, o) => DataType::into_common_super_variant(s, o)
2683 .and_then(|(s, o)| s.super_union(&o))
2684 .or(Ok(DataType::Any)),
2685 }
2686 }
2687 )
2688 }
2689
2690 fn super_intersection(&self, other: &Self) -> Result<Self> {
2691 for_all_variant_pairs!(
2692 self,
2693 other,
2694 s,
2695 o,
2696 Ok(DataType::from(s.super_intersection(o)?)),
2697 [
2698 Boolean, Integer, Enum, Float, Text, Struct, Union, Optional, List, Set, Array,
2699 Date, Time, DateTime, Duration, Id, Function
2700 ],
2701 {
2702 match (self, other) {
2703 (DataType::Null, _) => Ok(DataType::Null),
2704 (_, DataType::Null) => Ok(DataType::Null),
2705 (DataType::Unit(_), DataType::Unit(_)) => Ok(DataType::unit()),
2706 (DataType::Bytes(_), DataType::Bytes(_)) => Ok(DataType::bytes()),
2707 (DataType::Any, o) => Ok(o.clone()),
2708 (s, DataType::Any) => Ok(s.clone()),
2709 (
2710 DataType::Optional(Optional { data_type: l }),
2711 DataType::Optional(Optional { data_type: r }),
2712 ) => DataType::super_intersection(l.as_ref(), r.as_ref()),
2713 (DataType::Optional(Optional { data_type: l }), _) => {
2714 DataType::super_intersection(l.as_ref(), other)
2715 }
2716 (_, DataType::Optional(Optional { data_type: r })) => {
2717 DataType::super_intersection(self, r.as_ref())
2718 }
2719 (s, o) => DataType::into_common_sub_variant(s, o)
2721 .or(DataType::into_common_super_variant(s, o))
2722 .and_then(|(s, o)| s.super_intersection(&o))
2723 .or(Ok(DataType::Any)),
2724 }
2725 }
2726 )
2727 }
2728
2729 fn minimal_subset(&self) -> Result<Self> {
2730 for_all_variants!(
2731 self,
2732 x,
2733 Ok(x.minimal_subset()?.into()),
2734 [
2735 Unit, Boolean, Integer, Enum, Float, Text, Bytes, Struct, Union, Optional, List,
2736 Set, Array, Date, Time, DateTime, Duration, Id, Function
2737 ],
2738 Ok(DataType::Null)
2739 )
2740 }
2741
2742 fn maximal_superset(&self) -> Result<Self> {
2743 for_all_variants!(
2744 self,
2745 x,
2746 Ok(x.maximal_superset()?.into()),
2747 [
2748 Unit, Boolean, Integer, Enum, Float, Text, Bytes, Struct, Union, Optional, List,
2749 Set, Array, Date, Time, DateTime, Duration, Id, Function
2750 ],
2751 Ok(DataType::Any)
2752 )
2753 }
2754
2755 fn try_empty(&self) -> Result<Self> {
2756 for_all_variants!(
2757 self,
2758 x,
2759 Ok(x.try_empty()?.into()),
2760 [
2761 Unit, Boolean, Integer, Enum, Float, Text, Bytes, Struct, Union, Optional, List,
2762 Set, Array, Date, Time, DateTime, Duration, Id, Function
2763 ],
2764 match self {
2765 DataType::Null => Ok(DataType::Null),
2766 _ => Err(Error::other("Cannot build an empty DataType")),
2767 }
2768 )
2769 }
2770}
2771
2772impl InjectInto<DataType> for DataType {
2773 type Injection = Base<Self, DataType>;
2774 fn inject_into(&self, other: &DataType) -> injection::Result<Base<Self, DataType>> {
2775 injection::From(self.clone()).into(other.clone())
2776 }
2777}
2778
2779macro_rules! impl_from {
2780 ( $Variant:ident ) => {
2781 impl From<$Variant> for DataType {
2782 fn from(x: $Variant) -> DataType {
2783 DataType::$Variant(x)
2784 }
2785 }
2786 };
2787}
2788
2789macro_rules! impl_conversions {
2790 ( $Variant:ident ) => {
2791 impl_from!($Variant);
2792
2793 impl TryFrom<DataType> for $Variant {
2794 type Error = Error;
2795 fn try_from(x: DataType) -> Result<Self> {
2796 match x {
2797 DataType::$Variant(t) => Ok(t),
2798 _ => Err(Error::invalid_conversion(x, stringify!($Variant))),
2799 }
2800 }
2801 }
2802
2803 impl TryFrom<&DataType> for $Variant {
2804 type Error = Error;
2805 fn try_from(x: &DataType) -> Result<Self> {
2806 match x {
2807 DataType::$Variant(t) => Ok(t.clone()),
2808 _ => Err(Error::invalid_conversion(x, stringify!($Variant))),
2809 }
2810 }
2811 }
2812 };
2813}
2814
2815impl_conversions!(Unit);
2816impl_conversions!(Boolean);
2817impl_conversions!(Integer);
2818impl_conversions!(Enum);
2819impl_conversions!(Float);
2820impl_conversions!(Text);
2821impl_conversions!(Bytes);
2822impl_conversions!(Struct);
2823impl_conversions!(Union);
2824impl_from!(Optional);
2825impl_conversions!(List);
2826impl_conversions!(Set);
2827impl_conversions!(Array);
2828impl_conversions!(Date);
2829impl_conversions!(Time);
2830impl_conversions!(DateTime);
2831impl_conversions!(Duration);
2832impl_conversions!(Id);
2833impl_conversions!(Function);
2834
2835macro_rules! impl_into_values {
2836 ( $Variant:ident ) => {
2837 impl TryInto<Vec<Value>> for $Variant {
2838 type Error = Error;
2839
2840 fn try_into(self) -> Result<Vec<Value>> {
2841 if self.all_values() {
2842 Ok(self.into_iter().map(|[v, _]| Value::from(v)).collect())
2843 } else {
2844 Err(Error::invalid_conversion(
2845 stringify!($Variant),
2846 "Vec<Value>",
2847 ))
2848 }
2849 }
2850 }
2851 };
2852}
2853
2854impl_into_values!(Boolean);
2855impl_into_values!(Integer);
2856impl_into_values!(Float);
2857impl_into_values!(Text);
2858impl_into_values!(Date);
2859impl_into_values!(Time);
2860impl_into_values!(DateTime);
2861impl_into_values!(Duration);
2862
2863impl TryInto<Vec<Value>> for DataType {
2864 type Error = Error;
2865
2866 fn try_into(self) -> Result<Vec<Value>> {
2867 match self {
2868 DataType::Unit(_) => Ok(vec![Value::unit()]),
2869 DataType::Boolean(b) => b.try_into(),
2870 DataType::Integer(i) => i.try_into(),
2871 DataType::Enum(e) => Ok(e
2872 .values
2873 .iter()
2874 .map(|(_, i)| Value::enumeration(*i, e.values.clone()))
2875 .collect()),
2876 DataType::Float(f) => f.try_into(),
2877 DataType::Text(t) => t.try_into(),
2878 DataType::Date(d) => d.try_into(),
2879 DataType::Time(t) => t.try_into(),
2880 DataType::DateTime(d) => d.try_into(),
2881 DataType::Duration(d) => d.try_into(),
2882 DataType::Struct(s) => {
2883 let vec_of_vec = s
2884 .into_iter()
2885 .map(|(f, d)| {
2886 TryInto::<Vec<Value>>::try_into(d.deref().clone()).map(|vec| {
2887 vec.into_iter()
2888 .map(|v| Value::structured(vec![(f.to_string(), v)]))
2889 .collect::<Vec<_>>()
2890 })
2891 })
2892 .collect::<Result<Vec<_>>>()?;
2893
2894 let first = vec_of_vec[0].clone();
2895 Ok(vec_of_vec
2896 .into_iter()
2897 .skip(1)
2898 .fold(first, |res, s| combine_vec_of_values(res, s)))
2899 }
2900 _ => Err(Error::invalid_conversion(
2901 stringify!($Variant),
2902 "Vec<Value>",
2903 )),
2904 }
2905 }
2906}
2907
2908fn combine_vec_of_values(a: Vec<Value>, b: Vec<Value>) -> Vec<Value> {
2909 a.into_iter()
2910 .flat_map(|k| b.clone().into_iter().map(move |v| k.clone().and(v)))
2911 .collect()
2912}
2913
2914impl cmp::PartialEq for DataType {
2915 fn eq(&self, other: &Self) -> bool {
2916 matches!(self.partial_cmp(other), Some(cmp::Ordering::Equal))
2917 }
2918}
2919
2920impl cmp::Eq for DataType {}
2922
2923impl cmp::PartialOrd for DataType {
2924 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
2925 partial_cmp(self, other)
2926 }
2927}
2928
2929impl fmt::Display for DataType {
2930 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2931 for_all_variants!(
2932 self,
2933 x,
2934 write!(f, "{}", x),
2935 [
2936 Unit, Boolean, Integer, Enum, Float, Text, Bytes, Struct, Union, Optional, List,
2937 Set, Array, Date, Time, DateTime, Duration, Id, Function
2938 ],
2939 {
2940 match self {
2941 DataType::Null => write!(f, "null"),
2942 DataType::Any => write!(f, "any"),
2943 _ => write!(f, ""),
2944 }
2945 }
2946 )
2947 }
2948}
2949
2950macro_rules! impl_default_builder {
2951 ( $Variant:ident ) => {
2952 impl DataType {
2953 paste! {
2954 pub fn [<$Variant:snake>]() -> DataType { DataType::from($Variant::default()) }
2955 }
2956 }
2957 };
2958}
2959
2960macro_rules! impl_interval_builders {
2961 ( $Intervals:ident, $Bound:ty ) => {
2962 impl DataType {
2963 paste! {
2964 pub fn [<$Intervals:snake>]() -> DataType { DataType::from($Intervals::default()) }
2965 pub fn [<$Intervals:snake _interval>](start: $Bound, end: $Bound) -> DataType { DataType::from($Intervals::from_interval(start, end)) }
2966 pub fn [<$Intervals:snake _min>](min: $Bound) -> DataType { DataType::from($Intervals::from_min(min)) }
2967 pub fn [<$Intervals:snake _max>](max: $Bound) -> DataType { DataType::from($Intervals::from_max(max)) }
2968 pub fn [<$Intervals:snake _value>](value: $Bound) -> DataType { DataType::from($Intervals::from_value(value)) }
2969 pub fn [<$Intervals:snake _values>]<V: AsRef<[$Bound]>>(values: V) -> DataType { DataType::from($Intervals::from_values(values)) }
2970 pub fn [<$Intervals:snake _range>]<R: ops::RangeBounds<$Bound>>(range: R) -> DataType { DataType::from($Intervals::from_range(range)) }
2971 }
2972 }
2973 };
2974}
2975
2976impl_default_builder!(Unit);
2978impl_interval_builders!(Boolean, bool);
2979impl_interval_builders!(Integer, i64);
2980impl_interval_builders!(Float, f64);
2981impl_interval_builders!(Text, String);
2982impl_default_builder!(Bytes);
2983impl_interval_builders!(Date, chrono::NaiveDate);
2984impl_interval_builders!(Time, chrono::NaiveTime);
2985impl_interval_builders!(DateTime, chrono::NaiveDateTime);
2986impl_interval_builders!(Duration, chrono::Duration);
2987impl_default_builder!(Id);
2988
2989impl DataType {
2991 pub fn enumeration<S: Clone + Into<String>, V: AsRef<[S]>>(values: V) -> DataType {
2992 DataType::from(Enum::from(values.as_ref()))
2993 }
2994
2995 pub fn structured<
2996 S: Clone + Into<String>,
2997 T: Clone + Into<Arc<DataType>>,
2998 F: AsRef<[(S, T)]>,
2999 >(
3000 fields: F,
3001 ) -> DataType {
3002 DataType::from(Struct::from(fields.as_ref()))
3003 }
3004
3005 pub fn structured_from_data_types<F: AsRef<[DataType]>>(fields: F) -> DataType {
3006 DataType::from(Struct::from_data_types(fields))
3007 }
3008
3009 pub fn union<S: Clone + Into<String>, F: AsRef<[(S, DataType)]>>(fields: F) -> DataType {
3010 DataType::from(Union::from(fields.as_ref()))
3011 }
3012
3013 pub fn optional(data_type: DataType) -> DataType {
3014 DataType::from(Optional::from(data_type))
3015 }
3016
3017 pub fn list(data_type: DataType, min_size: usize, max_size: usize) -> DataType {
3018 DataType::from(List::from((
3019 data_type,
3020 Integer::from_interval(min_size as i64, max_size as i64),
3021 )))
3022 }
3023
3024 pub fn set(data_type: DataType, min_size: usize, max_size: usize) -> DataType {
3025 DataType::from(Set::from((
3026 data_type,
3027 Integer::from_interval(min_size as i64, max_size as i64),
3028 )))
3029 }
3030
3031 pub fn array<S: AsRef<[usize]>>(data_type: DataType, shape: S) -> DataType {
3032 DataType::from(Array::from((data_type, shape.as_ref())))
3033 }
3034
3035 pub fn function(domain: DataType, co_domain: DataType) -> DataType {
3036 DataType::from(Function::from((domain, co_domain)))
3037 }
3038}
3039
3040impl<P: Path> Index<P> for DataType {
3041 type Output = DataType;
3042
3043 fn index(&self, index: P) -> &Self::Output {
3044 self.hierarchy()[index]
3045 }
3046}
3047
3048impl<A: Bound> TryFrom<DataType> for (Intervals<A>,)
3052where
3053 Intervals<A>: TryFrom<DataType, Error = Error>,
3054{
3055 type Error = Error;
3056 fn try_from(value: DataType) -> Result<Self> {
3057 let intervals: Intervals<A> = value.try_into()?;
3058 Ok((intervals,))
3059 }
3060}
3061
3062impl<A: Bound, B: Bound> TryFrom<DataType> for (Intervals<A>, Intervals<B>)
3064where
3065 Intervals<A>: TryFrom<DataType, Error = Error>,
3066 Intervals<B>: TryFrom<DataType, Error = Error>,
3067{
3068 type Error = Error;
3069 fn try_from(value: DataType) -> Result<Self> {
3070 let structured: Struct = value.try_into()?;
3071 let left: Intervals<A> = (*structured.data_type("0")).clone().try_into()?;
3072 let right: Intervals<B> = (*structured.data_type("1")).clone().try_into()?;
3073 Ok((left, right))
3074 }
3075}
3076
3077impl<A: Bound, B: Bound, C: Bound> TryFrom<DataType> for (Intervals<A>, Intervals<B>, Intervals<C>)
3079where
3080 Intervals<A>: TryFrom<DataType, Error = Error>,
3081 Intervals<B>: TryFrom<DataType, Error = Error>,
3082 Intervals<C>: TryFrom<DataType, Error = Error>,
3083{
3084 type Error = Error;
3085 fn try_from(value: DataType) -> Result<Self> {
3086 let structured: Struct = value.try_into()?;
3087 let inter_a: Intervals<A> = (*structured.data_type("0")).clone().try_into()?;
3088 let inter_b: Intervals<B> = (*structured.data_type("1")).clone().try_into()?;
3089 let inter_c: Intervals<C> = (*structured.data_type("2")).clone().try_into()?;
3090 Ok((inter_a, inter_b, inter_c))
3091 }
3092}
3093
3094impl<A: Bound> From<(Intervals<A>,)> for DataType
3096where
3097 Intervals<A>: Into<DataType>,
3098{
3099 fn from(value: (Intervals<A>,)) -> Self {
3100 value.0.into()
3101 }
3102}
3103
3104impl<A: Bound, B: Bound> From<(Intervals<A>, Intervals<B>)> for DataType
3106where
3107 Intervals<A>: Into<DataType>,
3108 Intervals<B>: Into<DataType>,
3109{
3110 fn from(value: (Intervals<A>, Intervals<B>)) -> Self {
3111 DataType::from(Struct::from_data_types(&[value.0.into(), value.1.into()]))
3112 }
3113}
3114
3115impl<A: Bound, B: Bound, C: Bound> From<(Intervals<A>, Intervals<B>, Intervals<C>)> for DataType
3117where
3118 Intervals<A>: Into<DataType>,
3119 Intervals<B>: Into<DataType>,
3120 Intervals<C>: Into<DataType>,
3121{
3122 fn from(value: (Intervals<A>, Intervals<B>, Intervals<C>)) -> Self {
3123 DataType::from(Struct::from_data_types(&[
3124 value.0.into(),
3125 value.1.into(),
3126 value.2.into(),
3127 ]))
3128 }
3129}
3130
3131impl And<DataType> for DataType {
3143 type Product = DataType;
3144 fn and(self, other: DataType) -> Self::Product {
3145 match self {
3147 DataType::Null => DataType::Null,
3148 DataType::Unit(_) => other,
3149 DataType::Struct(s) => s.and(other).into(),
3150 s => Struct::from_data_type(s).and(other).into(),
3151 }
3152 }
3153}
3154
3155impl<S: Into<String>, T: Into<Arc<DataType>>> And<(S, T)> for DataType {
3156 type Product = DataType;
3157 fn and(self, other: (S, T)) -> Self::Product {
3158 self.and(DataType::from(Struct::from(other)))
3159 }
3160}
3161
3162impl<T> ops::BitAnd<T> for DataType
3163where
3164 Self: And<T>,
3165{
3166 type Output = <Self as And<T>>::Product;
3167
3168 fn bitand(self, rhs: T) -> Self::Output {
3169 self.and(rhs)
3170 }
3171}
3172
3173impl Or<DataType> for DataType {
3174 type Sum = DataType;
3175 fn or(self, other: DataType) -> Self::Sum {
3176 match (self, other) {
3177 (DataType::Null, d) => d,
3178 (DataType::Unit(_), DataType::Unit(_)) => DataType::unit(),
3179 (DataType::Unit(u), d) | (d, DataType::Unit(u)) => u.or(d).into(),
3180 (DataType::Optional(o), d) | (d, DataType::Optional(o)) => o.or(d).into(),
3181 (s, o) => Union::from_data_type(s).or(o).into(),
3182 }
3183 }
3184}
3185
3186impl<S: Into<String>, T: Into<Arc<DataType>>> Or<(S, T)> for DataType {
3187 type Sum = DataType;
3188 fn or(self, other: (S, T)) -> Self::Sum {
3189 self.or(DataType::from(Union::from(other)))
3190 }
3191}
3192
3193impl<T> ops::BitOr<T> for DataType
3194where
3195 Self: Or<T>,
3196{
3197 type Output = <Self as Or<T>>::Sum;
3198
3199 fn bitor(self, rhs: T) -> Self::Output {
3200 self.or(rhs)
3201 }
3202}
3203
3204impl DataType {
3206 pub fn sum<I: IntoIterator<Item = DataType>>(data_types: I) -> DataType {
3208 data_types.into_iter().fold(DataType::Null, |s, t| s.or(t))
3209 }
3210
3211 pub fn product<I: IntoIterator<Item = DataType>>(data_types: I) -> DataType {
3213 data_types
3214 .into_iter()
3215 .fold(DataType::unit(), |s, t| s.and(t))
3216 }
3217}
3218
3219impl FromIterator<value::Value> for DataType {
3222 fn from_iter<T: IntoIterator<Item = value::Value>>(iter: T) -> Self {
3223 iter.into_iter().fold(DataType::Null, |data_type, value| {
3225 data_type
3226 .super_union(&value.into())
3227 .unwrap_or(DataType::Any)
3228 })
3229 }
3230}
3231
3232impl<'a> Acceptor<'a> for DataType {
3234 fn dependencies(&'a self) -> visitor::Dependencies<'a, Self> {
3235 match self {
3236 DataType::Struct(s) => s.fields.iter().map(|(_, t)| t.as_ref()).collect(),
3237 DataType::Union(u) => u.fields.iter().map(|(_, t)| t.as_ref()).collect(),
3238 DataType::Optional(o) => visitor::Dependencies::from([o.data_type.as_ref()]),
3239 DataType::List(l) => visitor::Dependencies::from([l.data_type.as_ref()]),
3240 DataType::Set(s) => visitor::Dependencies::from([s.data_type.as_ref()]),
3241 DataType::Array(a) => visitor::Dependencies::from([a.data_type.as_ref()]),
3242 DataType::Function(f) => {
3243 visitor::Dependencies::from([f.domain.as_ref(), f.co_domain.as_ref()])
3244 }
3245 _ => visitor::Dependencies::empty(),
3246 }
3247 }
3248}
3249
3250pub trait Visitor<'a, T: Clone> {
3254 fn structured(&self, fields: Vec<(String, T)>) -> T;
3256 fn union(&self, fields: Vec<(String, T)>) -> T;
3257 fn optional(&self, data_type: T) -> T;
3258 fn list(&self, data_type: T, size: &'a Integer) -> T;
3259 fn set(&self, data_type: T, size: &'a Integer) -> T;
3260 fn array(&self, data_type: T, shape: &'a [usize]) -> T;
3261 fn function(&self, domain: T, co_domain: T) -> T;
3262 fn primitive(&self, acceptor: &'a DataType) -> T;
3263}
3264
3265impl<'a, T: Clone, V: Visitor<'a, T>> visitor::Visitor<'a, DataType, T> for V {
3267 fn visit(&self, acceptor: &'a DataType, dependencies: visitor::Visited<'a, DataType, T>) -> T {
3268 match acceptor {
3269 DataType::Struct(s) => self.structured(
3270 s.fields
3271 .iter()
3272 .map(|(s, t)| (s.clone(), dependencies.get(t.as_ref()).clone()))
3273 .collect(),
3274 ),
3275 DataType::Union(u) => self.union(
3276 u.fields
3277 .iter()
3278 .map(|(s, t)| (s.clone(), dependencies.get(t.as_ref()).clone()))
3279 .collect(),
3280 ),
3281 DataType::Optional(o) => self.optional(dependencies.get(o.data_type()).clone()),
3282 DataType::List(l) => self.list(dependencies.get(l.data_type()).clone(), l.size()),
3283 DataType::Set(s) => self.set(dependencies.get(s.data_type()).clone(), s.size()),
3284 DataType::Array(a) => self.array(dependencies.get(a.data_type()).clone(), a.shape()),
3285 DataType::Function(f) => self.function(
3286 dependencies.get(f.domain()).clone(),
3287 dependencies.get(f.co_domain()).clone(),
3288 ),
3289 primitive => self.primitive(primitive),
3290 }
3291 }
3292}
3293
3294struct FlattenOptionalVisitor;
3296
3297impl<'a> Visitor<'a, (bool, DataType)> for FlattenOptionalVisitor {
3298 fn structured(&self, fields: Vec<(String, (bool, DataType))>) -> (bool, DataType) {
3299 fields
3300 .into_iter()
3301 .fold((false, DataType::unit()), |a, (s, (o, d))| {
3302 (a.0 || o, a.1 & (s, d))
3303 })
3304 }
3305
3306 fn union(&self, fields: Vec<(String, (bool, DataType))>) -> (bool, DataType) {
3307 fields
3308 .into_iter()
3309 .fold((false, DataType::Null), |a, (s, (o, d))| {
3310 (a.0 || o, a.1 | (s, d))
3311 })
3312 }
3313
3314 fn optional(&self, data_type: (bool, DataType)) -> (bool, DataType) {
3315 (true, data_type.1)
3316 }
3317
3318 fn list(&self, data_type: (bool, DataType), size: &'a Integer) -> (bool, DataType) {
3319 (
3320 data_type.0,
3321 List::new(Arc::new(data_type.1), size.clone()).into(),
3322 )
3323 }
3324
3325 fn set(&self, data_type: (bool, DataType), size: &'a Integer) -> (bool, DataType) {
3326 (
3327 data_type.0,
3328 Set::new(Arc::new(data_type.1), size.clone()).into(),
3329 )
3330 }
3331
3332 fn array(&self, data_type: (bool, DataType), shape: &'a [usize]) -> (bool, DataType) {
3333 (data_type.0, DataType::array(data_type.1, shape))
3334 }
3335
3336 fn function(&self, domain: (bool, DataType), co_domain: (bool, DataType)) -> (bool, DataType) {
3337 (
3338 domain.0 || co_domain.0,
3339 DataType::function(domain.1, co_domain.1),
3340 )
3341 }
3342
3343 fn primitive(&self, acceptor: &'a DataType) -> (bool, DataType) {
3344 (false, acceptor.clone())
3345 }
3346}
3347
3348impl DataType {
3349 pub fn flatten_optional(&self) -> DataType {
3351 let (is_optional, flat) = self.accept(FlattenOptionalVisitor);
3352 if is_optional {
3353 DataType::optional(flat)
3354 } else {
3355 flat
3356 }
3357 }
3358}
3359
3360impl DataType {
3362 pub fn absolute_upper_bound(&self) -> Option<f64> {
3363 match self {
3364 DataType::Boolean(b) => Some(if *b.max()? { 1. } else { 0. }),
3365 DataType::Integer(i) => Some(f64::max(i.min()?.abs() as f64, i.max()?.abs() as f64)),
3366 DataType::Float(f) => Some(f64::max(f.min()?.abs(), f.max()?.abs())),
3367 DataType::Optional(o) => o.data_type().absolute_upper_bound(),
3368 _ => None,
3369 }
3370 }
3371}
3372
3373#[cfg(test)]
3375mod tests {
3376 use super::*;
3377 use std::convert::TryFrom;
3378
3379 #[test]
3380 fn test_null() {
3381 let null = DataType::Null;
3383 println!("type = {}", null);
3384 let a_type = DataType::float_interval(-1., 3.);
3385 assert!(null <= a_type);
3386 assert!(!(a_type <= null));
3387 assert!(a_type <= a_type);
3388 assert!(null <= null);
3389 assert!(a_type == a_type);
3390 assert!(null == null);
3391 assert!(null <= DataType::Any);
3392 }
3393
3394 #[test]
3395 fn test_text() {
3396 let all_text = DataType::text();
3398 println!("type = {}", all_text);
3399 match &all_text {
3400 DataType::Text(t) => assert_eq!(t, &Text::full()),
3401 _ => (),
3402 }
3403 let some_text = DataType::from(Text::from_values(
3404 [String::from("Hello"), String::from("World")].as_ref(),
3405 ));
3406 println!("type = {}", some_text);
3407 match &some_text {
3408 DataType::Text(t) => assert_ne!(t, &Text::full()),
3409 _ => (),
3410 }
3411 assert!(some_text.is_subset_of(&all_text));
3412 assert!(some_text.is_subset_of(&some_text));
3413 assert!(all_text.is_subset_of(&all_text));
3414 assert!(some_text == some_text);
3415 assert!(all_text == all_text);
3416 assert!(!all_text.is_subset_of(&some_text));
3417 println!(
3419 "{}",
3420 DataType::integer_values(&[0, 2, 1, 3])
3421 .into_data_type(&DataType::text())
3422 .unwrap()
3423 );
3424 assert_eq!(
3425 DataType::integer_values(&[0, 2, 1, 3])
3426 .into_data_type(&DataType::text())
3427 .unwrap(),
3428 DataType::from(Text::from_values([
3429 String::from("0"),
3430 String::from("1"),
3431 String::from("2"),
3432 String::from("3")
3433 ]))
3434 )
3435 }
3436
3437 #[test]
3438 fn test_build() {
3439 let data_type = DataType::structured(&[
3440 ("i", DataType::integer()),
3441 ("j", DataType::from(Integer::from_interval(5, 20))),
3442 (
3443 "k",
3444 DataType::from(Integer::from_intervals(&[
3445 [Bound::min(), 2],
3446 [3, 4],
3447 [7, Bound::max()],
3448 ])),
3449 ),
3450 ("l", DataType::from(Integer::from_values(&[5, -2, 20]))),
3451 ]);
3452 println!("type = {}", data_type);
3453 assert_eq!(
3454 format!("{}", data_type),
3455 "struct{i: int, j: int[5 20], k: int(-∞, 2]∪[3 4]∪[7, +∞), l: int{-2, 5, 20}}",
3456 )
3457 }
3458
3459 #[test]
3460 fn test_equalities() {
3461 let empty_interval = DataType::from(Intervals::<f64>::empty());
3462 println!(
3463 "{} == {} is {}",
3464 empty_interval,
3465 DataType::Null,
3466 empty_interval == DataType::Null
3467 );
3468 println!(
3469 "{} == {} is {}",
3470 DataType::Null,
3471 empty_interval,
3472 DataType::Null == empty_interval
3473 );
3474 println!(
3475 "{}.cmp({}) = {:?}",
3476 empty_interval,
3477 DataType::Null,
3478 empty_interval.partial_cmp(&DataType::Null)
3479 );
3480 println!(
3481 "{}.cmp({}) = {:#?}",
3482 DataType::Null,
3483 empty_interval,
3484 DataType::Null.partial_cmp(&empty_interval)
3485 );
3486 assert_eq!(empty_interval, DataType::Null);
3487 assert_eq!(DataType::Null, empty_interval);
3488
3489 let s1 = DataType::structured([("a", DataType::float()), ("b", DataType::float())]);
3491 let s2 = DataType::structured([("a", DataType::boolean()), ("b", DataType::integer())]);
3492 assert!(s1 != s2);
3493
3494 let ss1 = DataType::structured([("table1", s1.clone()), ("table2", s2.clone())]);
3496 let ss2 = DataType::structured([("table1", s1.clone()), ("table2", s1.clone())]);
3497 assert!(ss1 != ss2);
3498
3499 let ss1 = DataType::union([("table1", s1.clone()), ("table2", s2.clone())]);
3501 let ss2 = DataType::union([("table1", s1.clone()), ("table2", s1.clone())]);
3502 assert!(ss1 != ss2);
3503 }
3504
3505 #[test]
3506 fn test_inequalities() {
3507 let empty_interval = DataType::from(Intervals::<f64>::empty());
3508 assert!(empty_interval <= DataType::text());
3509 println!(
3510 "{} <= {} is {}",
3511 empty_interval,
3512 DataType::Null,
3513 empty_interval <= DataType::Null
3514 );
3515 println!(
3516 "{} <= {} is {}",
3517 DataType::Null,
3518 empty_interval,
3519 DataType::Null <= empty_interval
3520 );
3521 println!(
3522 "{} <= {} is {}",
3523 empty_interval,
3524 DataType::text(),
3525 empty_interval <= DataType::text()
3526 );
3527 println!(
3528 "{} <= {} is {}",
3529 DataType::Null,
3530 DataType::text(),
3531 DataType::Null <= DataType::text()
3532 );
3533 assert!(DataType::Null <= DataType::text());
3534 assert!(DataType::text() <= DataType::text());
3535 assert!(
3536 DataType::from(Text::from_values([
3537 String::from("Qrlew"),
3538 String::from("Code")
3539 ])) <= DataType::text()
3540 );
3541 println!(
3542 "{} <= {} is {}",
3543 DataType::text(),
3544 empty_interval,
3545 DataType::text() <= empty_interval
3546 );
3547 println!(
3548 "{} <= {} is {}",
3549 DataType::text(),
3550 DataType::Null,
3551 DataType::text() <= DataType::Null
3552 );
3553 assert!(!(DataType::text() <= empty_interval));
3554 assert!(!(DataType::text() <= DataType::Null));
3555 println!(
3556 "{} <= {} is {}",
3557 DataType::float(),
3558 DataType::optional(DataType::float()),
3559 DataType::float() <= DataType::optional(DataType::float())
3560 );
3561 assert!(DataType::float() <= DataType::optional(DataType::float()));
3562 println!(
3563 "{} <= {} is {}",
3564 DataType::unit(),
3565 DataType::optional(DataType::float()),
3566 DataType::unit() <= DataType::optional(DataType::float())
3567 );
3568 assert!(DataType::unit() <= DataType::optional(DataType::float()));
3569 }
3570
3571 #[test]
3572 fn test_string_key() {
3573 let data_type = DataType::structured(&[
3574 ("i", DataType::integer()),
3575 ("j", DataType::from(Integer::from_interval(5, 20))),
3576 (
3577 "k",
3578 DataType::from(Integer::from_intervals(&[
3579 [Bound::min(), 2],
3580 [3, 4],
3581 [7, Bound::max()],
3582 ])),
3583 ),
3584 ("l", DataType::from(Integer::from_values(&[5, -2, 20]))),
3585 ]);
3586 println!("type = {}", data_type);
3587 }
3588
3589 #[test]
3590 fn test_match() {
3591 let data_type = DataType::from(Integer::from_interval(5, 20));
3592 println!("type = {}", data_type);
3593 if let DataType::Integer(intervals) = data_type.clone() {
3594 println!("min = {}", intervals.min().unwrap());
3595 }
3596 if let DataType::Integer(i) = data_type {
3597 println!("max = {}", i.max().unwrap());
3598 }
3599 }
3600
3601 #[test]
3602 fn test_partial_ord() {
3603 let a = DataType::integer_interval(0, 10);
3604 let b = DataType::float_interval(0., 10.);
3605 println!("{} <= {} is {}", a, b, a <= b);
3606 assert!(a <= b);
3607
3608 let a = DataType::integer_values(&[1, 10, 20]);
3609 let b = DataType::float_interval(0., 10.);
3610 println!("{} <= {} is {}", a, b, a <= b);
3611 assert!(!(a <= b));
3612
3613 let a = DataType::integer_values(&[1, 10, 20]);
3614 let b = DataType::float_interval(0., 30.);
3615 println!("{} <= {} is {}", a, b, a <= b);
3616 assert!(a <= b);
3617
3618 let date_a = chrono::NaiveDate::from_isoywd_opt(2022, 10, chrono::Weekday::Mon).unwrap();
3620 let date_b = date_a + chrono::Duration::days(10);
3621 let a = DataType::date_interval(date_a, date_b);
3622 let b = DataType::date_time_interval(
3623 date_a.and_hms_opt(0, 0, 0).unwrap(),
3624 date_b.and_hms_opt(0, 0, 0).unwrap(),
3625 );
3626 println!("{} <= {} is {}", a, b, a <= b);
3627 assert!(a <= b);
3628 }
3629
3630 fn print_conversions(a: &DataType, b: &DataType) {
3632 let (ca, cb) = if let Ok((ca, cb)) = DataType::into_common_super_variant(a, b) {
3633 (ca, cb)
3634 } else {
3635 (DataType::Null, DataType::Null)
3636 };
3637 println!(
3638 "a = {}, b = {}, a.into(b) = {}, b.into(a) = {}, unified(a,b) = ({},{})",
3639 a,
3640 b,
3641 a.clone().into_data_type(b).unwrap_or(DataType::Null),
3642 b.clone().into_data_type(a).unwrap_or(DataType::Null),
3643 ca,
3644 cb,
3645 );
3646 }
3647
3648 #[test]
3649 fn test_unify() {
3650 let i = DataType::integer();
3651 let f = DataType::float();
3652 let t = DataType::text();
3653 let iv = DataType::integer_interval(5, 10);
3654 let fv = DataType::float_values(&[5.7, 10.1]);
3655 let d = DataType::date();
3656 print_conversions(&i, &f);
3657 print_conversions(&i, &t);
3658 print_conversions(&f, &t);
3659 print_conversions(&i, &iv);
3660 print_conversions(&t, &fv);
3661 print_conversions(&i, &d);
3662 }
3663
3664 #[test]
3665 fn test_unify_null_struct() {
3666 let n = DataType::Null;
3667 let s = DataType::from(Struct::from_data_types(&[
3668 DataType::float(),
3669 DataType::integer(),
3670 ]));
3671 println!("{} <= {} = {}", &n, &s, n.is_subset_of(&s));
3672 println!("{} >= {} = {}", &n, &s, s.is_subset_of(&n));
3673 print_conversions(&n, &s);
3674 }
3675
3676 #[test]
3677 fn test_display() {
3678 let data_type = DataType::function(
3679 DataType::structured(&[
3680 ("name", DataType::list(DataType::text(), 100, 100)),
3681 ("age", DataType::optional(DataType::integer())),
3682 ]),
3683 DataType::float(),
3684 );
3685 println!("type = {}", data_type);
3686 }
3687
3688 #[test]
3689 #[should_panic]
3690 fn test_inconsistent_type() {
3691 let data_type = DataType::from(Float::from_interval(15.0, 7.0));
3692 println!("type = {}", data_type);
3693 }
3694
3695 #[test]
3696 fn test_struct_use() {
3697 let data_type = DataType::from(Struct::from_data_types(&[
3698 DataType::integer(),
3699 DataType::date(),
3700 ]));
3701 println!("type = {}", data_type);
3702 let structured: Struct = Struct::try_from(data_type).unwrap();
3703 assert_eq!(
3704 structured
3705 .fields()
3706 .iter()
3707 .map(|(s, _)| s.clone())
3708 .collect_vec(),
3709 vec!["0".to_string(), "1".to_string()]
3710 )
3711 }
3712
3713 #[test]
3714 fn test_struct_inclusion() {
3715 let type_a = DataType::from(Struct::from_data_types(&[
3716 DataType::float_interval(0., 2.),
3717 DataType::float_interval(-3., 3.),
3718 DataType::float_values(&[-5., 5.]),
3719 ]));
3720 let type_b = DataType::from(Struct::from_data_types(&[
3721 DataType::float(),
3722 DataType::float_interval(-3., 3.),
3723 ]));
3724 let type_c = DataType::from(Struct::from_data_types(&[
3725 DataType::float(),
3726 DataType::float(),
3727 ]));
3728 println!("a = {}, b = {}, c = {}", &type_a, &type_b, &type_c);
3729 assert!(type_a.is_subset_of(&type_b));
3730 assert!(type_a.is_subset_of(&type_c));
3731 assert!(type_b.is_subset_of(&type_c));
3732 }
3733
3734 #[test]
3735 fn test_struct_any_inclusion() {
3736 let struct_any = DataType::Any & DataType::Any;
3737 let struct_float = DataType::float_values([1., 2., 3.]) & DataType::float();
3738 println!("struct_any = {}", struct_any);
3739 println!("struct_float = {}", struct_float);
3740 println!(
3741 "struct_float ⊂ struct_any = {}",
3742 struct_float.is_subset_of(&struct_any)
3743 );
3744 assert!(struct_float.is_subset_of(&struct_any));
3745 }
3746
3747 #[test]
3748 fn test_optional_inclusion() {
3749 let typ = DataType::float();
3750 let opt = DataType::optional(typ.clone());
3751 println!("typ = {}", typ);
3752 println!("opt = {}", opt);
3753 println!(
3754 "typ.clone().into_data_type(opt) {}",
3755 typ.clone().into_data_type(&opt).unwrap()
3756 );
3757 println!("typ ⊂ opt = {}", typ.is_subset_of(&opt));
3758 println!("opt ⊄ typ = {}", opt.is_subset_of(&typ));
3760 assert!(!opt.is_subset_of(&typ));
3761 }
3762
3763 #[test]
3764 fn test_struct_and() {
3765 let a = Struct::default()
3766 .and(("a", DataType::integer_interval(-10, 10)))
3767 .and(("a", DataType::float_interval(1., 3.)));
3768 println!("a = {a}");
3769 assert_eq!(
3770 a,
3771 Struct::default().and(("a", DataType::float_values([1., 2., 3.])))
3772 );
3773
3774 let a = Struct::default()
3775 .and(DataType::float())
3776 .and(("a", DataType::integer_interval(-10, 10)))
3777 .and(DataType::float())
3778 .and(DataType::float())
3779 .and(DataType::float());
3780 let b = Struct::default()
3781 .and(("b", DataType::integer()))
3782 .and(("c", DataType::float()))
3783 .and(("d", DataType::float()))
3784 .and(("d", DataType::float()))
3785 .and(("a", DataType::float_interval(1., 3.)));
3786 println!("a = {a}");
3787 println!("b = {b}");
3788
3789 let c = a.clone().and(b.clone());
3791 let true_c = Struct::default()
3792 .and(("0", DataType::float()))
3793 .and(("a", DataType::float_values([1., 2., 3.])))
3794 .and(("1", DataType::float()))
3795 .and(("2", DataType::float()))
3796 .and(("3", DataType::float()))
3797 .and(("b", DataType::integer()))
3798 .and(("c", DataType::float()))
3799 .and(("d", DataType::float()));
3800 println!("\na and b = {c}");
3801 println!("\na and b = {true_c}");
3802 assert_eq!(c, true_c);
3803
3804 let d = a.clone().and(DataType::unit());
3806 println!("\na and unit = {d}");
3807 assert_eq!(
3808 d,
3809 Struct::default()
3810 .and(("0", DataType::float()))
3811 .and(("a", DataType::integer_interval(-10, 10)))
3812 .and(("1", DataType::float()))
3813 .and(("2", DataType::float()))
3814 .and(("3", DataType::float()))
3815 .and(("4", DataType::unit()))
3816 );
3817
3818 let e = a.clone().and(DataType::Struct(b.clone()));
3820 println!("\na and b = {e}");
3821 assert_eq!(e.fields().len(), 8);
3822 assert_eq!(
3823 e,
3824 Struct::default()
3825 .and(("0", DataType::float()))
3826 .and(("a", DataType::float_values([1., 2., 3.])))
3827 .and(("1", DataType::float()))
3828 .and(("2", DataType::float()))
3829 .and(("3", DataType::float()))
3830 .and(("b", DataType::integer()))
3831 .and(("c", DataType::float()))
3832 .and(("d", DataType::float()))
3833 );
3834
3835 let f = DataType::structured([("table1", DataType::Struct(a.clone()))])
3837 .and(DataType::Struct(b.clone()));
3838 println!("\na and struct(table1: b) = {f}");
3839 assert_eq!(
3840 f,
3841 DataType::structured([
3842 (
3843 "table1",
3844 DataType::structured([
3845 ("0", DataType::float()),
3846 ("a", DataType::integer_interval(-10, 10)),
3847 ("1", DataType::float()),
3848 ("2", DataType::float()),
3849 ("3", DataType::float())
3850 ])
3851 ),
3852 ("b", DataType::integer()),
3853 ("c", DataType::float()),
3854 ("d", DataType::float()),
3855 ("a", DataType::float_interval(1., 3.))
3856 ])
3857 );
3858
3859 let g = DataType::structured([("table1", DataType::Struct(a.clone()))]).and(
3861 DataType::structured([("table1", DataType::Struct(b.clone()))]),
3862 );
3863 println!("\nstruct(table1: a) and struct(table1: b) = {g}");
3864 assert_eq!(
3865 g,
3866 DataType::structured([(
3867 "table1",
3868 DataType::structured([
3869 ("0", DataType::float()),
3870 ("1", DataType::float()),
3871 ("2", DataType::float()),
3872 ("3", DataType::float()),
3873 ("a", DataType::float_values([1., 2., 3.])),
3874 ("b", DataType::integer()),
3875 ("c", DataType::float()),
3876 ("d", DataType::float()),
3877 ])
3878 )])
3879 );
3880
3881 let h = DataType::structured([("table1", DataType::Struct(a))])
3883 .and(DataType::structured([("table2", DataType::Struct(b))]));
3884 println!("\nstruct(table1: a) and struct(table2: b) = {h}");
3885 assert_eq!(
3886 h,
3887 DataType::structured([
3888 (
3889 "table1",
3890 DataType::structured([
3891 ("0", DataType::float()),
3892 ("a", DataType::integer_interval(-10, 10)),
3893 ("1", DataType::float()),
3894 ("2", DataType::float()),
3895 ("3", DataType::float())
3896 ])
3897 ),
3898 (
3899 "table2",
3900 DataType::structured([
3901 ("b", DataType::integer()),
3902 ("c", DataType::float()),
3903 ("d", DataType::float()),
3904 ("a", DataType::float_interval(1., 3.))
3905 ])
3906 )
3907 ])
3908 );
3909 }
3910
3911 #[test]
3912 fn test_and() {
3913 let a = DataType::unit()
3914 .and(DataType::boolean())
3915 .and(DataType::boolean())
3916 .and(DataType::float());
3917 println!("a = {}", &a);
3918 let b = DataType::unit()
3919 & ("a", DataType::boolean())
3920 & DataType::unit()
3921 & a
3922 & ("c", DataType::boolean())
3923 & ("d", DataType::float());
3924 println!("b = {b}");
3925 assert_eq!(Struct::try_from(b).unwrap().fields.len(), 6);
3926 }
3927
3928 #[test]
3929 fn test_index() {
3930 let dt = DataType::float();
3931 assert_eq!(dt[Vec::<String>::new()], dt);
3932 let dt1 = DataType::structured([("a", DataType::integer()), ("b", DataType::boolean())]);
3933 let dt2 = DataType::structured([("a", DataType::float()), ("c", DataType::integer())]);
3934 let dt = DataType::Null | ("table1", dt1.clone()) | ("table2", dt2.clone());
3935 assert_eq!(dt["table1"], dt1);
3936 assert_eq!(dt["table2"], dt2);
3937 assert_eq!(dt[["table1", "a"]], DataType::integer());
3938 assert_eq!(dt[["table2", "a"]], DataType::float());
3939 assert_eq!(dt["b"], DataType::boolean());
3940 assert_eq!(dt[["c"]], DataType::integer());
3941
3942 let a = DataType::structured([
3943 ("a_0", DataType::integer_min(-10)),
3944 ("a_1", DataType::integer()),
3945 ]);
3946 let b = DataType::structured([
3947 ("b_0", DataType::float()),
3948 ("b_1", DataType::float_interval(0., 1.)),
3949 ]);
3950 let x = DataType::structured([("a", a.clone()), ("b", b)]);
3951 println!("{}", x);
3952 assert_eq!(x[["a"]], a);
3953 assert_eq!(x[["a", "a_1"]], DataType::integer());
3954 }
3955
3956 #[test]
3957 fn test_union_or() {
3958 let a = Union::default()
3959 .or(DataType::float())
3960 .or(("a", DataType::integer()))
3961 .or(DataType::float())
3962 .or(DataType::float())
3963 .or(DataType::float());
3964 let b = Union::default()
3965 .or(("b", DataType::integer()))
3966 .or(("c", DataType::float()))
3967 .or(("d", DataType::float()))
3968 .or(("d", DataType::float()));
3969 let c = a.clone().or(b.clone());
3970 let d = a.clone().or(DataType::Null);
3971 let e = a.or(DataType::Union(b));
3972 println!("{c}");
3973 println!("{d}");
3974 println!("{e}");
3975 assert_eq!(e.fields().len(), 8);
3976 }
3977
3978 #[test]
3979 fn test_union_unit() {
3980 let a = DataType::unit().or(DataType::float());
3981 println!("{:?}", a);
3982
3983 let a = DataType::unit().and(DataType::float());
3984 println!("{:?}", a);
3985
3986 let a = DataType::float().or(DataType::unit());
3987 println!("{:?}", a);
3988
3989 let a = DataType::float().and(DataType::unit());
3990 println!("{:?}", a);
3991 }
3992
3993 #[test]
3994 fn test_union_inclusion() {
3995 let type_a = DataType::float() & DataType::float();
3996 let type_b = DataType::integer() & DataType::integer();
3997 let union_c = Union::null().or(type_a.clone()).or(type_b.clone());
3998 let union_a = Union::from_field("0", type_a);
3999 let union_b = Union::from_field("1", type_b);
4000 println!("a = {}, b = {}, c = {}\n", &union_a, &union_b, &union_c);
4001 assert!(union_a.is_subset_of(&union_c));
4002 assert!(union_b.is_subset_of(&union_c));
4003
4004 let union1 = Union::null()
4005 .or(("a", DataType::float()))
4006 .or(("b", DataType::float()));
4007 let union2 = Union::null()
4008 .or(("a", DataType::boolean()))
4009 .or(("b", DataType::integer()));
4010 assert!(union2.is_subset_of(&union1));
4011 assert!(!union1.is_subset_of(&union2));
4012 }
4013
4014 #[test]
4015 fn test_or() {
4016 let a = DataType::Null
4017 .or(DataType::boolean())
4018 .or(DataType::boolean())
4019 .or(DataType::float());
4020 println!("a = {}", &a);
4021
4022 let b = DataType::Null
4023 | ("a", DataType::boolean())
4024 | a
4025 | ("c", DataType::boolean())
4026 | ("d", DataType::float());
4027 println!("b = {b}");
4028 assert_eq!(Union::try_from(b).unwrap().fields.len(), 6);
4029
4030 assert_eq!(
4032 DataType::unit() | DataType::float(),
4033 DataType::optional(DataType::float())
4034 );
4035
4036 assert_eq!(
4038 DataType::float() | DataType::unit(),
4039 DataType::optional(DataType::float())
4040 );
4041
4042 assert_eq!(DataType::unit() | DataType::unit(), DataType::unit());
4044
4045 assert_eq!(
4047 DataType::optional(DataType::float()) | DataType::float(),
4048 DataType::optional(
4049 Union::from_data_types(vec!(DataType::float(), DataType::float()).as_slice())
4050 .into()
4051 )
4052 );
4053
4054 assert_eq!(
4056 DataType::float() | DataType::optional(DataType::float()),
4057 DataType::optional(
4058 Union::from_data_types(vec!(DataType::float(), DataType::float()).as_slice())
4059 .into()
4060 )
4061 );
4062
4063 assert_eq!(
4065 DataType::optional(DataType::float()) | DataType::optional(DataType::float()),
4066 DataType::optional(
4067 Union::from_data_types(vec!(DataType::float(), DataType::float()).as_slice())
4068 .into()
4069 )
4070 );
4071 }
4072
4073 #[test]
4074 fn test_intersection() {
4075 let left = DataType::float_interval(1., 3.);
4076 let right = DataType::integer_interval(-10, 10);
4077 let inter = left.super_intersection(&right).unwrap();
4078 println!("{left} ∩ {right} = {inter}");
4079 assert_eq!(inter, DataType::integer_interval(1, 3));
4080 assert_eq!(inter, right.super_intersection(&left).unwrap());
4081
4082 let left = DataType::integer_interval(0, 10);
4083 let right = DataType::float_interval(5., 12.);
4084
4085 let intersection = left.super_intersection(&DataType::Any).unwrap();
4086 println!("left ∩ any = {}", intersection);
4087 assert_eq!(intersection, left);
4088
4089 let intersection = right.super_intersection(&DataType::Any).unwrap();
4090 println!("right ∩ any = {}", intersection);
4091 assert_eq!(intersection, right);
4092
4093 let intersection = left.super_intersection(&DataType::Null).unwrap();
4094 println!("left ∩ ∅ = {}", intersection);
4095 assert_eq!(intersection, DataType::Null);
4096
4097 let intersection = right.super_intersection(&DataType::Null).unwrap();
4098 println!("right ∩ ∅ = {}", intersection);
4099 assert_eq!(intersection, DataType::Null);
4100
4101 let intersection = left.super_intersection(&right).unwrap();
4103 println!("{} ∩ {} = {}", left, right, intersection);
4104 assert_eq!(intersection, DataType::integer_interval(5, 10));
4105
4106 let left = DataType::integer_interval(0, 10);
4108 let right = DataType::float_values([5., 8.]);
4109 let intersection = left.super_intersection(&right).unwrap();
4110 println!("{} ∩ {} = {}", left, right, intersection);
4111 assert_eq!(intersection, DataType::integer_values([5, 8]));
4112
4113 let left = DataType::optional(DataType::integer_interval(0, 10));
4115 let right = DataType::float_values([5., 8.]);
4116 let intersection = left.super_intersection(&right).unwrap();
4117 println!("{} ∩ {} = {}", left, right, intersection);
4118 assert_eq!(intersection, DataType::integer_values([5, 8]));
4119
4120 let left = DataType::integer_interval(0, 10);
4122 let right = DataType::optional(DataType::float_values([5., 8.]));
4123 let intersection = left.super_intersection(&right).unwrap();
4124 println!("{} ∩ {} = {}", left, right, intersection);
4125 assert_eq!(intersection, DataType::integer_values([5, 8]));
4126
4127 let left = DataType::optional(DataType::integer_interval(0, 10));
4129 let right = DataType::optional(DataType::float_values([5., 8.]));
4130 let intersection = left.super_intersection(&right).unwrap();
4131 println!("{} ∩ {} = {}", left, right, intersection);
4132 assert_eq!(
4133 intersection,
4134 DataType::optional(DataType::integer_values([5, 8]))
4135 );
4136 }
4137
4138 #[test]
4139 fn test_union() {
4140 let left = DataType::integer_interval(0, 10);
4141 let right = DataType::float_interval(5., 12.);
4142
4143 let union = left.super_union(&DataType::Null).unwrap();
4144 println!("left ∪ ∅ = {}", union);
4145 assert_eq!(union, left);
4146
4147 let union = right.super_union(&DataType::Null).unwrap();
4148 println!("right ∪ ∅ = {}", union);
4149 assert_eq!(union, right);
4150
4151 let union = left.super_union(&DataType::Any).unwrap();
4152 println!("left ∪ any = {}", union);
4153 assert_eq!(union, DataType::Any);
4154
4155 let union = right.super_union(&DataType::Any).unwrap();
4156 println!("right ∪ any = {}", union);
4157 assert_eq!(union, DataType::Any);
4158
4159 let union = left.super_union(&right).unwrap();
4161 println!("{} ∪ {} = {}", left, right, union);
4162 assert!(left.is_subset_of(&union));
4163 assert!(right.is_subset_of(&union));
4164 assert_eq!(
4165 union,
4166 DataType::float_values([0., 1., 2., 3., 4.])
4167 .super_union(&DataType::float_interval(5., 12.))
4168 .unwrap()
4169 );
4170
4171 let left = DataType::integer_interval(0, 10);
4173 let right = DataType::float_values([5., 8.]);
4174 let union = left.super_union(&right).unwrap();
4175 println!("{} ∪ {} = {}", left, right, union);
4176 assert!(left.is_subset_of(&union));
4177 assert!(right.is_subset_of(&union));
4178 assert_eq!(union, DataType::integer_interval(0, 10));
4179
4180 let left = DataType::optional(DataType::integer_interval(0, 10));
4182 let right = DataType::float_values([5., 8.]);
4183 let intersection = left.super_intersection(&right).unwrap();
4184 println!("{} ∩ {} = {}", left, right, intersection);
4185 assert_eq!(intersection, DataType::integer_values([5, 8]));
4186
4187 let left = DataType::integer_interval(0, 10);
4189 let right = DataType::optional(DataType::float_values([5., 8.]));
4190 let intersection = left.super_intersection(&right).unwrap();
4191 println!("{} ∩ {} = {}", left, right, intersection);
4192 assert_eq!(intersection, DataType::integer_values([5, 8]));
4193
4194 let left = DataType::optional(DataType::integer_interval(0, 10));
4196 let right = DataType::optional(DataType::float_values([5., 8.]));
4197 let intersection = left.super_intersection(&right).unwrap();
4198 println!("{} ∩ {} = {}", left, right, intersection);
4199 assert_eq!(
4200 intersection,
4201 DataType::optional(DataType::integer_values([5, 8]))
4202 );
4203 }
4204
4205 #[test]
4206 fn test_inclusion_in_union() {
4207 let left = DataType::structured_from_data_types([DataType::float(), DataType::float()]);
4208 let right =
4209 DataType::structured_from_data_types([DataType::integer(), DataType::integer()]);
4210 let union_type = left.clone() | right.clone();
4211 println!("Union = {}", union_type);
4212 assert!(left.is_subset_of(&union_type));
4213 assert!(right.is_subset_of(&union_type));
4214 let set = DataType::structured_from_data_types([
4215 DataType::integer_interval(0, 5),
4216 DataType::integer_interval(-3, 2),
4217 ]);
4218 assert!(set.is_subset_of(&union_type));
4219 }
4220
4221 #[test]
4222 fn test_struct_intersection_and_union() {
4223 let left = DataType::unit()
4224 & ("a", DataType::integer_interval(0, 10))
4225 & ("b", DataType::integer_interval(-5, 0))
4226 & ("c", DataType::integer_interval(-1, 1));
4227 println!("left = {}", left);
4228 let right = DataType::unit()
4229 & ("a", DataType::float_interval(-2., 2.))
4230 & ("b", DataType::float_interval(-2., 2.))
4231 & ("d", DataType::float_interval(-2., 2.));
4232 println!("right = {}", right);
4233 println!(
4234 "intersection = {}",
4235 left.super_intersection(&right).unwrap()
4236 );
4237 println!("union = {}", left.super_union(&right).unwrap());
4238 assert!(left.is_subset_of(&left.super_union(&right).unwrap()));
4239 assert!(right.is_subset_of(&left.super_union(&right).unwrap()));
4240
4241 let dt = DataType::structured([
4243 (
4244 "B",
4245 DataType::structured([
4246 ("c", DataType::integer_interval(1, 8)),
4247 ("b", DataType::integer_interval(-5, 20)),
4248 ]),
4249 ),
4250 (
4251 "A",
4252 DataType::structured([
4253 ("c", DataType::integer_min(0)),
4254 ("d", DataType::integer_interval(-1, 5)),
4255 ("a", DataType::integer_interval(-2, 25)),
4256 ]),
4257 ),
4258 ]);
4259 println!("\nintersection = {}", dt.super_intersection(&dt).unwrap());
4260 println!("union = {}", dt.super_union(&dt).unwrap());
4261 let true_schema = crate::relation::Schema::from(dt.clone());
4263 assert_eq!(
4264 crate::relation::Schema::from(dt.super_intersection(&dt).unwrap()),
4265 true_schema
4266 );
4267 assert_eq!(
4268 crate::relation::Schema::from(dt.super_union(&dt).unwrap()),
4269 true_schema
4270 );
4271 }
4272
4273 #[test]
4274 fn test_list() {
4275 let il = List::from_data_type_size(
4276 DataType::integer_interval(0, 10),
4277 Integer::from_interval(1, 100),
4278 );
4279 println!("il = {il}");
4280 let fl = List::from_data_type_size(DataType::float(), Integer::from_interval(1, 100));
4281 println!("fl = {fl}");
4282 println!("il <= fl = {}", il.is_subset_of(&fl));
4283 let fld: DataType = fl.clone().into();
4284 let l = il.into_data_type(&fld).unwrap();
4285 println!("l = {l}");
4286 }
4287
4288 fn print_common_variant(left: DataType, right: DataType) {
4290 let common = DataType::into_common_super_variant(&left, &right)
4291 .unwrap_or((DataType::Null, DataType::Null));
4292 println!("({left}, {right}) ~ ({}, {})", common.0, common.1);
4293 }
4294
4295 #[test]
4296 fn test_some_common_variant() {
4297 print_common_variant(DataType::integer_interval(0, 20), DataType::text());
4298 print_common_variant(DataType::integer_interval(0, 100), DataType::float());
4299 print_common_variant(DataType::integer_interval(0, 10000), DataType::float());
4300 }
4301
4302 #[test]
4303 fn test_all_common_variant() {
4304 for_all_pairs!(
4305 print_common_variant,
4306 DataType::Null,
4307 DataType::unit(),
4308 DataType::boolean(),
4309 DataType::integer(),
4310 DataType::float(),
4311 DataType::text(),
4312 DataType::date(),
4313 DataType::time(),
4314 DataType::date_time(),
4315 DataType::duration(),
4316 DataType::Any
4317 );
4318 }
4319
4320 #[test]
4321 fn test_from_values() {
4322 let values: Vec<value::Value> = [0.0, 1.0, 2.0]
4324 .iter()
4325 .map(|x| value::Value::from(*x))
4326 .collect();
4327 println!(
4328 "values = {}",
4329 values.iter().map(ToString::to_string).join(", ")
4330 );
4331 let data_type: DataType = values.into_iter().collect();
4332 println!("data_type = {data_type}");
4333 let values: Vec<value::Value> = [3, 4, 5, 6, 7]
4335 .iter()
4336 .map(|x| value::Value::from(*x))
4337 .collect();
4338 println!(
4339 "values = {}",
4340 values.iter().map(ToString::to_string).join(", ")
4341 );
4342 let data_type: DataType = values.into_iter().collect();
4343 println!("data_type = {data_type}");
4344 let values: Vec<value::Value> = ["A", "B", "C", "Hello", "World"]
4346 .iter()
4347 .map(|x| value::Value::from(x.to_string()))
4348 .collect();
4349 println!(
4350 "values = {}",
4351 values.iter().map(ToString::to_string).join(", ")
4352 );
4353 let data_type: DataType = values.into_iter().collect();
4354 println!("data_type = {data_type}");
4355 let values: Vec<value::Value> = [
4357 chrono::NaiveDate::from_ymd_opt(2000, 1, 12)
4358 .unwrap()
4359 .and_hms_opt(2, 0, 0)
4360 .unwrap(),
4361 chrono::NaiveDate::from_ymd_opt(2020, 1, 12)
4362 .unwrap()
4363 .and_hms_opt(2, 0, 0)
4364 .unwrap(),
4365 ]
4366 .iter()
4367 .map(|x| value::Value::from(*x))
4368 .collect();
4369 println!(
4370 "values = {}",
4371 values.iter().map(ToString::to_string).join(", ")
4372 );
4373 let data_type: DataType = values.into_iter().collect();
4374 println!("data_type = {data_type}");
4375 }
4376
4377 #[test]
4378 fn test_try_into_values() {
4379 let dt = DataType::float_values([-1., 2., 3.]);
4380 assert_eq!(
4381 TryInto::<Vec<Value>>::try_into(dt).unwrap(),
4382 vec![(-1.0).into(), 2.0.into(), 3.0.into()]
4383 );
4384
4385 let dt = DataType::float_interval(1., 1.);
4386 assert_eq!(
4387 TryInto::<Vec<Value>>::try_into(dt).unwrap(),
4388 vec![1.0.into()]
4389 );
4390
4391 let dt = DataType::float_interval(1., 3.);
4392 assert!(TryInto::<Vec<Value>>::try_into(dt).is_err());
4393
4394 let dt = DataType::structured(vec![
4396 ("a", DataType::integer_values([1, 2])),
4397 ("b", DataType::float_value(3.)),
4398 (
4399 "c",
4400 DataType::text_values(["e".to_string(), "f".to_string(), "g".to_string()]),
4401 ),
4402 ]);
4403 let values = TryInto::<Vec<Value>>::try_into(dt).unwrap();
4404 assert_eq!(
4405 values,
4406 vec![
4407 Value::structured([
4408 ("a", Value::from(1)),
4409 ("b", Value::from(3.)),
4410 ("c", Value::from("e".to_string()))
4411 ]),
4412 Value::structured([
4413 ("a", Value::from(1)),
4414 ("b", Value::from(3.)),
4415 ("c", Value::from("f".to_string()))
4416 ]),
4417 Value::structured([
4418 ("a", Value::from(1)),
4419 ("b", Value::from(3.)),
4420 ("c", Value::from("g".to_string()))
4421 ]),
4422 Value::structured([
4423 ("a", Value::from(2)),
4424 ("b", Value::from(3.)),
4425 ("c", Value::from("e".to_string()))
4426 ]),
4427 Value::structured([
4428 ("a", Value::from(2)),
4429 ("b", Value::from(3.)),
4430 ("c", Value::from("f".to_string()))
4431 ]),
4432 Value::structured([
4433 ("a", Value::from(2)),
4434 ("b", Value::from(3.)),
4435 ("c", Value::from("g".to_string()))
4436 ]),
4437 ]
4438 )
4439 }
4440
4441 #[test]
4442 fn test_into_common_super_variant() {
4443 let left = DataType::integer_interval(3, 7);
4445 let right = DataType::integer_interval(2, 5);
4446 let (new_left, new_right) = DataType::into_common_super_variant(&left, &right).unwrap();
4447 println!("( {}, {} ) -> ( {}, {} )", left, right, new_left, new_right);
4448 assert_eq!(new_right, right);
4449 assert_eq!(new_left, left);
4450
4451 let left = DataType::float_values([7., 10.5]);
4453 let right = DataType::integer_interval(2, 5);
4454 let (new_left, new_right) = DataType::into_common_super_variant(&left, &right).unwrap();
4455 println!("( {}, {} ) -> ( {}, {} )", left, right, new_left, new_right);
4456 assert_eq!(new_left, left);
4457 assert_eq!(new_right, DataType::float_values([2., 3., 4., 5.]));
4458
4459 let left = DataType::optional(DataType::integer_interval(3, 7));
4461 let right = DataType::optional(DataType::integer_interval(2, 5));
4462 let (new_left, new_right) = DataType::into_common_super_variant(&left, &right).unwrap();
4463 println!("( {}, {} ) -> ( {}, {} )", left, right, new_left, new_right);
4464 assert_eq!(new_left, left);
4465 assert_eq!(new_right, right);
4466
4467 let left = DataType::integer_interval(3, 7);
4469 let right = DataType::optional(DataType::integer_interval(2, 5));
4470 let (new_left, new_right) = DataType::into_common_super_variant(&left, &right).unwrap();
4471 println!("( {}, {} ) -> ( {}, {} )", left, right, new_left, new_right);
4472 assert_eq!(new_left, DataType::optional(left));
4473 assert_eq!(new_right, right);
4474 }
4475
4476 #[test]
4477 fn test_into_common_sub_variant() {
4478 let left = DataType::integer_interval(3, 7);
4480 let right = DataType::integer_interval(2, 5);
4481 let (new_left, new_right) = DataType::into_common_sub_variant(&left, &right).unwrap();
4482 println!("( {}, {} ) -> ( {}, {} )", left, right, new_left, new_right);
4483 assert_eq!(new_right, right);
4484 assert_eq!(new_left, left);
4485
4486 let left = DataType::float_interval(0., 10.);
4488 let right = DataType::float_max(9.);
4489 let (new_left, new_right) = DataType::into_common_sub_variant(&left, &right).unwrap();
4490 println!("( {}, {} ) -> ( {}, {} )", left, right, new_left, new_right);
4491 assert_eq!(new_right, right);
4492 assert_eq!(new_left, left);
4493
4494 let left = DataType::float_values([7., 10.]);
4496 let right = DataType::integer_interval(2, 5);
4497 let (new_left, new_right) = DataType::into_common_sub_variant(&left, &right).unwrap();
4498 println!("( {}, {} ) -> ( {}, {} )", left, right, new_left, new_right);
4499 assert_eq!(new_left, DataType::integer_values([7, 10]));
4500 assert_eq!(new_right, right);
4501
4502 let left = DataType::float();
4504 let right = DataType::integer_interval(2, 5);
4505 assert!(DataType::into_common_sub_variant(&left, &right).is_err());
4506
4507 let left = DataType::optional(DataType::integer_interval(3, 7));
4509 let right = DataType::optional(DataType::integer_interval(2, 5));
4510 let (new_left, new_right) = DataType::into_common_sub_variant(&left, &right).unwrap();
4511 println!("( {}, {} ) -> ( {}, {} )", left, right, new_left, new_right);
4512 assert_eq!(new_left, left);
4513 assert_eq!(new_right, right);
4514
4515 let left = DataType::integer_interval(3, 7);
4517 let right = DataType::optional(DataType::integer_interval(2, 5));
4518 assert!(DataType::into_common_sub_variant(&left, &right).is_err());
4519 }
4520
4521 #[test]
4522 fn test_hierarchy() {
4523 let dt_float = DataType::float();
4524 let dt_int = DataType::integer();
4525 let struct_dt =
4526 DataType::structured([("a", DataType::float()), ("b", DataType::integer())]);
4527 println!("{}", struct_dt.hierarchy());
4528 let correct_hierarchy = Hierarchy::from([(vec!["a"], &dt_float), (vec!["b"], &dt_int)]);
4529 assert_eq!(struct_dt.hierarchy(), correct_hierarchy);
4530 let struct_dt2 =
4531 DataType::structured([("a", DataType::integer()), ("c", DataType::integer())]);
4532 let union_dt = DataType::union([
4533 ("table1", struct_dt.clone()),
4534 ("table2", struct_dt2.clone()),
4535 ]);
4536 let correct_hierarchy = Hierarchy::from([
4537 (vec!["table1"], &struct_dt),
4538 (vec!["table2"], &struct_dt2),
4539 (vec!["table1", "a"], &dt_float),
4540 (vec!["table1", "b"], &dt_int),
4541 (vec!["table2", "a"], &dt_int),
4542 (vec!["table2", "c"], &dt_int),
4543 ]);
4544 let h = union_dt.hierarchy();
4545 println!("{}", h);
4546 assert_eq!(h, correct_hierarchy);
4547 }
4548
4549 #[test]
4550 fn test_flatten_optional() {
4551 let a = DataType::unit()
4552 & DataType::float()
4553 & DataType::optional(DataType::integer_interval(0, 10));
4554 println!("a = {a}");
4555 println!("flat opt a = {}", a.flatten_optional());
4556 assert_eq!(
4557 a.flatten_optional(),
4558 DataType::optional(
4559 DataType::unit() & DataType::float() & DataType::integer_interval(0, 10)
4560 )
4561 );
4562 let b = DataType::unit() & DataType::float() & DataType::integer_interval(0, 10);
4563 println!("b = {b}");
4564 println!("flat opt b = {}", b.flatten_optional());
4565 assert_eq!(
4566 b.flatten_optional(),
4567 DataType::unit() & DataType::float() & DataType::integer_interval(0, 10)
4568 );
4569 }
4570
4571 #[test]
4572 fn test_try_empty() {
4573 assert_eq!(
4574 DataType::boolean().try_empty().unwrap(),
4575 Boolean::empty().into()
4576 );
4577 let dt = DataType::structured([
4578 ("bool", DataType::boolean()),
4579 ("int", DataType::integer()),
4580 ("float", DataType::float()),
4581 ("date", DataType::date()),
4582 ]);
4583 assert_eq!(
4584 dt.try_empty().unwrap(),
4585 DataType::structured([
4586 ("bool", DataType::from(Boolean::empty())),
4587 ("int", DataType::from(Integer::empty())),
4588 ("float", DataType::from(Float::empty())),
4589 ("date", DataType::from(Date::empty())),
4590 ])
4591 );
4592
4593 let dt_union = DataType::union([("bool", DataType::boolean()), ("struct", dt.clone())]);
4594 assert_eq!(
4595 dt_union.try_empty().unwrap(),
4596 DataType::union([
4597 ("bool", DataType::from(Boolean::empty())),
4598 ("struct", dt.try_empty().unwrap()),
4599 ])
4600 );
4601 }
4602
4603 #[test]
4604 fn test_id() {
4605 let id = Id::new(
4606 None,
4607 false,
4608 BTreeMap::from([("base".to_string(), "string".to_string())]),
4609 );
4610 let left = DataType::Id(id.clone());
4611 let right = DataType::Id(id);
4612
4613 let union = left.super_union(&right).unwrap();
4614 println!("left ∪ right = {}", union);
4615 assert_eq!(union, left);
4616
4617 let intersection = left.super_intersection(&right).unwrap();
4618 println!("left ∩ left = {}", intersection);
4619 assert_eq!(intersection, left);
4620
4621 let left = DataType::Id(Id::new(
4622 None,
4623 false,
4624 BTreeMap::from([("base".to_string(), "string".to_string())]),
4625 ));
4626 let right = DataType::Id(Id::new(
4627 None,
4628 false,
4629 BTreeMap::from([("base".to_string(), "int".to_string())]),
4630 ));
4631
4632 let union = left.super_union(&right).unwrap();
4633 println!("left ∪ right = {}", union);
4634 assert_eq!(union, DataType::Id(Id::new(None, false, BTreeMap::new())));
4635
4636 let intersection = left.super_intersection(&right).unwrap();
4637 println!("left ∩ right = {}", intersection);
4638 assert_eq!(
4639 intersection,
4640 DataType::Id(Id::new(None, false, BTreeMap::new()))
4641 );
4642
4643 let left = DataType::Id(Id::new(
4644 None,
4645 false,
4646 BTreeMap::from([("base".to_string(), "string".to_string())]),
4647 ));
4648 let right = DataType::Id(Id::new(None, false, BTreeMap::new()));
4649
4650 let union = left.super_union(&right).unwrap();
4651 println!("left ∪ right = {}", union);
4652 assert_eq!(union, DataType::Id(Id::new(None, false, BTreeMap::new())));
4653
4654 let intersection = left.super_intersection(&right).unwrap();
4655 println!("left ∩ right = {}", intersection);
4656 assert_eq!(
4657 intersection,
4658 DataType::Id(Id::new(None, false, BTreeMap::new()))
4659 );
4660 }
4661}