XiaoHongbo-Hope commented on code in PR #9461: URL: https://github.com/apache/paimon/pull/9461#discussion_r3885897877
########## 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: payloadWriter.write() may return zero, but VideoFileMeta rejects non-positive lengths. Python reject empty payloads here. ########## 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: PayloadWriter.write() may return zero, but VideoFileMeta rejects non-positive lengths. Python reject empty payloads here. -- 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]
