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 start offset of the file to scan.
55    pub start: u64,
56    /// The length of the file to scan.
57    pub length: u64,
58    /// The number of records in the file to scan.
59    ///
60    /// This is an optional field, and only available if we are
61    /// reading the entire data file.
62    pub record_count: Option<u64>,
63
64    /// The data file path corresponding to the task.
65    pub data_file_path: String,
66
67    /// The format of the file to scan.
68    pub data_file_format: DataFileFormat,
69
70    /// The schema of the file to scan.
71    pub schema: SchemaRef,
72    /// The field ids to project.
73    pub project_field_ids: Vec<i32>,
74    /// The predicate to filter.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub predicate: Option<BoundPredicate>,
77
78    /// The list of delete files that may need to be applied to this data file
79    pub deletes: Vec<FileScanTaskDeleteFile>,
80
81    /// Partition data from the manifest entry, used to identify which columns can use
82    /// constant values from partition metadata vs. reading from the data file.
83    /// Per the Iceberg spec, only identity-transformed partition fields should use constants.
84    #[serde(default)]
85    #[serde(skip_serializing_if = "Option::is_none")]
86    #[serde(serialize_with = "serialize_not_implemented")]
87    #[serde(deserialize_with = "deserialize_not_implemented")]
88    pub partition: Option<Struct>,
89
90    /// The partition spec for this file, used to distinguish identity transforms
91    /// (which use partition metadata constants) from non-identity transforms like
92    /// bucket/truncate (which must read source columns from the data file).
93    #[serde(default)]
94    #[serde(skip_serializing_if = "Option::is_none")]
95    #[serde(serialize_with = "serialize_not_implemented")]
96    #[serde(deserialize_with = "deserialize_not_implemented")]
97    pub partition_spec: Option<Arc<PartitionSpec>>,
98
99    /// Name mapping from table metadata (property: schema.name-mapping.default),
100    /// used to resolve field IDs from column names when Parquet files lack field IDs
101    /// or have field ID conflicts.
102    #[serde(default)]
103    #[serde(skip_serializing_if = "Option::is_none")]
104    #[serde(serialize_with = "serialize_not_implemented")]
105    #[serde(deserialize_with = "deserialize_not_implemented")]
106    pub name_mapping: Option<Arc<NameMapping>>,
107}
108
109impl FileScanTask {
110    /// Returns the data file path of this file scan task.
111    pub fn data_file_path(&self) -> &str {
112        &self.data_file_path
113    }
114
115    /// Returns the project field id of this file scan task.
116    pub fn project_field_ids(&self) -> &[i32] {
117        &self.project_field_ids
118    }
119
120    /// Returns the predicate of this file scan task.
121    pub fn predicate(&self) -> Option<&BoundPredicate> {
122        self.predicate.as_ref()
123    }
124
125    /// Returns the schema of this file scan task as a reference
126    pub fn schema(&self) -> &Schema {
127        &self.schema
128    }
129
130    /// Returns the schema of this file scan task as a SchemaRef
131    pub fn schema_ref(&self) -> SchemaRef {
132        self.schema.clone()
133    }
134}
135
136#[derive(Debug)]
137pub(crate) struct DeleteFileContext {
138    pub(crate) manifest_entry: ManifestEntryRef,
139    pub(crate) partition_spec_id: i32,
140}
141
142impl From<&DeleteFileContext> for FileScanTaskDeleteFile {
143    fn from(ctx: &DeleteFileContext) -> Self {
144        FileScanTaskDeleteFile {
145            file_path: ctx.manifest_entry.file_path().to_string(),
146            file_type: ctx.manifest_entry.content_type(),
147            partition_spec_id: ctx.partition_spec_id,
148            equality_ids: ctx.manifest_entry.data_file.equality_ids.clone(),
149        }
150    }
151}
152
153/// A task to scan part of file.
154#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
155pub struct FileScanTaskDeleteFile {
156    /// The delete file path
157    pub file_path: String,
158
159    /// delete file type
160    pub file_type: DataContentType,
161
162    /// partition id
163    pub partition_spec_id: i32,
164
165    /// equality ids for equality deletes (null for anything other than equality-deletes)
166    pub equality_ids: Option<Vec<i32>>,
167}