Skip to content

Commit

Permalink
Add more parameter types for Node.js API (#3037)
Browse files Browse the repository at this point in the history
* Add more parameter types for Node.js API

* Fix gcc compile error
  • Loading branch information
mewim committed Mar 13, 2024
1 parent 7da8a62 commit 7c16897
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 20 deletions.
77 changes: 68 additions & 9 deletions tools/nodejs_api/src_cpp/node_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,11 @@ Value Util::TransformNapiValue(
case LogicalTypeID::BOOL: {
return Value(napiValue.ToBoolean().Value());
}
case LogicalTypeID::INT16: {
case LogicalTypeID::INT64: {
if (!napiValue.IsNumber()) {
throw Exception("Expected a number for parameter " + key + ".");
}
int16_t val = napiValue.ToNumber().Int32Value();
int64_t val = napiValue.ToNumber().Int64Value();
return Value(val);
}
case LogicalTypeID::INT32: {
Expand All @@ -324,18 +324,65 @@ Value Util::TransformNapiValue(
int32_t val = napiValue.ToNumber().Int32Value();
return Value(val);
}
case LogicalTypeID::INT64: {
case LogicalTypeID::INT16: {
if (!napiValue.IsNumber()) {
throw Exception("Expected a number for parameter " + key + ".");
}
int64_t val = napiValue.ToNumber().Int64Value();
int16_t val = napiValue.ToNumber().Int32Value();
return Value(val);
}
case LogicalTypeID::FLOAT: {
case LogicalTypeID::INT8: {
if (!napiValue.IsNumber()) {
throw Exception("Expected a number for parameter " + key + ".");
}
float val = napiValue.ToNumber().FloatValue();
int8_t val = napiValue.ToNumber().Int32Value();
return Value(val);
}
case LogicalTypeID::UINT64: {
if (!napiValue.IsNumber()) {
throw Exception("Expected a number for parameter " + key + ".");
}
auto valStr = napiValue.ToNumber().ToString();
uint64_t val = std::stoull(valStr);
return Value(val);
}
case LogicalTypeID::UINT32: {
if (!napiValue.IsNumber()) {
throw Exception("Expected a number for parameter " + key + ".");
}
uint32_t val = napiValue.ToNumber().Uint32Value();
return Value(val);
}
case LogicalTypeID::UINT16: {
if (!napiValue.IsNumber()) {
throw Exception("Expected a number for parameter " + key + ".");
}
uint16_t val = napiValue.ToNumber().Uint32Value();
return Value(val);
}
case LogicalTypeID::UINT8: {
if (!napiValue.IsNumber()) {
throw Exception("Expected a number for parameter " + key + ".");
}
uint8_t val = napiValue.ToNumber().Uint32Value();
return Value(val);
}
case LogicalTypeID::INT128: {
if (!napiValue.IsBigInt()) {
throw Exception("Expected a BigInt for parameter " + key + ".");
}
auto bigInt = napiValue.As<Napi::BigInt>();
size_t wordsCount = bigInt.WordCount();
std::unique_ptr<uint64_t[]> words(new uint64_t[wordsCount]);
int signBit;
bigInt.ToWords(&signBit, &wordsCount, words.get());
kuzu::common::int128_t val;
val.low = words[0];
val.high = words[1];
// Ignore words[2] and beyond as we only support 128-bit integers but BigInt can be larger.
if (signBit) {
kuzu::common::Int128_t::negateInPlace(val);
}
return Value(val);
}
case LogicalTypeID::DOUBLE: {
Expand All @@ -345,9 +392,12 @@ Value Util::TransformNapiValue(
double val = napiValue.ToNumber().DoubleValue();
return Value(val);
}
case LogicalTypeID::STRING: {
std::string val = napiValue.ToString().Utf8Value();
return Value(LogicalType::STRING(), val);
case LogicalTypeID::FLOAT: {
if (!napiValue.IsNumber()) {
throw Exception("Expected a number for parameter " + key + ".");
}
float val = napiValue.ToNumber().FloatValue();
return Value(val);
}
case LogicalTypeID::DATE: {
if (!napiValue.IsDate()) {
Expand Down Expand Up @@ -416,6 +466,15 @@ Value Util::TransformNapiValue(
auto normalizedInterval = interval_t(normalizedMonths, normalizedDays, normalizedMicros);
return Value(normalizedInterval);
}
case LogicalTypeID::STRING: {
std::string val = napiValue.ToString().Utf8Value();
return Value(LogicalType::STRING(), val);
}
case LogicalTypeID::UUID: {
std::string stringVal = napiValue.ToString().Utf8Value();
auto val = UUID::fromString(stringVal);
return Value(val);
}
default:
throw Exception(
"Unsupported type " + expectedDataType->toString() + " for parameter: " + key);
Expand Down
5 changes: 3 additions & 2 deletions tools/nodejs_api/src_js/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@ class Connection {
typeof value === "boolean" ||
typeof value === "number" ||
typeof value === "string" ||
(typeof value === "object" && value.constructor.name === "Date")
(typeof value === "object" && value.constructor.name === "Date") ||
typeof value === "bigint"
) {
paramArray.push([key, value]);
} else {
return reject(
new Error(
"The value of each parameter must be a boolean, number, string, or Date object."
"The value of each parameter must be a boolean, number, string, Date or BigInt."
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion tools/nodejs_api/test/test_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe("Execute", function () {
} catch (e) {
assert.equal(
e.message,
"The value of each parameter must be a boolean, number, string, or Date object."
"The value of each parameter must be a boolean, number, string, Date or BigInt."
);
}
});
Expand Down
Loading

0 comments on commit 7c16897

Please sign in to comment.