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

feat: [ODIN-895] LIST and STRUCT types #99

Merged
merged 6 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
71 changes: 47 additions & 24 deletions addon/result_iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,80 +141,103 @@ Napi::Value ResultIterator::getRowObject(Napi::Env env) {
}

Napi::Value ResultIterator::getCellValue(Napi::Env env, duckdb::idx_t col_idx) {
auto val = current_chunk->data[col_idx].GetValue(chunk_offset);
auto value = current_chunk->data[col_idx].GetValue(chunk_offset);
return getMappedValue(env, value);
}

if (val.is_null) {
Napi::Value ResultIterator::getMappedValue(Napi::Env env, duckdb::Value value) {
if (value.is_null) {
return env.Null();
}

switch (result->types[col_idx].id()) {
switch (value.type().id()) {
case duckdb::LogicalTypeId::BOOLEAN:
return Napi::Boolean::New(env, val.GetValue<bool>());
return Napi::Boolean::New(env, value.GetValue<bool>());
case duckdb::LogicalTypeId::TINYINT:
return Napi::Number::New(env, val.GetValue<int8_t>());
return Napi::Number::New(env, value.GetValue<int8_t>());
case duckdb::LogicalTypeId::SMALLINT:
return Napi::Number::New(env, val.GetValue<int16_t>());
return Napi::Number::New(env, value.GetValue<int16_t>());
case duckdb::LogicalTypeId::INTEGER:
return Napi::Number::New(env, val.GetValue<int32_t>());
return Napi::Number::New(env, value.GetValue<int32_t>());
case duckdb::LogicalTypeId::BIGINT:
return Napi::BigInt::New(env, val.GetValue<int64_t>());
return Napi::BigInt::New(env, value.GetValue<int64_t>());
case duckdb::LogicalTypeId::HUGEINT: {
// hugeint_t represents a signed 128 bit integer in two's complement
// notation napi's BigInt is basically a regular signed integer (MSB) so we
// want to make sure we pass the absolute value of the huge int into napi
// plus the sign bit
auto huge_int = val.GetValue<duckdb::hugeint_t>();
auto huge_int = value.GetValue<duckdb::hugeint_t>();
int is_negative = huge_int.upper < 0;
duckdb::hugeint_t positive_huge_int =
is_negative ? huge_int * duckdb::hugeint_t(-1) : huge_int;
uint64_t arr[2]{positive_huge_int.lower, (uint64_t)positive_huge_int.upper};
return Napi::BigInt::New(env, is_negative, 2, &arr[0]);
}
case duckdb::LogicalTypeId::FLOAT:
return Napi::Number::New(env, val.GetValue<float>());
return Napi::Number::New(env, value.GetValue<float>());
case duckdb::LogicalTypeId::DOUBLE:
return Napi::Number::New(env, val.GetValue<double>());
return Napi::Number::New(env, value.GetValue<double>());
case duckdb::LogicalTypeId::DECIMAL:
return Napi::Number::New(
env, val.CastAs(duckdb::LogicalType::DOUBLE).GetValue<double>());
env, value.CastAs(duckdb::LogicalType::DOUBLE).GetValue<double>());
case duckdb::LogicalTypeId::VARCHAR:
return Napi::String::New(env, val.GetValue<string>());
return Napi::String::New(env, value.GetValue<string>());
case duckdb::LogicalTypeId::BLOB: {
int array_length = val.str_value.length();
int array_length = value.str_value.length();
char char_array[array_length + 1];
// TODO: multiple copies, improve
strcpy(char_array, val.str_value.c_str());
strcpy(char_array, value.str_value.c_str());
return Napi::Buffer<char>::Copy(env, char_array, array_length);
}
case duckdb::LogicalTypeId::TIMESTAMP: {
if (result->types[col_idx].InternalType() != duckdb::PhysicalType::INT64) {
if (value.type().InternalType() != duckdb::PhysicalType::INT64) {
throw runtime_error("expected int64 for timestamp");
}
int64_t tval = val.GetValue<int64_t>();
int64_t tval = value.GetValue<int64_t>();
return Napi::Number::New(env, tval / 1000);
}
case duckdb::LogicalTypeId::TIME: {
if (result->types[col_idx].InternalType() != duckdb::PhysicalType::INT64) {
if (value.type().InternalType() != duckdb::PhysicalType::INT64) {
throw runtime_error("expected int64 for time");
}
int64_t tval = val.GetValue<int64_t>();
int64_t tval = value.GetValue<int64_t>();
return Napi::Number::New(env, GetTime(tval));
}
case duckdb::LogicalTypeId::INTERVAL: {
return Napi::String::New(env, val.ToString());
return Napi::String::New(env, value.ToString());
}
case duckdb::LogicalTypeId::UTINYINT:
return Napi::Number::New(env, val.GetValue<uint8_t>());
return Napi::Number::New(env, value.GetValue<uint8_t>());
case duckdb::LogicalTypeId::USMALLINT:
return Napi::Number::New(env, val.GetValue<uint16_t>());
return Napi::Number::New(env, value.GetValue<uint16_t>());
case duckdb::LogicalTypeId::UINTEGER:
// GetValue is not supported for uint32_t, so using the wider type
return Napi::Number::New(env, val.GetValue<int64_t>());
return Napi::Number::New(env, value.GetValue<int64_t>());
case duckdb::LogicalTypeId::LIST: {
auto array = Napi::Array::New(env);
for (size_t i = 0; i < value.list_value.size(); i++) {
auto &element = value.list_value[i];
auto mapped_value = getMappedValue(env, element);
array.Set(i, mapped_value);
}
return array;
}
case duckdb::LogicalTypeId::STRUCT: {
auto object = Napi::Object::New(env);
for (size_t t = 0; t < value.struct_value.size(); t++) {
auto &key = value.type().child_types()[t].first;
auto &element = value.struct_value[t];
auto child_value = getMappedValue(env, element);
object.Set(key, child_value);
}
return object;
}
default:
// default to getting string representation
return Napi::String::New(env, val.ToString());
return Napi::String::New(env, value.ToString());
}
}

Napi::Value ResultIterator::Close(const Napi::CallbackInfo &info) {
result.reset();
return info.Env().Undefined();
Expand Down
1 change: 1 addition & 0 deletions addon/result_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ResultIterator : public Napi::ObjectWrap<ResultIterator> {
duckdb::unique_ptr<duckdb::DataChunk> current_chunk;
uint64_t chunk_offset = 0;
Napi::Value getCellValue(Napi::Env env, duckdb::idx_t col_idx);
Napi::Value getMappedValue(Napi::Env env, duckdb::Value duckdb_value);
Napi::Value getRowArray(Napi::Env env);
Napi::Value getRowObject(Napi::Env env);
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-duckdb",
"version": "0.0.70",
"version": "0.0.71",
"private": false,
"description": "DuckDB for Node.JS",
"keywords": [
Expand Down
65 changes: 65 additions & 0 deletions src/tests/data-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,69 @@ describe("Data type mapping", () => {

expect(result.fetchRow()).toMatchObject([1]);
});

it("supports LIST - integers", async () => {
const result = await connection.executeIterator<any[]>(`SELECT array_slice([1,2,3], 1, NULL)`);
expect(result.fetchRow()).toMatchObject({"array_slice(list_value(1, 2, 3), 1, NULL)": [2, 3]});
});

it("supports LIST - strings", async () => {
const result = await connection.executeIterator<any[]>(`SELECT array_slice(['a','b','c'], 1, NULL)`);
expect(result.fetchRow()).toEqual({"array_slice(list_value(a, b, c), 1, NULL)": ["b", "c"]});
});

it("supports LIST - STRUCTs", async () => {
const result = await connection.executeIterator<any[]>(
`SELECT raw_header from parquet_scan('src/tests/test-fixtures/crawl_urls.parquet')`,
);
expect(result.fetchRow()).toEqual({
"raw_header": [
{
"key": "Content-Encoding",
"value": "gzip",
},
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN",
},
{
"key": "Connection",
"value": "keep-alive",
},
{
"key": "X-Xss-Protection",
"value": "1; mode=block",
},
{
"key": "Content-Type",
"value": "text/html;charset=utf-8",
},
{
"key": "Date",
"value": "Tue, 18 Aug 2020 13:46:36 GMT",
},
{
"key": "Vary",
"value": "User-agent,Accept-Encoding",
},
{
"key": "Server",
"value": "nginx/1.10.3",
},
{
"key": "X-Content-Type-Options",
"value": "nosniff",
},
{
"key": "Content-Length",
"value": "1180",
},
],
});
});

it("supports STRUCT", async () => {
const result = await connection.executeIterator<any[]>(`SELECT struct_pack(i := 4, s := 'string')`);
expect(result.fetchRow()).toEqual({"struct_pack(4, string)": {"i": 4, "s": "string"}});
});
});
Binary file added src/tests/test-fixtures/crawl_urls.parquet
Binary file not shown.