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};
22
23use crate::Result;
24use crate::expr::BoundPredicate;
25use crate::spec::{
26    DataContentType, DataFileFormat, ManifestEntryRef, NameMapping, PartitionSpec, Schema,
27    SchemaRef, Struct,
28};
29
30/// A stream of [`FileScanTask`].
31pub type FileScanTaskStream = BoxStream<'static, Result<FileScanTask>>;
32
33/// Serialization helper that always returns NotImplementedError.
34/// Used for fields that should not be serialized but we want to be explicit about it.
35fn serialize_not_implemented<S, T>(_: &T, _: S) -> std::result::Result<S::Ok, S::Error>
36where S: Serializer {
37    Err(serde::ser::Error::custom(
38        "Serialization not implemented for this field",
39    ))
40}
41
42/// Deserialization helper that always returns NotImplementedError.
43/// Used for fields that should not be deserialized but we want to be explicit about it.
44fn deserialize_not_implemented<'de, D, T>(_: D) -> std::result::Result<T, D::Error>
45where D: serde::Deserializer<'de> {
46    Err(serde::de::Error::custom(
47        "Deserialization not implemented for this field",
48    ))
49}
50
51/// A task to scan part of file.
52#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
53pub struct FileScanTask {
54    /// The total size of the data file in bytes, from the manifest entry.
55    /// Used to skip a stat/HEAD request when reading Parquet footers.
56    pub file_size_in_bytes: u64,
57    /// The start offset of the file to scan.
58    pub start: u64,
59    /// The length of the file to scan.
60    pub length: u64,
61    /// The number of records in the file to scan.
62    ///
63    /// This is an optional field, and only available if we are
64    /// reading the entire data file.
65    pub record_count: Option<u64>,
66
67    /// The data file path corresponding to the task.
68    pub data_file_path: String,
69
70    /// The format of the file to scan.
71    pub data_file_format: DataFileFormat,
72
73    /// The schema of the file to scan.
74    pub schema: SchemaRef,
75    /// The field ids to project.
76    pub project_field_ids: Vec<i32>,
77    /// The predicate to filter.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub predicate: Option<BoundPredicate>,
80
81    /// The list of delete files that may need to be applied to this data file
82    pub deletes: Vec<FileScanTaskDeleteFile>,
83
84    /// Partition data from the manifest entry, used to identify which columns can use
85    /// constant values from partition metadata vs. reading from the data file.
86    /// Per the Iceberg spec, only identity-transformed partition fields should use constants.
87    #[serde(default)]
88    #[serde(skip_serializing_if = "Option::is_none")]
89    #[serde(serialize_with = "serialize_not_implemented")]
90    #[serde(deserialize_with = "deserialize_not_implemented")]
91    pub partition: Option<Struct>,
92
93    /// The partition spec for this file, used to distinguish identity transforms
94    /// (which use partition metadata constants) from non-identity transforms like
95    /// bucket/truncate (which must read source columns from the data file).
96    #[serde(default)]
97    #[serde(skip_serializing_if = "Option::is_none")]
98    #[serde(serialize_with = "serialize_not_implemented")]
99    #[serde(deserialize_with = "deserialize_not_implemented")]
100    pub partition_spec: Option<Arc<PartitionSpec>>,
101
102    /// Name mapping from table metadata (property: schema.name-mapping.default),
103    /// used to resolve field IDs from column names when Parquet files lack field IDs
104    /// or have field ID conflicts.
105    #[serde(default)]
106    #[serde(skip_serializing_if = "Option::is_none")]
107    #[serde(serialize_with = "serialize_not_implemented")]
108    #[serde(deserialize_with = "deserialize_not_implemented")]
109    pub name_mapping: Option<Arc<NameMapping>>,
110
111    /// Whether this scan task should treat column names as case-sensitive when binding predicates.
112    pub case_sensitive: bool,
113}
114
115impl FileScanTask {
116    /// Returns the data file path of this file scan task.
117    pub fn data_file_path(&self) -> &str {
118        &self.data_file_path
119    }
120
121    /// Returns the project field id of this file scan task.
122    pub fn project_field_ids(&self) -> &[i32] {
123        &self.project_field_ids
124    }
125
126    /// Returns the predicate of this file scan task.
127    pub fn predicate(&self) -> Option<&BoundPredicate> {
128        self.predicate.as_ref()
129    }
130
131    /// Returns the schema of this file scan task as a reference
132    pub fn schema(&self) -> &Schema {
133        &self.schema
134    }
135
136    /// Returns the schema of this file scan task as a SchemaRef
137    pub fn schema_ref(&self) -> SchemaRef {
138        self.schema.clone()
139    }
140}
141
142#[derive(Debug)]
143pub(crate) struct DeleteFileContext {
144    pub(crate) manifest_entry: ManifestEntryRef,
145    pub(crate) partition_spec_id: i32,
146}
147
148impl From<&DeleteFileContext> for FileScanTaskDeleteFile {
149    fn from(ctx: &DeleteFileContext) -> Self {
150        FileScanTaskDeleteFile {
151            file_path: ctx.manifest_entry.file_path().to_string(),
152            file_size_in_bytes: ctx.manifest_entry.file_size_in_bytes(),
153            file_type: ctx.manifest_entry.content_type(),
154            partition_spec_id: ctx.partition_spec_id,
155            equality_ids: ctx.manifest_entry.data_file.equality_ids.clone(),
156        }
157    }
158}
159
160/// A task to scan part of file.
161#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
162pub struct FileScanTaskDeleteFile {
163    /// The delete file path
164    pub file_path: String,
165
166    /// The total size of the delete file in bytes, from the manifest entry.
167    pub file_size_in_bytes: u64,
168
169    /// delete file type
170    pub file_type: DataContentType,
171
172    /// partition id
173    pub partition_spec_id: i32,
174
175    /// equality ids for equality deletes (null for anything other than equality-deletes)
176    pub equality_ids: Option<Vec<i32>>,
177}