This is an automated email from the ASF dual-hosted git repository.
LuciferYang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gluten.git
The following commit(s) were added to refs/heads/main by this push:
new d42824b854 [GLUTEN-13722][VL] Serialize V3 table cache columns
straight into the frame (#13723)
d42824b854 is described below
commit d42824b85474cd86cce10c23557ca931a3d8f499
Author: YangJie <[email protected]>
AuthorDate: Fri Nov 27 00:21:53 2026 -0500
[GLUTEN-13722][VL] Serialize V3 table cache columns straight into the frame
(#13723)
## What changes are proposed in this pull request?
`framedSerializeV3Impl` now gives `serializeSingleColumn` an `std::ostream`
whose streambuf appends to the frame vector, and fills in each column's 4-byte
length after the column is written. Before, each column went through an
`ostringstream`, a `std::string` and a `std::vector<uint8_t>` before it was
copied into the frame. The frame bytes don't change.
The streambuf can't seek. `serializeSingleColumn` doesn't need it to:
`VectorStream::flush` only calls `write` on the output.
Fixes #13722.
## How was this patch tested?
Added `framedSerializeV3ComplexTypesRoundTrip` to
`VeloxColumnarBatchSerializerTest`. It serializes 5000 rows of bigint, long
string, array, map and struct columns with nulls, with and without stats, and
decodes every column. The existing V3 writer tests use 5 rows of flat columns.
Not covered by CI:
- The frame bytes are unchanged: the SHA-256 of each of the 60 cached
batches of a 12-column table (nested types included, stats on and off) is the
same before and after.
- On macOS arm64 with a release build, `framedSerializeV3` on a 1M-row,
8-column batch (66 MB frame) takes 19.8 ms instead of 29.0 ms (median of 15
calls). `df.cache().count()` on 20M rows and 8 columns takes 1365 ms instead of
1510 ms (median of 7 runs).
## Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus
---
.../serializer/VeloxColumnarBatchSerializer.cc | 62 +++++++++++++++-------
.../tests/VeloxColumnarBatchSerializerTest.cc | 39 ++++++++++++++
2 files changed, 83 insertions(+), 18 deletions(-)
diff --git a/cpp/velox/operators/serializer/VeloxColumnarBatchSerializer.cc
b/cpp/velox/operators/serializer/VeloxColumnarBatchSerializer.cc
index 62a986fd99..59f9974bb9 100644
--- a/cpp/velox/operators/serializer/VeloxColumnarBatchSerializer.cc
+++ b/cpp/velox/operators/serializer/VeloxColumnarBatchSerializer.cc
@@ -23,7 +23,8 @@
#include <cstring>
#include <limits>
#include <optional>
-#include <sstream>
+#include <ostream>
+#include <streambuf>
#include "memory/ArrowMemory.h"
#include "memory/VeloxColumnarBatch.h"
@@ -653,6 +654,32 @@ std::vector<uint8_t>
VeloxColumnarBatchSerializer::framedSerializeWithStatsV3(
return framedSerializeV3Impl(batch, true);
}
+namespace {
+// A streambuf that appends what is written to it to a byte vector. It can't
seek;
+// serializeSingleColumn only appends.
+class AppendToVectorBuf : public std::streambuf {
+ public:
+ explicit AppendToVectorBuf(std::vector<uint8_t>& out) : out_(out) {}
+
+ protected:
+ std::streamsize xsputn(const char* s, std::streamsize n) override {
+ const auto* begin = reinterpret_cast<const uint8_t*>(s);
+ out_.insert(out_.end(), begin, begin + n);
+ return n;
+ }
+
+ int_type overflow(int_type ch) override {
+ if (!traits_type::eq_int_type(ch, traits_type::eof())) {
+ out_.push_back(static_cast<uint8_t>(ch));
+ }
+ return traits_type::not_eof(ch);
+ }
+
+ private:
+ std::vector<uint8_t>& out_;
+};
+} // namespace
+
std::vector<uint8_t> VeloxColumnarBatchSerializer::framedSerializeV3Impl(
const std::shared_ptr<ColumnarBatch>& batch,
bool includeStats) {
@@ -692,20 +719,6 @@ std::vector<uint8_t>
VeloxColumnarBatchSerializer::framedSerializeV3Impl(
buf.push_back(static_cast<uint8_t>((v >> 24) & 0xFF));
};
- std::vector<std::vector<uint8_t>> colBytesList(numCols);
- for (uint32_t col = 0; col < numCols; ++col) {
- std::ostringstream colStream;
- localSerde.serializeSingleColumn(rowVector->childAt(col), &options_,
veloxPool_.get(), &colStream);
- const std::string colStr = colStream.str();
- GLUTEN_CHECK(
- colStr.size() <=
static_cast<size_t>(std::numeric_limits<uint32_t>::max()),
- "V3 column " + std::to_string(col) + " size exceeds u32 limit");
- GLUTEN_CHECK(
- colStr.size() <=
static_cast<size_t>(std::numeric_limits<int32_t>::max()),
- "V3 column " + std::to_string(col) + " size exceeds int32 ByteRange
limit");
- colBytesList[col] = std::vector<uint8_t>(colStr.begin(), colStr.end());
- }
-
// 3. Assemble V3 framed format.
//
[magic=0x03(4B)][statsLen(4B)][statsBlob][numRows(4B)][numCols(4B)][per-col:
colLen+colBytes]
std::vector<uint8_t> framed;
@@ -722,10 +735,23 @@ std::vector<uint8_t>
VeloxColumnarBatchSerializer::framedSerializeV3Impl(
framed.insert(framed.end(), statsBlob.begin(), statsBlob.end());
pushU32LE(framed, numRows);
pushU32LE(framed, numCols);
+ // Serialize each column straight into the frame, then fill in its length
prefix.
+ AppendToVectorBuf frameBuf(framed);
+ std::ostream frameStream(&frameBuf);
+ // By default std::ostream catches a std::bad_alloc from growing the frame
and only sets badbit; rethrow it.
+ frameStream.exceptions(std::ios::badbit);
for (uint32_t col = 0; col < numCols; ++col) {
- const auto& cb = colBytesList[col];
- pushU32LE(framed, static_cast<uint32_t>(cb.size()));
- framed.insert(framed.end(), cb.begin(), cb.end());
+ const size_t lenPos = framed.size();
+ pushU32LE(framed, 0);
+ localSerde.serializeSingleColumn(rowVector->childAt(col), &options_,
veloxPool_.get(), &frameStream);
+ GLUTEN_CHECK(frameStream.good(), "V3 column " + std::to_string(col) + "
failed to serialize");
+ const size_t colSize = framed.size() - lenPos - 4;
+ GLUTEN_CHECK(
+ colSize <= static_cast<size_t>(std::numeric_limits<int32_t>::max()),
+ "V3 column " + std::to_string(col) + " size exceeds int32 ByteRange
limit");
+ for (int i = 0; i < 4; ++i) {
+ framed[lenPos + i] = static_cast<uint8_t>((colSize >> (8 * i)) & 0xFF);
+ }
}
GLUTEN_CHECK(
framed.size() <=
static_cast<size_t>(std::numeric_limits<int32_t>::max()),
diff --git a/cpp/velox/tests/VeloxColumnarBatchSerializerTest.cc
b/cpp/velox/tests/VeloxColumnarBatchSerializerTest.cc
index f341bc0ceb..161d3cf068 100644
--- a/cpp/velox/tests/VeloxColumnarBatchSerializerTest.cc
+++ b/cpp/velox/tests/VeloxColumnarBatchSerializerTest.cc
@@ -1227,6 +1227,45 @@ TEST_F(VeloxColumnarBatchSerializerTest,
framedSerializeWithStatsV3NonEmptyWrite
test::assertEqualVectors(expectedProjected, projectedVector);
}
+TEST_F(VeloxColumnarBatchSerializerTest,
framedSerializeV3ComplexTypesRoundTrip) {
+ constexpr vector_size_t kNumRows = 5'000;
+ auto* arrowPool = getDefaultMemoryManager()->defaultArrowMemoryPool();
+ auto serializer = std::make_shared<VeloxColumnarBatchSerializer>(arrowPool,
pool_, nullptr);
+ std::vector<VectorPtr> children = {
+ makeFlatVector<int64_t>(
+ kNumRows, [](vector_size_t row) { return row * 3; }, nullEvery(7)),
+ makeFlatVector<std::string>(
+ kNumRows,
+ [](vector_size_t row) { return "a string longer than inline " +
std::to_string(row); },
+ nullEvery(5)),
+ makeArrayVector<int32_t>(
+ kNumRows, [](vector_size_t row) { return row % 4; },
[](vector_size_t idx) { return idx; }, nullEvery(9)),
+ makeMapVector<std::string, int64_t>(
+ kNumRows,
+ [](vector_size_t row) { return row % 3; },
+ [](vector_size_t idx) { return "key-" + std::to_string(idx); },
+ [](vector_size_t idx) { return idx * 10; }),
+ makeRowVector(
+ {"x", "y"},
+ {makeFlatVector<int32_t>(kNumRows, [](vector_size_t row) { return
row; }),
+ makeFlatVector<std::string>(
+ kNumRows, [](vector_size_t row) { return std::to_string(row);
}, nullEvery(4))},
+ nullEvery(11)),
+ };
+ auto vector = makeRowVector({"i", "s", "arr", "m", "st"}, children);
+ auto batch = std::make_shared<VeloxColumnarBatch>(vector);
+ ArrowSchema cSchema;
+ exportToArrow(vector, cSchema, ArrowUtils::getBridgeOptions());
+ auto deserializer =
std::make_shared<VeloxColumnarBatchSerializer>(arrowPool, pool_, &cSchema);
+
+ for (const bool includeStats : {false, true}) {
+ auto framed = includeStats ? serializer->framedSerializeWithStatsV3(batch)
: serializer->framedSerializeV3(batch);
+ assertV3WriterFrameLayout(framed, kNumRows,
static_cast<uint32_t>(children.size()), includeStats);
+ auto result = deserializer->deserializeV3(framed.data(),
static_cast<int32_t>(framed.size()), std::nullopt);
+ test::assertEqualVectors(vector,
std::dynamic_pointer_cast<VeloxColumnarBatch>(result)->getRowVector());
+ }
+}
+
TEST_F(VeloxColumnarBatchSerializerTest,
deserializeV3ZeroProjectionNonEmptyNoStatsFrameFixture) {
auto* arrowPool = getDefaultMemoryManager()->defaultArrowMemoryPool();
auto deserializer = makeV3Deserializer(arrowPool);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]