Skip to content

Commit 201a3d0

Browse files
authored
Rename LogicalType and DataChunk to have Handle suffix (#361)
* Use manifest root for specifying versions * Added duckdb string type * Add Handle suffix to `DataChunk` and `LogicalType`
1 parent f40058b commit 201a3d0

File tree

14 files changed

+187
-125
lines changed

14 files changed

+187
-125
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ doc-comment = "0.3"
3434
fallible-iterator = "0.3"
3535
fallible-streaming-iterator = "0.1"
3636
flate2 = "1.0"
37-
hashlink = "0.8"
37+
hashlink = "0.9"
3838
lazy_static = "1.4"
3939
memchr = "2.3"
4040
num = { version = "0.4", default-features = false }
41+
num-integer = "0.1.46"
4142
pkg-config = "0.3.24"
4243
polars = "0.35.4"
4344
polars-core = "0.35.4"

crates/duckdb/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ calamine = { workspace = true, optional = true }
5656
num = { workspace = true, features = ["std"], optional = true }
5757
duckdb-loadable-macros = { workspace = true, optional = true }
5858
polars = { workspace = true, features = ["dtype-full"], optional = true }
59-
num-integer = {version = "0.1.46"}
59+
num-integer = { workspace = true }
6060

6161
[dev-dependencies]
6262
doc-comment = { workspace = true }

crates/duckdb/examples/hello-ext/main.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate duckdb_loadable_macros;
33
extern crate libduckdb_sys;
44

55
use duckdb::{
6-
core::{DataChunk, Inserter, LogicalType, LogicalTypeId},
6+
core::{DataChunkHandle, Inserter, LogicalTypeHandle, LogicalTypeId},
77
vtab::{BindInfo, Free, FunctionInfo, InitInfo, VTab},
88
Connection, Result,
99
};
@@ -44,7 +44,7 @@ impl VTab for HelloVTab {
4444
type BindData = HelloBindData;
4545

4646
unsafe fn bind(bind: &BindInfo, data: *mut HelloBindData) -> Result<(), Box<dyn std::error::Error>> {
47-
bind.add_result_column("column0", LogicalType::new(LogicalTypeId::Varchar));
47+
bind.add_result_column("column0", LogicalTypeHandle::from(LogicalTypeId::Varchar));
4848
let param = bind.get_parameter(0).to_string();
4949
unsafe {
5050
(*data).name = CString::new(param).unwrap().into_raw();
@@ -59,7 +59,7 @@ impl VTab for HelloVTab {
5959
Ok(())
6060
}
6161

62-
unsafe fn func(func: &FunctionInfo, output: &mut DataChunk) -> Result<(), Box<dyn std::error::Error>> {
62+
unsafe fn func(func: &FunctionInfo, output: &mut DataChunkHandle) -> Result<(), Box<dyn std::error::Error>> {
6363
let init_info = func.get_init_data::<HelloInitData>();
6464
let bind_info = func.get_bind_data::<HelloBindData>();
6565

@@ -80,8 +80,8 @@ impl VTab for HelloVTab {
8080
Ok(())
8181
}
8282

83-
fn parameters() -> Option<Vec<LogicalType>> {
84-
Some(vec![LogicalType::new(LogicalTypeId::Varchar)])
83+
fn parameters() -> Option<Vec<LogicalTypeHandle>> {
84+
Some(vec![LogicalTypeHandle::from(LogicalTypeId::Varchar)])
8585
}
8686
}
8787

crates/duckdb/src/appender/arrow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{ffi, Appender, Result};
22
use crate::{
3-
core::{DataChunk, LogicalType},
3+
core::{DataChunkHandle, LogicalTypeHandle},
44
error::result_from_duckdb_appender,
55
vtab::{record_batch_to_duckdb_data_chunk, to_duckdb_logical_type},
66
Error,
@@ -29,14 +29,14 @@ impl Appender<'_> {
2929
#[inline]
3030
pub fn append_record_batch(&mut self, record_batch: RecordBatch) -> Result<()> {
3131
let schema = record_batch.schema();
32-
let mut logical_type: Vec<LogicalType> = vec![];
32+
let mut logical_type: Vec<LogicalTypeHandle> = vec![];
3333
for field in schema.fields() {
3434
let logical_t = to_duckdb_logical_type(field.data_type())
3535
.map_err(|_op| Error::ArrowTypeToDuckdbType(field.to_string(), field.data_type().clone()))?;
3636
logical_type.push(logical_t);
3737
}
3838

39-
let mut data_chunk = DataChunk::new(&logical_type);
39+
let mut data_chunk = DataChunkHandle::new(&logical_type);
4040
record_batch_to_duckdb_data_chunk(&record_batch, &mut data_chunk).map_err(|_op| Error::AppendError)?;
4141

4242
let rc = unsafe { duckdb_append_data_chunk(self.app, data_chunk.get_ptr()) };

crates/duckdb/src/core/data_chunk.rs

+30-32
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
use super::{
2-
logical_type::LogicalType,
2+
logical_type::LogicalTypeHandle,
33
vector::{ArrayVector, FlatVector, ListVector, StructVector},
44
};
55
use crate::ffi::{
66
duckdb_create_data_chunk, duckdb_data_chunk, duckdb_data_chunk_get_column_count, duckdb_data_chunk_get_size,
77
duckdb_data_chunk_get_vector, duckdb_data_chunk_set_size, duckdb_destroy_data_chunk,
88
};
99

10-
/// DataChunk in DuckDB.
11-
pub struct DataChunk {
10+
/// Handle to the DataChunk in DuckDB.
11+
pub struct DataChunkHandle {
1212
/// Pointer to the DataChunk in duckdb C API.
1313
ptr: duckdb_data_chunk,
1414

15-
/// Whether this [DataChunk] own the [DataChunk::ptr].
15+
/// Whether this [DataChunkHandle] own the [DataChunk::ptr].
1616
owned: bool,
1717
}
1818

19-
impl DataChunk {
20-
/// Create a new [DataChunk] with the given [LogicalType]s.
21-
pub fn new(logical_types: &[LogicalType]) -> Self {
19+
impl Drop for DataChunkHandle {
20+
fn drop(&mut self) {
21+
if self.owned && !self.ptr.is_null() {
22+
unsafe { duckdb_destroy_data_chunk(&mut self.ptr) }
23+
self.ptr = std::ptr::null_mut();
24+
}
25+
}
26+
}
27+
28+
impl DataChunkHandle {
29+
pub(crate) unsafe fn new_unowned(ptr: duckdb_data_chunk) -> Self {
30+
Self { ptr, owned: false }
31+
}
32+
33+
/// Create a new [DataChunkHandle] with the given [LogicalTypeHandle]s.
34+
pub fn new(logical_types: &[LogicalTypeHandle]) -> Self {
2235
let num_columns = logical_types.len();
2336
let mut c_types = logical_types.iter().map(|t| t.ptr).collect::<Vec<_>>();
2437
let ptr = unsafe { duckdb_create_data_chunk(c_types.as_mut_ptr(), num_columns as u64) };
25-
DataChunk { ptr, owned: true }
38+
DataChunkHandle { ptr, owned: true }
2639
}
2740

2841
/// Get the vector at the specific column index: `idx`.
@@ -50,49 +63,34 @@ impl DataChunk {
5063
unsafe { duckdb_data_chunk_set_size(self.ptr, new_len as u64) };
5164
}
5265

53-
/// Get the length / the number of rows in this [DataChunk].
66+
/// Get the length / the number of rows in this [DataChunkHandle].
5467
pub fn len(&self) -> usize {
5568
unsafe { duckdb_data_chunk_get_size(self.ptr) as usize }
5669
}
5770

58-
/// Check whether this [DataChunk] is empty.
71+
/// Check whether this [DataChunkHandle] is empty.
5972
pub fn is_empty(&self) -> bool {
6073
self.len() == 0
6174
}
6275

63-
/// Get the number of columns in this [DataChunk].
76+
/// Get the number of columns in this [DataChunkHandle].
6477
pub fn num_columns(&self) -> usize {
6578
unsafe { duckdb_data_chunk_get_column_count(self.ptr) as usize }
6679
}
6780

68-
/// Get the ptr of duckdb_data_chunk in this [DataChunk].
81+
/// Get the ptr of duckdb_data_chunk in this [DataChunkHandle].
6982
pub fn get_ptr(&self) -> duckdb_data_chunk {
7083
self.ptr
7184
}
7285
}
7386

74-
impl From<duckdb_data_chunk> for DataChunk {
75-
fn from(ptr: duckdb_data_chunk) -> Self {
76-
Self { ptr, owned: false }
77-
}
78-
}
79-
80-
impl Drop for DataChunk {
81-
fn drop(&mut self) {
82-
if self.owned && !self.ptr.is_null() {
83-
unsafe { duckdb_destroy_data_chunk(&mut self.ptr) }
84-
self.ptr = std::ptr::null_mut();
85-
}
86-
}
87-
}
88-
8987
#[cfg(test)]
9088
mod test {
9189
use super::{super::logical_type::LogicalTypeId, *};
9290

9391
#[test]
9492
fn test_data_chunk_construction() {
95-
let dc = DataChunk::new(&[LogicalType::new(LogicalTypeId::Integer)]);
93+
let dc = DataChunkHandle::new(&[LogicalTypeHandle::from(LogicalTypeId::Integer)]);
9694

9795
assert_eq!(dc.num_columns(), 1);
9896

@@ -101,7 +99,7 @@ mod test {
10199

102100
#[test]
103101
fn test_vector() {
104-
let datachunk = DataChunk::new(&[LogicalType::new(LogicalTypeId::Bigint)]);
102+
let datachunk = DataChunkHandle::new(&[LogicalTypeHandle::from(LogicalTypeId::Bigint)]);
105103
let mut vector = datachunk.flat_vector(0);
106104
let data = vector.as_mut_slice::<i64>();
107105

@@ -110,11 +108,11 @@ mod test {
110108

111109
#[test]
112110
fn test_logi() {
113-
let key = LogicalType::new(LogicalTypeId::Varchar);
111+
let key = LogicalTypeHandle::from(LogicalTypeId::Varchar);
114112

115-
let value = LogicalType::new(LogicalTypeId::UTinyint);
113+
let value = LogicalTypeHandle::from(LogicalTypeId::UTinyint);
116114

117-
let map = LogicalType::map(&key, &value);
115+
let map = LogicalTypeHandle::map(&key, &value);
118116

119117
assert_eq!(map.id(), LogicalTypeId::Map);
120118

crates/duckdb/src/core/logical_type.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ impl From<u32> for LogicalTypeId {
110110

111111
/// DuckDB Logical Type.
112112
/// <https://duckdb.org/docs/sql/data_types/overview>
113-
pub struct LogicalType {
113+
pub struct LogicalTypeHandle {
114114
pub(crate) ptr: duckdb_logical_type,
115115
}
116116

117-
impl Debug for LogicalType {
117+
impl Debug for LogicalTypeHandle {
118118
/// Debug implementation for LogicalType
119119
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
120120
let id = self.id();
@@ -134,7 +134,7 @@ impl Debug for LogicalType {
134134
}
135135
}
136136

137-
impl Drop for LogicalType {
137+
impl Drop for LogicalTypeHandle {
138138
/// Drop implementation for LogicalType
139139
fn drop(&mut self) {
140140
if !self.ptr.is_null() {
@@ -147,25 +147,25 @@ impl Drop for LogicalType {
147147
}
148148
}
149149

150-
impl From<duckdb_logical_type> for LogicalType {
151-
/// Wrap a DuckDB logical type from C API
152-
fn from(ptr: duckdb_logical_type) -> Self {
153-
Self { ptr }
154-
}
155-
}
156-
157-
impl LogicalType {
158-
/// Create a new [LogicalType] from [LogicalTypeId]
159-
pub fn new(id: LogicalTypeId) -> Self {
150+
impl From<LogicalTypeId> for LogicalTypeHandle {
151+
/// Create a new [LogicalTypeHandle] from [LogicalTypeId]
152+
fn from(id: LogicalTypeId) -> Self {
160153
unsafe {
161154
Self {
162155
ptr: duckdb_create_logical_type(id as u32),
163156
}
164157
}
165158
}
159+
}
160+
161+
impl LogicalTypeHandle {
162+
/// Create a DuckDB logical type from C API
163+
pub(crate) unsafe fn new(ptr: duckdb_logical_type) -> Self {
164+
Self { ptr }
165+
}
166166

167167
/// Creates a map type from its child type.
168-
pub fn map(key: &LogicalType, value: &LogicalType) -> Self {
168+
pub fn map(key: &LogicalTypeHandle, value: &LogicalTypeHandle) -> Self {
169169
unsafe {
170170
Self {
171171
ptr: duckdb_create_map_type(key.ptr, value.ptr),
@@ -174,7 +174,7 @@ impl LogicalType {
174174
}
175175

176176
/// Creates a list type from its child type.
177-
pub fn list(child_type: &LogicalType) -> Self {
177+
pub fn list(child_type: &LogicalTypeHandle) -> Self {
178178
unsafe {
179179
Self {
180180
ptr: duckdb_create_list_type(child_type.ptr),
@@ -183,7 +183,7 @@ impl LogicalType {
183183
}
184184

185185
/// Creates an array type from its child type.
186-
pub fn array(child_type: &LogicalType, array_size: u64) -> Self {
186+
pub fn array(child_type: &LogicalTypeHandle, array_size: u64) -> Self {
187187
unsafe {
188188
Self {
189189
ptr: duckdb_create_array_type(child_type.ptr, array_size),
@@ -213,7 +213,7 @@ impl LogicalType {
213213
}
214214

215215
/// Make a `LogicalType` for `struct`
216-
pub fn struct_type(fields: &[(&str, LogicalType)]) -> Self {
216+
pub fn struct_type(fields: &[(&str, LogicalTypeHandle)]) -> Self {
217217
let keys: Vec<CString> = fields.iter().map(|f| CString::new(f.0).unwrap()).collect();
218218
let values: Vec<duckdb_logical_type> = fields.iter().map(|it| it.1.ptr).collect();
219219
let name_ptrs = keys.iter().map(|it| it.as_ptr()).collect::<Vec<*const c_char>>();
@@ -230,7 +230,7 @@ impl LogicalType {
230230
}
231231

232232
/// Make a `LogicalType` for `union`
233-
pub fn union_type(fields: &[(&str, LogicalType)]) -> Self {
233+
pub fn union_type(fields: &[(&str, LogicalTypeHandle)]) -> Self {
234234
let keys: Vec<CString> = fields.iter().map(|f| CString::new(f.0).unwrap()).collect();
235235
let values: Vec<duckdb_logical_type> = fields.iter().map(|it| it.1.ptr).collect();
236236
let name_ptrs = keys.iter().map(|it| it.as_ptr()).collect::<Vec<*const c_char>>();
@@ -287,18 +287,18 @@ impl LogicalType {
287287
_ => panic!("not a struct or union"),
288288
}
289289
};
290-
Self::from(c_logical_type)
290+
unsafe { Self::new(c_logical_type) }
291291
}
292292
}
293293

294294
#[cfg(test)]
295295
mod test {
296-
use crate::core::{LogicalType, LogicalTypeId};
296+
use crate::core::{LogicalTypeHandle, LogicalTypeId};
297297

298298
#[test]
299299
fn test_struct() {
300-
let fields = &[("hello", LogicalType::new(crate::core::LogicalTypeId::Boolean))];
301-
let typ = LogicalType::struct_type(fields);
300+
let fields = &[("hello", LogicalTypeHandle::from(crate::core::LogicalTypeId::Boolean))];
301+
let typ = LogicalTypeHandle::struct_type(fields);
302302

303303
assert_eq!(typ.num_children(), 1);
304304
assert_eq!(typ.child_name(0), "hello");
@@ -307,7 +307,7 @@ mod test {
307307

308308
#[test]
309309
fn test_decimal() {
310-
let typ = LogicalType::decimal(10, 2);
310+
let typ = LogicalTypeHandle::decimal(10, 2);
311311

312312
assert_eq!(typ.id(), crate::core::LogicalTypeId::Decimal);
313313
assert_eq!(typ.decimal_width(), 10);
@@ -316,7 +316,7 @@ mod test {
316316

317317
#[test]
318318
fn test_decimal_methods() {
319-
let typ = LogicalType::new(crate::core::LogicalTypeId::Varchar);
319+
let typ = LogicalTypeHandle::from(crate::core::LogicalTypeId::Varchar);
320320

321321
assert_eq!(typ.decimal_width(), 0);
322322
assert_eq!(typ.decimal_scale(), 0);
@@ -325,10 +325,10 @@ mod test {
325325
#[test]
326326
fn test_union_type() {
327327
let fields = &[
328-
("hello", LogicalType::new(LogicalTypeId::Boolean)),
329-
("world", LogicalType::new(LogicalTypeId::Integer)),
328+
("hello", LogicalTypeHandle::from(LogicalTypeId::Boolean)),
329+
("world", LogicalTypeHandle::from(LogicalTypeId::Integer)),
330330
];
331-
let typ = LogicalType::union_type(fields);
331+
let typ = LogicalTypeHandle::union_type(fields);
332332

333333
assert_eq!(typ.num_children(), 2);
334334

crates/duckdb/src/core/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ mod data_chunk;
22
mod logical_type;
33
mod vector;
44

5-
pub use data_chunk::DataChunk;
6-
pub use logical_type::{LogicalType, LogicalTypeId};
5+
pub use data_chunk::DataChunkHandle;
6+
pub use logical_type::{LogicalTypeHandle, LogicalTypeId};
77
pub use vector::*;

0 commit comments

Comments
 (0)