Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,25 @@
{
"type": "org.duckdb.DuckDBArray[]"
},
{
"type": "org.duckdb.DuckDBBignum",
"jniAccessible": true,
"methods": [
{
"name": "toBigDecimal",
"parameterTypes": [
"byte[]",
"int"
]
},
{
"name": "toBigInteger",
"parameterTypes": [
"byte[]"
]
}
]
},
{
"type": "org.duckdb.DuckDBDate",
"jniAccessible": true,
Expand Down
2 changes: 2 additions & 0 deletions duckdb_java.def
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ Java_org_duckdb_DuckDBBindings_duckdb_1get_1uint64
Java_org_duckdb_DuckDBBindings_duckdb_1get_1hugeint
Java_org_duckdb_DuckDBBindings_duckdb_1get_1uhugeint
Java_org_duckdb_DuckDBBindings_duckdb_1get_1bignum
Java_org_duckdb_DuckDBBindings_duckdb_1bignum_1decode
Java_org_duckdb_DuckDBBindings_duckdb_1bignum_1encode
Java_org_duckdb_DuckDBBindings_duckdb_1get_1decimal
Java_org_duckdb_DuckDBBindings_duckdb_1get_1float
Java_org_duckdb_DuckDBBindings_duckdb_1get_1double
Expand Down
2 changes: 2 additions & 0 deletions duckdb_java.exp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ _Java_org_duckdb_DuckDBBindings_duckdb_1get_1uint64
_Java_org_duckdb_DuckDBBindings_duckdb_1get_1hugeint
_Java_org_duckdb_DuckDBBindings_duckdb_1get_1uhugeint
_Java_org_duckdb_DuckDBBindings_duckdb_1get_1bignum
_Java_org_duckdb_DuckDBBindings_duckdb_1bignum_1decode
_Java_org_duckdb_DuckDBBindings_duckdb_1bignum_1encode
_Java_org_duckdb_DuckDBBindings_duckdb_1get_1decimal
_Java_org_duckdb_DuckDBBindings_duckdb_1get_1float
_Java_org_duckdb_DuckDBBindings_duckdb_1get_1double
Expand Down
2 changes: 2 additions & 0 deletions duckdb_java.map
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ DUCKDB_JAVA {
Java_org_duckdb_DuckDBBindings_duckdb_1get_1hugeint;
Java_org_duckdb_DuckDBBindings_duckdb_1get_1uhugeint;
Java_org_duckdb_DuckDBBindings_duckdb_1get_1bignum;
Java_org_duckdb_DuckDBBindings_duckdb_1bignum_1decode;
Java_org_duckdb_DuckDBBindings_duckdb_1bignum_1encode;
Java_org_duckdb_DuckDBBindings_duckdb_1get_1decimal;
Java_org_duckdb_DuckDBBindings_duckdb_1get_1float;
Java_org_duckdb_DuckDBBindings_duckdb_1get_1double;
Expand Down
6 changes: 6 additions & 0 deletions src/jni/bindings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ duckdb_v2_logical_type_handle logical_type_buf_to_logical_type(JNIEnv *env, jobj
*/
jobject make_logical_type_buf(JNIEnv *env, duckdb_v2_logical_type_handle logical_type);

/**
* Reads the width (index 0) or the scale (index 1) parameter of a DECIMAL type, returns 0 if the parameter
* cannot be read.
*/
uint32_t get_decimal_param(duckdb_v2_logical_type_handle logical_type, idx_t index);

/**
* Data chunks are passed to Java as C API v2 handles, owned (created or fetched chunks) or borrowed
* (chunks passed to function callbacks).
Expand Down
31 changes: 15 additions & 16 deletions src/jni/bindings_logical_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "refs.hpp"
#include "util.hpp"

#include <cstdint>
#include <cstring>
#include <vector>

Expand Down Expand Up @@ -66,10 +67,7 @@ static idx_t get_type_param_count(duckdb_v2_logical_type_handle lt) {
return count;
}

/*
* Reads the width (index 0) or the scale (index 1) parameter of a DECIMAL type.
*/
static uint32_t get_decimal_param(duckdb_v2_logical_type_handle lt, idx_t index) {
uint32_t get_decimal_param(duckdb_v2_logical_type_handle lt, idx_t index) {
v2_value value;
if (!get_type_param(lt, index, nullptr, &value)) {
return 0;
Expand All @@ -81,6 +79,13 @@ static uint32_t get_decimal_param(duckdb_v2_logical_type_handle lt, idx_t index)
return res;
}

/*
* The width and the scale of a DECIMAL are 32-bit unsigned, the values that do not fit a Java int are clamped.
*/
static jint decimal_param_to_jint(uint32_t param) {
return param > static_cast<uint32_t>(INT32_MAX) ? INT32_MAX : static_cast<jint>(param);
}

/*
* Creates a type from type ID with the specified parameter values, takes ownership of the values.
*/
Expand Down Expand Up @@ -177,8 +182,8 @@ JNIEXPORT jobject JNICALL Java_org_duckdb_DuckDBBindings_duckdb_1create_1logical
JNIEXPORT jobject JNICALL Java_org_duckdb_DuckDBBindings_duckdb_1create_1decimal_1type(JNIEnv *env, jclass, jint width,
jint scale) {

if (width < 1 || width > 38) {
env->ThrowNew(J_SQLException, "Invalid decimal width: expected 1..38");
if (width < 1) {
env->ThrowNew(J_SQLException, "Invalid decimal width: expected at least 1");
return nullptr;
}
if (scale < 0 || scale > width) {
Expand All @@ -187,8 +192,8 @@ JNIEXPORT jobject JNICALL Java_org_duckdb_DuckDBBindings_duckdb_1create_1decimal
}
auto factory = default_factory();
std::vector<v2_value> values(2);
duckdb_v2_value_create_utinyint(factory, static_cast<uint8_t>(width), values[0].out(), nullptr);
duckdb_v2_value_create_utinyint(factory, static_cast<uint8_t>(scale), values[1].out(), nullptr);
duckdb_v2_value_create_uint(factory, static_cast<uint32_t>(width), values[0].out(), nullptr);
duckdb_v2_value_create_uint(factory, static_cast<uint32_t>(scale), values[1].out(), nullptr);
auto lt = create_type(factory, DUCKDB_V2_LOGICAL_TYPE_ID_DECIMAL, {}, std::move(values));
return make_logical_type_buf(env, lt);
}
Expand Down Expand Up @@ -224,9 +229,7 @@ JNIEXPORT jint JNICALL Java_org_duckdb_DuckDBBindings_duckdb_1decimal_1width(JNI
if (logical_type_to_capi(lt) != capi_type::DECIMAL) {
return 0;
}
uint32_t width = get_decimal_param(lt, 0);
// same as in C API v1, wider decimals report 0
return width > UINT8_MAX ? 0 : static_cast<jint>(width);
return decimal_param_to_jint(get_decimal_param(lt, 0));
}

/*
Expand All @@ -245,11 +248,7 @@ JNIEXPORT jint JNICALL Java_org_duckdb_DuckDBBindings_duckdb_1decimal_1scale(JNI
if (logical_type_to_capi(lt) != capi_type::DECIMAL) {
return 0;
}
if (get_decimal_param(lt, 0) > UINT8_MAX) {
return 0;
}
uint32_t scale = get_decimal_param(lt, 1);
return scale > UINT8_MAX ? 0 : static_cast<jint>(scale);
return decimal_param_to_jint(get_decimal_param(lt, 1));
}

/*
Expand Down
146 changes: 140 additions & 6 deletions src/jni/bindings_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cstdint>
#include <cstring>
#include <limits>
#include <vector>

/*
* Values are passed to Java as owned C API v2 handles.
Expand Down Expand Up @@ -260,15 +261,140 @@ JNIEXPORT jobject JNICALL Java_org_duckdb_DuckDBBindings_duckdb_1get_1uhugeint(J
return env->CallStaticObjectMethod(J_HugeInt, J_HugeInt_toBigInteger, lower, upper);
}

/*
* Decodes BIGNUM storage bytes into the big-endian two's-complement bytes of a java.math.BigInteger, throws
* an SQLException and returns nullptr if the storage bytes are not valid.
*/
static jbyteArray bignum_storage_to_jbyteArray(JNIEnv *env, const char *storage, idx_t storage_len) {
std::vector<uint8_t> bytes;
v2_error err;
if (!bignum_storage_to_java(storage, storage_len, bytes, err)) {
throw_sql_exception(env, err);
return nullptr;
}
return make_jbyteArray(env, reinterpret_cast<const char *>(bytes.data()), static_cast<idx_t>(bytes.size()));
}

/*
* Reads a non-NULL value that is stored as a BIGNUM blob (a BIGNUM or a DECIMAL that is wider than 38 digits)
* as the big-endian two's-complement bytes of a java.math.BigInteger.
*/
static jbyteArray get_bignum_bytes(JNIEnv *env, duckdb_v2_value_handle val) {
const char *blob_ptr = nullptr;
idx_t blob_length = 0;
v2_error err;
if (duckdb_v2_value_get_blob(val, &blob_ptr, &blob_length, err.out()) != DUCKDB_V2_ERROR_NONE) {
throw_sql_exception(env, err);
return nullptr;
}
return bignum_storage_to_jbyteArray(env, blob_ptr, blob_length);
}

/*
* Class: org_duckdb_DuckDBBindings
* Method: duckdb_get_bignum
* Signature: (Ljava/nio/ByteBuffer;)Ljava/math/BigInteger;
*/
JNIEXPORT jobject JNICALL Java_org_duckdb_DuckDBBindings_duckdb_1get_1bignum(JNIEnv *env, jclass, jobject value) {
(void)env;
(void)value;
return nullptr;
auto val = value_buf_to_value(env, value);
if (env->ExceptionCheck()) {
return nullptr;
}
// null is returned for non-BIGNUM and NULL values
v2_logical_type lt;
if (duckdb_v2_value_get_logical_type(val, lt.out(), nullptr) != DUCKDB_V2_ERROR_NONE ||
logical_type_to_capi(lt.get()) != capi_type::BIGNUM) {
return nullptr;
}
bool is_null = false;
if (duckdb_v2_value_is_null(val, &is_null, nullptr) != DUCKDB_V2_ERROR_NONE || is_null) {
return nullptr;
}
jbyteArray bytes = get_bignum_bytes(env, val);
if (env->ExceptionCheck()) {
return nullptr;
}
return env->CallStaticObjectMethod(J_Bignum, J_Bignum_toBigInteger, bytes);
}

/*
* Class: org_duckdb_DuckDBBindings
* Method: duckdb_bignum_decode
* Signature: ([B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_org_duckdb_DuckDBBindings_duckdb_1bignum_1decode(JNIEnv *env, jclass,
jbyteArray storage) {
if (storage == nullptr) {
env->ThrowNew(J_SQLException, "Invalid BIGNUM storage");
return nullptr;
}
std::vector<char> storage_bytes(static_cast<size_t>(env->GetArrayLength(storage)));
env->GetByteArrayRegion(storage, 0, static_cast<jsize>(storage_bytes.size()),
reinterpret_cast<jbyte *>(storage_bytes.data()));
if (env->ExceptionCheck()) {
return nullptr;
}
return bignum_storage_to_jbyteArray(env, storage_bytes.data(), static_cast<idx_t>(storage_bytes.size()));
}

/*
* Class: org_duckdb_DuckDBBindings
* Method: duckdb_bignum_encode
* Signature: ([B)[B
*/
JNIEXPORT jbyteArray JNICALL Java_org_duckdb_DuckDBBindings_duckdb_1bignum_1encode(JNIEnv *env, jclass,
jbyteArray value) {
if (value == nullptr) {
env->ThrowNew(J_SQLException, "Invalid BIGNUM value");
return nullptr;
}
std::vector<uint8_t> value_bytes(static_cast<size_t>(env->GetArrayLength(value)));
env->GetByteArrayRegion(value, 0, static_cast<jsize>(value_bytes.size()),
reinterpret_cast<jbyte *>(value_bytes.data()));
if (env->ExceptionCheck()) {
return nullptr;
}
std::vector<uint8_t> storage;
v2_error err;
if (!bignum_storage_from_java(value_bytes.data(), static_cast<idx_t>(value_bytes.size()), storage, err)) {
throw_sql_exception(env, err);
return nullptr;
}
return make_jbyteArray(env, reinterpret_cast<const char *>(storage.data()), static_cast<idx_t>(storage.size()));
}

/*
* The widest DECIMAL that is stored as a fixed-size integer, wider decimals are stored as BIGNUM blobs.
*/
static constexpr uint32_t MAX_FIXED_SIZE_DECIMAL_WIDTH = 38;

/*
* Reads the type of the value, returns true if it is a DECIMAL that is stored as a BIGNUM blob.
*/
static bool is_wide_decimal(duckdb_v2_value_handle val, v2_logical_type &lt) {
if (duckdb_v2_value_get_logical_type(val, lt.out(), nullptr) != DUCKDB_V2_ERROR_NONE) {
return false;
}
if (logical_type_to_capi(lt.get()) != capi_type::DECIMAL) {
return false;
}
return get_decimal_param(lt.get(), 0) > MAX_FIXED_SIZE_DECIMAL_WIDTH;
}

/*
* Reads a DECIMAL that is stored as a BIGNUM blob (wider than 38 digits), returns nullptr for a NULL value.
*/
static jobject get_wide_decimal(JNIEnv *env, duckdb_v2_value_handle val, duckdb_v2_logical_type_handle lt) {
bool is_null = false;
if (duckdb_v2_value_is_null(val, &is_null, nullptr) != DUCKDB_V2_ERROR_NONE || is_null) {
return nullptr;
}
uint32_t scale = get_decimal_param(lt, 1);
jbyteArray unscaled = get_bignum_bytes(env, val);
if (env->ExceptionCheck()) {
return nullptr;
}
return env->CallStaticObjectMethod(J_Bignum, J_Bignum_toBigDecimal, unscaled, static_cast<jint>(scale));
}

/*
Expand All @@ -282,12 +408,20 @@ JNIEXPORT jobject JNICALL Java_org_duckdb_DuckDBBindings_duckdb_1get_1decimal(JN
if (env->ExceptionCheck()) {
return nullptr;
}
// same as in C API v1, zero is returned for non-decimal and NULL values and for the decimals
// that cannot be represented
// same as in C API v1, zero is returned for non-decimal and NULL values
duckdb_v2_hugeint_t dec_value = {0, 0};
uint32_t dec_width = 0;
uint32_t dec_scale = 0;
if (duckdb_v2_value_get_decimal(val, &dec_value, &dec_width, &dec_scale, nullptr) != DUCKDB_V2_ERROR_NONE) {
// A decimal that is wider than 38 digits is read exactly from its BIGNUM storage. value_get_decimal is not
// tried for it: that call fails, with an error that is created and formatted for every such value, when
// the unscaled value does not fit in 128 bits.
v2_logical_type lt;
if (is_wide_decimal(val, lt)) {
jobject wide = get_wide_decimal(env, val, lt.get());
if (wide != nullptr || env->ExceptionCheck()) {
return wide;
}
} else if (duckdb_v2_value_get_decimal(val, &dec_value, &dec_width, &dec_scale, nullptr) != DUCKDB_V2_ERROR_NONE) {
dec_value = {0, 0};
dec_scale = 0;
}
Expand Down
60 changes: 60 additions & 0 deletions src/jni/capi_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,66 @@ void set_callback_error(duckdb_v2_error_info_handle *err, DUCKDB_V2_ERROR code,
duckdb_v2_error_info_set_text(*err, message.data(), message.size());
}

/*
* Negates a big-endian two's-complement integer in place: all the bits are inverted and one is added.
*/
static void negate_twos_complement(std::vector<uint8_t> &bytes) {
bool carry = true;
for (idx_t i = bytes.size(); i-- > 0;) {
uint8_t inverted = static_cast<uint8_t>(~bytes[i]);
bytes[i] = carry ? static_cast<uint8_t>(inverted + 1) : inverted;
carry = carry && inverted == UINT8_MAX;
}
}

bool bignum_storage_to_java(const char *storage, idx_t storage_len, std::vector<uint8_t> &out, v2_error &err) {
auto in = reinterpret_cast<const uint8_t *>(storage);
idx_t magnitude_len = 0;
bool negative = false;
if (duckdb_v2_bignum_decode(in, storage_len, nullptr, 0, &magnitude_len, &negative, err.out()) !=
DUCKDB_V2_ERROR_NONE) {
return false;
}
// the magnitude is unsigned, the leading zero byte keeps its sign bit clear
out.assign(magnitude_len + 1, 0);
if (duckdb_v2_bignum_decode(in, storage_len, out.data() + 1, magnitude_len, &magnitude_len, &negative, err.out()) !=
DUCKDB_V2_ERROR_NONE) {
return false;
}
if (negative) {
negate_twos_complement(out);
}
return true;
}

bool bignum_storage_from_java(const uint8_t *bytes, idx_t bytes_len, std::vector<uint8_t> &out, v2_error &err) {
std::vector<uint8_t> magnitude;
bool negative = false;
if (bytes != nullptr && bytes_len > 0) {
magnitude.assign(bytes, bytes + bytes_len);
negative = (bytes[0] & 0x80) != 0;
if (negative) {
// the magnitude of a negative value always fits the same number of bytes
negate_twos_complement(magnitude);
}
}
// the magnitude must not have leading zero bytes, zero is a single zero byte
idx_t skip = 0;
while (skip + 1 < magnitude.size() && magnitude[skip] == 0) {
skip++;
}
const uint8_t *magnitude_ptr = magnitude.empty() ? nullptr : magnitude.data() + skip;
idx_t magnitude_len = magnitude.size() - skip;
idx_t storage_len = 0;
if (duckdb_v2_bignum_encode(magnitude_ptr, magnitude_len, negative, nullptr, 0, &storage_len, err.out()) !=
DUCKDB_V2_ERROR_NONE) {
return false;
}
out.assign(storage_len, 0);
return duckdb_v2_bignum_encode(magnitude_ptr, magnitude_len, negative, out.data(), storage_len, &storage_len,
err.out()) == DUCKDB_V2_ERROR_NONE;
}

bool make_v2_bytes(duckdb_v2_arena_handle arena, const char *data, idx_t len, duckdb_v2_bytes &out) {
std::memset(&out, 0, sizeof(out));
if (len > UINT32_MAX) {
Expand Down
Loading
Loading