|
| 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 std::collections::HashMap; |
| 19 | + |
| 20 | +use iceberg::io::{FileRead, InputFile}; |
| 21 | +use iceberg::Result; |
| 22 | + |
| 23 | +use crate::compression::CompressionCodec; |
| 24 | +use crate::metadata::BlobMetadata; |
| 25 | + |
| 26 | +/// A serialized form of a "compact" Theta sketch produced by the Apache DataSketches library. |
| 27 | +pub const APACHE_DATASKETCHES_THETA_V1: &str = "apache-datasketches-theta-v1"; |
| 28 | + |
| 29 | +/// The blob |
| 30 | +#[derive(Debug)] |
| 31 | +pub struct Blob { |
| 32 | + /// See Blob types: https://iceberg.apache.org/puffin-spec/#blob-types |
| 33 | + pub r#type: String, |
| 34 | + /// List of field IDs the blob was computed for; the order of items is used to compute sketches stored in the blob. |
| 35 | + pub input_fields: Vec<i32>, |
| 36 | + /// ID of the Iceberg table's snapshot the blob was computed from |
| 37 | + pub snapshot_id: i64, |
| 38 | + /// Sequence number of the Iceberg table's snapshot the blob was computed from |
| 39 | + pub sequence_number: i64, |
| 40 | + /// The actual blob data |
| 41 | + pub data: Vec<u8>, |
| 42 | + /// The compression codec to use to compress the blob data at write time |
| 43 | + pub requested_compression_codec: Option<CompressionCodec>, |
| 44 | + /// Arbitrary meta-information about the blob |
| 45 | + pub properties: HashMap<String, String>, |
| 46 | +} |
| 47 | + |
| 48 | +impl Blob { |
| 49 | + /// Returns blob from a Puffin file |
| 50 | + pub async fn read(input_file: &InputFile, blob_metadata: &BlobMetadata) -> Result<Blob> { |
| 51 | + let file_read = input_file.reader().await?; |
| 52 | + let start = blob_metadata.offset; |
| 53 | + let end = start + u64::try_from(blob_metadata.length)?; |
| 54 | + let bytes = file_read.read(start..end).await?.to_vec(); |
| 55 | + let data = blob_metadata.compression_codec.decompress(bytes)?; |
| 56 | + Ok(Blob { |
| 57 | + r#type: blob_metadata.r#type.clone(), |
| 58 | + input_fields: blob_metadata.input_fields.clone(), |
| 59 | + snapshot_id: blob_metadata.snapshot_id, |
| 60 | + sequence_number: blob_metadata.sequence_number, |
| 61 | + data, |
| 62 | + requested_compression_codec: Some(blob_metadata.compression_codec), |
| 63 | + properties: blob_metadata.properties.clone(), |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(test)] |
| 69 | +mod tests { |
| 70 | + |
| 71 | + use crate::test_utils::{ |
| 72 | + read_blob_as_utf8_string, read_test_file, METRIC_BLOB_0_DATA, METRIC_BLOB_1_DATA, |
| 73 | + }; |
| 74 | + |
| 75 | + #[tokio::test] |
| 76 | + async fn test_read_rust_generated_uncompressed_metric_data() { |
| 77 | + let input_file = read_test_file("v1/rust-generated/sample-metric-data-uncompressed.bin"); |
| 78 | + assert_eq!( |
| 79 | + read_blob_as_utf8_string(&input_file, 0).await, |
| 80 | + METRIC_BLOB_0_DATA |
| 81 | + ); |
| 82 | + assert_eq!( |
| 83 | + read_blob_as_utf8_string(&input_file, 1).await, |
| 84 | + METRIC_BLOB_1_DATA |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + #[tokio::test] |
| 89 | + async fn test_read_rust_generated_ztd_compressed_metric_data() { |
| 90 | + let input_file = read_test_file("v1/rust-generated/sample-metric-data-compressed-zstd.bin"); |
| 91 | + assert_eq!( |
| 92 | + read_blob_as_utf8_string(&input_file, 0).await, |
| 93 | + METRIC_BLOB_0_DATA |
| 94 | + ); |
| 95 | + assert_eq!( |
| 96 | + read_blob_as_utf8_string(&input_file, 1).await, |
| 97 | + METRIC_BLOB_1_DATA |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + #[tokio::test] |
| 102 | + async fn test_read_java_generated_uncompressed_metric_data() { |
| 103 | + let input_file = read_test_file("v1/java-generated/sample-metric-data-uncompressed.bin"); |
| 104 | + assert_eq!( |
| 105 | + read_blob_as_utf8_string(&input_file, 0).await, |
| 106 | + METRIC_BLOB_0_DATA |
| 107 | + ); |
| 108 | + assert_eq!( |
| 109 | + read_blob_as_utf8_string(&input_file, 1).await, |
| 110 | + METRIC_BLOB_1_DATA |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + #[tokio::test] |
| 115 | + async fn test_read_java_generated_ztd_compressed_metric_data() { |
| 116 | + let input_file = read_test_file("v1/java-generated/sample-metric-data-compressed-zstd.bin"); |
| 117 | + assert_eq!( |
| 118 | + read_blob_as_utf8_string(&input_file, 0).await, |
| 119 | + METRIC_BLOB_0_DATA |
| 120 | + ); |
| 121 | + assert_eq!( |
| 122 | + read_blob_as_utf8_string(&input_file, 1).await, |
| 123 | + METRIC_BLOB_1_DATA |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
0 commit comments