JingsongLi commented on code in PR #9461:
URL: https://github.com/apache/paimon/pull/9461#discussion_r3886666392
##########
paimon-python/pypaimon/read/reader/format_blob_reader.py:
##########
@@ -290,6 +306,17 @@ def close(self):
self._input_stream = None
def _read_index(self) -> None:
+ if self._is_video:
+ self._video_meta = VideoFileMeta(
+ self._input_stream, self._file_size
+ )
+ # BlobFallbackBatchReader uses this public list for the selected
+ # logical row count. Video rows have no ordinary BLOB lengths, so
+ # retain count-only sentinels and let VideoFileMeta resolve them.
+ self.blob_lengths = [0] * self._video_meta.record_count
+ self.blob_offsets = [0] * self._video_meta.record_count
+ return
Review Comment:
Fixed in 1c0708dad0. FormatBlobReader now exposes record_count directly from
VideoFileMeta for .video files, BlobFallbackBatchReader uses it, and the two
O(frame count) sentinel lists are no longer allocated. Added a regression test
for selected video rows.
##########
paimon-format/src/main/java/org/apache/paimon/format/blob/VideoFormatWriter.java:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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.
+ */
+
+package org.apache.paimon.format.blob;
+
+import org.apache.paimon.data.Blob;
+import org.apache.paimon.data.BlobDescriptor;
+import org.apache.paimon.data.BlobFetchMetricReporter;
+import org.apache.paimon.data.BlobPlaceholder;
+import org.apache.paimon.data.BlobRef;
+import org.apache.paimon.data.InternalRow;
+import org.apache.paimon.data.VideoFrameDescriptor;
+import org.apache.paimon.format.FileAwareFormatWriter;
+import org.apache.paimon.format.FormatWriter;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.fs.PositionOutputStream;
+import org.apache.paimon.types.RowType;
+import org.apache.paimon.utils.DeltaVarintCompressor;
+import org.apache.paimon.utils.LongArrayList;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.apache.paimon.utils.Preconditions.checkArgument;
+import static org.apache.paimon.utils.StreamUtils.intToLittleEndian;
+
+/**
+ * {@link FormatWriter} for a Paimon video pack.
+ *
+ * <p>The data region concatenates complete encoded-video payloads without
per-payload wrappers.
+ * Logical frame rows are represented by compact contiguous runs. A run points
to one physical video
+ * and stores its first frame; subsequent rows increment the frame ordinal by
one.
+ */
+public class VideoFormatWriter implements FileAwareFormatWriter {
+
+ public static final byte VERSION = 1;
+ public static final int MAGIC_NUMBER = 0x4F454449; // "IDEO" in little
endian
+ public static final long NULL_REFERENCE = -1L;
+ public static final long PLACEHOLDER_REFERENCE = -2L;
+ public static final int FILE_FOOTER_LENGTH = Integer.BYTES * 5 +
Byte.BYTES;
+
+ private final PositionOutputStream out;
+ private final RawVideoPayloadWriter payloadWriter;
+ private final LongArrayList physicalVideoLengths;
+ private final LongArrayList runLengths;
+ private final LongArrayList runReferences;
+ private final LongArrayList runFirstFrames;
+ private final Map<BlobDescriptor, Integer> physicalVideos;
+
+ private long currentRunLength;
+ private long currentRunReference;
+ private long currentRunFirstFrame;
+ private long currentRunLastFrame;
+ private boolean closed;
+
+ public VideoFormatWriter(
+ PositionOutputStream out,
+ RowType type,
+ boolean writeNullOnMissingFile,
+ boolean writeNullOnFetchFailure,
+ BlobFetchMetricReporter blobFetchMetricReporter,
+ int copyBufferSize) {
+ checkArgument(type.getFieldCount() == 1, "VideoFormatWriter only
supports one field.");
+ this.out = out;
+ this.payloadWriter =
+ new RawVideoPayloadWriter(
+ out,
+ type.getFieldNames().get(0),
+ writeNullOnMissingFile,
+ writeNullOnFetchFailure,
+ blobFetchMetricReporter,
+ copyBufferSize);
+ this.physicalVideoLengths = new LongArrayList(16);
+ this.runLengths = new LongArrayList(16);
+ this.runReferences = new LongArrayList(16);
+ this.runFirstFrames = new LongArrayList(16);
+ this.physicalVideos = new HashMap<>();
+ }
+
+ @Override
+ public void setFile(Path file) {
+ payloadWriter.setFile(file);
+ }
+
+ @Override
+ public boolean deleteFileUponAbort() {
+ return true;
+ }
+
+ @Override
+ public void addElement(InternalRow element) throws IOException {
+ checkArgument(element.getFieldCount() == 1, "VideoFormatWriter only
supports one field.");
+ if (element.isNullAt(0)) {
+ append(NULL_REFERENCE, 0);
+ return;
+ }
+
+ Blob blob = element.getBlob(0);
+ if (blob == BlobPlaceholder.INSTANCE) {
+ append(PLACEHOLDER_REFERENCE, 0);
+ return;
+ }
+ checkArgument(
+ blob != null
+ && blob.getClass() == BlobRef.class
+ && blob.toDescriptor() instanceof VideoFrameDescriptor,
+ "Video fields require an exact BlobRef containing a
VideoFrameDescriptor.");
+
+ VideoFrameDescriptor frame = (VideoFrameDescriptor)
blob.toDescriptor();
+ BlobDescriptor payload = frame.payloadDescriptor();
+ Integer ordinal = physicalVideos.get(payload);
+ if (ordinal == null) {
+ long length = payloadWriter.write(element);
+ if (length == BlobFormatWriter.NULL_LENGTH) {
+ append(NULL_REFERENCE, 0);
+ return;
+ }
+ ordinal = physicalVideoLengths.size();
+ physicalVideoLengths.add(length);
Review Comment:
Fixed in 99060efc38. RawVideoPayloadWriter now rejects zero-length encoded
video payloads, matching the Python writer and VideoFileMeta contract. Added a
Java regression test.
##########
paimon-python/pypaimon/multimodal/video.py:
##########
@@ -0,0 +1,194 @@
+# 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.
+
+"""PyTorch DataLoader helpers for descriptor-backed video frame rows."""
+
+import os
+from collections import OrderedDict
+from collections.abc import Mapping
+
+from pypaimon.table.row.blob import Blob, VideoFrameDescriptor
+
+
+class VideoFrameCollator:
+ """Decode frame rows in a DataLoader worker while reusing video sessions.
+
+ ``decoder_factory`` receives a seekable stream containing exactly one
+ descriptor-backed video. ``decode_fn`` receives the cached decoder, the
+ frame ordinal embedded in the descriptor, and one row dictionary. This
+ keeps Paimon independent of a particular video codec library while allowing
+ PyAV, TorchCodec, or an application decoder to be plugged in.
+
+ The cache is process-local and keyed by physical video payload identity.
+ ``collate_fn`` defaults to PyTorch's ``default_collate`` and may be
replaced
+ for decoders that already return batched objects.
+ """
+
+ def __init__(
+ self,
+ table,
+ *,
+ video_column,
+ decoder_factory,
+ decode_fn,
+ output_column="frame",
+ max_open_videos=8,
+ collate_fn=None):
+ if not video_column:
+ raise ValueError("video_column is required.")
+ if not callable(decoder_factory):
+ raise ValueError("decoder_factory must be callable.")
+ if not callable(decode_fn):
+ raise ValueError("decode_fn must be callable.")
+ if (
+ isinstance(max_open_videos, bool)
+ or not isinstance(max_open_videos, int)
+ or max_open_videos <= 0
+ ):
+ raise ValueError("max_open_videos must be a positive int.")
+ if collate_fn is not None and not callable(collate_fn):
+ raise ValueError("collate_fn must be callable or None.")
+
+ raw_table = getattr(table, "raw_table", table)
+ file_io = getattr(raw_table, "file_io", None)
+ if file_io is None:
+ raise ValueError("table must provide raw_table.file_io or
file_io.")
+
+ self.file_io = file_io
+ self.video_column = video_column
+ self.decoder_factory = decoder_factory
+ self.decode_fn = decode_fn
+ self.output_column = output_column
+ self.max_open_videos = max_open_videos
+ self.collate_fn = collate_fn
+ self._decoders = OrderedDict()
+ self._owner_pid = os.getpid()
+
+ def __call__(self, rows):
+ self._ensure_process_local_cache()
+ single_row = isinstance(rows, Mapping)
+ input_rows = [rows] if single_row else list(rows)
+ decoded_rows = [self._decode_row(row) for row in input_rows]
Review Comment:
Agreed. I kept the current collator API unchanged and will leave batch/range
decode optimization to a focused follow-up PR so this format PR stays scoped.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]