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

Implement appender for date/time types #313

Merged
merged 3 commits into from
Jun 6, 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
47 changes: 39 additions & 8 deletions crates/duckdb/src/appender/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{ffi::c_void, fmt, os::raw::c_char};

use crate::{
error::result_from_duckdb_appender,
types::{TimeUnit, ToSql, ToSqlOutput},
types::{ToSql, ToSqlOutput},
Error,
};

Expand Down 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 Expand Up @@ -255,6 +263,29 @@ mod test {
Ok(())
}

#[test]
#[cfg(feature = "chrono")]
fn test_append_datetime() -> Result<()> {
use crate::params;
use chrono::{NaiveDate, NaiveDateTime};

let db = Connection::open_in_memory()?;
db.execute_batch("CREATE TABLE foo(x DATE, y TIMESTAMP)")?;

let date = NaiveDate::from_ymd_opt(2024, 6, 5).unwrap();
let timestamp = date.and_hms_opt(18, 26, 53).unwrap();
{
let mut app = db.appender("foo")?;
app.append_row(params![date, timestamp])?;
}
let (date2, timestamp2) = db.query_row("SELECT x, y FROM foo", [], |row| {
Ok((row.get::<_, NaiveDate>(0)?, row.get::<_, NaiveDateTime>(1)?))
})?;
assert_eq!(date, date2);
assert_eq!(timestamp, timestamp2);
Ok(())
}

#[test]
fn test_appender_error() -> Result<(), crate::Error> {
let conn = Connection::open_in_memory()?;
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
Loading