Skip to content
Closed
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
1 change: 1 addition & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ set(ARROW_SRCS

src/arrow/util/bit-util.cc
src/arrow/util/decimal.cc
src/arrow/util/key_value_metadata.cc
)

if (ARROW_IPC)
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Status Array::Validate() const {
static inline void ConformSliceParams(
int64_t array_offset, int64_t array_length, int64_t* offset, int64_t* length) {
DCHECK_LE(*offset, array_length);
DCHECK_GE(offset, 0);
DCHECK_NE(offset, nullptr);
*length = std::min(array_length - *offset, *length);
*offset = array_offset + *offset;
}
Expand Down
13 changes: 6 additions & 7 deletions cpp/src/arrow/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,6 @@ ARROW_EXPORT Status DecimalBuilder::Append(const decimal::Decimal128& value) {
return Status::OK();
}

template ARROW_EXPORT Status DecimalBuilder::Append(const decimal::Decimal128& val);

Status DecimalBuilder::Init(int64_t capacity) {
RETURN_NOT_OK(FixedSizeBinaryBuilder::Init(capacity));
if (byte_width_ == 16) {
Expand Down Expand Up @@ -408,16 +406,17 @@ Status DecimalBuilder::Finish(std::shared_ptr<Array>* out) {

ListBuilder::ListBuilder(MemoryPool* pool, std::shared_ptr<ArrayBuilder> value_builder,
const std::shared_ptr<DataType>& type)
: ArrayBuilder(
pool, type ? type : std::static_pointer_cast<DataType>(
std::make_shared<ListType>(value_builder->type()))),
: ArrayBuilder(pool,
type ? type : std::static_pointer_cast<DataType>(
std::make_shared<ListType>(value_builder->type()))),
offset_builder_(pool),
value_builder_(value_builder) {}

ListBuilder::ListBuilder(MemoryPool* pool, std::shared_ptr<Array> values,
const std::shared_ptr<DataType>& type)
: ArrayBuilder(pool, type ? type : std::static_pointer_cast<DataType>(
std::make_shared<ListType>(values->type()))),
: ArrayBuilder(pool,
type ? type : std::static_pointer_cast<DataType>(
std::make_shared<ListType>(values->type()))),
offset_builder_(pool),
values_(values) {}

Expand Down
30 changes: 28 additions & 2 deletions cpp/src/arrow/ipc/metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace ipc {
using FBB = flatbuffers::FlatBufferBuilder;
using DictionaryOffset = flatbuffers::Offset<flatbuf::DictionaryEncoding>;
using FieldOffset = flatbuffers::Offset<flatbuf::Field>;
using KeyValueOffset = flatbuffers::Offset<flatbuf::KeyValue>;
using RecordBatchOffset = flatbuffers::Offset<flatbuf::RecordBatch>;
using VectorLayoutOffset = flatbuffers::Offset<arrow::flatbuf::VectorLayout>;
using Offset = flatbuffers::Offset<void>;
Expand Down Expand Up @@ -583,6 +584,7 @@ flatbuf::Endianness endianness() {

static Status SchemaToFlatbuffer(FBB& fbb, const Schema& schema,
DictionaryMemo* dictionary_memo, flatbuffers::Offset<flatbuf::Schema>* out) {
/// Fields
std::vector<FieldOffset> field_offsets;
for (int i = 0; i < schema.num_fields(); ++i) {
std::shared_ptr<Field> field = schema.field(i);
Expand All @@ -591,7 +593,20 @@ static Status SchemaToFlatbuffer(FBB& fbb, const Schema& schema,
field_offsets.push_back(offset);
}

*out = flatbuf::CreateSchema(fbb, endianness(), fbb.CreateVector(field_offsets));
/// Custom metadata
const auto& custom_metadata_ = schema.custom_metadata();
std::vector<KeyValueOffset> key_value_offsets;
size_t metadata_size = custom_metadata_.size();
key_value_offsets.reserve(metadata_size);
for (size_t i = 0; i < metadata_size; ++i) {
const auto& key = custom_metadata_.key(i);
const auto& value = custom_metadata_.value(i);
key_value_offsets.push_back(
flatbuf::CreateKeyValue(fbb, fbb.CreateString(key), fbb.CreateString(value)));
}

*out = flatbuf::CreateSchema(fbb, endianness(), fbb.CreateVector(field_offsets),
fbb.CreateVector(key_value_offsets));
return Status::OK();
}

Expand Down Expand Up @@ -939,7 +954,18 @@ Status GetSchema(const void* opaque_schema, const DictionaryMemo& dictionary_mem
const flatbuf::Field* field = schema->fields()->Get(i);
RETURN_NOT_OK(FieldFromFlatbuffer(field, dictionary_memo, &fields[i]));
}
*out = std::make_shared<Schema>(fields);

KeyValueMetadata custom_metadata;
auto fb_metadata = schema->custom_metadata();
if (fb_metadata != nullptr) {
custom_metadata.reserve(fb_metadata->size());

for (const auto& pair : *fb_metadata) {
custom_metadata.Append(pair->key()->str(), pair->value()->str());
}
}

*out = std::make_shared<Schema>(fields, custom_metadata);
return Status::OK();
}

Expand Down
34 changes: 34 additions & 0 deletions cpp/src/arrow/type-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,40 @@ TEST_F(TestSchema, GetFieldByName) {
ASSERT_TRUE(result == nullptr);
}

TEST_F(TestSchema, TestCustomMetadataConstruction) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
vector<shared_ptr<Field>> fields = {f0, f1, f2};
KeyValueMetadata metadata({"foo", "bar"}, {"bizz", "buzz"});
auto schema = std::make_shared<Schema>(fields, metadata);
ASSERT_TRUE(metadata.Equals(schema->custom_metadata()));
}

TEST_F(TestSchema, TestAddCustomMetadata) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
vector<shared_ptr<Field>> fields = {f0, f1, f2};
KeyValueMetadata metadata({"foo", "bar"}, {"bizz", "buzz"});
auto schema = std::make_shared<Schema>(fields);
std::shared_ptr<Schema> new_schema;
schema->AddCustomMetadata(metadata, &new_schema);
ASSERT_TRUE(metadata.Equals(new_schema->custom_metadata()));
}

TEST_F(TestSchema, TestRemoveCustomMetadata) {
auto f0 = field("f0", int32());
auto f1 = field("f1", uint8(), false);
auto f2 = field("f2", utf8());
vector<shared_ptr<Field>> fields = {f0, f1, f2};
KeyValueMetadata metadata({"foo", "bar"}, {"bizz", "buzz"});
auto schema = std::make_shared<Schema>(fields);
std::shared_ptr<Schema> new_schema;
schema->RemoveCustomMetadata(&new_schema);
ASSERT_EQ(0, new_schema->custom_metadata().size());
}

#define PRIMITIVE_TEST(KLASS, ENUM, NAME) \
TEST(TypesTest, TestPrimitive_##ENUM) { \
KLASS tp; \
Expand Down
20 changes: 17 additions & 3 deletions cpp/src/arrow/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "arrow/array.h"
#include "arrow/compare.h"
#include "arrow/status.h"
#include "arrow/util/key_value_metadata.h"
#include "arrow/util/logging.h"
#include "arrow/util/stl.h"
#include "arrow/visitor.h"
Expand Down Expand Up @@ -231,7 +232,9 @@ std::string NullType::ToString() const {
// ----------------------------------------------------------------------
// Schema implementation

Schema::Schema(const std::vector<std::shared_ptr<Field>>& fields) : fields_(fields) {}
Schema::Schema(const std::vector<std::shared_ptr<Field>>& fields,
const KeyValueMetadata& custom_metadata)
: fields_(fields), custom_metadata_(custom_metadata) {}

bool Schema::Equals(const Schema& other) const {
if (this == &other) { return true; }
Expand Down Expand Up @@ -263,15 +266,26 @@ Status Schema::AddField(
DCHECK_GE(i, 0);
DCHECK_LE(i, this->num_fields());

*out = std::make_shared<Schema>(AddVectorElement(fields_, i, field));
*out = std::make_shared<Schema>(AddVectorElement(fields_, i, field), custom_metadata_);
return Status::OK();
}

Status Schema::AddCustomMetadata(
const KeyValueMetadata& custom_metadata, std::shared_ptr<Schema>* out) const {
*out = std::make_shared<Schema>(fields_, custom_metadata);
return Status::OK();
}

Status Schema::RemoveCustomMetadata(std::shared_ptr<Schema>* out) {
*out = std::make_shared<Schema>(fields_, KeyValueMetadata());
return Status::OK();
}

Status Schema::RemoveField(int i, std::shared_ptr<Schema>* out) const {
DCHECK_GE(i, 0);
DCHECK_LT(i, this->num_fields());

*out = std::make_shared<Schema>(DeleteVectorElement(fields_, i));
*out = std::make_shared<Schema>(DeleteVectorElement(fields_, i), custom_metadata_);
return Status::OK();
}

Expand Down
10 changes: 9 additions & 1 deletion cpp/src/arrow/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "arrow/status.h"
#include "arrow/type_fwd.h"
#include "arrow/util/key_value_metadata.h"
#include "arrow/util/macros.h"
#include "arrow/util/visibility.h"
#include "arrow/visitor.h"
Expand Down Expand Up @@ -677,7 +678,8 @@ class ARROW_EXPORT DictionaryType : public FixedWidthType {

class ARROW_EXPORT Schema {
public:
explicit Schema(const std::vector<std::shared_ptr<Field>>& fields);
explicit Schema(const std::vector<std::shared_ptr<Field>>& fields,
const KeyValueMetadata& custom_metadata = KeyValueMetadata());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this suggests we might want to make the API

Schema(..., std::unique_ptr<KeyValueMetadata> custom_metadata) and
Schema(..., std::unique_ptr<const KeyValueMetadata> custom_metadata) and

with the storage in this class as std::unique_ptr<const KeyValueMetadata>. I think you can move std::unique_ptr<T> into std::unique_ptr<const T>

We could further make KeyValueMetadata non-copyable. Still custom_metadata() returns const KeyValueMetadata&. We could do this in a follow up patch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this seems like a lot of additional complexity relative to what the purpose of this class is. What do we get from using unique_ptr?


// Returns true if all of the schema fields are equal
bool Equals(const Schema& other) const;
Expand All @@ -689,6 +691,7 @@ class ARROW_EXPORT Schema {
std::shared_ptr<Field> GetFieldByName(const std::string& name);

const std::vector<std::shared_ptr<Field>>& fields() const { return fields_; }
const KeyValueMetadata& custom_metadata() const { return custom_metadata_; }

// Render a string representation of the schema suitable for debugging
std::string ToString() const;
Expand All @@ -697,11 +700,16 @@ class ARROW_EXPORT Schema {
int i, const std::shared_ptr<Field>& field, std::shared_ptr<Schema>* out) const;
Status RemoveField(int i, std::shared_ptr<Schema>* out) const;

Status AddCustomMetadata(
const KeyValueMetadata& metadata, std::shared_ptr<Schema>* out) const;
Status RemoveCustomMetadata(std::shared_ptr<Schema>* out);

int num_fields() const { return static_cast<int>(fields_.size()); }

private:
std::vector<std::shared_ptr<Field>> fields_;
std::unordered_map<std::string, int> name_to_index_;
KeyValueMetadata custom_metadata_;
};

// ----------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/arrow/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ install(FILES
macros.h
random.h
visibility.h
key_value_metadata.h
DESTINATION include/arrow/util)

#######################################
Expand All @@ -52,3 +53,4 @@ endif()
ADD_ARROW_TEST(bit-util-test)
ADD_ARROW_TEST(stl-util-test)
ADD_ARROW_TEST(decimal-test)
ADD_ARROW_TEST(key-value-metadata-test)
87 changes: 87 additions & 0 deletions cpp/src/arrow/util/key-value-metadata-test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include "gtest/gtest.h"

#include "arrow/util/key_value_metadata.h"

#include "arrow/test-util.h"

namespace arrow {

TEST(KeyValueMetadataTest, SimpleConstruction) {
KeyValueMetadata metadata;
ASSERT_EQ(0, metadata.size());
}

TEST(KeyValueMetadataTest, StringVectorConstruction) {
std::vector<std::string> keys = {"foo", "bar"};
std::vector<std::string> values = {"bizz", "buzz"};

KeyValueMetadata metadata(keys, values);
ASSERT_EQ("foo", metadata.key(0));
ASSERT_EQ("bar", metadata.key(1));
ASSERT_EQ("bizz", metadata.value(0));
ASSERT_EQ("buzz", metadata.value(1));
ASSERT_EQ(2, metadata.size());
}

TEST(KeyValueMetadataTest, StringMapConstruction) {
std::unordered_map<std::string, std::string> pairs = {{"foo", "bizz"}, {"bar", "buzz"}};
std::unordered_map<std::string, std::string> result_map;
result_map.reserve(pairs.size());

KeyValueMetadata metadata(pairs);
metadata.ToUnorderedMap(&result_map);
ASSERT_EQ(pairs, result_map);
ASSERT_EQ(2, metadata.size());
}

TEST(KeyValueMetadataTest, StringAppend) {
std::vector<std::string> keys = {"foo", "bar"};
std::vector<std::string> values = {"bizz", "buzz"};

KeyValueMetadata metadata(keys, values);
ASSERT_EQ("foo", metadata.key(0));
ASSERT_EQ("bar", metadata.key(1));
ASSERT_EQ("bizz", metadata.value(0));
ASSERT_EQ("buzz", metadata.value(1));
ASSERT_EQ(2, metadata.size());

metadata.Append("purple", "orange");
metadata.Append("blue", "red");

ASSERT_EQ("purple", metadata.key(2));
ASSERT_EQ("blue", metadata.key(3));

ASSERT_EQ("orange", metadata.value(2));
ASSERT_EQ("red", metadata.value(3));
}

TEST(KeyValueMetadataTest, Equals) {
std::vector<std::string> keys = {"foo", "bar"};
std::vector<std::string> values = {"bizz", "buzz"};

KeyValueMetadata metadata(keys, values);
KeyValueMetadata metadata2(keys, values);
KeyValueMetadata metadata3(keys, {"buzz", "bizz"});

ASSERT_TRUE(metadata.Equals(metadata2));
ASSERT_FALSE(metadata.Equals(metadata3));
}

} // namespace arrow
Loading