1use libduckdb_sys::{
12 DUCKDB_TYPE, DUCKDB_TYPE_DUCKDB_TYPE_ARRAY, DUCKDB_TYPE_DUCKDB_TYPE_BIGINT,
13 DUCKDB_TYPE_DUCKDB_TYPE_BIT, DUCKDB_TYPE_DUCKDB_TYPE_BLOB, DUCKDB_TYPE_DUCKDB_TYPE_BOOLEAN,
14 DUCKDB_TYPE_DUCKDB_TYPE_DATE, DUCKDB_TYPE_DUCKDB_TYPE_DECIMAL, DUCKDB_TYPE_DUCKDB_TYPE_DOUBLE,
15 DUCKDB_TYPE_DUCKDB_TYPE_ENUM, DUCKDB_TYPE_DUCKDB_TYPE_FLOAT, DUCKDB_TYPE_DUCKDB_TYPE_HUGEINT,
16 DUCKDB_TYPE_DUCKDB_TYPE_INTEGER, DUCKDB_TYPE_DUCKDB_TYPE_INTERVAL,
17 DUCKDB_TYPE_DUCKDB_TYPE_LIST, DUCKDB_TYPE_DUCKDB_TYPE_MAP, DUCKDB_TYPE_DUCKDB_TYPE_SMALLINT,
18 DUCKDB_TYPE_DUCKDB_TYPE_STRUCT, DUCKDB_TYPE_DUCKDB_TYPE_TIME,
19 DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP, DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_MS,
20 DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_NS, DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_S,
21 DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_TZ, DUCKDB_TYPE_DUCKDB_TYPE_TIME_TZ,
22 DUCKDB_TYPE_DUCKDB_TYPE_TINYINT, DUCKDB_TYPE_DUCKDB_TYPE_UBIGINT,
23 DUCKDB_TYPE_DUCKDB_TYPE_UHUGEINT, DUCKDB_TYPE_DUCKDB_TYPE_UINTEGER,
24 DUCKDB_TYPE_DUCKDB_TYPE_UNION, DUCKDB_TYPE_DUCKDB_TYPE_USMALLINT,
25 DUCKDB_TYPE_DUCKDB_TYPE_UTINYINT, DUCKDB_TYPE_DUCKDB_TYPE_UUID,
26 DUCKDB_TYPE_DUCKDB_TYPE_VARCHAR,
27};
28use libduckdb_sys::{
29 DUCKDB_TYPE_DUCKDB_TYPE_ANY, DUCKDB_TYPE_DUCKDB_TYPE_BIGNUM,
30 DUCKDB_TYPE_DUCKDB_TYPE_INTEGER_LITERAL, DUCKDB_TYPE_DUCKDB_TYPE_SQLNULL,
31 DUCKDB_TYPE_DUCKDB_TYPE_STRING_LITERAL, DUCKDB_TYPE_DUCKDB_TYPE_TIME_NS,
32};
33#[cfg(feature = "duckdb-1-5-3")]
34use libduckdb_sys::{DUCKDB_TYPE_DUCKDB_TYPE_GEOMETRY, DUCKDB_TYPE_DUCKDB_TYPE_VARIANT};
35
36const DUCKDB_TYPE_TIMESTAMP_TZ_NS: DUCKDB_TYPE = 42;
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
60#[non_exhaustive]
61pub enum TypeId {
62 Boolean,
64 TinyInt,
66 SmallInt,
68 Integer,
70 BigInt,
72 UTinyInt,
74 USmallInt,
76 UInteger,
78 UBigInt,
80 HugeInt,
82 Float,
84 Double,
86 Timestamp,
88 TimestampTz,
90 Date,
92 Time,
94 Interval,
96 Varchar,
98 Blob,
100 Decimal,
102 TimestampS,
104 TimestampMs,
106 TimestampNs,
108 Enum,
110 List,
112 Struct,
114 Map,
116 Uuid,
118 Union,
120 Bit,
122 TimeTz,
124 UHugeInt,
126 Array,
128 TimeNs,
130 Any,
135 Varint,
141 SqlNull,
145 IntegerLiteral,
150 StringLiteral,
155 #[cfg(feature = "duckdb-1-5-3")]
161 Geometry,
162 #[cfg(feature = "duckdb-1-5-3")]
166 Variant,
167 TimestampTzNs,
179}
180
181impl TypeId {
182 #[must_use]
195 pub const fn to_duckdb_type(self) -> DUCKDB_TYPE {
196 match self {
197 Self::Boolean => DUCKDB_TYPE_DUCKDB_TYPE_BOOLEAN,
198 Self::TinyInt => DUCKDB_TYPE_DUCKDB_TYPE_TINYINT,
199 Self::SmallInt => DUCKDB_TYPE_DUCKDB_TYPE_SMALLINT,
200 Self::Integer => DUCKDB_TYPE_DUCKDB_TYPE_INTEGER,
201 Self::BigInt => DUCKDB_TYPE_DUCKDB_TYPE_BIGINT,
202 Self::UTinyInt => DUCKDB_TYPE_DUCKDB_TYPE_UTINYINT,
203 Self::USmallInt => DUCKDB_TYPE_DUCKDB_TYPE_USMALLINT,
204 Self::UInteger => DUCKDB_TYPE_DUCKDB_TYPE_UINTEGER,
205 Self::UBigInt => DUCKDB_TYPE_DUCKDB_TYPE_UBIGINT,
206 Self::HugeInt => DUCKDB_TYPE_DUCKDB_TYPE_HUGEINT,
207 Self::Float => DUCKDB_TYPE_DUCKDB_TYPE_FLOAT,
208 Self::Double => DUCKDB_TYPE_DUCKDB_TYPE_DOUBLE,
209 Self::Timestamp => DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP,
210 Self::TimestampTz => DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_TZ,
211 Self::Date => DUCKDB_TYPE_DUCKDB_TYPE_DATE,
212 Self::Time => DUCKDB_TYPE_DUCKDB_TYPE_TIME,
213 Self::Interval => DUCKDB_TYPE_DUCKDB_TYPE_INTERVAL,
214 Self::Varchar => DUCKDB_TYPE_DUCKDB_TYPE_VARCHAR,
215 Self::Blob => DUCKDB_TYPE_DUCKDB_TYPE_BLOB,
216 Self::Decimal => DUCKDB_TYPE_DUCKDB_TYPE_DECIMAL,
217 Self::TimestampS => DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_S,
218 Self::TimestampMs => DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_MS,
219 Self::TimestampNs => DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_NS,
220 Self::Enum => DUCKDB_TYPE_DUCKDB_TYPE_ENUM,
221 Self::List => DUCKDB_TYPE_DUCKDB_TYPE_LIST,
222 Self::Struct => DUCKDB_TYPE_DUCKDB_TYPE_STRUCT,
223 Self::Map => DUCKDB_TYPE_DUCKDB_TYPE_MAP,
224 Self::Uuid => DUCKDB_TYPE_DUCKDB_TYPE_UUID,
225 Self::Union => DUCKDB_TYPE_DUCKDB_TYPE_UNION,
226 Self::Bit => DUCKDB_TYPE_DUCKDB_TYPE_BIT,
227 Self::TimeTz => DUCKDB_TYPE_DUCKDB_TYPE_TIME_TZ,
228 Self::UHugeInt => DUCKDB_TYPE_DUCKDB_TYPE_UHUGEINT,
229 Self::Array => DUCKDB_TYPE_DUCKDB_TYPE_ARRAY,
230 Self::TimeNs => DUCKDB_TYPE_DUCKDB_TYPE_TIME_NS,
231 Self::Any => DUCKDB_TYPE_DUCKDB_TYPE_ANY,
232 Self::Varint => DUCKDB_TYPE_DUCKDB_TYPE_BIGNUM,
233 Self::SqlNull => DUCKDB_TYPE_DUCKDB_TYPE_SQLNULL,
234 Self::IntegerLiteral => DUCKDB_TYPE_DUCKDB_TYPE_INTEGER_LITERAL,
235 Self::StringLiteral => DUCKDB_TYPE_DUCKDB_TYPE_STRING_LITERAL,
236 #[cfg(feature = "duckdb-1-5-3")]
237 Self::Geometry => DUCKDB_TYPE_DUCKDB_TYPE_GEOMETRY,
238 #[cfg(feature = "duckdb-1-5-3")]
239 Self::Variant => DUCKDB_TYPE_DUCKDB_TYPE_VARIANT,
240 Self::TimestampTzNs => DUCKDB_TYPE_TIMESTAMP_TZ_NS,
241 }
242 }
243
244 #[must_use]
275 pub const fn try_from_duckdb_type(raw: DUCKDB_TYPE) -> Option<Self> {
276 if raw == DUCKDB_TYPE_DUCKDB_TYPE_BOOLEAN {
279 Some(Self::Boolean)
280 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_TINYINT {
281 Some(Self::TinyInt)
282 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_SMALLINT {
283 Some(Self::SmallInt)
284 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_INTEGER {
285 Some(Self::Integer)
286 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_BIGINT {
287 Some(Self::BigInt)
288 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_UTINYINT {
289 Some(Self::UTinyInt)
290 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_USMALLINT {
291 Some(Self::USmallInt)
292 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_UINTEGER {
293 Some(Self::UInteger)
294 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_UBIGINT {
295 Some(Self::UBigInt)
296 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_HUGEINT {
297 Some(Self::HugeInt)
298 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_FLOAT {
299 Some(Self::Float)
300 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_DOUBLE {
301 Some(Self::Double)
302 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP {
303 Some(Self::Timestamp)
304 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_TZ {
305 Some(Self::TimestampTz)
306 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_DATE {
307 Some(Self::Date)
308 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_TIME {
309 Some(Self::Time)
310 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_INTERVAL {
311 Some(Self::Interval)
312 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_VARCHAR {
313 Some(Self::Varchar)
314 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_BLOB {
315 Some(Self::Blob)
316 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_DECIMAL {
317 Some(Self::Decimal)
318 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_S {
319 Some(Self::TimestampS)
320 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_MS {
321 Some(Self::TimestampMs)
322 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP_NS {
323 Some(Self::TimestampNs)
324 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_ENUM {
325 Some(Self::Enum)
326 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_LIST {
327 Some(Self::List)
328 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_STRUCT {
329 Some(Self::Struct)
330 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_MAP {
331 Some(Self::Map)
332 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_UUID {
333 Some(Self::Uuid)
334 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_UNION {
335 Some(Self::Union)
336 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_BIT {
337 Some(Self::Bit)
338 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_TIME_TZ {
339 Some(Self::TimeTz)
340 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_UHUGEINT {
341 Some(Self::UHugeInt)
342 } else if raw == DUCKDB_TYPE_DUCKDB_TYPE_ARRAY {
343 Some(Self::Array)
344 } else {
345 Self::try_from_later_duckdb_type(raw)
346 }
347 }
348
349 const fn try_from_later_duckdb_type(raw: DUCKDB_TYPE) -> Option<Self> {
352 if raw == DUCKDB_TYPE_DUCKDB_TYPE_TIME_NS {
354 return Some(Self::TimeNs);
355 }
356 if raw == DUCKDB_TYPE_DUCKDB_TYPE_ANY {
357 return Some(Self::Any);
358 }
359 if raw == DUCKDB_TYPE_DUCKDB_TYPE_BIGNUM {
360 return Some(Self::Varint);
361 }
362 if raw == DUCKDB_TYPE_DUCKDB_TYPE_SQLNULL {
363 return Some(Self::SqlNull);
364 }
365 if raw == DUCKDB_TYPE_DUCKDB_TYPE_INTEGER_LITERAL {
366 return Some(Self::IntegerLiteral);
367 }
368 if raw == DUCKDB_TYPE_DUCKDB_TYPE_STRING_LITERAL {
369 return Some(Self::StringLiteral);
370 }
371 if raw == DUCKDB_TYPE_TIMESTAMP_TZ_NS {
373 return Some(Self::TimestampTzNs);
374 }
375 #[cfg(feature = "duckdb-1-5-3")]
377 {
378 if raw == DUCKDB_TYPE_DUCKDB_TYPE_GEOMETRY {
379 return Some(Self::Geometry);
380 }
381 if raw == DUCKDB_TYPE_DUCKDB_TYPE_VARIANT {
382 return Some(Self::Variant);
383 }
384 }
385 None
386 }
387
388 #[must_use]
402 pub const fn from_duckdb_type(raw: DUCKDB_TYPE) -> Self {
403 match Self::try_from_duckdb_type(raw) {
404 Some(id) => id,
405 None => panic!("unknown DUCKDB_TYPE value"),
406 }
407 }
408
409 #[must_use]
426 pub const fn composite_constructor_hint(self) -> Option<&'static str> {
427 match self {
438 Self::Decimal => Some("LogicalType::decimal(width, scale)"),
439 Self::Enum => Some("LogicalType::enum_type(&members)"),
440 Self::List => Some("LogicalType::list(element_type)"),
441 Self::Struct => Some("LogicalType::struct_type(&fields)"),
442 Self::Map => Some("LogicalType::map(key_type, value_type)"),
443 Self::Array => Some("LogicalType::array(element_type, size)"),
444 Self::Union => Some("LogicalType::union_type(&members)"),
445 _ => None,
446 }
447 }
448
449 #[must_use]
473 pub const fn is_composite(self) -> bool {
474 self.composite_constructor_hint().is_some()
475 }
476
477 #[must_use]
488 pub const fn sql_name(self) -> &'static str {
489 match self {
490 Self::Boolean => "BOOLEAN",
491 Self::TinyInt => "TINYINT",
492 Self::SmallInt => "SMALLINT",
493 Self::Integer => "INTEGER",
494 Self::BigInt => "BIGINT",
495 Self::UTinyInt => "UTINYINT",
496 Self::USmallInt => "USMALLINT",
497 Self::UInteger => "UINTEGER",
498 Self::UBigInt => "UBIGINT",
499 Self::HugeInt => "HUGEINT",
500 Self::Float => "FLOAT",
501 Self::Double => "DOUBLE",
502 Self::Timestamp => "TIMESTAMP",
503 Self::TimestampTz => "TIMESTAMPTZ",
504 Self::Date => "DATE",
505 Self::Time => "TIME",
506 Self::Interval => "INTERVAL",
507 Self::Varchar => "VARCHAR",
508 Self::Blob => "BLOB",
509 Self::Decimal => "DECIMAL",
510 Self::TimestampS => "TIMESTAMP_S",
511 Self::TimestampMs => "TIMESTAMP_MS",
512 Self::TimestampNs => "TIMESTAMP_NS",
513 Self::Enum => "ENUM",
514 Self::List => "LIST",
515 Self::Struct => "STRUCT",
516 Self::Map => "MAP",
517 Self::Uuid => "UUID",
518 Self::Union => "UNION",
519 Self::Bit => "BIT",
520 Self::TimeTz => "TIMETZ",
521 Self::UHugeInt => "UHUGEINT",
522 Self::Array => "ARRAY",
523 Self::TimeNs => "TIME_NS",
524 Self::Any => "ANY",
525 Self::Varint => "BIGNUM",
526 Self::SqlNull => "SQLNULL",
527 Self::IntegerLiteral => "INTEGER_LITERAL",
528 Self::StringLiteral => "STRING_LITERAL",
529 #[cfg(feature = "duckdb-1-5-3")]
530 Self::Geometry => "GEOMETRY",
531 #[cfg(feature = "duckdb-1-5-3")]
532 Self::Variant => "VARIANT",
533 Self::TimestampTzNs => "TIMESTAMPTZ_NS",
534 }
535 }
536}
537
538impl std::fmt::Display for TypeId {
539 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
540 f.write_str(self.sql_name())
541 }
542}
543
544#[cfg(test)]
545mod tests {
546 #[test]
552 fn every_composite_type_names_its_own_constructor() {
553 for (type_id, expected) in [
554 (TypeId::Decimal, "LogicalType::decimal(width, scale)"),
555 (TypeId::Enum, "LogicalType::enum_type(&members)"),
556 (TypeId::List, "LogicalType::list(element_type)"),
557 (TypeId::Struct, "LogicalType::struct_type(&fields)"),
558 (TypeId::Map, "LogicalType::map(key_type, value_type)"),
559 (TypeId::Array, "LogicalType::array(element_type, size)"),
560 (TypeId::Union, "LogicalType::union_type(&members)"),
561 ] {
562 assert_eq!(
563 type_id.composite_constructor_hint(),
564 Some(expected),
565 "{type_id:?} must name its own constructor"
566 );
567 assert!(type_id.is_composite(), "{type_id:?} is composite");
568 }
569 }
570
571 #[test]
572 fn primitive_types_have_no_constructor_hint_and_are_not_composite() {
573 for type_id in [
576 TypeId::Boolean,
577 TypeId::TinyInt,
578 TypeId::SmallInt,
579 TypeId::Integer,
580 TypeId::BigInt,
581 TypeId::HugeInt,
582 TypeId::Float,
583 TypeId::Double,
584 TypeId::Varchar,
585 TypeId::Blob,
586 TypeId::Date,
587 TypeId::Time,
588 TypeId::Timestamp,
589 TypeId::Interval,
590 TypeId::Uuid,
591 ] {
592 assert_eq!(
593 type_id.composite_constructor_hint(),
594 None,
595 "{type_id:?} is primitive"
596 );
597 assert!(!type_id.is_composite(), "{type_id:?} is not composite");
598 }
599 }
600
601 use super::*;
602
603 #[test]
604 fn all_types_round_trip_display() {
605 let types = [
606 TypeId::Boolean,
607 TypeId::TinyInt,
608 TypeId::SmallInt,
609 TypeId::Integer,
610 TypeId::BigInt,
611 TypeId::UTinyInt,
612 TypeId::USmallInt,
613 TypeId::UInteger,
614 TypeId::UBigInt,
615 TypeId::HugeInt,
616 TypeId::Float,
617 TypeId::Double,
618 TypeId::Timestamp,
619 TypeId::TimestampTz,
620 TypeId::Date,
621 TypeId::Time,
622 TypeId::Interval,
623 TypeId::Varchar,
624 TypeId::Blob,
625 TypeId::Decimal,
626 TypeId::TimestampS,
627 TypeId::TimestampMs,
628 TypeId::TimestampNs,
629 TypeId::Enum,
630 TypeId::List,
631 TypeId::Struct,
632 TypeId::Map,
633 TypeId::Uuid,
634 TypeId::Union,
635 TypeId::Bit,
636 TypeId::TimeTz,
637 TypeId::UHugeInt,
638 TypeId::Array,
639 TypeId::TimeNs,
640 TypeId::Any,
641 TypeId::Varint,
642 TypeId::SqlNull,
643 TypeId::IntegerLiteral,
644 TypeId::StringLiteral,
645 #[cfg(feature = "duckdb-1-5-3")]
646 TypeId::Geometry,
647 #[cfg(feature = "duckdb-1-5-3")]
648 TypeId::Variant,
649 TypeId::TimestampTzNs,
650 ];
651 for t in types {
652 assert_ne!(t.sql_name(), "");
654 assert_eq!(t.sql_name(), format!("{t}"));
655 }
656 }
657
658 #[test]
659 fn bigint_maps_to_correct_duckdb_type() {
660 assert_eq!(
661 TypeId::BigInt.to_duckdb_type(),
662 DUCKDB_TYPE_DUCKDB_TYPE_BIGINT
663 );
664 }
665
666 #[test]
667 fn boolean_maps_to_correct_duckdb_type() {
668 assert_eq!(
669 TypeId::Boolean.to_duckdb_type(),
670 DUCKDB_TYPE_DUCKDB_TYPE_BOOLEAN
671 );
672 }
673
674 #[test]
675 fn timestamp_maps_to_correct_duckdb_type() {
676 assert_eq!(
677 TypeId::Timestamp.to_duckdb_type(),
678 DUCKDB_TYPE_DUCKDB_TYPE_TIMESTAMP
679 );
680 }
681
682 #[test]
683 fn varchar_maps_to_correct_duckdb_type() {
684 assert_eq!(
685 TypeId::Varchar.to_duckdb_type(),
686 DUCKDB_TYPE_DUCKDB_TYPE_VARCHAR
687 );
688 }
689
690 #[test]
691 fn type_id_clone_copy() {
692 let t = TypeId::Integer;
693 let t2 = t; assert_eq!(t, t2);
695 let t3 = t;
696 assert_eq!(t, t3);
697 }
698
699 #[test]
700 fn type_id_debug() {
701 let s = format!("{:?}", TypeId::Interval);
702 assert!(s.contains("Interval"));
703 }
704
705 #[test]
708 fn time_ns_maps_to_correct_duckdb_type() {
709 assert_eq!(
710 TypeId::TimeNs.to_duckdb_type(),
711 DUCKDB_TYPE_DUCKDB_TYPE_TIME_NS
712 );
713 }
714
715 #[test]
716 fn any_maps_to_correct_duckdb_type() {
717 assert_eq!(TypeId::Any.to_duckdb_type(), DUCKDB_TYPE_DUCKDB_TYPE_ANY);
718 }
719
720 #[test]
721 fn varint_maps_to_correct_duckdb_type() {
722 assert_eq!(
723 TypeId::Varint.to_duckdb_type(),
724 DUCKDB_TYPE_DUCKDB_TYPE_BIGNUM
725 );
726 }
727
728 #[test]
729 fn sql_null_maps_to_correct_duckdb_type() {
730 assert_eq!(
731 TypeId::SqlNull.to_duckdb_type(),
732 DUCKDB_TYPE_DUCKDB_TYPE_SQLNULL
733 );
734 }
735
736 #[test]
737 fn later_type_id_variants_sql_names() {
738 assert_eq!(TypeId::TimeNs.sql_name(), "TIME_NS");
739 assert_eq!(TypeId::Any.sql_name(), "ANY");
740 assert_eq!(TypeId::Varint.sql_name(), "BIGNUM");
741 assert_eq!(TypeId::SqlNull.sql_name(), "SQLNULL");
742 assert_eq!(TypeId::IntegerLiteral.sql_name(), "INTEGER_LITERAL");
743 assert_eq!(TypeId::StringLiteral.sql_name(), "STRING_LITERAL");
744 }
745
746 #[test]
747 fn later_type_id_variants_display_matches_sql_name() {
748 assert_eq!(format!("{}", TypeId::TimeNs), "TIME_NS");
749 assert_eq!(format!("{}", TypeId::Any), "ANY");
750 assert_eq!(format!("{}", TypeId::Varint), "BIGNUM");
751 assert_eq!(format!("{}", TypeId::SqlNull), "SQLNULL");
752 assert_eq!(format!("{}", TypeId::IntegerLiteral), "INTEGER_LITERAL");
753 assert_eq!(format!("{}", TypeId::StringLiteral), "STRING_LITERAL");
754 }
755
756 #[test]
757 fn later_type_id_variants_hash_eq() {
758 use std::collections::HashSet;
759 let mut set = HashSet::new();
760 set.insert(TypeId::TimeNs);
761 set.insert(TypeId::Any);
762 set.insert(TypeId::Varint);
763 set.insert(TypeId::SqlNull);
764 set.insert(TypeId::IntegerLiteral);
765 set.insert(TypeId::StringLiteral);
766 assert_eq!(set.len(), 6);
767 assert!(set.contains(&TypeId::TimeNs));
768 assert!(set.contains(&TypeId::Any));
769 assert!(set.contains(&TypeId::Varint));
770 assert!(set.contains(&TypeId::SqlNull));
771 assert!(set.contains(&TypeId::IntegerLiteral));
772 assert!(set.contains(&TypeId::StringLiteral));
773 }
774
775 #[test]
776 fn integer_literal_maps_to_correct_duckdb_type() {
777 assert_eq!(
778 TypeId::IntegerLiteral.to_duckdb_type(),
779 DUCKDB_TYPE_DUCKDB_TYPE_INTEGER_LITERAL
780 );
781 }
782
783 #[test]
784 fn string_literal_maps_to_correct_duckdb_type() {
785 assert_eq!(
786 TypeId::StringLiteral.to_duckdb_type(),
787 DUCKDB_TYPE_DUCKDB_TYPE_STRING_LITERAL
788 );
789 }
790
791 fn all_type_ids() -> Vec<TypeId> {
793 vec![
794 TypeId::Boolean,
795 TypeId::TinyInt,
796 TypeId::SmallInt,
797 TypeId::Integer,
798 TypeId::BigInt,
799 TypeId::UTinyInt,
800 TypeId::USmallInt,
801 TypeId::UInteger,
802 TypeId::UBigInt,
803 TypeId::HugeInt,
804 TypeId::Float,
805 TypeId::Double,
806 TypeId::Timestamp,
807 TypeId::TimestampTz,
808 TypeId::Date,
809 TypeId::Time,
810 TypeId::Interval,
811 TypeId::Varchar,
812 TypeId::Blob,
813 TypeId::Decimal,
814 TypeId::TimestampS,
815 TypeId::TimestampMs,
816 TypeId::TimestampNs,
817 TypeId::Enum,
818 TypeId::List,
819 TypeId::Struct,
820 TypeId::Map,
821 TypeId::Uuid,
822 TypeId::Union,
823 TypeId::Bit,
824 TypeId::TimeTz,
825 TypeId::UHugeInt,
826 TypeId::Array,
827 TypeId::TimeNs,
830 TypeId::Any,
831 TypeId::Varint,
832 TypeId::SqlNull,
833 TypeId::IntegerLiteral,
834 TypeId::StringLiteral,
835 #[cfg(feature = "duckdb-1-5-3")]
836 TypeId::Geometry,
837 #[cfg(feature = "duckdb-1-5-3")]
838 TypeId::Variant,
839 TypeId::TimestampTzNs,
840 ]
841 }
842
843 #[test]
848 fn timestamp_tz_ns_is_type_42_and_named_like_duckdb_names_it() {
849 assert_eq!(TypeId::TimestampTzNs.to_duckdb_type(), 42);
850 assert_eq!(
851 TypeId::try_from_duckdb_type(42),
852 Some(TypeId::TimestampTzNs)
853 );
854 assert_eq!(TypeId::TimestampTzNs.sql_name(), "TIMESTAMPTZ_NS");
855 assert_eq!(TypeId::from_duckdb_type(42), TypeId::TimestampTzNs);
856 }
857
858 #[test]
859 fn try_from_duckdb_type_reports_unknown_values() {
860 for raw in [9999u32, 200, 100, u32::MAX] {
862 assert_eq!(
863 TypeId::try_from_duckdb_type(raw),
864 None,
865 "raw {raw} must not resolve to a TypeId"
866 );
867 }
868 }
869
870 #[test]
871 fn try_from_duckdb_type_agrees_with_to_duckdb_type() {
872 for id in all_type_ids() {
873 assert_eq!(TypeId::try_from_duckdb_type(id.to_duckdb_type()), Some(id));
874 }
875 }
876
877 #[test]
878 #[should_panic(expected = "unknown DUCKDB_TYPE value")]
879 fn from_duckdb_type_still_panics_on_unknown() {
880 let _ = TypeId::from_duckdb_type(9999);
881 }
882
883 #[test]
884 fn from_duckdb_type_roundtrip() {
885 let variants = all_type_ids();
886 for &tid in &variants {
887 let raw = tid.to_duckdb_type();
888 let back = TypeId::from_duckdb_type(raw);
889 assert_eq!(back, tid, "roundtrip failed for {tid:?}");
890 }
891 }
892
893 #[cfg(feature = "duckdb-1-5-3")]
894 #[test]
895 fn geometry_maps_to_correct_duckdb_type() {
896 assert_eq!(
897 TypeId::Geometry.to_duckdb_type(),
898 DUCKDB_TYPE_DUCKDB_TYPE_GEOMETRY
899 );
900 }
901
902 #[cfg(feature = "duckdb-1-5-3")]
903 #[test]
904 fn variant_maps_to_correct_duckdb_type() {
905 assert_eq!(
906 TypeId::Variant.to_duckdb_type(),
907 DUCKDB_TYPE_DUCKDB_TYPE_VARIANT
908 );
909 }
910
911 #[cfg(feature = "duckdb-1-5-3")]
912 #[test]
913 fn geometry_variant_sql_names_and_display() {
914 assert_eq!(TypeId::Geometry.sql_name(), "GEOMETRY");
915 assert_eq!(TypeId::Variant.sql_name(), "VARIANT");
916 assert_eq!(format!("{}", TypeId::Geometry), "GEOMETRY");
917 assert_eq!(format!("{}", TypeId::Variant), "VARIANT");
918 }
919}