Skip to content
Merged
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
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ costs nothing until you actually touch a field.
9. ROS1 `Time`, `Duration`, and `Header` intrinsic mappings
10. Hybrid dense/sparse field metadata and protobuf version compatibility
11. Reflection, enum conversion, field presence, and user metadata
12. Generated conversion to and from the Arrow C Data Interface
12. Generated conversion to and from Arrow, and from Phaser or protobuf wire format to Avro and Parquet
13. Bazel integration and modern C++17 with [Abseil](https://abseil.io)

## How it works
Expand Down Expand Up @@ -463,6 +463,51 @@ an Arrow schema. `ToArrow` calls it. See the user guide's
[Arrow](phaser/docs/phaser_user_guide.md#arrow) section for the column layout,
`Any`, and ownership of the schema and array.

## Avro and Parquet

The Arrow column layout can be written one way as an
[Avro](https://avro.apache.org/docs/1.11.1/specification/) object container or
an uncompressed [Parquet](https://parquet.apache.org/docs/file-format/) file.
Phaser does not link libavro or libparquet, and there is no reader. The source
is a Phaser message, protobuf wire bytes, or a native Phaser payload.

```c++
std::string avro;
absl::Status status =
foo::bar::phaser::TestMessage::ToAvro(batch, &avro);
status = message.ToAvro(&avro);
status = foo::bar::phaser::TestMessage::ProtobufToAvro(protobuf_bytes, &avro);
status = foo::bar::phaser::TestMessage::PhaserToAvro(phaser_bytes, &avro);

std::string parquet;
status = foo::bar::phaser::TestMessage::ToParquet(batch, &parquet);
status = foo::bar::phaser::TestMessage::ProtobufToParquet(protobuf_bytes, &parquet);
status = foo::bar::phaser::TestMessage::PhaserToParquet(phaser_bytes, &parquet);
```

Each of `ProtobufToAvro`, `PhaserToAvro`, `ProtobufToParquet`, and
`PhaserToParquet` also accepts a span of messages and writes one multi-record
file. An empty protobuf message is a valid empty record. An empty native
Phaser payload is an error.

When the caller has the protobuf full name and not the C++ type, the bank
writes one payload:

```c++
status = ::phaser::PhaserBankProtobufToAvro(
"foo.bar.TestMessage", protobuf_bytes, &avro);
status = ::phaser::PhaserBankPhaserToParquet(
"foo.bar.TestMessage", phaser_bytes, &parquet);
status = ::phaser::PhaserBankConvert(
"foo.bar.TestMessage", ::phaser::BankInputType::kProtobuf, protobuf_bytes,
::phaser::BankOutputType::kAvro, &avro);
```

`PhaserBankConvert` also accepts `BankOutputType::kParquet`, and
`BankInputType::kPhaser` for a native payload. See the user guide's
[Avro and Parquet](phaser/docs/phaser_user_guide.md#avro-and-parquet) section
for the type mapping and file layout.

## The Phaser Bank (type-erased operations & reflection)

The **Phaser Bank** lets you operate on messages given only their type *name* — stream,
Expand Down Expand Up @@ -491,6 +536,11 @@ status = ::phaser::PhaserBankPhaserToROS(
"foo.bar.TestMessage", phaser_bytes, ros_output);
status = ::phaser::PhaserBankPhaserToProtobuf(
"foo.bar.TestMessage", phaser_bytes, protobuf_output);

std::string converted;
status = ::phaser::PhaserBankConvert(
"foo.bar.TestMessage", ::phaser::BankInputType::kProtobuf, protobuf_bytes,
::phaser::BankOutputType::kRos, &converted);
```

`PhaserBankRosDataType`, `PhaserBankRosMd5`, and `PhaserBankRosDefinition`
Expand Down
12 changes: 12 additions & 0 deletions phaser/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ cc_library(
],
)

cc_test(
name = "columnar_test",
srcs = ["columnar_test.cc"],
copts = PHASER_COPTS + PHASER_PROTOBUF_GENERATED_COPTS,
data = ["valgrind.supp"],
deps = [
"//phaser/runtime:phaser_runtime",
"//phaser/testdata:test_message_phaser",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "arrow_test",
srcs = ["arrow_test.cc"],
Expand Down
133 changes: 133 additions & 0 deletions phaser/columnar_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright 2024-2026 David Allison
// All Rights Reserved
// See LICENSE file for licensing information.

#include <string>
#include <string_view>
#include <vector>

#include "gtest/gtest.h"
#include "phaser/runtime/phaser_bank.h"
#include "phaser/testdata/TestMessage.phaser.h"

namespace {

foo::bar::phaser::TestMessage Sample() {
foo::bar::phaser::TestMessage message;
message.set_x(0);
message.set_y(7);
message.set_s("hello");
message.set_buffer(std::string("a\0b", 3));
message.add_vi32(1);
message.add_vi32(2);
message.mutable_m()->set_str("inner");
auto entry = message.add_values();
entry->set_key("k");
entry->set_value(3);
return message;
}

} // namespace

TEST(ColumnarMessageTest, WireFormatsMatchTheMessage) {
const foo::bar::phaser::TestMessage message = Sample();
const std::string protobuf = message.SerializeAsString();
const auto* native = reinterpret_cast<const char*>(message.Data());
const absl::Span<const char> payload(native, message.Size());

std::string from_message;
std::string from_protobuf;
std::string from_phaser;
ASSERT_TRUE(message.ToAvro(&from_message).ok());
ASSERT_TRUE(
foo::bar::phaser::TestMessage::ProtobufToAvro(protobuf, &from_protobuf)
.ok());
ASSERT_TRUE(foo::bar::phaser::TestMessage::PhaserToAvro(payload, &from_phaser)
.ok());
EXPECT_EQ(from_message, from_protobuf);
EXPECT_EQ(from_message, from_phaser);
EXPECT_NE(from_message.find("hello"), std::string::npos);
EXPECT_NE(from_message.find("inner"), std::string::npos);

std::string bank_protobuf;
std::string bank_phaser;
ASSERT_TRUE(phaser::PhaserBankProtobufToAvro(foo::bar::phaser::TestMessage::FullName(),
protobuf, &bank_protobuf)
.ok());
ASSERT_TRUE(phaser::PhaserBankPhaserToAvro(foo::bar::phaser::TestMessage::FullName(),
payload, &bank_phaser)
.ok());
EXPECT_EQ(from_message, bank_protobuf);
EXPECT_EQ(from_message, bank_phaser);

std::string parquet_message;
std::string parquet_protobuf;
std::string parquet_phaser;
ASSERT_TRUE(message.ToParquet(&parquet_message).ok());
ASSERT_TRUE(foo::bar::phaser::TestMessage::ProtobufToParquet(protobuf,
&parquet_protobuf)
.ok());
ASSERT_TRUE(
foo::bar::phaser::TestMessage::PhaserToParquet(payload, &parquet_phaser)
.ok());
EXPECT_EQ(parquet_message, parquet_protobuf);
EXPECT_EQ(parquet_message, parquet_phaser);
EXPECT_EQ("PAR1", parquet_message.substr(0, 4));
EXPECT_EQ("PAR1", parquet_message.substr(parquet_message.size() - 4));
EXPECT_NE(parquet_message.find("hello"), std::string::npos);

std::string bank_parquet;
ASSERT_TRUE(phaser::PhaserBankProtobufToParquet(
foo::bar::phaser::TestMessage::FullName(), protobuf,
&bank_parquet)
.ok());
EXPECT_EQ(parquet_message, bank_parquet);

foo::bar::phaser::TestMessage other;
other.set_y(8);
const std::string other_wire = other.SerializeAsString();
const std::vector<std::string_view> wires = {protobuf, other_wire};
std::vector<foo::bar::phaser::TestMessage> batch = {message, other};
std::string batch_message;
std::string batch_wire;
ASSERT_TRUE(
foo::bar::phaser::TestMessage::ToAvro(batch, &batch_message).ok());
ASSERT_TRUE(
foo::bar::phaser::TestMessage::ProtobufToAvro(wires, &batch_wire).ok());
EXPECT_EQ(batch_message, batch_wire);

foo::bar::phaser::TestMessage empty;
std::string empty_message;
std::string empty_wire;
ASSERT_TRUE(empty.ToAvro(&empty_message).ok());
ASSERT_TRUE(foo::bar::phaser::TestMessage::ProtobufToAvro("", &empty_wire).ok());
EXPECT_EQ(empty_message, empty_wire);
EXPECT_FALSE(foo::bar::phaser::TestMessage::PhaserToAvro(
absl::Span<const char>(), &empty_message)
.ok());
EXPECT_FALSE(
foo::bar::phaser::TestMessage::ProtobufToAvro("\x01", &empty_wire).ok());

const std::string_view full_name = foo::bar::phaser::TestMessage::FullName();
std::string converted;
ASSERT_TRUE(phaser::PhaserBankConvert(full_name, phaser::BankInputType::kPhaser,
payload, phaser::BankOutputType::kAvro,
&converted)
.ok());
EXPECT_EQ(from_phaser, converted);
ASSERT_TRUE(phaser::PhaserBankConvert(
full_name, phaser::BankInputType::kProtobuf,
absl::MakeConstSpan(protobuf.data(), protobuf.size()),
phaser::BankOutputType::kParquet, &converted)
.ok());
EXPECT_EQ(parquet_protobuf, converted);
EXPECT_FALSE(phaser::PhaserBankConvert(full_name, phaser::BankInputType::kRos,
payload, phaser::BankOutputType::kAvro,
&converted)
.ok());
EXPECT_TRUE(converted.empty());
EXPECT_FALSE(phaser::PhaserBankConvert(full_name, phaser::BankInputType::kPhaser,
payload, phaser::BankOutputType::kAvro,
nullptr)
.ok());
}
7 changes: 7 additions & 0 deletions phaser/compiler/message_gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6134,6 +6134,13 @@ void MessageGenerator::GeneratePhaserBank(std::ostream& os) {
<< "WriteArrowAtOffset,\n";
os << " .read_arrow_at_offset = " << MessageName(message_)
<< "ReadArrowAtOffset,\n";
os << " .protobuf_to_avro = " << MessageName(message_)
<< "ProtobufToAvro,\n";
os << " .phaser_to_avro = " << MessageName(message_) << "PhaserToAvro,\n";
os << " .protobuf_to_parquet = " << MessageName(message_)
<< "ProtobufToParquet,\n";
os << " .phaser_to_parquet = " << MessageName(message_)
<< "PhaserToParquet,\n";
os << "};\n\n";

os << "static struct " << MessageName(message_) << "BankInitializer {\n";
Expand Down
125 changes: 125 additions & 0 deletions phaser/compiler/message_gen_arrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,114 @@ void MessageGenerator::GenerateArrow(std::ostream& os, bool decl) {
<< name << ">* out) {\n";
os << " return ::phaser::arrow::MessagesFromArrow(schema, array, out);\n";
os << " }\n";
os << " static absl::Status ToAvro(absl::Span<const " << name
<< "> messages, std::string* out) {\n";
os << " return ::phaser::avro::WriteMessages(messages, out);\n";
os << " }\n";
os << " absl::Status ToAvro(std::string* out) const {\n";
os << " const " << name << "* _phaser_self = this;\n";
os << " return ToAvro(absl::MakeConstSpan(_phaser_self, 1), out);\n";
os << " }\n";
os << " static absl::Status ProtobufToAvro(std::string_view bytes, "
"std::string* out) {\n";
os << " " << name << " message;\n";
os << " if (!message.ParseFromArray(bytes.data(), bytes.size())) {\n";
os << " return absl::InvalidArgumentError("
"\"Protobuf wire bytes are not a valid message\");\n";
os << " }\n";
os << " return message.ToAvro(out);\n";
os << " }\n";
os << " static absl::Status ProtobufToAvro("
"absl::Span<const std::string_view> bytes, std::string* out) {\n";
os << " std::vector<" << name << "> messages;\n";
os << " messages.reserve(bytes.size());\n";
os << " for (std::string_view item : bytes) {\n";
os << " " << name << " message;\n";
os << " if (!message.ParseFromArray(item.data(), item.size())) {\n";
os << " return absl::InvalidArgumentError("
"\"Protobuf wire bytes are not a valid message\");\n";
os << " }\n";
os << " messages.push_back(std::move(message));\n";
os << " }\n";
os << " return ToAvro(messages, out);\n";
os << " }\n";
os << " static absl::Status PhaserToAvro(absl::Span<const char> bytes, "
"std::string* out) {\n";
os << " if (bytes.empty()) {\n";
os << " return absl::InvalidArgumentError("
"\"Native Phaser payload is empty\");\n";
os << " }\n";
os << " " << name
<< " message = CreateReadonly(bytes.data(), bytes.size());\n";
os << " return message.ToAvro(out);\n";
os << " }\n";
os << " static absl::Status PhaserToAvro("
"absl::Span<const absl::Span<const char>> bytes, std::string* out) {\n";
os << " std::vector<" << name << "> messages;\n";
os << " messages.reserve(bytes.size());\n";
os << " for (absl::Span<const char> item : bytes) {\n";
os << " if (item.empty()) {\n";
os << " return absl::InvalidArgumentError("
"\"Native Phaser payload is empty\");\n";
os << " }\n";
os << " messages.push_back(CreateReadonly(item.data(), item.size()));\n";
os << " }\n";
os << " return ToAvro(messages, out);\n";
os << " }\n";
os << " static absl::Status ToParquet(absl::Span<const " << name
<< "> messages, std::string* out) {\n";
os << " return ::phaser::parquet::WriteMessages(messages, out);\n";
os << " }\n";
os << " absl::Status ToParquet(std::string* out) const {\n";
os << " const " << name << "* _phaser_self = this;\n";
os << " return ToParquet(absl::MakeConstSpan(_phaser_self, 1), out);\n";
os << " }\n";
os << " static absl::Status ProtobufToParquet(std::string_view bytes, "
"std::string* out) {\n";
os << " " << name << " message;\n";
os << " if (!message.ParseFromArray(bytes.data(), bytes.size())) {\n";
os << " return absl::InvalidArgumentError("
"\"Protobuf wire bytes are not a valid message\");\n";
os << " }\n";
os << " return message.ToParquet(out);\n";
os << " }\n";
os << " static absl::Status ProtobufToParquet("
"absl::Span<const std::string_view> bytes, std::string* out) {\n";
os << " std::vector<" << name << "> messages;\n";
os << " messages.reserve(bytes.size());\n";
os << " for (std::string_view item : bytes) {\n";
os << " " << name << " message;\n";
os << " if (!message.ParseFromArray(item.data(), item.size())) {\n";
os << " return absl::InvalidArgumentError("
"\"Protobuf wire bytes are not a valid message\");\n";
os << " }\n";
os << " messages.push_back(std::move(message));\n";
os << " }\n";
os << " return ToParquet(messages, out);\n";
os << " }\n";
os << " static absl::Status PhaserToParquet(absl::Span<const char> bytes, "
"std::string* out) {\n";
os << " if (bytes.empty()) {\n";
os << " return absl::InvalidArgumentError("
"\"Native Phaser payload is empty\");\n";
os << " }\n";
os << " " << name
<< " message = CreateReadonly(bytes.data(), bytes.size());\n";
os << " return message.ToParquet(out);\n";
os << " }\n";
os << " static absl::Status PhaserToParquet("
"absl::Span<const absl::Span<const char>> bytes, std::string* out) {\n";
os << " std::vector<" << name << "> messages;\n";
os << " messages.reserve(bytes.size());\n";
os << " for (absl::Span<const char> item : bytes) {\n";
os << " if (item.empty()) {\n";
os << " return absl::InvalidArgumentError("
"\"Native Phaser payload is empty\");\n";
os << " }\n";
os << " messages.push_back(CreateReadonly(item.data(), item.size()));\n";
os << " }\n";
os << " return ToParquet(messages, out);\n";
os << " }\n";
return;
}

Expand Down Expand Up @@ -881,6 +989,23 @@ void MessageGenerator::GenerateArrow(std::ostream& os, bool decl) {
<< ">()) return ::phaser::internal::AllocationFailedError();\n";
os << " return message.ReadArrow(*reader);\n";
os << "}\n\n";

os << "static absl::Status " << name
<< "ProtobufToAvro(std::string_view bytes, std::string* out) {\n";
os << " return " << name << "::ProtobufToAvro(bytes, out);\n";
os << "}\n\n";
os << "static absl::Status " << name
<< "PhaserToAvro(absl::Span<const char> bytes, std::string* out) {\n";
os << " return " << name << "::PhaserToAvro(bytes, out);\n";
os << "}\n\n";
os << "static absl::Status " << name
<< "ProtobufToParquet(std::string_view bytes, std::string* out) {\n";
os << " return " << name << "::ProtobufToParquet(bytes, out);\n";
os << "}\n\n";
os << "static absl::Status " << name
<< "PhaserToParquet(absl::Span<const char> bytes, std::string* out) {\n";
os << " return " << name << "::PhaserToParquet(bytes, out);\n";
os << "}\n\n";
}

} // namespace phaser
Loading
Loading