From fcbf64ad916d5c9f9e49b5b9bd2aa526b7125f04 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Mon, 27 Mar 2017 15:08:39 -0400 Subject: [PATCH 1/5] Refactoring, implement record batch round trip fixture for JSON, failing unit tests Change-Id: Ie5843fc7f18297a55b5930af6e72208edd341e95 --- cpp/src/arrow/compare.cc | 14 +- cpp/src/arrow/ipc/ipc-json-test.cc | 40 ++ cpp/src/arrow/ipc/ipc-read-write-test.cc | 44 +- cpp/src/arrow/ipc/json-internal.cc | 497 ++++++++++++----------- cpp/src/arrow/ipc/test-common.h | 15 + cpp/src/arrow/tensor.cc | 3 +- cpp/src/arrow/type.cc | 17 +- cpp/src/arrow/type.h | 36 +- 8 files changed, 372 insertions(+), 294 deletions(-) diff --git a/cpp/src/arrow/compare.cc b/cpp/src/arrow/compare.cc index 8274e0f80dc5..3e282f888662 100644 --- a/cpp/src/arrow/compare.cc +++ b/cpp/src/arrow/compare.cc @@ -666,14 +666,12 @@ class TypeEqualsVisitor { return Status::OK(); } - Status Visit(const Time32Type& left) { - const auto& right = static_cast(right_); - result_ = left.unit == right.unit; - return Status::OK(); - } - - Status Visit(const Time64Type& left) { - const auto& right = static_cast(right_); + template + typename std::enable_if::value || + std::is_base_of::value, + Status>::type + Visit(const T& left) { + const auto& right = static_cast(right_); result_ = left.unit == right.unit; return Status::OK(); } diff --git a/cpp/src/arrow/ipc/ipc-json-test.cc b/cpp/src/arrow/ipc/ipc-json-test.cc index e943ef1558a7..f74877c44a1c 100644 --- a/cpp/src/arrow/ipc/ipc-json-test.cc +++ b/cpp/src/arrow/ipc/ipc-json-test.cc @@ -351,5 +351,45 @@ TEST(TestJsonFileReadWrite, MinimalFormatExample) { ASSERT_TRUE(batch->column(1)->Equals(bar)); } +#define BATCH_CASES() \ + ::testing::Values(&MakeIntRecordBatch, &MakeListRecordBatch, &MakeNonNullRecordBatch, \ + &MakeZeroLengthRecordBatch, &MakeDeeplyNestedList, &MakeStringTypesRecordBatch, \ + &MakeStruct, &MakeUnion, &MakeDates, &MakeTimestamps, &MakeTimes); + +class TestJsonRoundTrip : public ::testing::TestWithParam { + public: + void SetUp() {} + void TearDown() {} +}; + +void CheckRoundtrip(const RecordBatch& batch) { + std::unique_ptr writer; + ASSERT_OK(JsonWriter::Open(batch.schema(), &writer)); + ASSERT_OK(writer->WriteRecordBatch(batch)); + + std::string result; + ASSERT_OK(writer->Finish(&result)); + + auto buffer = std::make_shared(reinterpret_cast(result.c_str()), + static_cast(result.size())); + + std::unique_ptr reader; + ASSERT_OK(JsonReader::Open(buffer, &reader)); + + std::shared_ptr result_batch; + ASSERT_OK(reader->GetRecordBatch(0, &result_batch)); + + CompareBatch(batch, *result_batch); +} + +TEST_P(TestJsonRoundTrip, RoundTrip) { + std::shared_ptr batch; + ASSERT_OK((*GetParam())(&batch)); // NOLINT clang-tidy gtest issue + + CheckRoundtrip(*batch); +} + +INSTANTIATE_TEST_CASE_P(TestJsonRoundTrip, TestJsonRoundTrip, BATCH_CASES()); + } // namespace ipc } // namespace arrow diff --git a/cpp/src/arrow/ipc/ipc-read-write-test.cc b/cpp/src/arrow/ipc/ipc-read-write-test.cc index 086cc6817678..2a4651d6a86b 100644 --- a/cpp/src/arrow/ipc/ipc-read-write-test.cc +++ b/cpp/src/arrow/ipc/ipc-read-write-test.cc @@ -41,20 +41,6 @@ namespace arrow { namespace ipc { -void CompareBatch(const RecordBatch& left, const RecordBatch& right) { - if (!left.schema()->Equals(right.schema())) { - FAIL() << "Left schema: " << left.schema()->ToString() - << "\nRight schema: " << right.schema()->ToString(); - } - ASSERT_EQ(left.num_columns(), right.num_columns()) - << left.schema()->ToString() << " result: " << right.schema()->ToString(); - EXPECT_EQ(left.num_rows(), right.num_rows()); - for (int i = 0; i < left.num_columns(); ++i) { - EXPECT_TRUE(left.column(i)->Equals(right.column(i))) - << "Idx: " << i << " Name: " << left.column_name(i); - } -} - using BatchVector = std::vector>; class TestSchemaMetadata : public ::testing::Test { @@ -85,17 +71,17 @@ class TestSchemaMetadata : public ::testing::Test { const std::shared_ptr INT32 = std::make_shared(); TEST_F(TestSchemaMetadata, PrimitiveFields) { - auto f0 = std::make_shared("f0", std::make_shared()); - auto f1 = std::make_shared("f1", std::make_shared(), false); - auto f2 = std::make_shared("f2", std::make_shared()); - auto f3 = std::make_shared("f3", std::make_shared()); - auto f4 = std::make_shared("f4", std::make_shared()); - auto f5 = std::make_shared("f5", std::make_shared()); - auto f6 = std::make_shared("f6", std::make_shared()); - auto f7 = std::make_shared("f7", std::make_shared()); - auto f8 = std::make_shared("f8", std::make_shared()); - auto f9 = std::make_shared("f9", std::make_shared(), false); - auto f10 = std::make_shared("f10", std::make_shared()); + auto f0 = field("f0", std::make_shared()); + auto f1 = field("f1", std::make_shared(), false); + auto f2 = field("f2", std::make_shared()); + auto f3 = field("f3", std::make_shared()); + auto f4 = field("f4", std::make_shared()); + auto f5 = field("f5", std::make_shared()); + auto f6 = field("f6", std::make_shared()); + auto f7 = field("f7", std::make_shared()); + auto f8 = field("f8", std::make_shared()); + auto f9 = field("f9", std::make_shared(), false); + auto f10 = field("f10", std::make_shared()); Schema schema({f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10}); DictionaryMemo memo; @@ -105,11 +91,11 @@ TEST_F(TestSchemaMetadata, PrimitiveFields) { TEST_F(TestSchemaMetadata, NestedFields) { auto type = std::make_shared(std::make_shared()); - auto f0 = std::make_shared("f0", type); + auto f0 = field("f0", type); - std::shared_ptr type2(new StructType({std::make_shared("k1", INT32), - std::make_shared("k2", INT32), std::make_shared("k3", INT32)})); - auto f1 = std::make_shared("f1", type2); + std::shared_ptr type2(new StructType({field("k1", INT32), + field("k2", INT32), field("k3", INT32)})); + auto f1 = field("f1", type2); Schema schema({f0, f1}); DictionaryMemo memo; diff --git a/cpp/src/arrow/ipc/json-internal.cc b/cpp/src/arrow/ipc/json-internal.cc index 348468006d0b..a02943b4e320 100644 --- a/cpp/src/arrow/ipc/json-internal.cc +++ b/cpp/src/arrow/ipc/json-internal.cc @@ -40,6 +40,8 @@ #include "arrow/util/bit-util.h" #include "arrow/util/logging.h" #include "arrow/util/string.h" +#include "arrow/visitor.h" +#include "arrow/visitor_inline.h" namespace arrow { namespace ipc { @@ -93,7 +95,7 @@ static std::string GetTimeUnitName(TimeUnit unit) { return "UNKNOWN"; } -class JsonSchemaWriter : public TypeVisitor { +class JsonSchemaWriter { public: explicit JsonSchemaWriter(const Schema& schema, RjWriter* writer) : schema_(schema), writer_(writer) {} @@ -120,7 +122,7 @@ class JsonSchemaWriter : public TypeVisitor { writer_->Bool(field.nullable); // Visit the type - RETURN_NOT_OK(field.type->Accept(this)); + RETURN_NOT_OK(VisitTypeInline(*field.type, this)); writer_->EndObject(); return Status::OK(); @@ -170,8 +172,7 @@ class JsonSchemaWriter : public TypeVisitor { } template - typename std::enable_if::value || - std::is_base_of::value || + typename std::enable_if::value || std::is_base_of::value, void>::type WriteTypeMetadata(const T& type) { @@ -179,6 +180,20 @@ class JsonSchemaWriter : public TypeVisitor { writer_->String(GetTimeUnitName(type.unit)); } + template + typename std::enable_if::value, void>::type + WriteTypeMetadata(const T& type) { + writer_->Key("unit"); + switch (type.unit) { + case DateUnit::DAY: + writer_->String("DAY"); + break; + case DateUnit::MILLI: + writer_->String("MILLISECOND"); + break; + } + } + template typename std::enable_if::value, void>::type WriteTypeMetadata(const T& type) { @@ -268,80 +283,73 @@ class JsonSchemaWriter : public TypeVisitor { return Status::OK(); } - Status Visit(const NullType& type) override { return WritePrimitive("null", type); } - - Status Visit(const BooleanType& type) override { return WritePrimitive("bool", type); } - - Status Visit(const Int8Type& type) override { return WritePrimitive("int", type); } - - Status Visit(const Int16Type& type) override { return WritePrimitive("int", type); } - - Status Visit(const Int32Type& type) override { return WritePrimitive("int", type); } - - Status Visit(const Int64Type& type) override { return WritePrimitive("int", type); } + Status Visit(const NullType& type) { return WritePrimitive("null", type); } - Status Visit(const UInt8Type& type) override { return WritePrimitive("int", type); } + Status Visit(const BooleanType& type) { return WritePrimitive("bool", type); } - Status Visit(const UInt16Type& type) override { return WritePrimitive("int", type); } - - Status Visit(const UInt32Type& type) override { return WritePrimitive("int", type); } - - Status Visit(const UInt64Type& type) override { return WritePrimitive("int", type); } - - Status Visit(const HalfFloatType& type) override { - return WritePrimitive("floatingpoint", type); + template + typename std::enable_if::value, Status>::type Visit( + const T& type) { + return WritePrimitive("int", type); } - Status Visit(const FloatType& type) override { + template + typename std::enable_if::value, Status>::type + Visit(const T& type) { return WritePrimitive("floatingpoint", type); } - Status Visit(const DoubleType& type) override { - return WritePrimitive("floatingpoint", type); + template + typename std::enable_if::value, Status>::type Visit( + const T& type) { + return WritePrimitive("date", type); } - Status Visit(const StringType& type) override { return WriteVarBytes("utf8", type); } - - Status Visit(const BinaryType& type) override { return WriteVarBytes("binary", type); } - - // TODO - Status Visit(const Date32Type& type) override { return WritePrimitive("date", type); } - - Status Visit(const Date64Type& type) override { return WritePrimitive("date", type); } + template + typename std::enable_if::value, Status>::type Visit( + const T& type) { + return WritePrimitive("time", type); + } - Status Visit(const Time32Type& type) override { return WritePrimitive("time", type); } + Status Visit(const StringType& type) { return WriteVarBytes("utf8", type); } - Status Visit(const Time64Type& type) override { return WritePrimitive("time", type); } + Status Visit(const BinaryType& type) { return WriteVarBytes("binary", type); } - Status Visit(const TimestampType& type) override { - return WritePrimitive("timestamp", type); - } + Status Visit(const TimestampType& type) { return WritePrimitive("timestamp", type); } - Status Visit(const IntervalType& type) override { - return WritePrimitive("interval", type); - } + Status Visit(const IntervalType& type) { return WritePrimitive("interval", type); } - Status Visit(const ListType& type) override { + Status Visit(const ListType& type) { WriteName("list", type); RETURN_NOT_OK(WriteChildren(type.children())); WriteBufferLayout(type.GetBufferLayout()); return Status::OK(); } - Status Visit(const StructType& type) override { + Status Visit(const StructType& type) { WriteName("struct", type); WriteChildren(type.children()); WriteBufferLayout(type.GetBufferLayout()); return Status::OK(); } - Status Visit(const UnionType& type) override { + Status Visit(const UnionType& type) { WriteName("union", type); WriteChildren(type.children()); WriteBufferLayout(type.GetBufferLayout()); return Status::OK(); } + Status Visit(const FixedWidthBinaryType& type) { + return Status::NotImplemented("fixed width binary"); + } + + Status Visit(const DecimalType& type) { return Status::NotImplemented("decimal"); } + + Status Visit(const DictionaryType& type) { + return Status::NotImplemented("dictionary"); + } + private: const Schema& schema_; RjWriter* writer_; @@ -549,235 +557,234 @@ class JsonArrayWriter : public ArrayVisitor { RjWriter* writer_; }; -class JsonSchemaReader { - public: - explicit JsonSchemaReader(const rj::Value& json_schema) : json_schema_(json_schema) {} - - Status GetSchema(std::shared_ptr* schema) { - const auto& obj_schema = json_schema_.GetObject(); - - const auto& json_fields = obj_schema.FindMember("fields"); - RETURN_NOT_ARRAY("fields", json_fields, obj_schema); +static Status GetInteger( + const rj::Value::ConstObject& json_type, std::shared_ptr* type) { + const auto& json_bit_width = json_type.FindMember("bitWidth"); + RETURN_NOT_INT("bitWidth", json_bit_width, json_type); - std::vector> fields; - RETURN_NOT_OK(GetFieldsFromArray(json_fields->value, &fields)); + const auto& json_is_signed = json_type.FindMember("isSigned"); + RETURN_NOT_BOOL("isSigned", json_is_signed, json_type); - *schema = std::make_shared(fields); - return Status::OK(); - } + bool is_signed = json_is_signed->value.GetBool(); + int bit_width = json_bit_width->value.GetInt(); - Status GetFieldsFromArray( - const rj::Value& obj, std::vector>* fields) { - const auto& values = obj.GetArray(); - - fields->resize(values.Size()); - for (rj::SizeType i = 0; i < fields->size(); ++i) { - RETURN_NOT_OK(GetField(values[i], &(*fields)[i])); - } - return Status::OK(); + switch (bit_width) { + case 8: + *type = is_signed ? int8() : uint8(); + break; + case 16: + *type = is_signed ? int16() : uint16(); + break; + case 32: + *type = is_signed ? int32() : uint32(); + break; + case 64: + *type = is_signed ? int64() : uint64(); + break; + default: + std::stringstream ss; + ss << "Invalid bit width: " << bit_width; + return Status::Invalid(ss.str()); } + return Status::OK(); +} - Status GetField(const rj::Value& obj, std::shared_ptr* field) { - if (!obj.IsObject()) { return Status::Invalid("Field was not a JSON object"); } - const auto& json_field = obj.GetObject(); - - const auto& json_name = json_field.FindMember("name"); - RETURN_NOT_STRING("name", json_name, json_field); - - const auto& json_nullable = json_field.FindMember("nullable"); - RETURN_NOT_BOOL("nullable", json_nullable, json_field); +static Status GetFloatingPoint( + const RjObject& json_type, std::shared_ptr* type) { + const auto& json_precision = json_type.FindMember("precision"); + RETURN_NOT_STRING("precision", json_precision, json_type); - const auto& json_type = json_field.FindMember("type"); - RETURN_NOT_OBJECT("type", json_type, json_field); + std::string precision = json_precision->value.GetString(); - const auto& json_children = json_field.FindMember("children"); - RETURN_NOT_ARRAY("children", json_children, json_field); + if (precision == "DOUBLE") { + *type = float64(); + } else if (precision == "SINGLE") { + *type = float32(); + } else if (precision == "HALF") { + *type = float16(); + } else { + std::stringstream ss; + ss << "Invalid precision: " << precision; + return Status::Invalid(ss.str()); + } + return Status::OK(); +} - std::vector> children; - RETURN_NOT_OK(GetFieldsFromArray(json_children->value, &children)); +static Status GetDate(const RjObject& json_type, std::shared_ptr* type) { + const auto& json_unit = json_type.FindMember("unit"); + RETURN_NOT_STRING("unit", json_unit, json_type); - std::shared_ptr type; - RETURN_NOT_OK(GetType(json_type->value.GetObject(), children, &type)); + std::string unit_str = json_unit->value.GetString(); - *field = std::make_shared( - json_name->value.GetString(), type, json_nullable->value.GetBool()); - return Status::OK(); + if (unit_str == "DAY") { + *type = date32(); + } else if (unit_str == "MILLISECOND") { + *type = date64(); + } else { + std::stringstream ss; + ss << "Invalid date unit: " << unit_str; + return Status::Invalid(ss.str()); } + return Status::OK(); +} - Status GetInteger( - const rj::Value::ConstObject& json_type, std::shared_ptr* type) { - const auto& json_bit_width = json_type.FindMember("bitWidth"); - RETURN_NOT_INT("bitWidth", json_bit_width, json_type); +static Status GetTime(const RjObject& json_type, std::shared_ptr* type) { + const auto& json_unit = json_type.FindMember("unit"); + RETURN_NOT_STRING("unit", json_unit, json_type); + + std::string unit_str = json_unit->value.GetString(); + + if (unit_str == "SECOND") { + *type = time32(TimeUnit::SECOND); + } else if (unit_str == "MILLISECOND") { + *type = time32(TimeUnit::MILLI); + } else if (unit_str == "MICROSECOND") { + *type = time64(TimeUnit::MICRO); + } else if (unit_str == "NANOSECOND") { + *type = time64(TimeUnit::NANO); + } else { + std::stringstream ss; + ss << "Invalid time unit: " << unit_str; + return Status::Invalid(ss.str()); + } + return Status::OK(); +} - const auto& json_is_signed = json_type.FindMember("isSigned"); - RETURN_NOT_BOOL("isSigned", json_is_signed, json_type); +static Status GetTimestamp(const RjObject& json_type, std::shared_ptr* type) { + const auto& json_unit = json_type.FindMember("unit"); + RETURN_NOT_STRING("unit", json_unit, json_type); + + std::string unit_str = json_unit->value.GetString(); + + TimeUnit unit; + if (unit_str == "SECOND") { + unit = TimeUnit::SECOND; + } else if (unit_str == "MILLISECOND") { + unit = TimeUnit::MILLI; + } else if (unit_str == "MICROSECOND") { + unit = TimeUnit::MICRO; + } else if (unit_str == "NANOSECOND") { + unit = TimeUnit::NANO; + } else { + std::stringstream ss; + ss << "Invalid time unit: " << unit_str; + return Status::Invalid(ss.str()); + } - bool is_signed = json_is_signed->value.GetBool(); - int bit_width = json_bit_width->value.GetInt(); + *type = timestamp(unit); - switch (bit_width) { - case 8: - *type = is_signed ? int8() : uint8(); - break; - case 16: - *type = is_signed ? int16() : uint16(); - break; - case 32: - *type = is_signed ? int32() : uint32(); - break; - case 64: - *type = is_signed ? int64() : uint64(); - break; - default: - std::stringstream ss; - ss << "Invalid bit width: " << bit_width; - return Status::Invalid(ss.str()); - } - return Status::OK(); - } + return Status::OK(); +} - Status GetFloatingPoint(const RjObject& json_type, std::shared_ptr* type) { - const auto& json_precision = json_type.FindMember("precision"); - RETURN_NOT_STRING("precision", json_precision, json_type); +static Status GetUnion(const RjObject& json_type, + const std::vector>& children, + std::shared_ptr* type) { + const auto& json_mode = json_type.FindMember("mode"); + RETURN_NOT_STRING("mode", json_mode, json_type); - std::string precision = json_precision->value.GetString(); + std::string mode_str = json_mode->value.GetString(); + UnionMode mode; - if (precision == "DOUBLE") { - *type = float64(); - } else if (precision == "SINGLE") { - *type = float32(); - } else if (precision == "HALF") { - *type = float16(); - } else { - std::stringstream ss; - ss << "Invalid precision: " << precision; - return Status::Invalid(ss.str()); - } - return Status::OK(); + if (mode_str == "SPARSE") { + mode = UnionMode::SPARSE; + } else if (mode_str == "DENSE") { + mode = UnionMode::DENSE; + } else { + std::stringstream ss; + ss << "Invalid union mode: " << mode_str; + return Status::Invalid(ss.str()); } - Status GetTime(const RjObject& json_type, std::shared_ptr* type) { - const auto& json_unit = json_type.FindMember("unit"); - RETURN_NOT_STRING("unit", json_unit, json_type); + const auto& json_type_codes = json_type.FindMember("typeIds"); + RETURN_NOT_ARRAY("typeIds", json_type_codes, json_type); - std::string unit_str = json_unit->value.GetString(); - - if (unit_str == "SECOND") { - *type = time32(TimeUnit::SECOND); - } else if (unit_str == "MILLISECOND") { - *type = time32(TimeUnit::MILLI); - } else if (unit_str == "MICROSECOND") { - *type = time64(TimeUnit::MICRO); - } else if (unit_str == "NANOSECOND") { - *type = time64(TimeUnit::NANO); - } else { - std::stringstream ss; - ss << "Invalid time unit: " << unit_str; - return Status::Invalid(ss.str()); - } - return Status::OK(); + std::vector type_codes; + const auto& id_array = json_type_codes->value.GetArray(); + for (const rj::Value& val : id_array) { + DCHECK(val.IsUint()); + type_codes.push_back(static_cast(val.GetUint())); } - Status GetTimestamp(const RjObject& json_type, std::shared_ptr* type) { - const auto& json_unit = json_type.FindMember("unit"); - RETURN_NOT_STRING("unit", json_unit, json_type); + *type = union_(children, type_codes, mode); - std::string unit_str = json_unit->value.GetString(); + return Status::OK(); +} - TimeUnit unit; - if (unit_str == "SECOND") { - unit = TimeUnit::SECOND; - } else if (unit_str == "MILLISECOND") { - unit = TimeUnit::MILLI; - } else if (unit_str == "MICROSECOND") { - unit = TimeUnit::MICRO; - } else if (unit_str == "NANOSECOND") { - unit = TimeUnit::NANO; - } else { - std::stringstream ss; - ss << "Invalid time unit: " << unit_str; - return Status::Invalid(ss.str()); - } +static Status GetType(const RjObject& json_type, + const std::vector>& children, + std::shared_ptr* type) { + const auto& json_type_name = json_type.FindMember("name"); + RETURN_NOT_STRING("name", json_type_name, json_type); + + std::string type_name = json_type_name->value.GetString(); + + if (type_name == "int") { + return GetInteger(json_type, type); + } else if (type_name == "floatingpoint") { + return GetFloatingPoint(json_type, type); + } else if (type_name == "bool") { + *type = boolean(); + } else if (type_name == "utf8") { + *type = utf8(); + } else if (type_name == "binary") { + *type = binary(); + } else if (type_name == "null") { + *type = null(); + } else if (type_name == "date") { + return GetDate(json_type, type); + } else if (type_name == "time") { + return GetTime(json_type, type); + } else if (type_name == "timestamp") { + return GetTimestamp(json_type, type); + } else if (type_name == "list") { + *type = list(children[0]); + } else if (type_name == "struct") { + *type = struct_(children); + } else { + return GetUnion(json_type, children, type); + } + return Status::OK(); +} - *type = timestamp(unit); +static Status GetField(const rj::Value& obj, std::shared_ptr* field); - return Status::OK(); - } +static Status GetFieldsFromArray( + const rj::Value& obj, std::vector>* fields) { + const auto& values = obj.GetArray(); - Status GetUnion(const RjObject& json_type, - const std::vector>& children, - std::shared_ptr* type) { - const auto& json_mode = json_type.FindMember("mode"); - RETURN_NOT_STRING("mode", json_mode, json_type); + fields->resize(values.Size()); + for (rj::SizeType i = 0; i < fields->size(); ++i) { + RETURN_NOT_OK(GetField(values[i], &(*fields)[i])); + } + return Status::OK(); +} - std::string mode_str = json_mode->value.GetString(); - UnionMode mode; +static Status GetField(const rj::Value& obj, std::shared_ptr* field) { + if (!obj.IsObject()) { return Status::Invalid("Field was not a JSON object"); } + const auto& json_field = obj.GetObject(); - if (mode_str == "SPARSE") { - mode = UnionMode::SPARSE; - } else if (mode_str == "DENSE") { - mode = UnionMode::DENSE; - } else { - std::stringstream ss; - ss << "Invalid union mode: " << mode_str; - return Status::Invalid(ss.str()); - } + const auto& json_name = json_field.FindMember("name"); + RETURN_NOT_STRING("name", json_name, json_field); - const auto& json_type_codes = json_type.FindMember("typeIds"); - RETURN_NOT_ARRAY("typeIds", json_type_codes, json_type); + const auto& json_nullable = json_field.FindMember("nullable"); + RETURN_NOT_BOOL("nullable", json_nullable, json_field); - std::vector type_codes; - const auto& id_array = json_type_codes->value.GetArray(); - for (const rj::Value& val : id_array) { - DCHECK(val.IsUint()); - type_codes.push_back(static_cast(val.GetUint())); - } + const auto& json_type = json_field.FindMember("type"); + RETURN_NOT_OBJECT("type", json_type, json_field); - *type = union_(children, type_codes, mode); + const auto& json_children = json_field.FindMember("children"); + RETURN_NOT_ARRAY("children", json_children, json_field); - return Status::OK(); - } + std::vector> children; + RETURN_NOT_OK(GetFieldsFromArray(json_children->value, &children)); - Status GetType(const RjObject& json_type, - const std::vector>& children, - std::shared_ptr* type) { - const auto& json_type_name = json_type.FindMember("name"); - RETURN_NOT_STRING("name", json_type_name, json_type); - - std::string type_name = json_type_name->value.GetString(); - - if (type_name == "int") { - return GetInteger(json_type, type); - } else if (type_name == "floatingpoint") { - return GetFloatingPoint(json_type, type); - } else if (type_name == "bool") { - *type = boolean(); - } else if (type_name == "utf8") { - *type = utf8(); - } else if (type_name == "binary") { - *type = binary(); - } else if (type_name == "null") { - *type = null(); - } else if (type_name == "date") { - // TODO - *type = date64(); - } else if (type_name == "time") { - return GetTime(json_type, type); - } else if (type_name == "timestamp") { - return GetTimestamp(json_type, type); - } else if (type_name == "list") { - *type = list(children[0]); - } else if (type_name == "struct") { - *type = struct_(children); - } else { - return GetUnion(json_type, children, type); - } - return Status::OK(); - } + std::shared_ptr type; + RETURN_NOT_OK(GetType(json_type->value.GetObject(), children, &type)); - private: - const rj::Value& json_schema_; -}; + *field = std::make_shared( + json_name->value.GetString(), type, json_nullable->value.GetBool()); + return Status::OK(); +} template inline typename std::enable_if::value, typename T::c_type>::type @@ -1118,8 +1125,16 @@ Status WriteJsonSchema(const Schema& schema, RjWriter* json_writer) { } Status ReadJsonSchema(const rj::Value& json_schema, std::shared_ptr* schema) { - JsonSchemaReader converter(json_schema); - return converter.GetSchema(schema); + const auto& obj_schema = json_schema.GetObject(); + + const auto& json_fields = obj_schema.FindMember("fields"); + RETURN_NOT_ARRAY("fields", json_fields, obj_schema); + + std::vector> fields; + RETURN_NOT_OK(GetFieldsFromArray(json_fields->value, &fields)); + + *schema = std::make_shared(fields); + return Status::OK(); } Status WriteJsonArray( diff --git a/cpp/src/arrow/ipc/test-common.h b/cpp/src/arrow/ipc/test-common.h index 7ee57d2152c1..956877cb34a5 100644 --- a/cpp/src/arrow/ipc/test-common.h +++ b/cpp/src/arrow/ipc/test-common.h @@ -47,6 +47,21 @@ static inline void AssertSchemaEqual(const Schema& lhs, const Schema& rhs) { } } + +static inline void CompareBatch(const RecordBatch& left, const RecordBatch& right) { + if (!left.schema()->Equals(right.schema())) { + FAIL() << "Left schema: " << left.schema()->ToString() + << "\nRight schema: " << right.schema()->ToString(); + } + ASSERT_EQ(left.num_columns(), right.num_columns()) + << left.schema()->ToString() << " result: " << right.schema()->ToString(); + EXPECT_EQ(left.num_rows(), right.num_rows()); + for (int i = 0; i < left.num_columns(); ++i) { + EXPECT_TRUE(left.column(i)->Equals(right.column(i))) + << "Idx: " << i << " Name: " << left.column_name(i); + } +} + const auto kListInt32 = list(int32()); const auto kListListInt32 = list(kListInt32); diff --git a/cpp/src/arrow/tensor.cc b/cpp/src/arrow/tensor.cc index c0d128f56390..6489cd01d4c8 100644 --- a/cpp/src/arrow/tensor.cc +++ b/cpp/src/arrow/tensor.cc @@ -71,8 +71,7 @@ const std::string& Tensor::dim_name(int i) const { } int64_t Tensor::size() const { - return std::accumulate( - shape_.begin(), shape_.end(), 1, std::multiplies()); + return std::accumulate(shape_.begin(), shape_.end(), 1, std::multiplies()); } template diff --git a/cpp/src/arrow/type.cc b/cpp/src/arrow/type.cc index 388502214e73..23bdea7399e4 100644 --- a/cpp/src/arrow/type.cc +++ b/cpp/src/arrow/type.cc @@ -111,6 +111,16 @@ std::string StructType::ToString() const { return s.str(); } +// ---------------------------------------------------------------------- +// Date types + +DateType::DateType(Type::type type_id, DateUnit unit) + : FixedWidthType(type_id), unit(unit) {} + +Date32Type::Date32Type() : DateType(Type::DATE32, DateUnit::DAY) {} + +Date64Type::Date64Type() : DateType(Type::DATE64, DateUnit::MILLI) {} + std::string Date64Type::ToString() const { return std::string("date64[ms]"); } @@ -122,7 +132,10 @@ std::string Date32Type::ToString() const { // ---------------------------------------------------------------------- // Time types -Time32Type::Time32Type(TimeUnit unit) : FixedWidthType(Type::TIME32), unit(unit) { +TimeType::TimeType(Type::type type_id, TimeUnit unit) + : FixedWidthType(type_id), unit(unit) {} + +Time32Type::Time32Type(TimeUnit unit) : TimeType(Type::TIME32, unit) { DCHECK(unit == TimeUnit::SECOND || unit == TimeUnit::MILLI) << "Must be seconds or milliseconds"; } @@ -133,7 +146,7 @@ std::string Time32Type::ToString() const { return ss.str(); } -Time64Type::Time64Type(TimeUnit unit) : FixedWidthType(Type::TIME64), unit(unit) { +Time64Type::Time64Type(TimeUnit unit) : TimeType(Type::TIME64, unit) { DCHECK(unit == TimeUnit::MICRO || unit == TimeUnit::NANO) << "Must be microseconds or nanoseconds"; } diff --git a/cpp/src/arrow/type.h b/cpp/src/arrow/type.h index 7ae5ae3c4b72..649ec2bc3fa1 100644 --- a/cpp/src/arrow/type.h +++ b/cpp/src/arrow/type.h @@ -436,13 +436,23 @@ struct ARROW_EXPORT UnionType : public NestedType { // ---------------------------------------------------------------------- // Date and time types +enum class DateUnit : char { DAY = 0, MILLI = 1 }; + +struct DateType : public FixedWidthType { + public: + DateUnit unit; + + protected: + DateType(Type::type type_id, DateUnit unit); +}; + /// Date as int32_t days since UNIX epoch -struct ARROW_EXPORT Date32Type : public FixedWidthType, public NoExtraMeta { +struct ARROW_EXPORT Date32Type : public DateType { static constexpr Type::type type_id = Type::DATE32; using c_type = int32_t; - Date32Type() : FixedWidthType(Type::DATE32) {} + Date32Type(); int bit_width() const override { return static_cast(sizeof(c_type) * 4); } @@ -451,12 +461,12 @@ struct ARROW_EXPORT Date32Type : public FixedWidthType, public NoExtraMeta { }; /// Date as int64_t milliseconds since UNIX epoch -struct ARROW_EXPORT Date64Type : public FixedWidthType, public NoExtraMeta { +struct ARROW_EXPORT Date64Type : public DateType { static constexpr Type::type type_id = Type::DATE64; using c_type = int64_t; - Date64Type() : FixedWidthType(Type::DATE64) {} + Date64Type(); int bit_width() const override { return static_cast(sizeof(c_type) * 8); } @@ -485,13 +495,18 @@ static inline std::ostream& operator<<(std::ostream& os, TimeUnit unit) { return os; } -struct ARROW_EXPORT Time32Type : public FixedWidthType { +struct TimeType : public FixedWidthType { + public: + TimeUnit unit; + + protected: + TimeType(Type::type type_id, TimeUnit unit); +}; + +struct ARROW_EXPORT Time32Type : public TimeType { static constexpr Type::type type_id = Type::TIME32; - using Unit = TimeUnit; using c_type = int32_t; - TimeUnit unit; - int bit_width() const override { return static_cast(sizeof(c_type) * 4); } explicit Time32Type(TimeUnit unit = TimeUnit::MILLI); @@ -500,13 +515,10 @@ struct ARROW_EXPORT Time32Type : public FixedWidthType { std::string ToString() const override; }; -struct ARROW_EXPORT Time64Type : public FixedWidthType { +struct ARROW_EXPORT Time64Type : public TimeType { static constexpr Type::type type_id = Type::TIME64; - using Unit = TimeUnit; using c_type = int64_t; - TimeUnit unit; - int bit_width() const override { return static_cast(sizeof(c_type) * 8); } explicit Time64Type(TimeUnit unit = TimeUnit::MILLI); From bd5652ed9e9305f0fc8be2058220e09741c74aa3 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Mon, 27 Mar 2017 16:07:27 -0400 Subject: [PATCH 2/5] Get date/time/timestamp JSON tests passing. Cleanup, fix large metadata issues Change-Id: Ib3a45743d5a66a253e71f71c5ec898cd16487241 --- cpp/src/arrow/ipc/ipc-json-test.cc | 9 +- cpp/src/arrow/ipc/ipc-read-write-test.cc | 23 +-- cpp/src/arrow/ipc/json-internal.cc | 183 +++++++++-------------- cpp/src/arrow/ipc/metadata.cc | 6 +- cpp/src/arrow/ipc/metadata.h | 4 +- cpp/src/arrow/ipc/reader.cc | 2 + cpp/src/arrow/ipc/test-common.h | 25 +++- cpp/src/arrow/ipc/writer.cc | 6 +- cpp/src/arrow/type.cc | 14 +- cpp/src/arrow/type.h | 35 ++--- 10 files changed, 137 insertions(+), 170 deletions(-) diff --git a/cpp/src/arrow/ipc/ipc-json-test.cc b/cpp/src/arrow/ipc/ipc-json-test.cc index f74877c44a1c..ea74e951eede 100644 --- a/cpp/src/arrow/ipc/ipc-json-test.cc +++ b/cpp/src/arrow/ipc/ipc-json-test.cc @@ -75,7 +75,8 @@ void TestArrayRoundTrip(const Array& array) { std::shared_ptr out; ASSERT_OK(ReadJsonArray(default_memory_pool(), d, array.type(), &out)); - ASSERT_TRUE(array.Equals(out)) << array_as_json; + // std::cout << array_as_json << std::endl; + CompareArraysDetailed(0, *out, array); } template @@ -351,9 +352,9 @@ TEST(TestJsonFileReadWrite, MinimalFormatExample) { ASSERT_TRUE(batch->column(1)->Equals(bar)); } -#define BATCH_CASES() \ - ::testing::Values(&MakeIntRecordBatch, &MakeListRecordBatch, &MakeNonNullRecordBatch, \ - &MakeZeroLengthRecordBatch, &MakeDeeplyNestedList, &MakeStringTypesRecordBatch, \ +#define BATCH_CASES() \ + ::testing::Values(&MakeIntRecordBatch, &MakeListRecordBatch, &MakeNonNullRecordBatch, \ + &MakeZeroLengthRecordBatch, &MakeDeeplyNestedList, &MakeStringTypesRecordBatch, \ &MakeStruct, &MakeUnion, &MakeDates, &MakeTimestamps, &MakeTimes); class TestJsonRoundTrip : public ::testing::TestWithParam { diff --git a/cpp/src/arrow/ipc/ipc-read-write-test.cc b/cpp/src/arrow/ipc/ipc-read-write-test.cc index 2a4651d6a86b..cd3f190fe4a2 100644 --- a/cpp/src/arrow/ipc/ipc-read-write-test.cc +++ b/cpp/src/arrow/ipc/ipc-read-write-test.cc @@ -93,8 +93,8 @@ TEST_F(TestSchemaMetadata, NestedFields) { auto type = std::make_shared(std::make_shared()); auto f0 = field("f0", type); - std::shared_ptr type2(new StructType({field("k1", INT32), - field("k2", INT32), field("k3", INT32)})); + std::shared_ptr type2( + new StructType({field("k1", INT32), field("k2", INT32), field("k3", INT32)})); auto f1 = field("f1", type2); Schema schema({f0, f1}); @@ -158,20 +158,7 @@ class IpcTestFixture : public io::MemoryMapFixture { ASSERT_EQ(expected.num_columns(), result.num_columns()) << expected.schema()->ToString() << " result: " << result.schema()->ToString(); - for (int i = 0; i < expected.num_columns(); ++i) { - const auto& left = *expected.column(i); - const auto& right = *result.column(i); - if (!left.Equals(right)) { - std::stringstream pp_result; - std::stringstream pp_expected; - - ASSERT_OK(PrettyPrint(left, 0, &pp_expected)); - ASSERT_OK(PrettyPrint(right, 0, &pp_result)); - - FAIL() << "Index: " << i << " Expected: " << pp_expected.str() - << "\nGot: " << pp_result.str(); - } - } + CompareBatchColumnsDetailed(result, expected); } void CheckRoundtrip(const RecordBatch& batch, int64_t buffer_size) { @@ -535,7 +522,7 @@ TEST_F(TestIpcRoundTrip, LargeRecordBatch) { std::vector> fields = {f0}; auto schema = std::make_shared(fields); - RecordBatch batch(schema, 0, {array}); + RecordBatch batch(schema, length, {array}); std::string path = "test-write-large-record_batch"; @@ -548,6 +535,8 @@ TEST_F(TestIpcRoundTrip, LargeRecordBatch) { ASSERT_OK(DoLargeRoundTrip(batch, false, &result)); CheckReadResult(*result, batch); + ASSERT_EQ(length, result->num_rows()); + // Fails if we try to write this with the normal code path ASSERT_RAISES(Invalid, DoStandardRoundTrip(batch, false, &result)); } diff --git a/cpp/src/arrow/ipc/json-internal.cc b/cpp/src/arrow/ipc/json-internal.cc index a02943b4e320..d5763f0e209a 100644 --- a/cpp/src/arrow/ipc/json-internal.cc +++ b/cpp/src/arrow/ipc/json-internal.cc @@ -40,7 +40,6 @@ #include "arrow/util/bit-util.h" #include "arrow/util/logging.h" #include "arrow/util/string.h" -#include "arrow/visitor.h" #include "arrow/visitor_inline.h" namespace arrow { @@ -65,13 +64,13 @@ static std::string GetBufferTypeName(BufferType type) { return "UNKNOWN"; } -static std::string GetFloatingPrecisionName(FloatingPointMeta::Precision precision) { +static std::string GetFloatingPrecisionName(FloatingPoint::Precision precision) { switch (precision) { - case FloatingPointMeta::HALF: + case FloatingPoint::HALF: return "HALF"; - case FloatingPointMeta::SINGLE: + case FloatingPoint::SINGLE: return "SINGLE"; - case FloatingPointMeta::DOUBLE: + case FloatingPoint::DOUBLE: return "DOUBLE"; default: break; @@ -141,25 +140,19 @@ class JsonSchemaWriter { void>::type WriteTypeMetadata(const T& type) {} - template - typename std::enable_if::value, void>::type - WriteTypeMetadata(const T& type) { + void WriteTypeMetadata(const Integer& type) { writer_->Key("bitWidth"); writer_->Int(type.bit_width()); writer_->Key("isSigned"); writer_->Bool(type.is_signed()); } - template - typename std::enable_if::value, void>::type - WriteTypeMetadata(const T& type) { + void WriteTypeMetadata(const FloatingPoint& type) { writer_->Key("precision"); writer_->String(GetFloatingPrecisionName(type.precision())); } - template - typename std::enable_if::value, void>::type - WriteTypeMetadata(const T& type) { + void WriteTypeMetadata(const IntervalType& type) { writer_->Key("unit"); switch (type.unit) { case IntervalType::Unit::YEAR_MONTH: @@ -171,18 +164,21 @@ class JsonSchemaWriter { } } - template - typename std::enable_if::value || - std::is_base_of::value, - void>::type - WriteTypeMetadata(const T& type) { + void WriteTypeMetadata(const TimestampType& type) { writer_->Key("unit"); writer_->String(GetTimeUnitName(type.unit)); + if (type.timezone.size() > 0) { + writer_->Key("timezone"); + writer_->String(type.timezone); + } } - template - typename std::enable_if::value, void>::type - WriteTypeMetadata(const T& type) { + void WriteTypeMetadata(const TimeType& type) { + writer_->Key("unit"); + writer_->String(GetTimeUnitName(type.unit)); + } + + void WriteTypeMetadata(const DateType& type) { writer_->Key("unit"); switch (type.unit) { case DateUnit::DAY: @@ -194,18 +190,14 @@ class JsonSchemaWriter { } } - template - typename std::enable_if::value, void>::type - WriteTypeMetadata(const T& type) { + void WriteTypeMetadata(const DecimalType& type) { writer_->Key("precision"); writer_->Int(type.precision); writer_->Key("scale"); writer_->Int(type.scale); } - template - typename std::enable_if::value, void>::type - WriteTypeMetadata(const T& type) { + void WriteTypeMetadata(const UnionType& type) { writer_->Key("mode"); switch (type.mode) { case UnionMode::SPARSE: @@ -287,29 +279,15 @@ class JsonSchemaWriter { Status Visit(const BooleanType& type) { return WritePrimitive("bool", type); } - template - typename std::enable_if::value, Status>::type Visit( - const T& type) { - return WritePrimitive("int", type); - } + Status Visit(const Integer& type) { return WritePrimitive("int", type); } - template - typename std::enable_if::value, Status>::type - Visit(const T& type) { + Status Visit(const FloatingPoint& type) { return WritePrimitive("floatingpoint", type); } - template - typename std::enable_if::value, Status>::type Visit( - const T& type) { - return WritePrimitive("date", type); - } + Status Visit(const DateType& type) { return WritePrimitive("date", type); } - template - typename std::enable_if::value, Status>::type Visit( - const T& type) { - return WritePrimitive("time", type); - } + Status Visit(const TimeType& type) { return WritePrimitive("time", type); } Status Visit(const StringType& type) { return WriteVarBytes("utf8", type); } @@ -355,7 +333,7 @@ class JsonSchemaWriter { RjWriter* writer_; }; -class JsonArrayWriter : public ArrayVisitor { +class JsonArrayWriter { public: JsonArrayWriter(const std::string& name, const Array& array, RjWriter* writer) : name_(name), array_(array), writer_(writer) {} @@ -370,7 +348,7 @@ class JsonArrayWriter : public ArrayVisitor { writer_->Key("count"); writer_->Int(static_cast(arr.length())); - RETURN_NOT_OK(arr.Accept(this)); + RETURN_NOT_OK(VisitArrayInline(arr, this)); writer_->EndObject(); return Status::OK(); @@ -419,9 +397,7 @@ class JsonArrayWriter : public ArrayVisitor { } } - template - typename std::enable_if::value, void>::type - WriteDataValues(const T& arr) { + void WriteDataValues(const BooleanArray& arr) { for (int i = 0; i < arr.length(); ++i) { writer_->Bool(arr.Value(i)); } @@ -466,23 +442,6 @@ class JsonArrayWriter : public ArrayVisitor { writer_->EndArray(); } - template - Status WritePrimitive(const T& array) { - WriteValidityField(array); - WriteDataField(array); - SetNoChildren(); - return Status::OK(); - } - - template - Status WriteVarBytes(const T& array) { - WriteValidityField(array); - WriteIntegerField("OFFSET", array.raw_value_offsets(), array.length() + 1); - WriteDataField(array); - SetNoChildren(); - return Status::OK(); - } - Status WriteChildren(const std::vector>& fields, const std::vector>& arrays) { writer_->Key("children"); @@ -494,53 +453,52 @@ class JsonArrayWriter : public ArrayVisitor { return Status::OK(); } - Status Visit(const NullArray& array) override { + Status Visit(const NullArray& array) { SetNoChildren(); return Status::OK(); } - Status Visit(const BooleanArray& array) override { return WritePrimitive(array); } - - Status Visit(const Int8Array& array) override { return WritePrimitive(array); } - - Status Visit(const Int16Array& array) override { return WritePrimitive(array); } - - Status Visit(const Int32Array& array) override { return WritePrimitive(array); } - - Status Visit(const Int64Array& array) override { return WritePrimitive(array); } - - Status Visit(const UInt8Array& array) override { return WritePrimitive(array); } - - Status Visit(const UInt16Array& array) override { return WritePrimitive(array); } - - Status Visit(const UInt32Array& array) override { return WritePrimitive(array); } - - Status Visit(const UInt64Array& array) override { return WritePrimitive(array); } - - Status Visit(const HalfFloatArray& array) override { return WritePrimitive(array); } + template + typename std::enable_if::value, Status>::type Visit( + const T& array) { + WriteValidityField(array); + WriteDataField(array); + SetNoChildren(); + return Status::OK(); + } - Status Visit(const FloatArray& array) override { return WritePrimitive(array); } + template + typename std::enable_if::value, Status>::type Visit( + const T& array) { + WriteValidityField(array); + WriteIntegerField("OFFSET", array.raw_value_offsets(), array.length() + 1); + WriteDataField(array); + SetNoChildren(); + return Status::OK(); + } - Status Visit(const DoubleArray& array) override { return WritePrimitive(array); } + Status Visit(const FixedWidthBinaryArray& array) { + return Status::NotImplemented("fixed width binary"); + } - Status Visit(const StringArray& array) override { return WriteVarBytes(array); } + Status Visit(const DecimalArray& array) { return Status::NotImplemented("decimal"); } - Status Visit(const BinaryArray& array) override { return WriteVarBytes(array); } + Status Visit(const DictionaryArray& array) { return Status::NotImplemented("decimal"); } - Status Visit(const ListArray& array) override { + Status Visit(const ListArray& array) { WriteValidityField(array); WriteIntegerField("OFFSET", array.raw_value_offsets(), array.length() + 1); auto type = static_cast(array.type().get()); return WriteChildren(type->children(), {array.values()}); } - Status Visit(const StructArray& array) override { + Status Visit(const StructArray& array) { WriteValidityField(array); auto type = static_cast(array.type().get()); return WriteChildren(type->children(), array.fields()); } - Status Visit(const UnionArray& array) override { + Status Visit(const UnionArray& array) { WriteValidityField(array); auto type = static_cast(array.type().get()); @@ -671,7 +629,12 @@ static Status GetTimestamp(const RjObject& json_type, std::shared_ptr* return Status::Invalid(ss.str()); } - *type = timestamp(unit); + const auto& json_tz = json_type.FindMember("timezone"); + if (json_tz == json_type.MemberEnd()) { + *type = timestamp(unit); + } else { + *type = timestamp(unit, json_tz->value.GetString()); + } return Status::OK(); } @@ -789,7 +752,7 @@ static Status GetField(const rj::Value& obj, std::shared_ptr* field) { template inline typename std::enable_if::value, typename T::c_type>::type UnboxValue(const rj::Value& val) { - DCHECK(val.IsInt()); + DCHECK(val.IsInt64()); return static_cast(val.GetInt64()); } @@ -840,8 +803,10 @@ class JsonArrayReader { } template - typename std::enable_if::value || - std::is_base_of::value, + typename std::enable_if< + std::is_base_of::value || std::is_base_of::value || + std::is_base_of::value || + std::is_base_of::value || std::is_base_of::value, Status>::type ReadArray(const RjObject& json_array, int32_t length, const std::vector& is_valid, const std::shared_ptr& type, std::shared_ptr* array) { @@ -1070,13 +1035,6 @@ class JsonArrayReader { case TYPE::type_id: \ return ReadArray(json_array, length, is_valid, type, array); -#define NOT_IMPLEMENTED_CASE(TYPE_ENUM) \ - case Type::TYPE_ENUM: { \ - std::stringstream ss; \ - ss << type->ToString(); \ - return Status::NotImplemented(ss.str()); \ - } - switch (type->type) { TYPE_CASE(NullType); TYPE_CASE(BooleanType); @@ -1093,16 +1051,14 @@ class JsonArrayReader { TYPE_CASE(DoubleType); TYPE_CASE(StringType); TYPE_CASE(BinaryType); - NOT_IMPLEMENTED_CASE(DATE32); - NOT_IMPLEMENTED_CASE(DATE64); - NOT_IMPLEMENTED_CASE(TIMESTAMP); - NOT_IMPLEMENTED_CASE(TIME32); - NOT_IMPLEMENTED_CASE(TIME64); - NOT_IMPLEMENTED_CASE(INTERVAL); + TYPE_CASE(Date32Type); + TYPE_CASE(Date64Type); + TYPE_CASE(TimestampType); + TYPE_CASE(Time32Type); + TYPE_CASE(Time64Type); TYPE_CASE(ListType); TYPE_CASE(StructType); TYPE_CASE(UnionType); - NOT_IMPLEMENTED_CASE(DICTIONARY); default: std::stringstream ss; ss << type->ToString(); @@ -1110,7 +1066,6 @@ class JsonArrayReader { } #undef TYPE_CASE -#undef NOT_IMPLEMENTED_CASE return Status::OK(); } diff --git a/cpp/src/arrow/ipc/metadata.cc b/cpp/src/arrow/ipc/metadata.cc index 17af56380579..85dc8b321c41 100644 --- a/cpp/src/arrow/ipc/metadata.cc +++ b/cpp/src/arrow/ipc/metadata.cc @@ -602,7 +602,7 @@ static Status WriteBuffers( return Status::OK(); } -static Status MakeRecordBatch(FBB& fbb, int32_t length, int64_t body_length, +static Status MakeRecordBatch(FBB& fbb, int64_t length, int64_t body_length, const std::vector& nodes, const std::vector& buffers, RecordBatchOffset* offset) { FieldNodeVector fb_nodes; @@ -615,7 +615,7 @@ static Status MakeRecordBatch(FBB& fbb, int32_t length, int64_t body_length, return Status::OK(); } -Status WriteRecordBatchMessage(int32_t length, int64_t body_length, +Status WriteRecordBatchMessage(int64_t length, int64_t body_length, const std::vector& nodes, const std::vector& buffers, std::shared_ptr* out) { FBB fbb; @@ -625,7 +625,7 @@ Status WriteRecordBatchMessage(int32_t length, int64_t body_length, fbb, flatbuf::MessageHeader_RecordBatch, record_batch.Union(), body_length, out); } -Status WriteDictionaryMessage(int64_t id, int32_t length, int64_t body_length, +Status WriteDictionaryMessage(int64_t id, int64_t length, int64_t body_length, const std::vector& nodes, const std::vector& buffers, std::shared_ptr* out) { FBB fbb; diff --git a/cpp/src/arrow/ipc/metadata.h b/cpp/src/arrow/ipc/metadata.h index 6e903c0a18ef..f60fb770c369 100644 --- a/cpp/src/arrow/ipc/metadata.h +++ b/cpp/src/arrow/ipc/metadata.h @@ -188,11 +188,11 @@ Status ReadMessage(int64_t offset, int32_t metadata_length, io::RandomAccessFile Status WriteSchemaMessage( const Schema& schema, DictionaryMemo* dictionary_memo, std::shared_ptr* out); -Status WriteRecordBatchMessage(int32_t length, int64_t body_length, +Status WriteRecordBatchMessage(int64_t length, int64_t body_length, const std::vector& nodes, const std::vector& buffers, std::shared_ptr* out); -Status WriteDictionaryMessage(int64_t id, int32_t length, int64_t body_length, +Status WriteDictionaryMessage(int64_t id, int64_t length, int64_t body_length, const std::vector& nodes, const std::vector& buffers, std::shared_ptr* out); diff --git a/cpp/src/arrow/ipc/reader.cc b/cpp/src/arrow/ipc/reader.cc index 83e03aa0b36b..03c678ab7e28 100644 --- a/cpp/src/arrow/ipc/reader.cc +++ b/cpp/src/arrow/ipc/reader.cc @@ -97,6 +97,8 @@ static Status LoadRecordBatchFromSource(const std::shared_ptr& schema, for (int i = 0; i < schema->num_fields(); ++i) { RETURN_NOT_OK(LoadArray(schema->field(i)->type, &context, &arrays[i])); + DCHECK_EQ(num_rows, arrays[i]->length()) + << "Array length did not match record batch length"; } *out = std::make_shared(schema, num_rows, std::move(arrays)); diff --git a/cpp/src/arrow/ipc/test-common.h b/cpp/src/arrow/ipc/test-common.h index 956877cb34a5..00cf733bfc07 100644 --- a/cpp/src/arrow/ipc/test-common.h +++ b/cpp/src/arrow/ipc/test-common.h @@ -29,6 +29,7 @@ #include "arrow/buffer.h" #include "arrow/builder.h" #include "arrow/memory_pool.h" +#include "arrow/pretty_print.h" #include "arrow/status.h" #include "arrow/table.h" #include "arrow/test-util.h" @@ -47,7 +48,6 @@ static inline void AssertSchemaEqual(const Schema& lhs, const Schema& rhs) { } } - static inline void CompareBatch(const RecordBatch& left, const RecordBatch& right) { if (!left.schema()->Equals(right.schema())) { FAIL() << "Left schema: " << left.schema()->ToString() @@ -62,6 +62,27 @@ static inline void CompareBatch(const RecordBatch& left, const RecordBatch& righ } } +static inline void CompareArraysDetailed( + int index, const Array& result, const Array& expected) { + if (!expected.Equals(result)) { + std::stringstream pp_result; + std::stringstream pp_expected; + + ASSERT_OK(PrettyPrint(expected, 0, &pp_expected)); + ASSERT_OK(PrettyPrint(result, 0, &pp_result)); + + FAIL() << "Index: " << index << " Expected: " << pp_expected.str() + << "\nGot: " << pp_result.str(); + } +} + +static inline void CompareBatchColumnsDetailed( + const RecordBatch& result, const RecordBatch& expected) { + for (int i = 0; i < expected.num_columns(); ++i) { + CompareArraysDetailed(i, *result.column(i), *expected.column(i)); + } +} + const auto kListInt32 = list(int32()); const auto kListListInt32 = list(kListInt32); @@ -489,7 +510,7 @@ Status MakeDates(std::shared_ptr* out) { ArrayFromVector(is_valid, date32_values, &date32_array); std::vector date64_values = {1489269000000, 1489270000000, 1489271000000, - 1489272000000, 1489272000000, 1489273000000}; + 1489272000000, 1489272000000, 1489273000000, 1489274000000}; std::shared_ptr date64_array; ArrayFromVector(is_valid, date64_values, &date64_array); diff --git a/cpp/src/arrow/ipc/writer.cc b/cpp/src/arrow/ipc/writer.cc index e795ef961cb6..4ec096be455f 100644 --- a/cpp/src/arrow/ipc/writer.cc +++ b/cpp/src/arrow/ipc/writer.cc @@ -141,7 +141,7 @@ class RecordBatchWriter : public ArrayVisitor { virtual Status WriteMetadataMessage( int64_t num_rows, int64_t body_length, std::shared_ptr* out) { return WriteRecordBatchMessage( - static_cast(num_rows), body_length, field_nodes_, buffer_meta_, out); + num_rows, body_length, field_nodes_, buffer_meta_, out); } Status WriteMetadata(int64_t num_rows, int64_t body_length, io::OutputStream* dst, @@ -476,8 +476,8 @@ class DictionaryWriter : public RecordBatchWriter { Status WriteMetadataMessage( int64_t num_rows, int64_t body_length, std::shared_ptr* out) override { - return WriteDictionaryMessage(dictionary_id_, static_cast(num_rows), - body_length, field_nodes_, buffer_meta_, out); + return WriteDictionaryMessage( + dictionary_id_, num_rows, body_length, field_nodes_, buffer_meta_, out); } Status Write(int64_t dictionary_id, const std::shared_ptr& dictionary, diff --git a/cpp/src/arrow/type.cc b/cpp/src/arrow/type.cc index 23bdea7399e4..c790f6e5a434 100644 --- a/cpp/src/arrow/type.cc +++ b/cpp/src/arrow/type.cc @@ -45,8 +45,6 @@ std::string Field::ToString() const { return ss.str(); } -DataType::~DataType() {} - bool DataType::Equals(const DataType& other) const { bool are_equal = false; Status error = TypeEquals(*this, other, &are_equal); @@ -63,16 +61,16 @@ std::string BooleanType::ToString() const { return name(); } -FloatingPointMeta::Precision HalfFloatType::precision() const { - return FloatingPointMeta::HALF; +FloatingPoint::Precision HalfFloatType::precision() const { + return FloatingPoint::HALF; } -FloatingPointMeta::Precision FloatType::precision() const { - return FloatingPointMeta::SINGLE; +FloatingPoint::Precision FloatType::precision() const { + return FloatingPoint::SINGLE; } -FloatingPointMeta::Precision DoubleType::precision() const { - return FloatingPointMeta::DOUBLE; +FloatingPoint::Precision DoubleType::precision() const { + return FloatingPoint::DOUBLE; } std::string StringType::ToString() const { diff --git a/cpp/src/arrow/type.h b/cpp/src/arrow/type.h index 649ec2bc3fa1..dc50ecd669ca 100644 --- a/cpp/src/arrow/type.h +++ b/cpp/src/arrow/type.h @@ -132,7 +132,7 @@ struct ARROW_EXPORT DataType { explicit DataType(Type::type type) : type(type) {} - virtual ~DataType(); + virtual ~DataType() = default; // Return whether the types are equal // @@ -167,11 +167,17 @@ struct ARROW_EXPORT FixedWidthType : public DataType { std::vector GetBufferLayout() const override; }; -struct ARROW_EXPORT IntegerMeta { +struct ARROW_EXPORT PrimitiveCType : public FixedWidthType { + using FixedWidthType::FixedWidthType; +}; + +struct ARROW_EXPORT Integer : public PrimitiveCType { + using PrimitiveCType::PrimitiveCType; virtual bool is_signed() const = 0; }; -struct ARROW_EXPORT FloatingPointMeta { +struct ARROW_EXPORT FloatingPoint : public PrimitiveCType { + using PrimitiveCType::PrimitiveCType; enum Precision { HALF, SINGLE, DOUBLE }; virtual Precision precision() const = 0; }; @@ -206,16 +212,12 @@ struct ARROW_EXPORT Field { typedef std::shared_ptr FieldPtr; -struct ARROW_EXPORT PrimitiveCType : public FixedWidthType { - using FixedWidthType::FixedWidthType; -}; - -template -struct ARROW_EXPORT CTypeImpl : public PrimitiveCType { +template +struct ARROW_EXPORT CTypeImpl : public BASE { using c_type = C_TYPE; static constexpr Type::type type_id = TYPE_ID; - CTypeImpl() : PrimitiveCType(TYPE_ID) {} + CTypeImpl() : BASE(TYPE_ID) {} int bit_width() const override { return static_cast(sizeof(C_TYPE) * 8); } @@ -240,7 +242,7 @@ struct ARROW_EXPORT NullType : public DataType, public NoExtraMeta { }; template -struct IntegerTypeImpl : public CTypeImpl, public IntegerMeta { +struct IntegerTypeImpl : public CTypeImpl { bool is_signed() const override { return std::is_signed::value; } }; @@ -292,20 +294,19 @@ struct ARROW_EXPORT Int64Type : public IntegerTypeImpl, - public FloatingPointMeta { + : public CTypeImpl { Precision precision() const override; static std::string name() { return "halffloat"; } }; -struct ARROW_EXPORT FloatType : public CTypeImpl, - public FloatingPointMeta { +struct ARROW_EXPORT FloatType + : public CTypeImpl { Precision precision() const override; static std::string name() { return "float"; } }; -struct ARROW_EXPORT DoubleType : public CTypeImpl, - public FloatingPointMeta { +struct ARROW_EXPORT DoubleType + : public CTypeImpl { Precision precision() const override; static std::string name() { return "double"; } }; From d26ad1412d8db3c00a25e48a4eac6b9e83467adb Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Mon, 27 Mar 2017 16:27:30 -0400 Subject: [PATCH 3/5] Implement FixedWidthBinary support in JSON reader/writer. Make FWBinaryArray subclass of PrimitiveArray Change-Id: I5132896006b10d7acb96fa674292619994e2c295 --- cpp/src/arrow/array.cc | 5 +- cpp/src/arrow/array.h | 7 +-- cpp/src/arrow/ipc/ipc-json-test.cc | 2 +- cpp/src/arrow/ipc/json-internal.cc | 79 +++++++++++++++++++++++++++--- cpp/src/arrow/ipc/test-common.h | 2 +- 5 files changed, 75 insertions(+), 20 deletions(-) diff --git a/cpp/src/arrow/array.cc b/cpp/src/arrow/array.cc index cff012664764..3ea033376fca 100644 --- a/cpp/src/arrow/array.cc +++ b/cpp/src/arrow/array.cc @@ -283,12 +283,9 @@ std::shared_ptr StringArray::Slice(int64_t offset, int64_t length) const FixedWidthBinaryArray::FixedWidthBinaryArray(const std::shared_ptr& type, int64_t length, const std::shared_ptr& data, const std::shared_ptr& null_bitmap, int64_t null_count, int64_t offset) - : Array(type, length, null_bitmap, null_count, offset), - data_(data), - raw_data_(nullptr) { + : PrimitiveArray(type, length, data, null_bitmap, null_count, offset) { DCHECK(type->type == Type::FIXED_WIDTH_BINARY); byte_width_ = static_cast(*type).byte_width(); - if (data) { raw_data_ = data->data(); } } std::shared_ptr FixedWidthBinaryArray::Slice( diff --git a/cpp/src/arrow/array.h b/cpp/src/arrow/array.h index cc0cf98092a8..c0ec571e4598 100644 --- a/cpp/src/arrow/array.h +++ b/cpp/src/arrow/array.h @@ -347,7 +347,7 @@ class ARROW_EXPORT StringArray : public BinaryArray { // ---------------------------------------------------------------------- // Fixed width binary -class ARROW_EXPORT FixedWidthBinaryArray : public Array { +class ARROW_EXPORT FixedWidthBinaryArray : public PrimitiveArray { public: using TypeClass = FixedWidthBinaryType; @@ -360,9 +360,6 @@ class ARROW_EXPORT FixedWidthBinaryArray : public Array { return raw_data_ + (i + offset_) * byte_width_; } - /// Note that this buffer does not account for any slice offset - std::shared_ptr data() const { return data_; } - int32_t byte_width() const { return byte_width_; } const uint8_t* raw_data() const { return raw_data_; } @@ -371,8 +368,6 @@ class ARROW_EXPORT FixedWidthBinaryArray : public Array { protected: int32_t byte_width_; - std::shared_ptr data_; - const uint8_t* raw_data_; }; // ---------------------------------------------------------------------- diff --git a/cpp/src/arrow/ipc/ipc-json-test.cc b/cpp/src/arrow/ipc/ipc-json-test.cc index ea74e951eede..68261ab25a43 100644 --- a/cpp/src/arrow/ipc/ipc-json-test.cc +++ b/cpp/src/arrow/ipc/ipc-json-test.cc @@ -355,7 +355,7 @@ TEST(TestJsonFileReadWrite, MinimalFormatExample) { #define BATCH_CASES() \ ::testing::Values(&MakeIntRecordBatch, &MakeListRecordBatch, &MakeNonNullRecordBatch, \ &MakeZeroLengthRecordBatch, &MakeDeeplyNestedList, &MakeStringTypesRecordBatch, \ - &MakeStruct, &MakeUnion, &MakeDates, &MakeTimestamps, &MakeTimes); + &MakeStruct, &MakeUnion, &MakeDates, &MakeTimestamps, &MakeTimes, &MakeFWBinary); class TestJsonRoundTrip : public ::testing::TestWithParam { public: diff --git a/cpp/src/arrow/ipc/json-internal.cc b/cpp/src/arrow/ipc/json-internal.cc index d5763f0e209a..8604f418f110 100644 --- a/cpp/src/arrow/ipc/json-internal.cc +++ b/cpp/src/arrow/ipc/json-internal.cc @@ -190,6 +190,11 @@ class JsonSchemaWriter { } } + void WriteTypeMetadata(const FixedWidthBinaryType& type) { + writer_->Key("byteWidth"); + writer_->Int(type.byte_width()); + } + void WriteTypeMetadata(const DecimalType& type) { writer_->Key("precision"); writer_->Int(type.precision); @@ -293,6 +298,10 @@ class JsonSchemaWriter { Status Visit(const BinaryType& type) { return WriteVarBytes("binary", type); } + Status Visit(const FixedWidthBinaryType& type) { + return WritePrimitive("fixed_width_binary", type); + } + Status Visit(const TimestampType& type) { return WritePrimitive("timestamp", type); } Status Visit(const IntervalType& type) { return WritePrimitive("interval", type); } @@ -318,10 +327,6 @@ class JsonSchemaWriter { return Status::OK(); } - Status Visit(const FixedWidthBinaryType& type) { - return Status::NotImplemented("fixed width binary"); - } - Status Visit(const DecimalType& type) { return Status::NotImplemented("decimal"); } Status Visit(const DictionaryType& type) { @@ -397,6 +402,14 @@ class JsonArrayWriter { } } + void WriteDataValues(const FixedWidthBinaryArray& arr) { + int32_t width = arr.byte_width(); + for (int64_t i = 0; i < arr.length(); ++i) { + const char* buf = reinterpret_cast(arr.GetValue(i)); + writer_->String(HexEncode(buf, width)); + } + } + void WriteDataValues(const BooleanArray& arr) { for (int i = 0; i < arr.length(); ++i) { writer_->Bool(arr.Value(i)); @@ -477,10 +490,6 @@ class JsonArrayWriter { return Status::OK(); } - Status Visit(const FixedWidthBinaryArray& array) { - return Status::NotImplemented("fixed width binary"); - } - Status Visit(const DecimalArray& array) { return Status::NotImplemented("decimal"); } Status Visit(const DictionaryArray& array) { return Status::NotImplemented("decimal"); } @@ -568,6 +577,16 @@ static Status GetFloatingPoint( return Status::OK(); } +static Status GetFixedWidthBinary( + const RjObject& json_type, std::shared_ptr* type) { + const auto& json_byte_width = json_type.FindMember("byteWidth"); + RETURN_NOT_INT("byteWidth", json_byte_width, json_type); + + int32_t byte_width = json_byte_width->value.GetInt(); + *type = fixed_width_binary(byte_width); + return Status::OK(); +} + static Status GetDate(const RjObject& json_type, std::shared_ptr* type) { const auto& json_unit = json_type.FindMember("unit"); RETURN_NOT_STRING("unit", json_unit, json_type); @@ -691,6 +710,8 @@ static Status GetType(const RjObject& json_type, *type = utf8(); } else if (type_name == "binary") { *type = binary(); + } else if (type_name == "fixed_width_binary") { + return GetFixedWidthBinary(json_type, type); } else if (type_name == "null") { *type = null(); } else if (type_name == "date") { @@ -875,6 +896,47 @@ class JsonArrayReader { return builder.Finish(array); } + template + typename std::enable_if::value, Status>::type + ReadArray(const RjObject& json_array, int32_t length, const std::vector& is_valid, + const std::shared_ptr& type, std::shared_ptr* array) { + FixedWidthBinaryBuilder builder(pool_, type); + + const auto& json_data = json_array.FindMember("DATA"); + RETURN_NOT_ARRAY("DATA", json_data, json_array); + + const auto& json_data_arr = json_data->value.GetArray(); + + DCHECK_EQ(static_cast(json_data_arr.Size()), length); + + int32_t byte_width = static_cast(*type).byte_width(); + + // Allocate space for parsed values + std::shared_ptr byte_buffer; + RETURN_NOT_OK(AllocateBuffer(pool_, byte_width, &byte_buffer)); + uint8_t* byte_buffer_data = byte_buffer->mutable_data(); + + for (int i = 0; i < length; ++i) { + if (!is_valid[i]) { + builder.AppendNull(); + continue; + } + + const rj::Value& val = json_data_arr[i]; + DCHECK(val.IsString()); + std::string hex_string = val.GetString(); + DCHECK_EQ(hex_string.size(), byte_width * 2) << "Expected size: " << byte_width * 2 + << " got: " << hex_string.size(); + const char* hex_data = hex_string.c_str(); + + for (int32_t j = 0; j < byte_width; ++j) { + RETURN_NOT_OK(ParseHexValue(hex_data + j * 2, &byte_buffer_data[j])); + } + RETURN_NOT_OK(builder.Append(byte_buffer_data)); + } + return builder.Finish(array); + } + template Status GetIntArray( const RjArray& json_array, const int32_t length, std::shared_ptr* out) { @@ -1051,6 +1113,7 @@ class JsonArrayReader { TYPE_CASE(DoubleType); TYPE_CASE(StringType); TYPE_CASE(BinaryType); + TYPE_CASE(FixedWidthBinaryType); TYPE_CASE(Date32Type); TYPE_CASE(Date64Type); TYPE_CASE(TimestampType); diff --git a/cpp/src/arrow/ipc/test-common.h b/cpp/src/arrow/ipc/test-common.h index 00cf733bfc07..994e1283004a 100644 --- a/cpp/src/arrow/ipc/test-common.h +++ b/cpp/src/arrow/ipc/test-common.h @@ -584,7 +584,7 @@ Status MakeFWBinary(std::shared_ptr* out) { std::shared_ptr a1, a2; FixedWidthBinaryBuilder b1(default_memory_pool(), f0->type); - FixedWidthBinaryBuilder b2(default_memory_pool(), f0->type); + FixedWidthBinaryBuilder b2(default_memory_pool(), f1->type); std::vector values1 = {"foo1", "foo2", "foo3", "foo4"}; AppendValues(is_valid, values1, &b1); From cd714f8e078c72bb274a5b8bdaca1b4e69f585ad Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Mon, 27 Mar 2017 16:36:26 -0400 Subject: [PATCH 4/5] No underscores. Fix bug slicing null buffer Change-Id: Ibb922356502114522abb8159b2c24d0855e857a8 --- cpp/src/arrow/ipc/json-internal.cc | 4 ++-- cpp/src/arrow/ipc/writer.cc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cpp/src/arrow/ipc/json-internal.cc b/cpp/src/arrow/ipc/json-internal.cc index 8604f418f110..44f403ad5404 100644 --- a/cpp/src/arrow/ipc/json-internal.cc +++ b/cpp/src/arrow/ipc/json-internal.cc @@ -299,7 +299,7 @@ class JsonSchemaWriter { Status Visit(const BinaryType& type) { return WriteVarBytes("binary", type); } Status Visit(const FixedWidthBinaryType& type) { - return WritePrimitive("fixed_width_binary", type); + return WritePrimitive("fixedwidthbinary", type); } Status Visit(const TimestampType& type) { return WritePrimitive("timestamp", type); } @@ -710,7 +710,7 @@ static Status GetType(const RjObject& json_type, *type = utf8(); } else if (type_name == "binary") { *type = binary(); - } else if (type_name == "fixed_width_binary") { + } else if (type_name == "fixedwidthbinary") { return GetFixedWidthBinary(json_type, type); } else if (type_name == "null") { *type = null(); diff --git a/cpp/src/arrow/ipc/writer.cc b/cpp/src/arrow/ipc/writer.cc index 4ec096be455f..da360f31641b 100644 --- a/cpp/src/arrow/ipc/writer.cc +++ b/cpp/src/arrow/ipc/writer.cc @@ -306,7 +306,7 @@ class RecordBatchWriter : public ArrayVisitor { auto data = array.data(); int32_t width = array.byte_width(); - if (array.offset() != 0) { + if (data && array.offset() != 0) { data = SliceBuffer(data, array.offset() * width, width * array.length()); } buffers_.push_back(data); From 46978aaa348e2472d4593ae5efa63fff9e100ec8 Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Mon, 27 Mar 2017 17:44:25 -0400 Subject: [PATCH 5/5] Fix compiler warning Change-Id: I362163f6cc965def1f39a2cec0c97c05bdb01e8b --- cpp/src/arrow/ipc/json-internal.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/ipc/json-internal.cc b/cpp/src/arrow/ipc/json-internal.cc index 44f403ad5404..95ab011bd087 100644 --- a/cpp/src/arrow/ipc/json-internal.cc +++ b/cpp/src/arrow/ipc/json-internal.cc @@ -925,8 +925,8 @@ class JsonArrayReader { const rj::Value& val = json_data_arr[i]; DCHECK(val.IsString()); std::string hex_string = val.GetString(); - DCHECK_EQ(hex_string.size(), byte_width * 2) << "Expected size: " << byte_width * 2 - << " got: " << hex_string.size(); + DCHECK_EQ(static_cast(hex_string.size()), byte_width * 2) + << "Expected size: " << byte_width * 2 << " got: " << hex_string.size(); const char* hex_data = hex_string.c_str(); for (int32_t j = 0; j < byte_width; ++j) {