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    /// Whether this scan task should treat column names as case-sensitive when binding predicates.
109    pub case_sensitive: bool,
110}
111
112impl FileScanTask {
113    /// Returns the data file path of this file scan task.
114    pub fn data_file_path(&self) -> &str {
115        &self.data_file_path
116    }
117
118    /// Returns the project field id of this file scan task.
119    pub fn project_field_ids(&self) -> &[i32] {
120        &self.project_field_ids
121    }
122
123    /// Returns the predicate of this file scan task.
124    pub fn predicate(&self) -> Option<&BoundPredicate> {
125        self.predicate.as_ref()
126    }
127
128    /// Returns the schema of this file scan task as a reference
129    pub fn schema(&self) -> &Schema {
130        &self.schema
131    }
132
133    /// Returns the schema of this file scan task as a SchemaRef
134    pub fn schema_ref(&self) -> SchemaRef {
135        self.schema.clone()
136    }
137}
138
139#[derive(Debug)]
140pub(crate) struct DeleteFileContext {
141    pub(crate) manifest_entry: ManifestEntryRef,
142    pub(crate) partition_spec_id: i32,
143}
144
145impl From<&DeleteFileContext> for FileScanTaskDeleteFile {
146    fn from(ctx: &DeleteFileContext) -> Self {
147        FileScanTaskDeleteFile {
148            file_path: ctx.manifest_entry.file_path().to_string(),
149            file_type: ctx.manifest_entry.content_type(),
150            partition_spec_id: ctx.partition_spec_id,
151            equality_ids: ctx.manifest_entry.data_file.equality_ids.clone(),
152        }
153    }
154}
155
156/// A task to scan part of file.
157#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
158pub struct FileScanTaskDeleteFile {
159    /// The delete file path
160    pub file_path: String,
161
162    /// delete file type
163    pub file_type: DataContentType,
164
165    /// partition id
166    pub partition_spec_id: i32,
167
168    /// equality ids for equality deletes (null for anything other than equality-deletes)
169    pub equality_ids: Option<Vec<i32>>,
170}