Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create struct & decimal types for virtual tables #246

Merged
merged 8 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "libduckdb-sys/duckdb-sources"]
path = libduckdb-sys/duckdb-sources
url = git@github.com:duckdb/duckdb.git
update = none
url = https://github.com/duckdb/duckdb
update = none
2 changes: 1 addition & 1 deletion src/vtab/excel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl VTab for ExcelVTab {
for data in rows.by_ref() {
// find the first row with no empty cell
let mut found = true;
for (_, cell) in data.iter().enumerate() {
for cell in data.iter() {
match cell {
DataType::Error(_) | DataType::Empty => {
found = false;
Expand Down
91 changes: 72 additions & 19 deletions src/vtab/logical_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{ffi::CString, fmt::Debug};
use std::{
ffi::{c_char, CString},
fmt::Debug,
};

use crate::ffi::*;

Expand Down Expand Up @@ -176,25 +179,43 @@ impl LogicalType {
}
}

/// Creates a decimal type from its `width` and `scale`.
pub fn decimal(width: u8, scale: u8) -> Self {
unsafe {
Self {
ptr: duckdb_create_decimal_type(width, scale),
}
}
}

/// Retrieves the decimal width
/// Returns 0 if the LogicalType is not a decimal
pub fn decimal_width(&self) -> u8 {
unsafe { duckdb_decimal_width(self.ptr) }
}

/// Retrieves the decimal scale
/// Returns 0 if the LogicalType is not a decimal
pub fn decimal_scale(&self) -> u8 {
unsafe { duckdb_decimal_scale(self.ptr) }
}

/// Make a `LogicalType` for `struct`
// pub fn struct_type(fields: &[(&str, LogicalType)]) -> Self {
// let keys: Vec<CString> = fields.iter().map(|f| CString::new(f.0).unwrap()).collect();
// let values: Vec<duckdb_logical_type> = fields.iter().map(|it| it.1.ptr).collect();
// let name_ptrs = keys
// .iter()
// .map(|it| it.as_ptr())
// .collect::<Vec<*const c_char>>();

// unsafe {
// Self {
// ptr: duckdb_create_struct_type(
// fields.len() as idx_t,
// name_ptrs.as_slice().as_ptr().cast_mut(),
// values.as_slice().as_ptr(),
// ),
// }
// }
// }
pub fn struct_type(fields: &[(&str, LogicalType)]) -> Self {
let keys: Vec<CString> = fields.iter().map(|f| CString::new(f.0).unwrap()).collect();
let values: Vec<duckdb_logical_type> = fields.iter().map(|it| it.1.ptr).collect();
let name_ptrs = keys.iter().map(|it| it.as_ptr()).collect::<Vec<*const c_char>>();

unsafe {
Self {
ptr: duckdb_create_struct_type(
values.as_slice().as_ptr().cast_mut(),
name_ptrs.as_slice().as_ptr().cast_mut(),
fields.len() as idx_t,
),
}
}
}

/// Logical type ID
pub fn id(&self) -> LogicalTypeId {
Expand Down Expand Up @@ -228,3 +249,35 @@ impl LogicalType {
Self::from(c_logical_type)
}
}

#[cfg(test)]
mod test {
use crate::vtab::LogicalType;

#[test]
fn test_struct() {
let fields = &[("hello", LogicalType::new(crate::vtab::LogicalTypeId::Boolean))];
let typ = LogicalType::struct_type(fields);

assert_eq!(typ.num_children(), 1);
assert_eq!(typ.child_name(0), "hello");
assert_eq!(typ.child(0).id(), crate::vtab::LogicalTypeId::Boolean);
}

#[test]
fn test_decimal() {
let typ = LogicalType::decimal(10, 2);

assert_eq!(typ.id(), crate::vtab::LogicalTypeId::Decimal);
assert_eq!(typ.decimal_width(), 10);
assert_eq!(typ.decimal_scale(), 2);
}

#[test]
fn test_decimal_methods() {
let typ = LogicalType::new(crate::vtab::LogicalTypeId::Varchar);

assert_eq!(typ.decimal_width(), 0);
assert_eq!(typ.decimal_scale(), 0);
}
}
Loading