Skip to content
Open
20 changes: 20 additions & 0 deletions docs/source/user_guide/read.rst
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,26 @@ Implementation Guidance
- Do not assume identical overflow semantics across C++ and Java; tests should validate acceptable ranges and nullability.
- For timestamp handling, consider precision/range constraints in C++ when interoperating with Java-produced data splits.

Reading Video Columns
---------------------

Paimon C++ reads scalar BLOB columns from v1 ``.video`` files that match only
the six-index layout (29-byte footer). It rejects files whose estimated peak
index memory exceeds 64 MiB.

Reimport five-index or ambiguous files with a Java reader for the source
layout and a current Java writer. Verify row counts, frame ordinals and row
alignment in C++ before switching.

Non-null rows return frame descriptors even with ``blob-as-descriptor=false``.
Open them with ``Blob::FromDescriptor()``. Use ``FrameIndex()`` for the zero-based
frame ordinal, ``NewInputStream()`` for the encoded payload, and ``MoovOffset()``
and ``MoovLength()`` for the optional MP4 ``moov`` range.

Use Java Paimon to write video columns. C++ rejects writes to
``video-frame-field`` columns and rejects frame descriptors in ordinary BLOB
writes.

.. _data-evolution-deletion-vectors:

Deletion Vectors on Data-Evolution Tables
Expand Down
14 changes: 14 additions & 0 deletions include/paimon/data/blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,22 @@ class PAIMON_EXPORT Blob {
/// Gets the URI of the blob.
const std::string& Uri() const;

/// Whether this blob refers to one logical frame in an encoded video payload.
bool IsVideoFrame() const;

/// Zero-based frame ordinal, or -1 for a regular blob.
int64_t FrameIndex() const;

/// Offset and length of the MP4 moov box relative to the encoded video payload.
/// Both values are 0 if absent.
int64_t MoovOffset() const;
int64_t MoovLength() const;

/// Creates an input stream for reading the blob data.
///
/// For a video frame descriptor, the stream contains the encoded video payload.
/// Use `FrameIndex()` to locate the logical frame when decoding the payload.
///
/// @param fs The file system to use for reading.
/// @return A result containing the input stream or an error.
Result<std::unique_ptr<InputStream>> NewInputStream(
Expand Down
1 change: 1 addition & 0 deletions src/paimon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(PAIMON_COMMON_SRCS
common/data/blob_descriptor.cpp
common/data/blob_view.cpp
common/data/blob_view_struct.cpp
common/data/video_frame_descriptor.cpp
common/data/blob_utils.cpp
common/data/columnar/columnar_array.cpp
common/data/columnar/columnar_map.cpp
Expand Down
35 changes: 32 additions & 3 deletions src/paimon/common/data/blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "arrow/c/bridge.h"
#include "paimon/common/data/blob_utils.h"
#include "paimon/common/data/video_frame_descriptor.h"
#include "paimon/common/io/offset_input_stream.h"
#include "paimon/common/utils/arrow/status_utils.h"
#include "paimon/common/utils/path_util.h"
Expand All @@ -40,21 +41,28 @@ class Blob::Impl {
public:
explicit Impl(std::unique_ptr<BlobDescriptor>&& descriptor)
: descriptor_(std::move(descriptor)) {}
explicit Impl(std::unique_ptr<VideoFrameDescriptor>&& video_frame)
: video_frame_(std::move(video_frame)) {}

PAIMON_UNIQUE_PTR<Bytes> SerializeDescriptor(const std::shared_ptr<MemoryPool>& pool) const {
return descriptor_->Serialize(pool);
return video_frame_ ? video_frame_->Serialize(pool) : descriptor_->Serialize(pool);
}

const BlobDescriptor* GetDescriptor() const {
return descriptor_.get();
return video_frame_ ? &video_frame_->Payload() : descriptor_.get();
}

const std::string& Uri() const {
return descriptor_->Uri();
return GetDescriptor()->Uri();
}

const VideoFrameDescriptor* VideoFrame() const {
return video_frame_.get();
}

private:
std::unique_ptr<BlobDescriptor> descriptor_;
std::unique_ptr<VideoFrameDescriptor> video_frame_;
};

Result<std::unique_ptr<Blob>> Blob::FromPath(const std::string& path) {
Expand All @@ -74,6 +82,11 @@ Blob::Blob(std::unique_ptr<Impl>&& impl) : impl_(std::move(impl)) {}
Blob::~Blob() = default;

Result<std::unique_ptr<Blob>> Blob::FromDescriptor(const char* buffer, uint64_t length) {
if (VideoFrameDescriptor::HasMagic(buffer, length)) {
PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<VideoFrameDescriptor> video_frame,
VideoFrameDescriptor::Deserialize(buffer, length));
return std::unique_ptr<Blob>(new Blob(std::make_unique<Impl>(std::move(video_frame))));
}
PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<BlobDescriptor> descriptor,
BlobDescriptor::Deserialize(buffer, length));

Expand All @@ -89,6 +102,22 @@ const std::string& Blob::Uri() const {
return impl_->Uri();
}

bool Blob::IsVideoFrame() const {
return impl_->VideoFrame() != nullptr;
}

int64_t Blob::FrameIndex() const {
return impl_->VideoFrame() ? impl_->VideoFrame()->FrameIndex() : -1;
}

int64_t Blob::MoovOffset() const {
return impl_->VideoFrame() ? impl_->VideoFrame()->MoovOffset() : 0;
}

int64_t Blob::MoovLength() const {
return impl_->VideoFrame() ? impl_->VideoFrame()->MoovLength() : 0;
}

Result<std::unique_ptr<InputStream>> Blob::NewInputStream(
const std::shared_ptr<FileSystem>& fs) const {
if (fs == nullptr) {
Expand Down
3 changes: 3 additions & 0 deletions src/paimon/common/data/blob_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class BlobDefs {
/// Metadata value identifying a Paimon Blob extension type field.
static constexpr char kExtensionTypeValue[] = "paimon.type.blob";

/// Java Paimon table option naming scalar BLOB fields stored as video frames.
static constexpr char kVideoFrameFieldOption[] = "video-frame-field";

/// A bin_length value of -1 in the index indicates a null blob entry.
static constexpr int64_t kNullBinLength = -1;
/// A bin_length value of -2 in the index indicates a placeholder blob entry, written by
Expand Down
16 changes: 15 additions & 1 deletion src/paimon/common/data/blob_descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <utility>

#include "fmt/format.h"
#include "paimon/common/data/video_frame_descriptor.h"
#include "paimon/common/io/memory_segment_output_stream.h"
#include "paimon/common/memory/memory_segment_utils.h"
#include "paimon/io/byte_array_input_stream.h"
Expand Down Expand Up @@ -73,6 +74,12 @@ PAIMON_UNIQUE_PTR<Bytes> BlobDescriptor::Serialize(const std::shared_ptr<MemoryP

Result<std::unique_ptr<BlobDescriptor>> BlobDescriptor::Deserialize(const char* buffer,
uint64_t size) {
if (buffer == nullptr || size == 0) {
return Status::Invalid("empty blob descriptor");
}
if (VideoFrameDescriptor::HasMagic(buffer, size)) {
return Status::Invalid("video frame descriptors must be opened with Blob::FromDescriptor");
}
auto input_stream = std::make_shared<ByteArrayInputStream>(buffer, size);
DataInputStream in(std::move(input_stream));
in.SetOrder(ByteOrder::PAIMON_LITTLE_ENDIAN);
Expand All @@ -91,6 +98,13 @@ Result<std::unique_ptr<BlobDescriptor>> BlobDescriptor::Deserialize(const char*
}
}
PAIMON_ASSIGN_OR_RAISE(int32_t uri_length, in.ReadValue<int32_t>());
PAIMON_ASSIGN_OR_RAISE(int64_t uri_start, in.GetPos());
constexpr uint64_t kTailSize = 2 * sizeof(int64_t);
const uint64_t remaining = size - uri_start;
if (uri_length < 0 || remaining < kTailSize ||
static_cast<uint64_t>(uri_length) > remaining - kTailSize) {
return Status::Invalid("invalid blob descriptor length");
}
std::string uri(uri_length, '\0');
PAIMON_RETURN_NOT_OK(in.Read(uri.data(), uri.size()));
PAIMON_ASSIGN_OR_RAISE(int64_t offset, in.ReadValue<int64_t>());
Expand All @@ -99,7 +113,7 @@ Result<std::unique_ptr<BlobDescriptor>> BlobDescriptor::Deserialize(const char*
}

Result<bool> BlobDescriptor::IsBlobDescriptor(const char* buffer, uint64_t size) {
if (size < kMinDescriptorLength) {
if (buffer == nullptr || size < kMinDescriptorLength) {
return false;
}
auto input_stream = std::make_shared<ByteArrayInputStream>(buffer, size);
Expand Down
2 changes: 1 addition & 1 deletion src/paimon/common/data/blob_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ bool BlobUtils::IsBlobMetadata(const std::shared_ptr<const arrow::KeyValueMetada
}

bool BlobUtils::IsBlobFile(const std::string& file_name) {
return StringUtils::EndsWith(file_name, ".blob");
return StringUtils::EndsWith(file_name, ".blob") || StringUtils::EndsWith(file_name, ".video");
}

std::shared_ptr<arrow::Field> BlobUtils::ToArrowField(
Expand Down
111 changes: 111 additions & 0 deletions src/paimon/common/data/video_frame_descriptor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "paimon/common/data/video_frame_descriptor.h"

#include <algorithm>
#include <limits>
#include <string_view>
#include <utility>

#include "paimon/common/io/memory_segment_output_stream.h"
#include "paimon/common/memory/memory_segment_utils.h"
#include "paimon/io/byte_array_input_stream.h"
#include "paimon/io/data_input_stream.h"

namespace paimon {
namespace {

constexpr int8_t kVersion = 1;
constexpr std::string_view kMagic = "MRFOEDIV"; // 0x564944454F46524D in little endian
constexpr uint64_t kHeaderSize = sizeof(kVersion) + kMagic.size();
constexpr uint64_t kFixedSize = kHeaderSize + sizeof(int32_t) + 5 * sizeof(int64_t);

} // namespace

VideoFrameDescriptor::VideoFrameDescriptor(std::unique_ptr<BlobDescriptor> payload,
int64_t frame_index, int64_t moov_offset,
int64_t moov_length)
: payload_(std::move(payload)),
frame_index_(frame_index),
moov_offset_(moov_offset),
moov_length_(moov_length) {}

Result<std::unique_ptr<VideoFrameDescriptor>> VideoFrameDescriptor::Create(
const std::string& uri, int64_t offset, int64_t length, int64_t frame_index,
int64_t moov_offset, int64_t moov_length) {
if (frame_index < 0 || moov_offset < 0 || moov_length < 0 ||
uri.size() > std::numeric_limits<int32_t>::max() ||
(moov_length == 0 ? moov_offset != 0
: moov_offset > length || moov_length > length - moov_offset)) {
return Status::Invalid("invalid video frame descriptor range");
}
PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<BlobDescriptor> payload,
BlobDescriptor::Create(uri, offset, length));
return std::unique_ptr<VideoFrameDescriptor>(
new VideoFrameDescriptor(std::move(payload), frame_index, moov_offset, moov_length));
}

bool VideoFrameDescriptor::HasMagic(const char* buffer, uint64_t size) {
return buffer != nullptr && size >= kHeaderSize &&
std::string_view(buffer + sizeof(kVersion), kMagic.size()) == kMagic;
}

Result<std::unique_ptr<VideoFrameDescriptor>> VideoFrameDescriptor::Deserialize(const char* buffer,
uint64_t size) {
if (size < kFixedSize || !HasMagic(buffer, size) || buffer[0] != kVersion) {
return Status::Invalid("invalid or unsupported video frame descriptor header");
}
DataInputStream input(std::make_shared<ByteArrayInputStream>(buffer, size));
input.SetOrder(ByteOrder::PAIMON_LITTLE_ENDIAN);
PAIMON_RETURN_NOT_OK(input.Seek(kHeaderSize));
PAIMON_ASSIGN_OR_RAISE(int32_t uri_length, input.ReadValue<int32_t>());
if (uri_length < 0 || static_cast<uint64_t>(uri_length) != size - kFixedSize) {
return Status::Invalid("invalid video frame descriptor length");
}
std::string uri(uri_length, '\0');
PAIMON_RETURN_NOT_OK(input.Read(uri.data(), uri_length));
PAIMON_ASSIGN_OR_RAISE(int64_t offset, input.ReadValue<int64_t>());
PAIMON_ASSIGN_OR_RAISE(int64_t length, input.ReadValue<int64_t>());
PAIMON_ASSIGN_OR_RAISE(int64_t frame_index, input.ReadValue<int64_t>());
PAIMON_ASSIGN_OR_RAISE(int64_t moov_offset, input.ReadValue<int64_t>());
PAIMON_ASSIGN_OR_RAISE(int64_t moov_length, input.ReadValue<int64_t>());
return Create(uri, offset, length, frame_index, moov_offset, moov_length);
}

PAIMON_UNIQUE_PTR<Bytes> VideoFrameDescriptor::Serialize(
const std::shared_ptr<MemoryPool>& pool) const {
const std::string& uri = payload_->Uri();
const int32_t segment_size = std::min<uint64_t>(
kFixedSize + uri.size(), MemorySegmentOutputStream::DEFAULT_SEGMENT_SIZE);
MemorySegmentOutputStream output(segment_size, pool);
output.SetOrder(ByteOrder::PAIMON_LITTLE_ENDIAN);
output.WriteValue<int8_t>(kVersion);
output.Write(kMagic.data(), kMagic.size());
output.WriteValue<int32_t>(uri.size());
output.Write(uri.data(), uri.size());
output.WriteValue<int64_t>(payload_->Offset());
output.WriteValue<int64_t>(payload_->Length());
output.WriteValue<int64_t>(frame_index_);
output.WriteValue<int64_t>(moov_offset_);
output.WriteValue<int64_t>(moov_length_);
return MemorySegmentUtils::CopyToBytes(output.Segments(), 0, output.CurrentSize(), pool.get());
}

} // namespace paimon
72 changes: 72 additions & 0 deletions src/paimon/common/data/video_frame_descriptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#pragma once

#include <cstdint>
#include <memory>
#include <string>

#include "paimon/data/blob_descriptor.h"
#include "paimon/visibility.h"

namespace paimon {

/// Wire representation of a frame in an encoded video. The moov range is relative to its payload.
class PAIMON_EXPORT VideoFrameDescriptor {
public:
static Result<std::unique_ptr<VideoFrameDescriptor>> Create(const std::string& uri,
int64_t offset, int64_t length,
int64_t frame_index,
int64_t moov_offset,
int64_t moov_length);

static bool HasMagic(const char* buffer, uint64_t size);
static Result<std::unique_ptr<VideoFrameDescriptor>> Deserialize(const char* buffer,
uint64_t size);

PAIMON_UNIQUE_PTR<Bytes> Serialize(const std::shared_ptr<MemoryPool>& pool) const;

const BlobDescriptor& Payload() const {
return *payload_;
}

int64_t FrameIndex() const {
return frame_index_;
}

int64_t MoovOffset() const {
return moov_offset_;
}

int64_t MoovLength() const {
return moov_length_;
}

private:
VideoFrameDescriptor(std::unique_ptr<BlobDescriptor> payload, int64_t frame_index,
int64_t moov_offset, int64_t moov_length);

std::unique_ptr<BlobDescriptor> payload_;
int64_t frame_index_;
int64_t moov_offset_;
int64_t moov_length_;
};

} // namespace paimon
6 changes: 3 additions & 3 deletions src/paimon/common/reader/data_file_reader_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ namespace {
// files (FormatTable::ParseFormat accepts only parquet/orc), so this only concerns the managed
// table path.
bool FormatSupportsPrefetch(const std::string& format_identifier) {
return format_identifier != "blob" && format_identifier != "avro" &&
format_identifier != "lance" && format_identifier != "mosaic" &&
format_identifier != "vortex";
return format_identifier != "blob" && format_identifier != "video" &&
format_identifier != "avro" && format_identifier != "lance" &&
format_identifier != "mosaic" && format_identifier != "vortex";
}

} // namespace
Expand Down
Loading
Loading