iceberg/scan/task.rs
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
18use std::sync::Arc;
19
20use futures::stream::BoxStream;
21use serde::{Deserialize, Serialize, Serializer};
22use typed_builder::TypedBuilder;
23
24use crate::Result;
25use crate::expr::BoundPredicate;
26use crate::spec::{
27 DataContentType, DataFileFormat, ManifestEntryRef, NameMapping, PartitionSpec, Schema,
28 SchemaRef, Struct, StructType,
29};
30
31/// A stream of [`FileScanTask`].
32pub type FileScanTaskStream = BoxStream<'static, Result<FileScanTask>>;
33
34/// Serialization helper that always returns NotImplementedError.
35/// Used for fields that should not be serialized but we want to be explicit about it.
36fn serialize_not_implemented<S, T>(_: &T, _: S) -> std::result::Result<S::Ok, S::Error>
37where S: Serializer {
38 Err(serde::ser::Error::custom(
39 "Serialization not implemented for this field",
40 ))
41}
42
43/// Deserialization helper that always returns NotImplementedError.
44/// Used for fields that should not be deserialized but we want to be explicit about it.
45fn deserialize_not_implemented<'de, D, T>(_: D) -> std::result::Result<T, D::Error>
46where D: serde::Deserializer<'de> {
47 Err(serde::de::Error::custom(
48 "Deserialization not implemented for this field",
49 ))
50}
51
52/// A task to scan part of file.
53#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, TypedBuilder)]
54#[builder(field_defaults(setter(prefix = "with_")))]
55pub struct FileScanTask {
56 /// The total size of the data file in bytes, from the manifest entry.
57 /// Used to skip a stat/HEAD request when reading Parquet footers.
58 pub file_size_in_bytes: u64,
59 /// The start offset of the file to scan.
60 pub start: u64,
61 /// The length of the file to scan.
62 pub length: u64,
63 /// The number of records in the file to scan.
64 ///
65 /// This is an optional field, and only available if we are
66 /// reading the entire data file.
67 #[builder(default)]
68 pub record_count: Option<u64>,
69
70 /// The first row id assigned to the data file.
71 ///
72 /// Used to derive the `_row_id` metadata column: for a row without an
73 /// explicit `_row_id`, it is this value plus the row's ordinal position.
74 #[serde(skip_serializing_if = "Option::is_none")]
75 #[builder(default)]
76 pub first_row_id: Option<i64>,
77
78 /// The data sequence number of the file, as opposed to its file sequence
79 /// number: the sequence number preserved when a file is carried forward
80 /// across a rewrite. May be null for an existing entry in a malformed
81 /// manifest that lacks one.
82 ///
83 /// Used to derive the `_last_updated_sequence_number` metadata column.
84 #[serde(skip_serializing_if = "Option::is_none")]
85 #[builder(default)]
86 pub data_sequence_number: Option<i64>,
87
88 /// The data file path corresponding to the task.
89 pub data_file_path: String,
90
91 /// The format of the file to scan.
92 pub data_file_format: DataFileFormat,
93
94 /// The schema of the file to scan.
95 pub schema: SchemaRef,
96 /// The field ids to project.
97 pub project_field_ids: Vec<i32>,
98 /// The predicate to filter.
99 #[serde(skip_serializing_if = "Option::is_none")]
100 #[builder(default)]
101 pub predicate: Option<BoundPredicate>,
102
103 /// The list of delete files that may need to be applied to this data file
104 #[builder(default)]
105 pub deletes: Vec<FileScanTaskDeleteFile>,
106
107 /// Partition data from the manifest entry, used to identify which columns can use
108 /// constant values from partition metadata vs. reading from the data file.
109 /// Per the Iceberg spec, only identity-transformed partition fields should use constants.
110 #[serde(default)]
111 #[serde(skip_serializing_if = "Option::is_none")]
112 #[serde(serialize_with = "serialize_not_implemented")]
113 #[serde(deserialize_with = "deserialize_not_implemented")]
114 #[builder(default)]
115 pub partition: Option<Struct>,
116
117 /// The partition spec for this file, used to distinguish identity transforms
118 /// (which use partition metadata constants) from non-identity transforms like
119 /// bucket/truncate (which must read source columns from the data file).
120 #[serde(default)]
121 #[serde(skip_serializing_if = "Option::is_none")]
122 #[serde(serialize_with = "serialize_not_implemented")]
123 #[serde(deserialize_with = "deserialize_not_implemented")]
124 #[builder(default)]
125 pub partition_spec: Option<Arc<PartitionSpec>>,
126
127 /// Name mapping from table metadata (property: schema.name-mapping.default),
128 /// used to resolve field IDs from column names when Parquet files lack field IDs
129 /// or have field ID conflicts.
130 #[serde(default)]
131 #[serde(skip_serializing_if = "Option::is_none")]
132 #[serde(serialize_with = "serialize_not_implemented")]
133 #[serde(deserialize_with = "deserialize_not_implemented")]
134 #[builder(default)]
135 pub name_mapping: Option<Arc<NameMapping>>,
136
137 /// The unified partition type across all specs in the table.
138 /// When `RESERVED_FIELD_ID_PARTITION` is in the projected field IDs, the reader
139 /// uses this type along with the task's partition_spec and partition data to
140 /// materialize the `_partition` struct column at read time.
141 ///
142 /// This is a table-level value (same for all tasks in a scan), stored per-task
143 /// so that readers are self-contained without needing back-pointers to table
144 /// metadata. The cost is one Arc clone per task.
145 /// Serde: not yet implemented (same pattern as partition, partition_spec, name_mapping).
146 #[serde(default)]
147 #[serde(skip_serializing_if = "Option::is_none")]
148 #[serde(serialize_with = "serialize_not_implemented")]
149 #[serde(deserialize_with = "deserialize_not_implemented")]
150 #[builder(default)]
151 pub unified_partition_type: Option<Arc<StructType>>,
152
153 /// Whether this scan task should treat column names as case-sensitive when binding predicates.
154 pub case_sensitive: bool,
155
156 /// Key metadata for encrypted data files (Parquet Modular Encryption).
157 /// When present, the reader uses this to build `FileDecryptionProperties`.
158 ///
159 /// Note on the trust boundary: for the standard encryption scheme this
160 /// carries `StandardKeyMetadata`, whose payload is the *plaintext* DEK.
161 /// Because `FileScanTask` derives `Serialize`, that plaintext DEK is part
162 /// of the serialized scan plan should these tasks ever be serialized and sent
163 /// over the network.
164 #[serde(default)]
165 #[serde(skip_serializing_if = "Option::is_none")]
166 #[builder(default)]
167 pub key_metadata: Option<Box<[u8]>>,
168}
169
170impl FileScanTask {
171 /// Returns the data file path of this file scan task.
172 pub fn data_file_path(&self) -> &str {
173 &self.data_file_path
174 }
175
176 /// Returns the project field id of this file scan task.
177 pub fn project_field_ids(&self) -> &[i32] {
178 &self.project_field_ids
179 }
180
181 /// Returns the predicate of this file scan task.
182 pub fn predicate(&self) -> Option<&BoundPredicate> {
183 self.predicate.as_ref()
184 }
185
186 /// Returns the schema of this file scan task as a reference
187 pub fn schema(&self) -> &Schema {
188 &self.schema
189 }
190
191 /// Returns the schema of this file scan task as a SchemaRef
192 pub fn schema_ref(&self) -> SchemaRef {
193 self.schema.clone()
194 }
195}
196
197#[derive(Debug)]
198pub(crate) struct DeleteFileContext {
199 pub(crate) manifest_entry: ManifestEntryRef,
200 pub(crate) partition_spec_id: i32,
201}
202
203impl From<&DeleteFileContext> for FileScanTaskDeleteFile {
204 fn from(ctx: &DeleteFileContext) -> Self {
205 FileScanTaskDeleteFile::builder()
206 .with_file_path(ctx.manifest_entry.file_path().to_string())
207 .with_file_size_in_bytes(ctx.manifest_entry.file_size_in_bytes())
208 .with_file_type(ctx.manifest_entry.content_type())
209 .with_partition_spec_id(ctx.partition_spec_id)
210 .with_equality_ids(ctx.manifest_entry.data_file.equality_ids.clone())
211 .with_key_metadata(
212 ctx.manifest_entry
213 .data_file
214 .key_metadata
215 .as_deref()
216 .map(Box::from),
217 )
218 .build()
219 }
220}
221
222/// A task to scan part of file.
223#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, TypedBuilder)]
224#[builder(field_defaults(setter(prefix = "with_")))]
225pub struct FileScanTaskDeleteFile {
226 /// The delete file path
227 pub file_path: String,
228
229 /// The total size of the delete file in bytes, from the manifest entry.
230 pub file_size_in_bytes: u64,
231
232 /// delete file type
233 pub file_type: DataContentType,
234
235 /// partition id
236 pub partition_spec_id: i32,
237
238 /// equality ids for equality deletes (null for anything other than equality-deletes)
239 #[builder(default)]
240 pub equality_ids: Option<Vec<i32>>,
241
242 /// Key metadata for encrypted delete files (Parquet Modular Encryption).
243 /// When present, the reader uses this to build `FileDecryptionProperties`.
244 ///
245 /// Same plaintext-DEK trust boundary as [`FileScanTask::key_metadata`]:
246 /// this is serialized into the scan plan and crosses the planner -> worker
247 /// channel in the clear for the standard encryption scheme.
248 #[serde(default)]
249 #[serde(skip_serializing_if = "Option::is_none")]
250 #[builder(default)]
251 pub key_metadata: Option<Box<[u8]>>,
252}