Skip to content

Commit

Permalink
Add function serializeSingleColumn to PrestoVectorSerde
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsatya committed Oct 29, 2024
1 parent 6440a44 commit 8c6f2a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions velox/serializers/PrestoSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4265,6 +4265,26 @@ void PrestoVectorSerde::deserializeSingleColumn(
*result = row->childAt(0);
}

void PrestoVectorSerde::serializeSingleColumn(
const VectorPtr& vector,
const PrestoOptions& opts,
memory::MemoryPool* pool,
std::ostream* output) {
const auto numRows = vector->size();
VELOX_USER_CHECK_EQ(numRows, 1, "Input vector should have a single element");
const IndexRange allRows{0, numRows};
const auto ranges = folly::Range(&allRows, 1);
const auto arena = std::make_unique<StreamArena>(pool);
auto stream = std::make_unique<VectorStream>(
vector->type(), std::nullopt, std::nullopt, arena.get(), numRows, opts);
Scratch scratch;
serializeColumn(vector, ranges, stream.get(), scratch);

PrestoOutputStreamListener listener;
OStreamOutputStream outputStream(output, &listener);
stream->flush(&outputStream);
}

// static
void PrestoVectorSerde::registerVectorSerde() {
auto toByte = [](int32_t number, int32_t bit) {
Expand Down
14 changes: 14 additions & 0 deletions velox/serializers/PrestoSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ namespace facebook::velox::serializer::presto {
/// 2. To serialize a single RowVector, one can use the BatchVectorSerializer
/// returned by createBatchSerializer(). Since it serializes a single RowVector,
/// it tries to preserve the encodings of the input data.
///
/// 3. To serialize data from a vector containing a single element into one
/// column, adhering to PrestoPage's column format and excluding the PrestoPage
/// header, one can use serializeSingleColumn() directly.
class PrestoVectorSerde : public VectorSerde {
public:
// Input options that the serializer recognizes.
Expand Down Expand Up @@ -134,6 +138,16 @@ class PrestoVectorSerde : public VectorSerde {
VectorPtr* result,
const Options* options);

/// This function is used to serialize data from a vector containing a single
/// element into one column that conforms to PrestoPage's column format. The
/// PrestoPage header is not included, so the serialized binary data starts
/// at the column header.
void serializeSingleColumn(
const VectorPtr& vector,
const PrestoOptions& opts,
memory::MemoryPool* pool,
std::ostream* output);

enum class TokenType {
HEADER,
NUM_COLUMNS,
Expand Down

0 comments on commit 8c6f2a3

Please sign in to comment.