Skip to content
This repository has been archived by the owner on May 5, 2024. It is now read-only.

Commit

Permalink
feat: add basic map support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mause committed Oct 25, 2022
1 parent 7fcfe30 commit fc734aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::duckly::*;

#[derive(num_derive::FromPrimitive)]
#[derive(Debug, Eq, PartialEq, num_derive::FromPrimitive)]
pub enum DuckDBType {
Boolean = DUCKDB_TYPE_DUCKDB_TYPE_BOOLEAN as isize,
Tinyint = DUCKDB_TYPE_DUCKDB_TYPE_TINYINT as isize,
Expand Down
34 changes: 32 additions & 2 deletions src/logical_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::constants::DuckDBType;
use crate::duckly::{
duckdb_create_logical_type, duckdb_destroy_logical_type, duckdb_get_type_id,
duckdb_logical_type,
duckdb_create_logical_type, duckdb_create_map_type, duckdb_destroy_logical_type,
duckdb_get_type_id, duckdb_logical_type,
};
use num_traits::FromPrimitive;

Expand All @@ -18,12 +18,26 @@ impl LogicalType {
}
}
}
pub fn new_map_type(key: &LogicalType, value: &LogicalType) -> Self {
unsafe {
Self {
typ: duckdb_create_map_type(key.typ, value.typ),
}
}
}
pub fn type_id(&self) -> DuckDBType {
let id = unsafe { duckdb_get_type_id(self.typ) };

FromPrimitive::from_u32(id).unwrap()
}
}
impl Clone for LogicalType {
fn clone(&self) -> Self {
let type_id = self.type_id();

Self::new(type_id)
}
}

impl From<duckdb_logical_type> for LogicalType {
fn from(ptr: duckdb_logical_type) -> Self {
Expand All @@ -38,3 +52,19 @@ impl Drop for LogicalType {
}
}
}

#[cfg(test)]
mod test {
use crate::constants::DuckDBType;
use crate::LogicalType;
#[test]
fn test_logi() {
let key = LogicalType::new(DuckDBType::Varchar);

let value = LogicalType::new(DuckDBType::Utinyint);

let map = LogicalType::new_map_type(&key, &value);

assert_eq!(map.type_id(), DuckDBType::Map);
}
}

0 comments on commit fc734aa

Please sign in to comment.