Skip to content

Commit 78e50c4

Browse files
committed
Add try_id helper
1 parent 7b6fe67 commit 78e50c4

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

crates/duckdb/src/core/logical_type.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl Debug for LogicalTypeHandle {
157157
let id = self.id();
158158
match id {
159159
LogicalTypeId::Invalid => write!(f, "Invalid"),
160+
LogicalTypeId::Unsupported => write!(f, "Unsupported({})", self.raw_id()),
160161
LogicalTypeId::Struct => {
161162
write!(f, "struct<")?;
162163
for i in 0..self.num_children() {
@@ -289,6 +290,19 @@ impl LogicalTypeHandle {
289290
self.raw_id().into()
290291
}
291292

293+
/// Logical type ID, with forward-compatibility awareness.
294+
///
295+
/// Returns `Ok(LogicalTypeId)` for all known ids (including `Invalid`), and
296+
/// `Err(raw_id)` when DuckDB returns an id this wrapper does not yet
297+
/// recognize.
298+
pub fn try_id(&self) -> Result<LogicalTypeId, u32> {
299+
let raw = self.raw_id();
300+
match LogicalTypeId::from(raw) {
301+
LogicalTypeId::Unsupported => Err(raw),
302+
id => Ok(id),
303+
}
304+
}
305+
292306
/// Raw logical type id returned by DuckDB C API
293307
pub fn raw_id(&self) -> u32 {
294308
unsafe { duckdb_get_type_id(self.ptr) }
@@ -313,6 +327,7 @@ impl LogicalTypeHandle {
313327
let child_name_ptr = match self.id() {
314328
LogicalTypeId::Struct => duckdb_struct_type_child_name(self.ptr, idx as u64),
315329
LogicalTypeId::Union => duckdb_union_type_member_name(self.ptr, idx as u64),
330+
LogicalTypeId::Unsupported => panic!("unsupported logical type {}", self.raw_id()),
316331
_ => panic!("not a struct or union"),
317332
};
318333
let c_str = CString::from_raw(child_name_ptr);
@@ -328,6 +343,7 @@ impl LogicalTypeHandle {
328343
LogicalTypeId::Struct => duckdb_struct_type_child_type(self.ptr, idx as u64),
329344
LogicalTypeId::Union => duckdb_union_type_member_type(self.ptr, idx as u64),
330345
LogicalTypeId::Array => duckdb_array_type_child_type(self.ptr),
346+
LogicalTypeId::Unsupported => panic!("unsupported logical type {}", self.raw_id()),
331347
_ => panic!("not a struct, union, or array"),
332348
}
333349
};
@@ -424,6 +440,8 @@ mod test {
424440
unsafe { LogicalTypeHandle::new(duckdb_create_logical_type(DUCKDB_TYPE_DUCKDB_TYPE_INVALID)) };
425441

426442
assert_eq!(invalid_type.id(), LogicalTypeId::Invalid);
443+
assert_eq!(invalid_type.try_id().unwrap(), LogicalTypeId::Invalid);
444+
assert_eq!(invalid_type.raw_id(), DUCKDB_TYPE_DUCKDB_TYPE_INVALID);
427445

428446
let debug_str = format!("{invalid_type:?}");
429447
assert_eq!(debug_str, "Invalid");

0 commit comments

Comments
 (0)