Skip to content

Commit

Permalink
Implement appender for date/time types
Browse files Browse the repository at this point in the history
  • Loading branch information
rijkvp committed Jun 6, 2024
1 parent 4f772b3 commit af5befd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
22 changes: 15 additions & 7 deletions crates/duckdb/src/appender/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,23 @@ impl Appender<'_> {
ffi::duckdb_append_varchar_length(ptr, s.as_ptr() as *const c_char, s.len() as u64)
},
ValueRef::Timestamp(u, i) => unsafe {
let micros = match u {
TimeUnit::Second => i * 1_000_000,
TimeUnit::Millisecond => i * 1_000,
TimeUnit::Microsecond => i,
TimeUnit::Nanosecond => i / 1_000,
};
ffi::duckdb_append_timestamp(ptr, ffi::duckdb_timestamp { micros })
ffi::duckdb_append_timestamp(ptr, ffi::duckdb_timestamp { micros: u.to_micros(i) })
},
ValueRef::Blob(b) => unsafe { ffi::duckdb_append_blob(ptr, b.as_ptr() as *const c_void, b.len() as u64) },
ValueRef::Date32(d) => unsafe { ffi::duckdb_append_date(ptr, ffi::duckdb_date { days: d }) },
ValueRef::Time64(u, v) => unsafe {
ffi::duckdb_append_time(ptr, ffi::duckdb_time { micros: u.to_micros(v) })
},
ValueRef::Interval { months, days, nanos } => unsafe {
ffi::duckdb_append_interval(
ptr,
ffi::duckdb_interval {
months,
days,
micros: nanos / 1000,
},
)
},
_ => unreachable!("not supported"),
};
if rc != 0 {
Expand Down
12 changes: 12 additions & 0 deletions crates/duckdb/src/types/value_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ pub enum TimeUnit {
Nanosecond,
}

impl TimeUnit {
/// Convert a number of `TimeUnit` to microseconds.
pub fn to_micros(&self, value: i64) -> i64 {
match self {
TimeUnit::Second => value * 1_000_000,
TimeUnit::Millisecond => value * 1000,
TimeUnit::Microsecond => value,
TimeUnit::Nanosecond => value / 1000,
}
}
}

/// A non-owning [static type value](https://duckdb.org/docs/sql/data_types/overview). Typically the
/// memory backing this value is owned by SQLite.
///
Expand Down

0 comments on commit af5befd

Please sign in to comment.