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

vendor: Update vendored sources to duckdb/duckdb@6c3fdfc17b3690db2892703956e2a4f3739adf6b #1022

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
39 changes: 30 additions & 9 deletions src/duckdb/src/common/arrow/arrow_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@
#include "duckdb/function/table/arrow.hpp"
#include "duckdb/common/arrow/appender/append_data.hpp"
#include "duckdb/common/arrow/appender/list.hpp"
#include "duckdb/function/table/arrow/arrow_duck_schema.hpp"

namespace duckdb {

//===--------------------------------------------------------------------===//
// ArrowAppender
//===--------------------------------------------------------------------===//

ArrowAppender::ArrowAppender(vector<LogicalType> types_p, const idx_t initial_capacity, ClientProperties options)
: types(std::move(types_p)) {
for (auto &type : types) {
auto entry = InitializeChild(type, initial_capacity, options);
ArrowAppender::ArrowAppender(vector<LogicalType> types_p, const idx_t initial_capacity, ClientProperties options,
unordered_map<idx_t, const shared_ptr<ArrowTypeExtensionData>> extension_type_cast)
: types(std::move(types_p)), options(options) {
for (idx_t i = 0; i < types.size(); i++) {
unique_ptr<ArrowAppendData> entry;
bool bitshift_boolean = types[i].id() == LogicalTypeId::BOOLEAN && !options.arrow_lossless_conversion;
if (extension_type_cast.find(i) != extension_type_cast.end() && !bitshift_boolean) {
entry = InitializeChild(types[i], initial_capacity, options, extension_type_cast[i]);
} else {
entry = InitializeChild(types[i], initial_capacity, options);
}
root_data.push_back(std::move(entry));
}
}
Expand All @@ -26,11 +34,18 @@ ArrowAppender::~ArrowAppender() {
}

//! Append a data chunk to the underlying arrow array
void ArrowAppender::Append(DataChunk &input, idx_t from, idx_t to, idx_t input_size) {
void ArrowAppender::Append(DataChunk &input, const idx_t from, const idx_t to, const idx_t input_size) {
D_ASSERT(types == input.GetTypes());
D_ASSERT(to >= from);
for (idx_t i = 0; i < input.ColumnCount(); i++) {
root_data[i]->append_vector(*root_data[i], input.data[i], from, to, input_size);
if (root_data[i]->extension_data && root_data[i]->extension_data->duckdb_to_arrow) {
Vector input_data(root_data[i]->extension_data->GetInternalType());
root_data[i]->extension_data->duckdb_to_arrow(*options.client_context, input.data[i], input_data,
input_size);
root_data[i]->append_vector(*root_data[i], input_data, from, to, input_size);
} else {
root_data[i]->append_vector(*root_data[i], input.data[i], from, to, input_size);
}
}
row_count += to - from;
}
Expand Down Expand Up @@ -285,13 +300,19 @@ static void InitializeFunctionPointers(ArrowAppendData &append_data, const Logic
}

unique_ptr<ArrowAppendData> ArrowAppender::InitializeChild(const LogicalType &type, const idx_t capacity,
ClientProperties &options) {
ClientProperties &options,
const shared_ptr<ArrowTypeExtensionData> &extension_type) {
auto result = make_uniq<ArrowAppendData>(options);
InitializeFunctionPointers(*result, type);
LogicalType array_type = type;
if (extension_type) {
array_type = extension_type->GetInternalType();
}
InitializeFunctionPointers(*result, array_type);
result->extension_data = extension_type;

const auto byte_count = (capacity + 7) / 8;
result->GetValidityBuffer().reserve(byte_count);
result->initialize(*result, type, capacity);
result->initialize(*result, array_type, capacity);
return result;
}

Expand Down
145 changes: 63 additions & 82 deletions src/duckdb/src/common/arrow/arrow_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
#include "duckdb/common/types/vector_cache.hpp"
#include "duckdb/common/unordered_map.hpp"
#include "duckdb/common/vector.hpp"
#include <list>
#include "duckdb/main/config.hpp"

#include "duckdb/common/arrow/arrow_appender.hpp"
#include "duckdb/common/arrow/schema_metadata.hpp"

#include "duckdb/main/client_context.hpp"
namespace duckdb {

void ArrowConverter::ToArrowArray(DataChunk &input, ArrowArray *out_array, ClientProperties options) {
ArrowAppender appender(input.GetTypes(), input.size(), std::move(options));
void ArrowConverter::ToArrowArray(
DataChunk &input, ArrowArray *out_array, ClientProperties options,
const unordered_map<idx_t, const shared_ptr<ArrowTypeExtensionData>> &extension_type_cast) {
ArrowAppender appender(input.GetTypes(), input.size(), std::move(options), extension_type_cast);
appender.Append(input, 0, input.size(), input.size());
*out_array = appender.Finalize();
}
Expand All @@ -30,24 +33,6 @@ unsafe_unique_array<char> AddName(const string &name) {
return name_ptr;
}

//===--------------------------------------------------------------------===//
// Arrow Schema
//===--------------------------------------------------------------------===//
struct DuckDBArrowSchemaHolder {
// unused in children
vector<ArrowSchema> children;
// unused in children
vector<ArrowSchema *> children_ptrs;
//! used for nested structures
std::list<vector<ArrowSchema>> nested_children;
std::list<vector<ArrowSchema *>> nested_children_ptr;
//! This holds strings created to represent decimal types
vector<unsafe_unique_array<char>> owned_type_names;
vector<unsafe_unique_array<char>> owned_column_names;
//! This holds any values created for metadata info
vector<unsafe_unique_array<char>> metadata_info;
};

static void ReleaseDuckDBArrowSchema(ArrowSchema *schema) {
if (!schema || !schema->release) {
return;
Expand All @@ -74,10 +59,10 @@ void InitializeChild(ArrowSchema &child, DuckDBArrowSchemaHolder &root_holder, c
}

void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, const LogicalType &type,
const ClientProperties &options);
ClientProperties &options, ClientContext &context);

void SetArrowMapFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, const LogicalType &type,
const ClientProperties &options) {
ClientProperties &options, ClientContext &context) {
child.format = "+m";
//! Map has one child which is a struct
child.n_children = 1;
Expand All @@ -88,14 +73,38 @@ void SetArrowMapFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child,
InitializeChild(root_holder.nested_children.back()[0], root_holder);
child.children = &root_holder.nested_children_ptr.back()[0];
child.children[0]->name = "entries";
SetArrowFormat(root_holder, **child.children, ListType::GetChildType(type), options);
SetArrowFormat(root_holder, **child.children, ListType::GetChildType(type), options, context);
}

bool SetArrowExtension(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, const LogicalType &type,
ClientContext &context) {
auto &config = DBConfig::GetConfig(context);
if (config.HasArrowExtension(type.id())) {
auto arrow_extension = config.GetArrowExtension(type.id());
arrow_extension.PopulateArrowSchema(root_holder, child, type, context, arrow_extension);
return true;
}
return false;
}

void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, const LogicalType &type,
const ClientProperties &options) {
ClientProperties &options, ClientContext &context) {
if (type.HasAlias()) {
// If it is a json type, we only export it as json if arrow_lossless_conversion = True
if (!(type.IsJSONType() && !options.arrow_lossless_conversion)) {
// If the type has an alias, we check if it is an Arrow-Type extension
if (SetArrowExtension(root_holder, child, type, context)) {
return;
}
}
}
switch (type.id()) {
case LogicalTypeId::BOOLEAN:
child.format = "b";
if (options.arrow_lossless_conversion) {
SetArrowExtension(root_holder, child, type, context);
} else {
child.format = "b";
}
break;
case LogicalTypeId::TINYINT:
child.format = "c";
Expand Down Expand Up @@ -126,43 +135,18 @@ void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, co
break;
case LogicalTypeId::HUGEINT: {
if (options.arrow_lossless_conversion) {
child.format = "w:16";
auto schema_metadata = ArrowSchemaMetadata::DuckDBInternalType("hugeint");
root_holder.metadata_info.emplace_back(schema_metadata.SerializeMetadata());
child.metadata = root_holder.metadata_info.back().get();
SetArrowExtension(root_holder, child, type, context);
} else {
child.format = "d:38,0";
}
break;
}
case LogicalTypeId::UHUGEINT: {
child.format = "w:16";
auto schema_metadata = ArrowSchemaMetadata::DuckDBInternalType("uhugeint");
root_holder.metadata_info.emplace_back(schema_metadata.SerializeMetadata());
child.metadata = root_holder.metadata_info.back().get();
break;
}
case LogicalTypeId::VARINT: {
if (options.arrow_offset_size == ArrowOffsetSize::LARGE) {
child.format = "Z";
} else {
child.format = "z";
}
auto schema_metadata = ArrowSchemaMetadata::DuckDBInternalType("varint");
root_holder.metadata_info.emplace_back(schema_metadata.SerializeMetadata());
child.metadata = root_holder.metadata_info.back().get();
break;
}
case LogicalTypeId::DOUBLE:
child.format = "g";
break;
case LogicalTypeId::UUID: {
if (options.arrow_lossless_conversion) {
// This is a canonical extension, hence needs the "arrow." prefix
child.format = "w:16";
auto schema_metadata = ArrowSchemaMetadata::ArrowCanonicalType("arrow.uuid");
root_holder.metadata_info.emplace_back(schema_metadata.SerializeMetadata());
child.metadata = root_holder.metadata_info.back().get();
SetArrowExtension(root_holder, child, type, context);
} else {
if (options.produce_arrow_string_view) {
child.format = "vu";
Expand All @@ -177,11 +161,6 @@ void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, co
break;
}
case LogicalTypeId::VARCHAR:
if (type.IsJSONType() && options.arrow_lossless_conversion) {
auto schema_metadata = ArrowSchemaMetadata::ArrowCanonicalType("arrow.json");
root_holder.metadata_info.emplace_back(schema_metadata.SerializeMetadata());
child.metadata = root_holder.metadata_info.back().get();
}
if (options.produce_arrow_string_view) {
child.format = "vu";
} else {
Expand All @@ -197,10 +176,7 @@ void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, co
break;
case LogicalTypeId::TIME_TZ: {
if (options.arrow_lossless_conversion) {
child.format = "w:8";
auto schema_metadata = ArrowSchemaMetadata::DuckDBInternalType("time_tz");
root_holder.metadata_info.emplace_back(schema_metadata.SerializeMetadata());
child.metadata = root_holder.metadata_info.back().get();
SetArrowExtension(root_holder, child, type, context);
} else {
child.format = "ttu";
}
Expand Down Expand Up @@ -250,16 +226,16 @@ void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, co
}
break;
case LogicalTypeId::BIT: {
if (options.arrow_offset_size == ArrowOffsetSize::LARGE) {
child.format = "Z";
} else {
child.format = "z";
}
if (options.arrow_lossless_conversion) {
auto schema_metadata = ArrowSchemaMetadata::DuckDBInternalType("bit");
root_holder.metadata_info.emplace_back(schema_metadata.SerializeMetadata());
child.metadata = root_holder.metadata_info.back().get();
SetArrowExtension(root_holder, child, type, context);
} else {
if (options.arrow_offset_size == ArrowOffsetSize::LARGE) {
child.format = "Z";
} else {
child.format = "z";
}
}

break;
}
case LogicalTypeId::LIST: {
Expand All @@ -284,7 +260,7 @@ void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, co
InitializeChild(root_holder.nested_children.back()[0], root_holder);
child.children = &root_holder.nested_children_ptr.back()[0];
child.children[0]->name = "l";
SetArrowFormat(root_holder, **child.children, ListType::GetChildType(type), options);
SetArrowFormat(root_holder, **child.children, ListType::GetChildType(type), options, context);
break;
}
case LogicalTypeId::STRUCT: {
Expand All @@ -306,7 +282,7 @@ void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, co
root_holder.owned_type_names.push_back(AddName(child_types[type_idx].first));

child.children[type_idx]->name = root_holder.owned_type_names.back().get();
SetArrowFormat(root_holder, *child.children[type_idx], child_types[type_idx].second, options);
SetArrowFormat(root_holder, *child.children[type_idx], child_types[type_idx].second, options, context);
}
break;
}
Expand All @@ -324,11 +300,11 @@ void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, co
root_holder.nested_children_ptr.back().push_back(&root_holder.nested_children.back()[0]);
InitializeChild(root_holder.nested_children.back()[0], root_holder);
child.children = &root_holder.nested_children_ptr.back()[0];
SetArrowFormat(root_holder, **child.children, child_type, options);
SetArrowFormat(root_holder, **child.children, child_type, options, context);
break;
}
case LogicalTypeId::MAP: {
SetArrowMapFormat(root_holder, child, type, options);
SetArrowMapFormat(root_holder, child, type, options, context);
break;
}
case LogicalTypeId::UNION: {
Expand All @@ -351,7 +327,7 @@ void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, co
root_holder.owned_type_names.push_back(AddName(child_types[type_idx].first));

child.children[type_idx]->name = root_holder.owned_type_names.back().get();
SetArrowFormat(root_holder, *child.children[type_idx], child_types[type_idx].second, options);
SetArrowFormat(root_holder, *child.children[type_idx], child_types[type_idx].second, options, context);

format += to_string(type_idx) + ",";
}
Expand Down Expand Up @@ -387,17 +363,22 @@ void SetArrowFormat(DuckDBArrowSchemaHolder &root_holder, ArrowSchema &child, co
child.dictionary->format = "u";
break;
}
default:
throw NotImplementedException("Unsupported Arrow type " + type.ToString());
default: {
// It is possible we can export this type as a registered extension
auto success = SetArrowExtension(root_holder, child, type, context);
if (!success) {
throw NotImplementedException("Unsupported Arrow type %s", type.ToString());
}
}
}
}

void ArrowConverter::ToArrowSchema(ArrowSchema *out_schema, const vector<LogicalType> &types,
const vector<string> &names, const ClientProperties &options) {
const vector<string> &names, ClientProperties &options) {
D_ASSERT(out_schema);
D_ASSERT(types.size() == names.size());
idx_t column_count = types.size();
// Allocate as unique_ptr first to cleanup properly on error
const idx_t column_count = types.size();
// Allocate as unique_ptr first to clean-up properly on error
auto root_holder = make_uniq<DuckDBArrowSchemaHolder>();

// Allocate the children
Expand All @@ -421,7 +402,7 @@ void ArrowConverter::ToArrowSchema(ArrowSchema *out_schema, const vector<Logical
root_holder->owned_column_names.push_back(AddName(names[col_idx]));
auto &child = root_holder->children[col_idx];
InitializeChild(child, *root_holder, names[col_idx]);
SetArrowFormat(*root_holder, child, types[col_idx], options);
SetArrowFormat(*root_holder, child, types[col_idx], options, *options.client_context);
}

// Release ownership to caller
Expand Down
5 changes: 3 additions & 2 deletions src/duckdb/src/common/arrow/arrow_merge_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ void ArrowBatchTask::ProduceRecordBatches() {
for (auto &index : record_batch_indices) {
auto &array = arrays[index];
D_ASSERT(array);
idx_t count;
count = ArrowUtil::FetchChunk(scan_state, arrow_options, batch_size, &array->arrow_array);
const idx_t count = ArrowUtil::FetchChunk(
scan_state, arrow_options, batch_size, &array->arrow_array,
ArrowTypeExtensionData::GetExtensionTypes(event->GetClientContext(), scan_state.Types()));
(void)count;
D_ASSERT(count != 0);
}
Expand Down
Loading