diff --git a/crates/duckdb/src/column.rs b/crates/duckdb/src/column.rs index 8ed89992..ba4773ed 100644 --- a/crates/duckdb/src/column.rs +++ b/crates/duckdb/src/column.rs @@ -2,7 +2,7 @@ use std::str; use arrow::datatypes::DataType; -use crate::{Error, Result, Statement}; +use crate::{core::LogicalTypeHandle, Error, Result, Statement}; /// Information about a column of a DuckDB query. #[derive(Debug)] @@ -160,6 +160,11 @@ impl Statement<'_> { pub fn column_type(&self, idx: usize) -> DataType { self.stmt.column_type(idx) } + + /// Returns the declared logical data type of the column. + pub fn column_logical_type(&self, idx: usize) -> LogicalTypeHandle { + self.stmt.column_logical_type(idx) + } } #[cfg(test)] diff --git a/crates/duckdb/src/core/logical_type.rs b/crates/duckdb/src/core/logical_type.rs index 96faecd1..8e6b5ce1 100644 --- a/crates/duckdb/src/core/logical_type.rs +++ b/crates/duckdb/src/core/logical_type.rs @@ -289,6 +289,28 @@ impl LogicalTypeHandle { }; unsafe { Self::new(c_logical_type) } } + + /// Alias of the logical type. + pub fn get_alias(&self) -> Option { + unsafe { + let alias_ptr = duckdb_logical_type_get_alias(self.ptr); + if alias_ptr.is_null() { + None + } else { + let c_str = CString::from_raw(alias_ptr); + let alias = c_str.to_str().unwrap(); + Some(alias.to_string()) + } + } + } + + /// Set the alias of the logical type. + pub fn set_alias(&self, alias: &str) { + unsafe { + let alias = CString::new(alias).unwrap(); + duckdb_logical_type_set_alias(self.ptr, alias.as_ptr()); + } + } } #[cfg(test)] diff --git a/crates/duckdb/src/raw_statement.rs b/crates/duckdb/src/raw_statement.rs index fca8eea5..40e3a099 100644 --- a/crates/duckdb/src/raw_statement.rs +++ b/crates/duckdb/src/raw_statement.rs @@ -9,7 +9,7 @@ use arrow::{ use super::{ffi, Result}; #[cfg(feature = "polars")] use crate::arrow2; -use crate::{error::result_from_duckdb_arrow, Error}; +use crate::{core::LogicalTypeHandle, error::result_from_duckdb_arrow, Error}; /// Private newtype for DuckDB prepared statements that finalize themselves when dropped. /// @@ -215,6 +215,14 @@ impl RawStatement { self.schema().field(idx).data_type().to_owned() } + #[inline] + pub fn column_logical_type(&self, idx: usize) -> LogicalTypeHandle { + unsafe { + let ptr = ffi::duckdb_prepared_statement_column_logical_type(self.ptr, idx as u64); + LogicalTypeHandle::new(ptr) + } + } + #[inline] pub fn schema(&self) -> SchemaRef { self.schema.clone().unwrap()