You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@paimon.apache.org by "JingsongLi (via GitHub)" <gi...@apache.org> on 2026/04/23 09:58:36 UTC

[PR] feat: add custom Avro OCF reader for manifest parsing with filtered decoding [paimon-rust]

JingsongLi opened a new pull request, #281:
URL: https://github.com/apache/paimon-rust/pull/281

   <!--
   Thank you very much for contributing to Paimon Rust - we are happy that you want to help us improve it. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [GitHub issue](https://github.com/apache/paimon-rust/issues). Exceptions are made for typos in documentation or comments, which need no issue.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
   
     - Make sure that the change passes the automated tests, i.e., `cargo test` passes.
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
   
   **(The sections below can be removed for hotfixes or typos)**
   -->
   
   ### Purpose
   
   <!-- Linking this pull request to the issue -->
   Replace apache-avro's read path (Value intermediate representation) with a zero-copy custom decoder that reads Avro binary directly into target structs. Key optimizations:
   
   - Custom AvroCursor for zero-copy Avro binary primitive decoding
   - OCF parser with snappy/deflate/zstd decompression support
   - Writer schema parsing with field index mapping for compatibility
   - Two-pass filtered ManifestEntry decoding: lightweight fields first, skip expensive DataFileMeta for filtered-out entries
   - SharedSchemaCache for cross-task schema reuse
   - Zero-copy entry consumption (into_identifier, into_parts)
   - Parallel base+delta manifest list reads via futures::try_join!
   - Remove redundant exists() checks before file reads
   
   <!-- What is the purpose of the change -->
   
   ### Brief change log
   
   <!-- Please describe the changes made in this pull request and explain how they address the issue -->
   
   ### Tests
   
   <!-- List unit tests or integration cases to verify this change -->
   
   ### API and Format
   
   <!-- Does this change affect API or storage format -->
   
   ### Documentation
   
   <!-- Does this change introduce a new feature or require documentation updates -->
   


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add custom Avro OCF reader for manifest parsing with filtered decoding [paimon-rust]

Posted by "XiaoHongbo-Hope (via GitHub)" <gi...@apache.org>.
XiaoHongbo-Hope commented on code in PR #281:
URL: https://github.com/apache/paimon-rust/pull/281#discussion_r3136882918


##########
crates/paimon/src/spec/avro/ocf.rs:
##########
@@ -0,0 +1,299 @@
+// 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.
+
+use super::cursor::AvroCursor;
+use super::decode::neg_count_to_usize;
+use crate::Error;
+use std::borrow::Cow;
+use std::collections::HashMap;
+
+const AVRO_MAGIC: &[u8; 4] = b"Obj\x01";
+const SYNC_MARKER_LEN: usize = 16;
+
+/// A decoded Avro OCF header.
+pub struct OcfHeader {
+    pub schema_json: String,
+    pub codec: OcfCodec,
+    pub sync_marker: [u8; SYNC_MARKER_LEN],
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum OcfCodec {
+    Null,
+    Snappy,
+    Zstandard,
+}
+
+/// A single data block from the OCF.
+pub struct OcfBlock<'a> {
+    pub object_count: usize,
+    pub data: Cow<'a, [u8]>,
+}
+
+/// Streaming iterator over OCF blocks with lazy decompression and reusable decoder state.
+pub struct OcfBlockIter<'a> {
+    cursor: AvroCursor<'a>,
+    codec: OcfCodec,
+    sync_marker: [u8; SYNC_MARKER_LEN],
+    snappy_decoder: snap::raw::Decoder,
+}
+
+impl<'a> OcfBlockIter<'a> {
+    fn new(cursor: AvroCursor<'a>, codec: OcfCodec, sync_marker: [u8; SYNC_MARKER_LEN]) -> Self {
+        Self {
+            cursor,
+            codec,
+            sync_marker,
+            snappy_decoder: snap::raw::Decoder::new(),
+        }
+    }
+
+    pub fn next_block(&mut self) -> crate::Result<Option<OcfBlock<'a>>> {
+        if self.cursor.remaining() == 0 {
+            return Ok(None);
+        }
+
+        let raw_object_count = self.cursor.read_long()?;
+        if raw_object_count < 0 {
+            return Err(Error::UnexpectedError {
+                message: format!("avro ocf: negative object count: {raw_object_count}"),
+                source: None,
+            });
+        }
+        let object_count = raw_object_count as usize;
+        let raw_compressed_size = self.cursor.read_long()?;
+        if raw_compressed_size < 0 {
+            return Err(Error::UnexpectedError {
+                message: format!("avro ocf: negative compressed size: {raw_compressed_size}"),
+                source: None,
+            });
+        }
+        let compressed_size = raw_compressed_size as usize;
+        let compressed_data = self.cursor.read_fixed(compressed_size)?;
+
+        let data = self.decompress(compressed_data)?;
+
+        let block_sync = self.cursor.read_fixed(SYNC_MARKER_LEN)?;
+        if block_sync != self.sync_marker {
+            return Err(Error::UnexpectedError {
+                message: "avro ocf: sync marker mismatch".into(),
+                source: None,
+            });
+        }
+
+        Ok(Some(OcfBlock { object_count, data }))
+    }
+
+    fn decompress(&mut self, data: &'a [u8]) -> crate::Result<Cow<'a, [u8]>> {
+        match self.codec {
+            OcfCodec::Null => Ok(Cow::Borrowed(data)),
+            OcfCodec::Snappy => {
+                if data.len() < 4 {
+                    return Err(Error::UnexpectedError {
+                        message: "avro ocf: snappy block too short for CRC".into(),
+                        source: None,
+                    });
+                }
+                let compressed = &data[..data.len() - 4];
+                let expected_crc = u32::from_be_bytes(data[data.len() - 4..].try_into().unwrap());
+                let decompressed = self
+                    .snappy_decoder
+                    .decompress_vec(compressed)
+                    .map_err(|e| Error::UnexpectedError {
+                        message: format!("avro ocf: snappy decompression failed: {e}"),
+                        source: None,
+                    })?;
+                let actual_crc = crc32fast::hash(&decompressed);
+                if actual_crc != expected_crc {
+                    return Err(Error::UnexpectedError {
+                        message: format!(
+                            "avro ocf: snappy CRC32C mismatch: expected {expected_crc:#010x}, got {actual_crc:#010x}"
+                        ),
+                        source: None,
+                    });
+                }
+                Ok(Cow::Owned(decompressed))
+            }
+            OcfCodec::Zstandard => {
+                let decompressed =
+                    zstd::stream::decode_all(data).map_err(|e| Error::UnexpectedError {
+                        message: format!("avro ocf: zstd decompression failed: {e}"),
+                        source: None,
+                    })?;
+                Ok(Cow::Owned(decompressed))
+            }
+        }
+    }
+}
+
+/// Parse an Avro OCF header and return a streaming block iterator.
+pub fn parse_ocf_streaming(bytes: &[u8]) -> crate::Result<(OcfHeader, OcfBlockIter<'_>)> {
+    let mut cursor = AvroCursor::new(bytes);
+
+    let magic = cursor.read_fixed(4)?;
+    if magic != AVRO_MAGIC {
+        return Err(Error::UnexpectedError {
+            message: "avro ocf: invalid magic bytes".into(),
+            source: None,
+        });
+    }
+
+    let meta = read_avro_map(&mut cursor)?;
+
+    let schema_json = meta
+        .get("avro.schema")
+        .ok_or_else(|| Error::UnexpectedError {
+            message: "avro ocf: missing avro.schema in header".into(),
+            source: None,
+        })?
+        .clone();
+
+    let codec = match meta.get("avro.codec").map(|s| s.as_str()) {
+        None | Some("null") => OcfCodec::Null,
+        Some("snappy") => OcfCodec::Snappy,
+        Some("zstandard") => OcfCodec::Zstandard,
+        Some(other) => {
+            return Err(Error::UnexpectedError {
+                message: format!("avro ocf: unsupported codec: {other}"),
+                source: None,
+            });
+        }
+    };

Review Comment:
   This doesn't handle `deflate`. I think we can support later, not blocker.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add custom Avro OCF reader for manifest parsing with filtered decoding [paimon-rust]

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi closed pull request #281: feat: add custom Avro OCF reader for manifest parsing with filtered decoding
URL: https://github.com/apache/paimon-rust/pull/281


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add custom Avro OCF reader for manifest parsing with filtered decoding [paimon-rust]

Posted by "XiaoHongbo-Hope (via GitHub)" <gi...@apache.org>.
XiaoHongbo-Hope commented on PR #281:
URL: https://github.com/apache/paimon-rust/pull/281#issuecomment-4312272864

   +1


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat: add custom Avro OCF reader for manifest parsing with filtered decoding [paimon-rust]

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi merged PR #281:
URL: https://github.com/apache/paimon-rust/pull/281


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org