Skip to content

Commit

Permalink
Merge pull request #28 from wangfenjin/hugeint
Browse files Browse the repository at this point in the history
support hugeint
  • Loading branch information
wangfenjin authored Sep 29, 2021
2 parents 38cd5fe + 1fee2eb commit 3e78ad4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ impl<'stmt> Row<'stmt> {
if array.is_null(row) {
return ValueRef::Null;
}
// hugeint: d:38,0
if array.scale() == 0 {
return ValueRef::HugeInt(array.value(row));
}
ValueRef::Decimal(Decimal::from_i128_with_scale(array.value(row), array.scale() as u32))
}
DataType::Timestamp(unit, _) if *unit == TimeUnit::Second => {
Expand Down
11 changes: 7 additions & 4 deletions src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,13 @@ impl Statement<'_> {
ValueRef::SmallInt(i) => unsafe { ffi::duckdb_bind_int16(ptr, col as u64, i) },
ValueRef::Int(i) => unsafe { ffi::duckdb_bind_int32(ptr, col as u64, i) },
ValueRef::BigInt(i) => unsafe { ffi::duckdb_bind_int64(ptr, col as u64, i) },

// FIXME
ValueRef::HugeInt(i) => unsafe { ffi::duckdb_bind_int64(ptr, col as u64, i as i64) },

ValueRef::HugeInt(i) => unsafe {
let hi = ffi::duckdb_hugeint {
lower: i as u64,
upper: (i >> 64) as i64,
};
ffi::duckdb_bind_hugeint(ptr, col as u64, hi)
},
ValueRef::Float(r) => unsafe { ffi::duckdb_bind_float(ptr, col as u64, r) },
ValueRef::Double(r) => unsafe { ffi::duckdb_bind_double(ptr, col as u64, r) },
ValueRef::Text(s) => unsafe {
Expand Down
14 changes: 14 additions & 0 deletions src/types/from_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,20 @@ mod test {
Ok(())
}

// This test asserts that i128s above/below the i64 max/min can written and retrieved properly.
#[test]
fn test_hugeint_max_min() -> Result<()> {
let db = Connection::open_in_memory()?;
db.execute("CREATE TABLE huge_int (u1 hugeint, u2 hugeint);", [])?;
// Min/Max value defined in here: https://duckdb.org/docs/sql/data_types/numeric
let i128max: i128 = i128::MAX;
let i128min: i128 = i128::MIN + 1;
db.execute("INSERT INTO huge_int VALUES (?, ?);", [&i128max, &i128min])?;
let v = db.query_row("SELECT * FROM huge_int", [], |row| <(i128, i128)>::try_from(row))?;
assert_eq!(v, (i128max, i128min));
Ok(())
}

#[test]
fn test_integral_ranges() -> Result<()> {
let db = Connection::open_in_memory()?;
Expand Down

0 comments on commit 3e78ad4

Please sign in to comment.