1use super::{LiteralValue, OwnedColumn, TableRef};
2use crate::base::{
3 math::decimal::Precision,
4 posql_time::{PoSQLTimeUnit, PoSQLTimeZone},
5 scalar::{Scalar, ScalarExt},
6 slice_ops::slice_cast_with,
7};
8use alloc::vec::Vec;
9use bumpalo::Bump;
10use core::{
11 fmt,
12 fmt::{Display, Formatter},
13 mem::size_of,
14};
15use serde::{Deserialize, Serialize};
16use sqlparser::ast::Ident;
17
18#[derive(Debug, Eq, PartialEq, Clone, Copy)]
25#[non_exhaustive]
26pub enum Column<'a, S: Scalar> {
27 Boolean(&'a [bool]),
29 Uint8(&'a [u8]),
31 TinyInt(&'a [i8]),
33 SmallInt(&'a [i16]),
35 Int(&'a [i32]),
37 BigInt(&'a [i64]),
39 Int128(&'a [i128]),
41 VarChar((&'a [&'a str], &'a [S])),
45 Decimal75(Precision, i8, &'a [S]),
48 TimestampTZ(PoSQLTimeUnit, PoSQLTimeZone, &'a [i64]),
53 Scalar(&'a [S]),
55 VarBinary((&'a [&'a [u8]], &'a [S])),
57}
58
59impl<'a, S: Scalar> Column<'a, S> {
60 #[must_use]
62 pub fn column_type(&self) -> ColumnType {
63 match self {
64 Self::Boolean(_) => ColumnType::Boolean,
65 Self::Uint8(_) => ColumnType::Uint8,
66 Self::TinyInt(_) => ColumnType::TinyInt,
67 Self::SmallInt(_) => ColumnType::SmallInt,
68 Self::Int(_) => ColumnType::Int,
69 Self::BigInt(_) => ColumnType::BigInt,
70 Self::VarChar(_) => ColumnType::VarChar,
71 Self::Int128(_) => ColumnType::Int128,
72 Self::Scalar(_) => ColumnType::Scalar,
73 Self::Decimal75(precision, scale, _) => ColumnType::Decimal75(*precision, *scale),
74 Self::TimestampTZ(time_unit, timezone, _) => {
75 ColumnType::TimestampTZ(*time_unit, *timezone)
76 }
77 Self::VarBinary(..) => ColumnType::VarBinary,
78 }
79 }
80 #[must_use]
84 pub fn len(&self) -> usize {
85 match self {
86 Self::Boolean(col) => col.len(),
87 Self::Uint8(col) => col.len(),
88 Self::TinyInt(col) => col.len(),
89 Self::SmallInt(col) => col.len(),
90 Self::Int(col) => col.len(),
91 Self::BigInt(col) | Self::TimestampTZ(_, _, col) => col.len(),
92 Self::VarChar((col, scals)) => {
93 assert_eq!(col.len(), scals.len());
94 col.len()
95 }
96 Self::VarBinary((col, scals)) => {
97 assert_eq!(col.len(), scals.len());
98 col.len()
99 }
100 Self::Int128(col) => col.len(),
101 Self::Scalar(col) | Self::Decimal75(_, _, col) => col.len(),
102 }
103 }
104 #[must_use]
106 pub fn is_empty(&self) -> bool {
107 self.len() == 0
108 }
109
110 pub fn from_literal_with_length(
112 literal: &LiteralValue,
113 length: usize,
114 alloc: &'a Bump,
115 ) -> Self {
116 match literal {
117 LiteralValue::Boolean(value) => {
118 Column::Boolean(alloc.alloc_slice_fill_copy(length, *value))
119 }
120 LiteralValue::Uint8(value) => {
121 Column::Uint8(alloc.alloc_slice_fill_copy(length, *value))
122 }
123 LiteralValue::TinyInt(value) => {
124 Column::TinyInt(alloc.alloc_slice_fill_copy(length, *value))
125 }
126 LiteralValue::SmallInt(value) => {
127 Column::SmallInt(alloc.alloc_slice_fill_copy(length, *value))
128 }
129 LiteralValue::Int(value) => Column::Int(alloc.alloc_slice_fill_copy(length, *value)),
130 LiteralValue::BigInt(value) => {
131 Column::BigInt(alloc.alloc_slice_fill_copy(length, *value))
132 }
133 LiteralValue::Int128(value) => {
134 Column::Int128(alloc.alloc_slice_fill_copy(length, *value))
135 }
136 LiteralValue::Scalar(value) => {
137 Column::Scalar(alloc.alloc_slice_fill_copy(length, (*value).into()))
138 }
139 LiteralValue::Decimal75(precision, scale, value) => Column::Decimal75(
140 *precision,
141 *scale,
142 alloc.alloc_slice_fill_copy(length, value.into_scalar()),
143 ),
144 LiteralValue::TimeStampTZ(tu, tz, value) => {
145 Column::TimestampTZ(*tu, *tz, alloc.alloc_slice_fill_copy(length, *value))
146 }
147 LiteralValue::VarChar(string) => Column::VarChar((
148 alloc.alloc_slice_fill_with(length, |_| alloc.alloc_str(string) as &str),
149 alloc.alloc_slice_fill_copy(length, S::from(string)),
150 )),
151 LiteralValue::VarBinary(bytes) => {
152 let bytes_slice = alloc
154 .alloc_slice_fill_with(length, |_| alloc.alloc_slice_copy(bytes) as &[_]);
155
156 let scalars =
158 alloc.alloc_slice_fill_copy(length, S::from_byte_slice_via_hash(bytes));
159
160 Column::VarBinary((bytes_slice, scalars))
161 }
162 }
163 }
164
165 pub fn rho(length: usize, alloc: &'a Bump) -> Self {
167 let raw_rho = (0..length as i128).collect::<Vec<_>>();
168 let rho = alloc.alloc_slice_copy(raw_rho.as_slice());
169 Column::<S>::Int128(rho as &[_])
170 }
171
172 pub fn from_owned_column(owned_column: &'a OwnedColumn<S>, alloc: &'a Bump) -> Self {
174 match owned_column {
175 OwnedColumn::Boolean(col) => Column::Boolean(col.as_slice()),
176 OwnedColumn::Uint8(col) => Column::Uint8(col.as_slice()),
177 OwnedColumn::TinyInt(col) => Column::TinyInt(col.as_slice()),
178 OwnedColumn::SmallInt(col) => Column::SmallInt(col.as_slice()),
179 OwnedColumn::Int(col) => Column::Int(col.as_slice()),
180 OwnedColumn::BigInt(col) => Column::BigInt(col.as_slice()),
181 OwnedColumn::Int128(col) => Column::Int128(col.as_slice()),
182 OwnedColumn::Decimal75(precision, scale, col) => {
183 Column::Decimal75(*precision, *scale, col.as_slice())
184 }
185 OwnedColumn::Scalar(col) => Column::Scalar(col.as_slice()),
186 OwnedColumn::VarChar(col) => {
187 let scalars = col.iter().map(S::from).collect::<Vec<_>>();
188 let strs = col
189 .iter()
190 .map(|s| s.as_str() as &'a str)
191 .collect::<Vec<_>>();
192 Column::VarChar((
193 alloc.alloc_slice_clone(strs.as_slice()),
194 alloc.alloc_slice_copy(scalars.as_slice()),
195 ))
196 }
197 OwnedColumn::VarBinary(col) => {
198 let scalars = col
199 .iter()
200 .map(|b| S::from_byte_slice_via_hash(b))
201 .collect::<Vec<_>>();
202 let bytes = col.iter().map(|s| s as &'a [u8]).collect::<Vec<_>>();
203 Column::VarBinary((
204 alloc.alloc_slice_clone(&bytes),
205 alloc.alloc_slice_copy(scalars.as_slice()),
206 ))
207 }
208 OwnedColumn::TimestampTZ(tu, tz, col) => Column::TimestampTZ(*tu, *tz, col.as_slice()),
209 }
210 }
211
212 pub(crate) fn as_boolean(&self) -> Option<&'a [bool]> {
214 match self {
215 Self::Boolean(col) => Some(col),
216 _ => None,
217 }
218 }
219
220 pub(crate) fn as_uint8(&self) -> Option<&'a [u8]> {
222 match self {
223 Self::Uint8(col) => Some(col),
224 _ => None,
225 }
226 }
227
228 pub(crate) fn as_tinyint(&self) -> Option<&'a [i8]> {
230 match self {
231 Self::TinyInt(col) => Some(col),
232 _ => None,
233 }
234 }
235
236 pub(crate) fn as_smallint(&self) -> Option<&'a [i16]> {
238 match self {
239 Self::SmallInt(col) => Some(col),
240 _ => None,
241 }
242 }
243
244 pub(crate) fn as_int(&self) -> Option<&'a [i32]> {
246 match self {
247 Self::Int(col) => Some(col),
248 _ => None,
249 }
250 }
251
252 pub(crate) fn as_bigint(&self) -> Option<&'a [i64]> {
254 match self {
255 Self::BigInt(col) => Some(col),
256 _ => None,
257 }
258 }
259
260 pub(crate) fn as_int128(&self) -> Option<&'a [i128]> {
262 match self {
263 Self::Int128(col) => Some(col),
264 _ => None,
265 }
266 }
267
268 pub(crate) fn as_scalar(&self) -> Option<&'a [S]> {
270 match self {
271 Self::Scalar(col) => Some(col),
272 _ => None,
273 }
274 }
275
276 pub(crate) fn as_decimal75(&self) -> Option<&'a [S]> {
278 match self {
279 Self::Decimal75(_, _, col) => Some(col),
280 _ => None,
281 }
282 }
283
284 pub(crate) fn as_varchar(&self) -> Option<(&'a [&'a str], &'a [S])> {
286 match self {
287 Self::VarChar((col, scals)) => Some((col, scals)),
288 _ => None,
289 }
290 }
291
292 pub(crate) fn as_varbinary(&self) -> Option<(&'a [&'a [u8]], &'a [S])> {
294 match self {
295 Self::VarBinary((col, scals)) => Some((col, scals)),
296 _ => None,
297 }
298 }
299
300 pub(crate) fn as_timestamptz(&self) -> Option<&'a [i64]> {
302 match self {
303 Self::TimestampTZ(_, _, col) => Some(col),
304 _ => None,
305 }
306 }
307
308 pub(crate) fn scalar_at(&self, index: usize) -> Option<S> {
312 (index < self.len()).then_some(match self {
313 Self::Boolean(col) => S::from(col[index]),
314 Self::Uint8(col) => S::from(col[index]),
315 Self::TinyInt(col) => S::from(col[index]),
316 Self::SmallInt(col) => S::from(col[index]),
317 Self::Int(col) => S::from(col[index]),
318 Self::BigInt(col) | Self::TimestampTZ(_, _, col) => S::from(col[index]),
319 Self::Int128(col) => S::from(col[index]),
320 Self::Scalar(col) | Self::Decimal75(_, _, col) => col[index],
321 Self::VarChar((_, scals)) | Self::VarBinary((_, scals)) => scals[index],
322 })
323 }
324
325 #[tracing::instrument(name = "Column::to_scalar", level = "debug", skip_all)]
327 pub(crate) fn to_scalar(self) -> Vec<S> {
328 match self {
329 Self::Boolean(col) => slice_cast_with(col, |b| S::from(b)),
330 Self::Decimal75(_, _, col) => slice_cast_with(col, |s| *s),
331 Self::VarChar((_, values)) => slice_cast_with(values, |s| *s),
332 Self::VarBinary((_, values)) => slice_cast_with(values, |s| *s),
333 Self::Uint8(col) => slice_cast_with(col, |i| S::from(i)),
334 Self::TinyInt(col) => slice_cast_with(col, |i| S::from(i)),
335 Self::SmallInt(col) => slice_cast_with(col, |i| S::from(i)),
336 Self::Int(col) => slice_cast_with(col, |i| S::from(i)),
337 Self::BigInt(col) => slice_cast_with(col, |i| S::from(i)),
338 Self::Int128(col) => slice_cast_with(col, |i| S::from(i)),
339 Self::Scalar(col) => slice_cast_with(col, |i| S::from(i)),
340 Self::TimestampTZ(_, _, col) => slice_cast_with(col, |i| S::from(i)),
341 }
342 }
343}
344
345#[derive(Eq, PartialEq, Debug, Clone, Hash, Serialize, Deserialize, Copy)]
351#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
352pub enum ColumnType {
353 #[serde(alias = "BOOLEAN", alias = "boolean")]
355 Boolean,
356 #[serde(alias = "UINT8", alias = "uint8")]
358 Uint8,
359 #[serde(alias = "TINYINT", alias = "tinyint")]
361 TinyInt,
362 #[serde(alias = "SMALLINT", alias = "smallint")]
364 SmallInt,
365 #[serde(alias = "INT", alias = "int")]
367 Int,
368 #[serde(alias = "BIGINT", alias = "bigint")]
370 BigInt,
371 #[serde(rename = "Decimal", alias = "DECIMAL", alias = "decimal")]
373 Int128,
374 #[serde(alias = "VARCHAR", alias = "varchar")]
376 VarChar,
377 #[serde(rename = "Decimal75", alias = "DECIMAL75", alias = "decimal75")]
379 Decimal75(Precision, i8),
380 #[serde(alias = "TIMESTAMP", alias = "timestamp")]
382 #[cfg_attr(test, proptest(skip))]
383 TimestampTZ(PoSQLTimeUnit, PoSQLTimeZone),
384 #[serde(alias = "SCALAR", alias = "scalar")]
386 #[cfg_attr(test, proptest(skip))]
387 Scalar,
388 #[serde(alias = "BINARY", alias = "BINARY")]
390 VarBinary,
391}
392
393impl ColumnType {
394 #[must_use]
396 pub fn is_numeric(&self) -> bool {
397 matches!(
398 self,
399 ColumnType::Uint8
400 | ColumnType::TinyInt
401 | ColumnType::SmallInt
402 | ColumnType::Int
403 | ColumnType::BigInt
404 | ColumnType::Int128
405 | ColumnType::Scalar
406 | ColumnType::Decimal75(_, _)
407 )
408 }
409
410 #[must_use]
412 pub fn is_integer(&self) -> bool {
413 matches!(
414 self,
415 ColumnType::Uint8
416 | ColumnType::TinyInt
417 | ColumnType::SmallInt
418 | ColumnType::Int
419 | ColumnType::BigInt
420 | ColumnType::Int128
421 )
422 }
423
424 #[must_use]
428 #[cfg_attr(not(test), expect(dead_code))]
429 #[expect(clippy::trivially_copy_pass_by_ref)]
430 fn sqrt_negative_min(&self) -> Option<u64> {
431 match self {
432 ColumnType::TinyInt => Some(11),
433 ColumnType::SmallInt => Some(181),
434 ColumnType::Int => Some(46_340),
435 ColumnType::BigInt => Some(3_037_000_499),
436 ColumnType::Int128 => Some(13_043_817_825_332_782_212),
437 _ => None,
438 }
439 }
440
441 fn to_integer_bits(self) -> Option<usize> {
443 match self {
444 ColumnType::Uint8 | ColumnType::TinyInt => Some(8),
445 ColumnType::SmallInt => Some(16),
446 ColumnType::Int => Some(32),
447 ColumnType::BigInt => Some(64),
448 ColumnType::Int128 => Some(128),
449 _ => None,
450 }
451 }
452
453 fn from_signed_integer_bits(bits: usize) -> Option<Self> {
457 match bits {
458 8 => Some(ColumnType::TinyInt),
459 16 => Some(ColumnType::SmallInt),
460 32 => Some(ColumnType::Int),
461 64 => Some(ColumnType::BigInt),
462 128 => Some(ColumnType::Int128),
463 _ => None,
464 }
465 }
466
467 fn from_unsigned_integer_bits(bits: usize) -> Option<Self> {
471 match bits {
472 8 => Some(ColumnType::Uint8),
473 _ => None,
474 }
475 }
476
477 #[must_use]
481 pub fn max_integer_type(&self, other: &Self) -> Option<Self> {
482 if !self.is_integer() || !other.is_integer() {
484 return None;
485 }
486 self.to_integer_bits().and_then(|self_bits| {
487 other
488 .to_integer_bits()
489 .and_then(|other_bits| Self::from_signed_integer_bits(self_bits.max(other_bits)))
490 })
491 }
492
493 #[must_use]
497 pub fn max_unsigned_integer_type(&self, other: &Self) -> Option<Self> {
498 if !self.is_integer() || !other.is_integer() {
500 return None;
501 }
502 self.to_integer_bits().and_then(|self_bits| {
503 other
504 .to_integer_bits()
505 .and_then(|other_bits| Self::from_unsigned_integer_bits(self_bits.max(other_bits)))
506 })
507 }
508
509 #[must_use]
511 pub fn precision_value(&self) -> Option<u8> {
512 match self {
513 Self::Uint8 | Self::TinyInt => Some(3_u8),
514 Self::SmallInt => Some(5_u8),
515 Self::Int => Some(10_u8),
516 Self::BigInt | Self::TimestampTZ(_, _) => Some(19_u8),
517 Self::Int128 => Some(39_u8),
518 Self::Decimal75(precision, _) => Some(precision.value()),
519 Self::Scalar => Some(0_u8),
522 Self::Boolean | Self::VarChar | Self::VarBinary => None,
523 }
524 }
525 #[must_use]
527 pub fn scale(&self) -> Option<i8> {
528 match self {
529 Self::Decimal75(_, scale) => Some(*scale),
530 Self::TinyInt
531 | Self::Uint8
532 | Self::SmallInt
533 | Self::Int
534 | Self::BigInt
535 | Self::Int128
536 | Self::Scalar => Some(0),
537 Self::Boolean | Self::VarBinary | Self::VarChar => None,
538 Self::TimestampTZ(tu, _) => match tu {
539 PoSQLTimeUnit::Second => Some(0),
540 PoSQLTimeUnit::Millisecond => Some(3),
541 PoSQLTimeUnit::Microsecond => Some(6),
542 PoSQLTimeUnit::Nanosecond => Some(9),
543 },
544 }
545 }
546
547 #[must_use]
549 pub fn byte_size(&self) -> usize {
550 match self {
551 Self::Boolean => size_of::<bool>(),
552 Self::Uint8 => size_of::<u8>(),
553 Self::TinyInt => size_of::<i8>(),
554 Self::SmallInt => size_of::<i16>(),
555 Self::Int => size_of::<i32>(),
556 Self::BigInt | Self::TimestampTZ(_, _) => size_of::<i64>(),
557 Self::Int128 => size_of::<i128>(),
558 Self::Scalar | Self::Decimal75(_, _) | Self::VarBinary | Self::VarChar => {
559 size_of::<[u64; 4]>()
560 }
561 }
562 }
563
564 #[expect(clippy::cast_possible_truncation)]
565 #[must_use]
567 pub fn bit_size(&self) -> u32 {
568 self.byte_size() as u32 * 8
569 }
570
571 #[must_use]
573 pub const fn is_signed(&self) -> bool {
574 match self {
575 Self::TinyInt
576 | Self::SmallInt
577 | Self::Int
578 | Self::BigInt
579 | Self::Int128
580 | Self::TimestampTZ(_, _) => true,
581 Self::Decimal75(_, _)
582 | Self::Scalar
583 | Self::VarBinary
584 | Self::VarChar
585 | Self::Boolean
586 | Self::Uint8 => false,
587 }
588 }
589
590 #[must_use]
592 pub fn min_scalar<S: Scalar>(&self) -> Option<S> {
593 match self {
594 ColumnType::TinyInt => Some(S::from(i8::MIN)),
595 ColumnType::SmallInt => Some(S::from(i16::MIN)),
596 ColumnType::Int => Some(S::from(i32::MIN)),
597 ColumnType::BigInt => Some(S::from(i64::MIN)),
598 ColumnType::Int128 => Some(S::from(i128::MIN)),
599 _ => None,
600 }
601 }
602}
603
604impl Display for ColumnType {
606 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
607 match self {
608 ColumnType::Boolean => write!(f, "BOOLEAN"),
609 ColumnType::Uint8 => write!(f, "UINT8"),
610 ColumnType::TinyInt => write!(f, "TINYINT"),
611 ColumnType::SmallInt => write!(f, "SMALLINT"),
612 ColumnType::Int => write!(f, "INT"),
613 ColumnType::BigInt => write!(f, "BIGINT"),
614 ColumnType::Int128 => write!(f, "DECIMAL"),
615 ColumnType::Decimal75(precision, scale) => {
616 write!(
617 f,
618 "DECIMAL75(PRECISION: {:?}, SCALE: {scale})",
619 precision.value()
620 )
621 }
622 ColumnType::VarChar => write!(f, "VARCHAR"),
623 ColumnType::VarBinary => write!(f, "BINARY"),
624 ColumnType::Scalar => write!(f, "SCALAR"),
625 ColumnType::TimestampTZ(timeunit, timezone) => {
626 write!(f, "TIMESTAMP(TIMEUNIT: {timeunit}, TIMEZONE: {timezone})")
627 }
628 }
629 }
630}
631
632#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
634pub struct ColumnRef {
635 column_id: Ident,
636 table_ref: TableRef,
637 column_type: ColumnType,
638}
639
640impl ColumnRef {
641 #[must_use]
643 pub fn new(table_ref: TableRef, column_id: Ident, column_type: ColumnType) -> Self {
644 Self {
645 column_id,
646 table_ref,
647 column_type,
648 }
649 }
650
651 #[must_use]
653 pub fn table_ref(&self) -> TableRef {
654 self.table_ref.clone()
655 }
656
657 #[must_use]
659 pub fn column_id(&self) -> Ident {
660 self.column_id.clone()
661 }
662
663 #[must_use]
665 pub fn column_type(&self) -> &ColumnType {
666 &self.column_type
667 }
668}
669
670#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
675pub struct ColumnField {
676 name: Ident,
677 data_type: ColumnType,
678}
679
680impl ColumnField {
681 #[must_use]
683 pub fn new(name: Ident, data_type: ColumnType) -> ColumnField {
684 ColumnField { name, data_type }
685 }
686
687 #[must_use]
689 pub fn name(&self) -> Ident {
690 self.name.clone()
691 }
692
693 #[must_use]
695 pub fn data_type(&self) -> ColumnType {
696 self.data_type
697 }
698}
699
700#[cfg(test)]
701mod tests {
702 use super::*;
703 use crate::{base::scalar::test_scalar::TestScalar, proof_primitive::dory::DoryScalar};
704 use alloc::{string::String, vec};
705
706 #[test]
707 fn column_type_serializes_to_string() {
708 let column_type = ColumnType::TimestampTZ(PoSQLTimeUnit::Second, PoSQLTimeZone::utc());
709 let serialized = serde_json::to_string(&column_type).unwrap();
710 assert_eq!(serialized, r#"{"TimestampTZ":["Second",{"offset":0}]}"#);
711
712 let column_type = ColumnType::Boolean;
713 let serialized = serde_json::to_string(&column_type).unwrap();
714 assert_eq!(serialized, r#""Boolean""#);
715
716 let column_type = ColumnType::TinyInt;
717 let serialized = serde_json::to_string(&column_type).unwrap();
718 assert_eq!(serialized, r#""TinyInt""#);
719
720 let column_type = ColumnType::SmallInt;
721 let serialized = serde_json::to_string(&column_type).unwrap();
722 assert_eq!(serialized, r#""SmallInt""#);
723
724 let column_type = ColumnType::Int;
725 let serialized = serde_json::to_string(&column_type).unwrap();
726 assert_eq!(serialized, r#""Int""#);
727
728 let column_type = ColumnType::BigInt;
729 let serialized = serde_json::to_string(&column_type).unwrap();
730 assert_eq!(serialized, r#""BigInt""#);
731
732 let column_type = ColumnType::Int128;
733 let serialized = serde_json::to_string(&column_type).unwrap();
734 assert_eq!(serialized, r#""Decimal""#);
735
736 let column_type = ColumnType::VarChar;
737 let serialized = serde_json::to_string(&column_type).unwrap();
738 assert_eq!(serialized, r#""VarChar""#);
739
740 let column_type = ColumnType::Scalar;
741 let serialized = serde_json::to_string(&column_type).unwrap();
742 assert_eq!(serialized, r#""Scalar""#);
743
744 let column_type = ColumnType::Decimal75(Precision::new(1).unwrap(), 0);
745 let serialized = serde_json::to_string(&column_type).unwrap();
746 assert_eq!(serialized, r#"{"Decimal75":[1,0]}"#);
747 }
748
749 #[test]
750 fn we_can_deserialize_columns_from_valid_strings() {
751 let expected_column_type =
752 ColumnType::TimestampTZ(PoSQLTimeUnit::Second, PoSQLTimeZone::utc());
753 let deserialized: ColumnType =
754 serde_json::from_str(r#"{"TimestampTZ":["Second",{"offset":0}]}"#).unwrap();
755 assert_eq!(deserialized, expected_column_type);
756
757 let expected_column_type = ColumnType::Boolean;
758 let deserialized: ColumnType = serde_json::from_str(r#""Boolean""#).unwrap();
759 assert_eq!(deserialized, expected_column_type);
760
761 let expected_column_type = ColumnType::TinyInt;
762 let deserialized: ColumnType = serde_json::from_str(r#""TinyInt""#).unwrap();
763 assert_eq!(deserialized, expected_column_type);
764
765 let expected_column_type = ColumnType::SmallInt;
766 let deserialized: ColumnType = serde_json::from_str(r#""SmallInt""#).unwrap();
767 assert_eq!(deserialized, expected_column_type);
768
769 let expected_column_type = ColumnType::Int;
770 let deserialized: ColumnType = serde_json::from_str(r#""Int""#).unwrap();
771 assert_eq!(deserialized, expected_column_type);
772
773 let expected_column_type = ColumnType::BigInt;
774 let deserialized: ColumnType = serde_json::from_str(r#""BigInt""#).unwrap();
775 assert_eq!(deserialized, expected_column_type);
776
777 let expected_column_type = ColumnType::TinyInt;
778 let deserialized: ColumnType = serde_json::from_str(r#""TINYINT""#).unwrap();
779 assert_eq!(deserialized, expected_column_type);
780
781 let expected_column_type = ColumnType::SmallInt;
782 let deserialized: ColumnType = serde_json::from_str(r#""SMALLINT""#).unwrap();
783 assert_eq!(deserialized, expected_column_type);
784
785 let expected_column_type = ColumnType::Int128;
786 let deserialized: ColumnType = serde_json::from_str(r#""DECIMAL""#).unwrap();
787 assert_eq!(deserialized, expected_column_type);
788
789 let expected_column_type = ColumnType::Int128;
790 let deserialized: ColumnType = serde_json::from_str(r#""Decimal""#).unwrap();
791 assert_eq!(deserialized, expected_column_type);
792
793 let expected_column_type = ColumnType::VarChar;
794 let deserialized: ColumnType = serde_json::from_str(r#""VarChar""#).unwrap();
795 assert_eq!(deserialized, expected_column_type);
796
797 let expected_column_type = ColumnType::Scalar;
798 let deserialized: ColumnType = serde_json::from_str(r#""SCALAR""#).unwrap();
799 assert_eq!(deserialized, expected_column_type);
800
801 let expected_column_type = ColumnType::Decimal75(Precision::new(75).unwrap(), i8::MAX);
802 let deserialized: ColumnType = serde_json::from_str(r#"{"Decimal75":[75, 127]}"#).unwrap();
803 assert_eq!(deserialized, expected_column_type);
804
805 let expected_column_type =
806 ColumnType::Decimal75(Precision::new(u8::MIN + 1).unwrap(), i8::MIN);
807 let deserialized: ColumnType = serde_json::from_str(r#"{"Decimal75":[1, -128]}"#).unwrap();
808 assert_eq!(deserialized, expected_column_type);
809
810 let expected_column_type = ColumnType::Decimal75(Precision::new(1).unwrap(), 0);
811 let deserialized: ColumnType = serde_json::from_str(r#"{"Decimal75":[1, 0]}"#).unwrap();
812 assert_eq!(deserialized, expected_column_type);
813 }
814
815 #[test]
816 fn we_can_deserialize_columns_from_lowercase_or_uppercase_strings() {
817 assert_eq!(
818 serde_json::from_str::<ColumnType>(r#""boolean""#).unwrap(),
819 ColumnType::Boolean
820 );
821 assert_eq!(
822 serde_json::from_str::<ColumnType>(r#""BOOLEAN""#).unwrap(),
823 ColumnType::Boolean
824 );
825
826 assert_eq!(
827 serde_json::from_str::<ColumnType>(r#""bigint""#).unwrap(),
828 ColumnType::BigInt
829 );
830 assert_eq!(
831 serde_json::from_str::<ColumnType>(r#""BIGINT""#).unwrap(),
832 ColumnType::BigInt
833 );
834 assert_eq!(
835 serde_json::from_str::<ColumnType>(r#""TINYINT""#).unwrap(),
836 ColumnType::TinyInt
837 );
838 assert_eq!(
839 serde_json::from_str::<ColumnType>(r#""tinyint""#).unwrap(),
840 ColumnType::TinyInt
841 );
842 assert_eq!(
843 serde_json::from_str::<ColumnType>(r#""SMALLINT""#).unwrap(),
844 ColumnType::SmallInt
845 );
846 assert_eq!(
847 serde_json::from_str::<ColumnType>(r#""smallint""#).unwrap(),
848 ColumnType::SmallInt
849 );
850 assert_eq!(
851 serde_json::from_str::<ColumnType>(r#""int""#).unwrap(),
852 ColumnType::Int
853 );
854 assert_eq!(
855 serde_json::from_str::<ColumnType>(r#""INT""#).unwrap(),
856 ColumnType::Int
857 );
858 assert_eq!(
859 serde_json::from_str::<ColumnType>(r#""decimal""#).unwrap(),
860 ColumnType::Int128
861 );
862 assert_eq!(
863 serde_json::from_str::<ColumnType>(r#""DECIMAL""#).unwrap(),
864 ColumnType::Int128
865 );
866
867 assert_eq!(
868 serde_json::from_str::<ColumnType>(r#""VARCHAR""#).unwrap(),
869 ColumnType::VarChar
870 );
871 assert_eq!(
872 serde_json::from_str::<ColumnType>(r#""varchar""#).unwrap(),
873 ColumnType::VarChar
874 );
875
876 assert_eq!(
877 serde_json::from_str::<ColumnType>(r#""SCALAR""#).unwrap(),
878 ColumnType::Scalar
879 );
880 assert_eq!(
881 serde_json::from_str::<ColumnType>(r#""scalar""#).unwrap(),
882 ColumnType::Scalar
883 );
884 assert_eq!(
885 serde_json::from_str::<ColumnType>(r#"{"decimal75":[1,0]}"#).unwrap(),
886 ColumnType::Decimal75(Precision::new(1).unwrap(), 0)
887 );
888 assert_eq!(
889 serde_json::from_str::<ColumnType>(r#"{"DECIMAL75":[1,0]}"#).unwrap(),
890 ColumnType::Decimal75(Precision::new(1).unwrap(), 0)
891 );
892
893 assert_eq!(
894 serde_json::from_str::<ColumnType>(r#"{"decimal75":[10,5]}"#).unwrap(),
895 ColumnType::Decimal75(Precision::new(10).unwrap(), 5)
896 );
897
898 assert_eq!(
899 serde_json::from_str::<ColumnType>(r#"{"DECIMAL75":[1,-128]}"#).unwrap(),
900 ColumnType::Decimal75(Precision::new(1).unwrap(), -128)
901 );
902 }
903
904 #[test]
905 fn we_cannot_deserialize_columns_from_invalid_strings() {
906 let deserialized: Result<ColumnType, _> = serde_json::from_str(r#""BooLean""#);
907 assert!(deserialized.is_err());
908
909 let deserialized: Result<ColumnType, _> = serde_json::from_str(r#""Tinyint""#);
910 assert!(deserialized.is_err());
911
912 let deserialized: Result<ColumnType, _> = serde_json::from_str(r#""Smallint""#);
913 assert!(deserialized.is_err());
914
915 let deserialized: Result<ColumnType, _> = serde_json::from_str(r#""iNt""#);
916 assert!(deserialized.is_err());
917
918 let deserialized: Result<ColumnType, _> = serde_json::from_str(r#""Bigint""#);
919 assert!(deserialized.is_err());
920
921 let deserialized: Result<ColumnType, _> = serde_json::from_str(r#""DecImal""#);
922 assert!(deserialized.is_err());
923
924 let deserialized: Result<ColumnType, _> = serde_json::from_str(r#""DecImal75""#);
925 assert!(deserialized.is_err());
926
927 let deserialized: Result<ColumnType, _> =
928 serde_json::from_str(r#"{"TimestampTZ":["Utc","Second"]}"#);
929 assert!(deserialized.is_err());
930
931 let deserialized: Result<ColumnType, _> = serde_json::from_str(r#""Varchar""#);
932 assert!(deserialized.is_err());
933
934 let deserialized: Result<ColumnType, _> = serde_json::from_str(r#""ScaLar""#);
935 assert!(deserialized.is_err());
936 }
937
938 #[test]
939 fn we_can_convert_columntype_to_json_string_and_back() {
940 let boolean = ColumnType::Boolean;
941 let boolean_json = serde_json::to_string(&boolean).unwrap();
942 assert_eq!(boolean_json, "\"Boolean\"");
943 assert_eq!(
944 serde_json::from_str::<ColumnType>(&boolean_json).unwrap(),
945 boolean
946 );
947
948 let tinyint = ColumnType::TinyInt;
949 let tinyint_json = serde_json::to_string(&tinyint).unwrap();
950 assert_eq!(tinyint_json, "\"TinyInt\"");
951 assert_eq!(
952 serde_json::from_str::<ColumnType>(&tinyint_json).unwrap(),
953 tinyint
954 );
955
956 let smallint = ColumnType::SmallInt;
957 let smallint_json = serde_json::to_string(&smallint).unwrap();
958 assert_eq!(smallint_json, "\"SmallInt\"");
959 assert_eq!(
960 serde_json::from_str::<ColumnType>(&smallint_json).unwrap(),
961 smallint
962 );
963
964 let int = ColumnType::Int;
965 let int_json = serde_json::to_string(&int).unwrap();
966 assert_eq!(int_json, "\"Int\"");
967 assert_eq!(serde_json::from_str::<ColumnType>(&int_json).unwrap(), int);
968
969 let bigint = ColumnType::BigInt;
970 let bigint_json = serde_json::to_string(&bigint).unwrap();
971 assert_eq!(bigint_json, "\"BigInt\"");
972 assert_eq!(
973 serde_json::from_str::<ColumnType>(&bigint_json).unwrap(),
974 bigint
975 );
976
977 let int128 = ColumnType::Int128;
978 let int128_json = serde_json::to_string(&int128).unwrap();
979 assert_eq!(int128_json, "\"Decimal\"");
980 assert_eq!(
981 serde_json::from_str::<ColumnType>(&int128_json).unwrap(),
982 int128
983 );
984
985 let varchar = ColumnType::VarChar;
986 let varchar_json = serde_json::to_string(&varchar).unwrap();
987 assert_eq!(varchar_json, "\"VarChar\"");
988 assert_eq!(
989 serde_json::from_str::<ColumnType>(&varchar_json).unwrap(),
990 varchar
991 );
992
993 let scalar = ColumnType::Scalar;
994 let scalar_json = serde_json::to_string(&scalar).unwrap();
995 assert_eq!(scalar_json, "\"Scalar\"");
996 assert_eq!(
997 serde_json::from_str::<ColumnType>(&scalar_json).unwrap(),
998 scalar
999 );
1000
1001 let decimal75 = ColumnType::Decimal75(Precision::new(75).unwrap(), 0);
1002 let decimal75_json = serde_json::to_string(&decimal75).unwrap();
1003 assert_eq!(decimal75_json, r#"{"Decimal75":[75,0]}"#);
1004 assert_eq!(
1005 serde_json::from_str::<ColumnType>(&decimal75_json).unwrap(),
1006 decimal75
1007 );
1008 }
1009
1010 #[test]
1011 fn we_can_get_the_len_of_a_column() {
1012 let precision = 10;
1013 let scale = 2;
1014
1015 let scalar_values = [
1016 TestScalar::from(1),
1017 TestScalar::from(2),
1018 TestScalar::from(3),
1019 ];
1020
1021 let column = Column::<DoryScalar>::Boolean(&[true, false, true]);
1023 assert_eq!(column.len(), 3);
1024 assert!(!column.is_empty());
1025
1026 let column = Column::<DoryScalar>::TinyInt(&[1, 2, 3]);
1027 assert_eq!(column.len(), 3);
1028 assert!(!column.is_empty());
1029
1030 let column = Column::<TestScalar>::SmallInt(&[1, 2, 3]);
1031 assert_eq!(column.len(), 3);
1032 assert!(!column.is_empty());
1033
1034 let column = Column::<TestScalar>::Int(&[1, 2, 3]);
1035 assert_eq!(column.len(), 3);
1036 assert!(!column.is_empty());
1037
1038 let column = Column::<TestScalar>::BigInt(&[1, 2, 3]);
1039 assert_eq!(column.len(), 3);
1040 assert!(!column.is_empty());
1041
1042 let column = Column::VarChar((&["a", "b", "c"], &scalar_values));
1043 assert_eq!(column.len(), 3);
1044 assert!(!column.is_empty());
1045
1046 let column = Column::<DoryScalar>::Int128(&[1, 2, 3]);
1047 assert_eq!(column.len(), 3);
1048 assert!(!column.is_empty());
1049
1050 let column = Column::Scalar(&scalar_values);
1051 assert_eq!(column.len(), 3);
1052 assert!(!column.is_empty());
1053
1054 let decimal_data = [
1055 TestScalar::from(1),
1056 TestScalar::from(2),
1057 TestScalar::from(3),
1058 ];
1059
1060 let precision = Precision::new(precision).unwrap();
1061 let column = Column::Decimal75(precision, scale, &decimal_data);
1062 assert_eq!(column.len(), 3);
1063 assert!(!column.is_empty());
1064
1065 let column = Column::<DoryScalar>::Boolean(&[]);
1067 assert_eq!(column.len(), 0);
1068 assert!(column.is_empty());
1069
1070 let column = Column::<DoryScalar>::TinyInt(&[]);
1071 assert_eq!(column.len(), 0);
1072 assert!(column.is_empty());
1073
1074 let column = Column::<TestScalar>::SmallInt(&[]);
1075 assert_eq!(column.len(), 0);
1076 assert!(column.is_empty());
1077
1078 let column = Column::<TestScalar>::Int(&[]);
1079 assert_eq!(column.len(), 0);
1080 assert!(column.is_empty());
1081
1082 let column = Column::<TestScalar>::BigInt(&[]);
1083 assert_eq!(column.len(), 0);
1084 assert!(column.is_empty());
1085
1086 let column = Column::<DoryScalar>::VarChar((&[], &[]));
1087 assert_eq!(column.len(), 0);
1088 assert!(column.is_empty());
1089
1090 let column = Column::<TestScalar>::Int128(&[]);
1091 assert_eq!(column.len(), 0);
1092 assert!(column.is_empty());
1093
1094 let column = Column::<DoryScalar>::Scalar(&[]);
1095 assert_eq!(column.len(), 0);
1096 assert!(column.is_empty());
1097
1098 let column: Column<'_, TestScalar> = Column::Decimal75(precision, scale, &[]);
1099 assert_eq!(column.len(), 0);
1100 assert!(column.is_empty());
1101 }
1102
1103 #[test]
1104 fn we_can_convert_owned_columns_to_columns_round_trip() {
1105 let alloc = Bump::new();
1106 let owned_col: OwnedColumn<TestScalar> = OwnedColumn::Int128(vec![1, 2, 3, 4, 5]);
1108 let col = Column::<TestScalar>::from_owned_column(&owned_col, &alloc);
1109 assert_eq!(col, Column::Int128(&[1, 2, 3, 4, 5]));
1110 let new_owned_col = (&col).into();
1111 assert_eq!(owned_col, new_owned_col);
1112
1113 let owned_col: OwnedColumn<TestScalar> =
1115 OwnedColumn::Boolean(vec![true, false, true, false, true]);
1116 let col = Column::<TestScalar>::from_owned_column(&owned_col, &alloc);
1117 assert_eq!(col, Column::Boolean(&[true, false, true, false, true]));
1118 let new_owned_col = (&col).into();
1119 assert_eq!(owned_col, new_owned_col);
1120
1121 let strs = [
1123 "Space and Time",
1124 "Tér és Idő",
1125 "Пространство и время",
1126 "Spațiu și Timp",
1127 "Spazju u Ħin",
1128 ];
1129 let scalars = strs.iter().map(TestScalar::from).collect::<Vec<_>>();
1130 let owned_col = OwnedColumn::VarChar(
1131 strs.iter()
1132 .map(ToString::to_string)
1133 .collect::<Vec<String>>(),
1134 );
1135 let col = Column::<TestScalar>::from_owned_column(&owned_col, &alloc);
1136 assert_eq!(col, Column::VarChar((&strs, &scalars)));
1137 let new_owned_col = (&col).into();
1138 assert_eq!(owned_col, new_owned_col);
1139
1140 let scalars: Vec<TestScalar> = [1, 2, 3, 4, 5].iter().map(TestScalar::from).collect();
1142 let owned_col: OwnedColumn<TestScalar> =
1143 OwnedColumn::Decimal75(Precision::new(75).unwrap(), 127, scalars.clone());
1144 let col = Column::<TestScalar>::from_owned_column(&owned_col, &alloc);
1145 assert_eq!(
1146 col,
1147 Column::Decimal75(Precision::new(75).unwrap(), 127, &scalars)
1148 );
1149 let new_owned_col = (&col).into();
1150 assert_eq!(owned_col, new_owned_col);
1151 }
1152
1153 #[test]
1154 fn we_can_get_the_data_size_of_a_column() {
1155 let column = Column::<DoryScalar>::Boolean(&[true, false, true]);
1156 assert_eq!(column.column_type().byte_size(), 1);
1157 assert_eq!(column.column_type().bit_size(), 8);
1158
1159 let column = Column::<TestScalar>::TinyInt(&[1, 2, 3, 4]);
1160 assert_eq!(column.column_type().byte_size(), 1);
1161 assert_eq!(column.column_type().bit_size(), 8);
1162
1163 let column = Column::<TestScalar>::SmallInt(&[1, 2, 3, 4]);
1164 assert_eq!(column.column_type().byte_size(), 2);
1165 assert_eq!(column.column_type().bit_size(), 16);
1166
1167 let column = Column::<TestScalar>::Int(&[1, 2, 3]);
1168 assert_eq!(column.column_type().byte_size(), 4);
1169 assert_eq!(column.column_type().bit_size(), 32);
1170
1171 let column = Column::<TestScalar>::BigInt(&[1]);
1172 assert_eq!(column.column_type().byte_size(), 8);
1173 assert_eq!(column.column_type().bit_size(), 64);
1174
1175 let column = Column::<DoryScalar>::Int128(&[1, 2]);
1176 assert_eq!(column.column_type().byte_size(), 16);
1177 assert_eq!(column.column_type().bit_size(), 128);
1178
1179 let scalar_values = [
1180 TestScalar::from(1),
1181 TestScalar::from(2),
1182 TestScalar::from(3),
1183 ];
1184
1185 let column = Column::VarChar((&["a", "b", "c", "d", "e"], &scalar_values));
1186 assert_eq!(column.column_type().byte_size(), 32);
1187 assert_eq!(column.column_type().bit_size(), 256);
1188
1189 let column = Column::Scalar(&scalar_values);
1190 assert_eq!(column.column_type().byte_size(), 32);
1191 assert_eq!(column.column_type().bit_size(), 256);
1192
1193 let precision = 10;
1194 let scale = 2;
1195 let decimal_data = [
1196 TestScalar::from(1),
1197 TestScalar::from(2),
1198 TestScalar::from(3),
1199 ];
1200
1201 let precision = Precision::new(precision).unwrap();
1202 let column = Column::Decimal75(precision, scale, &decimal_data);
1203 assert_eq!(column.column_type().byte_size(), 32);
1204 assert_eq!(column.column_type().bit_size(), 256);
1205
1206 let column: Column<'_, DoryScalar> =
1207 Column::TimestampTZ(PoSQLTimeUnit::Second, PoSQLTimeZone::utc(), &[1, 2, 3]);
1208 assert_eq!(column.column_type().byte_size(), 8);
1209 assert_eq!(column.column_type().bit_size(), 64);
1210 }
1211
1212 #[test]
1213 fn we_can_get_length_of_varbinary_column() {
1214 let raw_bytes: &[&[u8]] = &[b"foo", b"bar", b""];
1215 let scalars: Vec<TestScalar> = raw_bytes
1216 .iter()
1217 .map(|b| TestScalar::from_le_bytes_mod_order(b))
1218 .collect();
1219
1220 let column = Column::VarBinary((raw_bytes, &scalars));
1221 assert_eq!(column.len(), 3);
1222 assert!(!column.is_empty());
1223 assert_eq!(column.column_type(), ColumnType::VarBinary);
1224 }
1225
1226 #[test]
1227 fn we_can_convert_varbinary_owned_column_to_column_and_back() {
1228 use bumpalo::Bump;
1229 let alloc = Bump::new();
1230
1231 let owned_varbinary = OwnedColumn::VarBinary(vec![b"abc".to_vec(), b"xyz".to_vec()]);
1232
1233 let column = Column::<TestScalar>::from_owned_column(&owned_varbinary, &alloc);
1234 match column {
1235 Column::VarBinary((bytes, scalars)) => {
1236 assert_eq!(bytes.len(), 2);
1237 assert_eq!(scalars.len(), 2);
1238 assert_eq!(bytes[0], b"abc");
1239 assert_eq!(bytes[1], b"xyz");
1240 }
1241 _ => panic!("Expected VarBinary column"),
1242 }
1243
1244 let round_trip_owned: OwnedColumn<TestScalar> = (&column).into();
1245 assert_eq!(owned_varbinary, round_trip_owned);
1246 }
1247
1248 #[test]
1249 fn we_can_get_min_scalar() {
1250 assert_eq!(
1251 ColumnType::TinyInt.min_scalar(),
1252 Some(TestScalar::from(i8::MIN))
1253 );
1254 assert_eq!(
1255 ColumnType::SmallInt.min_scalar(),
1256 Some(TestScalar::from(i16::MIN))
1257 );
1258 assert_eq!(
1259 ColumnType::Int.min_scalar(),
1260 Some(TestScalar::from(i32::MIN))
1261 );
1262 assert_eq!(
1263 ColumnType::BigInt.min_scalar(),
1264 Some(TestScalar::from(i64::MIN))
1265 );
1266 assert_eq!(
1267 ColumnType::Int128.min_scalar(),
1268 Some(TestScalar::from(i128::MIN))
1269 );
1270 assert_eq!(ColumnType::Uint8.min_scalar::<TestScalar>(), None);
1271 assert_eq!(ColumnType::Scalar.min_scalar::<TestScalar>(), None);
1272 assert_eq!(ColumnType::Boolean.min_scalar::<TestScalar>(), None);
1273 assert_eq!(ColumnType::VarBinary.min_scalar::<TestScalar>(), None);
1274 assert_eq!(
1275 ColumnType::TimestampTZ(PoSQLTimeUnit::Second, PoSQLTimeZone::new(0))
1276 .min_scalar::<TestScalar>(),
1277 None
1278 );
1279 assert_eq!(
1280 ColumnType::Decimal75(Precision::new(1).unwrap(), 1).min_scalar::<TestScalar>(),
1281 None
1282 );
1283 assert_eq!(ColumnType::VarChar.min_scalar::<TestScalar>(), None);
1284 }
1285
1286 #[test]
1287 fn we_can_get_sqrt_negative_min() {
1288 for column_type in [
1289 ColumnType::TinyInt,
1290 ColumnType::SmallInt,
1291 ColumnType::Int,
1292 ColumnType::BigInt,
1293 ColumnType::Int128,
1294 ] {
1295 let floor = TestScalar::from(column_type.sqrt_negative_min().unwrap());
1296 let ceiling = floor + TestScalar::ONE;
1297 let floor_squared = floor * floor;
1298 let ceiling_squared = ceiling * ceiling;
1299 let negative_min_scalar = -column_type.min_scalar::<TestScalar>().unwrap();
1300 assert!(floor_squared <= negative_min_scalar);
1301 assert!(negative_min_scalar < ceiling_squared);
1302 }
1303 for column_type in [
1304 ColumnType::Uint8,
1305 ColumnType::Scalar,
1306 ColumnType::Boolean,
1307 ColumnType::VarBinary,
1308 ColumnType::TimestampTZ(PoSQLTimeUnit::Second, PoSQLTimeZone::new(1)),
1309 ColumnType::Decimal75(Precision::new(1).unwrap(), 1),
1310 ColumnType::VarChar,
1311 ] {
1312 assert_eq!(column_type.sqrt_negative_min(), None);
1313 }
1314 }
1315}