iceberg/spec/values/
struct_value.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
18//! Struct type for Iceberg values
19
20use std::ops::Index;
21
22use super::Literal;
23
24/// The partition struct stores the tuple of partition values for each file.
25/// Its type is derived from the partition fields of the partition spec used to write the manifest file.
26/// In v2, the partition struct's field ids must match the ids from the partition spec.
27#[derive(Debug, Clone, PartialEq, Eq, Hash)]
28pub struct Struct {
29    /// Vector to store the field values
30    fields: Vec<Option<Literal>>,
31}
32
33impl Struct {
34    /// Create a empty struct.
35    pub fn empty() -> Self {
36        Self { fields: Vec::new() }
37    }
38
39    /// Create a iterator to read the field in order of field_value.
40    pub fn iter(&self) -> impl ExactSizeIterator<Item = Option<&Literal>> {
41        self.fields.iter().map(|field| field.as_ref())
42    }
43
44    /// returns true if the field at position `index` is null
45    pub fn is_null_at_index(&self, index: usize) -> bool {
46        self.fields[index].is_none()
47    }
48
49    /// Return fields in the struct.
50    pub fn fields(&self) -> &[Option<Literal>] {
51        &self.fields
52    }
53}
54
55impl Index<usize> for Struct {
56    type Output = Option<Literal>;
57
58    fn index(&self, idx: usize) -> &Self::Output {
59        &self.fields[idx]
60    }
61}
62
63impl IntoIterator for Struct {
64    type Item = Option<Literal>;
65
66    type IntoIter = std::vec::IntoIter<Option<Literal>>;
67
68    fn into_iter(self) -> Self::IntoIter {
69        self.fields.into_iter()
70    }
71}
72
73impl FromIterator<Option<Literal>> for Struct {
74    fn from_iter<I: IntoIterator<Item = Option<Literal>>>(iter: I) -> Self {
75        Struct {
76            fields: iter.into_iter().collect(),
77        }
78    }
79}