|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::spec::manifest_common::FileKind; |
| 19 | +use crate::spec::IndexFileMeta; |
| 20 | +use serde::{Deserialize, Serialize}; |
| 21 | +use std::fmt::{Display, Formatter}; |
| 22 | + |
| 23 | +/// Manifest entry for index file. |
| 24 | +/// |
| 25 | +/// Impl Reference: <https://github.com/apache/paimon/blob/release-0.8.2/paimon-core/src/main/java/org/apache/paimon/manifest/IndexManifestEntry.java> |
| 26 | +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 27 | +pub struct IndexManifestEntry { |
| 28 | + #[serde(rename = "_KIND")] |
| 29 | + pub kind: FileKind, |
| 30 | + |
| 31 | + #[serde(rename = "_PARTITION", with = "serde_bytes")] |
| 32 | + pub partition: Vec<u8>, |
| 33 | + |
| 34 | + #[serde(rename = "_BUCKET")] |
| 35 | + pub bucket: i32, |
| 36 | + |
| 37 | + #[serde(flatten)] |
| 38 | + pub index_file: IndexFileMeta, |
| 39 | + |
| 40 | + #[serde(rename = "_VERSION")] |
| 41 | + pub version: i32, |
| 42 | +} |
| 43 | + |
| 44 | +impl Display for IndexManifestEntry { |
| 45 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 46 | + write!( |
| 47 | + f, |
| 48 | + "IndexManifestEntry{{kind={:?}, partition={:?}, bucket={}, index_file={}}}", |
| 49 | + self.kind, self.partition, self.bucket, self.index_file, |
| 50 | + ) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[cfg(test)] |
| 55 | +mod tests { |
| 56 | + use indexmap::IndexMap; |
| 57 | + |
| 58 | + use super::*; |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn test_read_index_manifest_file() { |
| 62 | + let workdir = |
| 63 | + std::env::current_dir().unwrap_or_else(|err| panic!("current_dir must exist: {err}")); |
| 64 | + let path = workdir |
| 65 | + .join("tests/fixtures/manifest/index-manifest-85cc6729-f5af-431a-a1c3-ef45319328fb-0"); |
| 66 | + let source = std::fs::read(path.to_str().unwrap()).unwrap(); |
| 67 | + let mut reader = |
| 68 | + serde_avro_fast::object_container_file_encoding::Reader::from_slice(source.as_slice()) |
| 69 | + .unwrap(); |
| 70 | + let res: Vec<_> = reader |
| 71 | + .deserialize::<IndexManifestEntry>() |
| 72 | + .collect::<Result<_, _>>() |
| 73 | + .unwrap(); |
| 74 | + assert_eq!( |
| 75 | + res, |
| 76 | + vec![ |
| 77 | + IndexManifestEntry { |
| 78 | + version: 1, |
| 79 | + kind: FileKind::Add, |
| 80 | + partition: vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 81 | + bucket: 0, |
| 82 | + index_file: IndexFileMeta { |
| 83 | + index_type: "HASH".into(), |
| 84 | + file_name: "index-a984b43a-c3fb-40b4-ad29-536343c239a6-0".into(), |
| 85 | + file_size: 16, |
| 86 | + row_count: 4, |
| 87 | + deletion_vectors_ranges: None, |
| 88 | + } |
| 89 | + }, |
| 90 | + IndexManifestEntry { |
| 91 | + version: 1, |
| 92 | + kind: FileKind::Add, |
| 93 | + partition: vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 94 | + bucket: 0, |
| 95 | + index_file: IndexFileMeta { |
| 96 | + index_type: "DELETION_VECTORS".into(), |
| 97 | + file_name: "index-3f0986c5-4398-449b-be82-95f019d7a748-0".into(), |
| 98 | + file_size: 33, |
| 99 | + row_count: 1, |
| 100 | + deletion_vectors_ranges: Some(IndexMap::from([( |
| 101 | + "data-9b76122c-6bb5-4952-a946-b5bce29694a1-0.orc".into(), |
| 102 | + (1, 24) |
| 103 | + )])), |
| 104 | + } |
| 105 | + } |
| 106 | + ] |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn test_single_object_serde() { |
| 112 | + let sample = IndexManifestEntry { |
| 113 | + version: 1, |
| 114 | + kind: FileKind::Delete, |
| 115 | + partition: vec![0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6], |
| 116 | + bucket: 0, |
| 117 | + index_file: IndexFileMeta { |
| 118 | + index_type: "DELETION_VECTORS".into(), |
| 119 | + file_name: "test1".into(), |
| 120 | + file_size: 33, |
| 121 | + row_count: 1, |
| 122 | + deletion_vectors_ranges: Some(IndexMap::from([("test1".into(), (1, 24))])), |
| 123 | + }, |
| 124 | + }; |
| 125 | + |
| 126 | + let schema: serde_avro_fast::Schema = r#"["null", { |
| 127 | + "type": "record", |
| 128 | + "name": "org.apache.paimon.avro.generated.record", |
| 129 | + "fields": [ |
| 130 | + {"name": "_VERSION", "type": "int"}, |
| 131 | + {"name": "_KIND", "type": "int"}, |
| 132 | + {"name": "_PARTITION", "type": "bytes"}, |
| 133 | + {"name": "_BUCKET", "type": "int"}, |
| 134 | + {"name": "_INDEX_TYPE", "type": "string"}, |
| 135 | + {"name": "_FILE_NAME", "type": "string"}, |
| 136 | + {"name": "_FILE_SIZE", "type": "long"}, |
| 137 | + {"name": "_ROW_COUNT", "type": "long"}, |
| 138 | + { |
| 139 | + "default": null, |
| 140 | + "name": "_DELETIONS_VECTORS_RANGES", |
| 141 | + "type": ["null", { |
| 142 | + "type": "array", |
| 143 | + "items": ["null", { |
| 144 | + "type": "record", |
| 145 | + "name": "org.apache.paimon.avro.generated.record__DELETIONS_VECTORS_RANGES", |
| 146 | + "fields": [ |
| 147 | + {"name": "f0", "type": "string"}, |
| 148 | + {"name": "f1", "type": "int"}, |
| 149 | + {"name": "f2", "type": "int"} |
| 150 | + ] |
| 151 | + }] |
| 152 | + }] |
| 153 | + } |
| 154 | + ] |
| 155 | + }]"# |
| 156 | + .parse().unwrap(); |
| 157 | + |
| 158 | + let serializer_config = &mut serde_avro_fast::ser::SerializerConfig::new(&schema); |
| 159 | + let encoded = serde_avro_fast::to_single_object_vec(&sample, serializer_config).unwrap(); |
| 160 | + let decoded: IndexManifestEntry = |
| 161 | + serde_avro_fast::from_single_object_slice(encoded.as_slice(), &schema).unwrap(); |
| 162 | + assert_eq!(sample, decoded); |
| 163 | + } |
| 164 | +} |
0 commit comments