iceberg/transform/
identity.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 arrow_array::ArrayRef;
19
20use super::TransformFunction;
21use crate::Result;
22
23/// Return identity array.
24#[derive(Debug)]
25pub struct Identity {}
26
27impl TransformFunction for Identity {
28    fn transform(&self, input: ArrayRef) -> Result<ArrayRef> {
29        Ok(input)
30    }
31
32    fn transform_literal(&self, input: &crate::spec::Datum) -> Result<Option<crate::spec::Datum>> {
33        Ok(Some(input.clone()))
34    }
35}
36
37#[cfg(test)]
38mod test {
39    use crate::spec::PrimitiveType::{
40        Binary, Date, Decimal, Fixed, Int, Long, String as StringType, Time, Timestamp,
41        TimestampNs, Timestamptz, TimestamptzNs, Uuid,
42    };
43    use crate::spec::Type::{Primitive, Struct};
44    use crate::spec::{NestedField, StructType, Transform};
45    use crate::transform::test::TestTransformFixture;
46
47    #[test]
48    fn test_identity_transform() {
49        let trans = Transform::Identity;
50
51        let fixture = TestTransformFixture {
52            display: "identity".to_string(),
53            json: r#""identity""#.to_string(),
54            dedup_name: "identity".to_string(),
55            preserves_order: true,
56            satisfies_order_of: vec![
57                (Transform::Truncate(4), true),
58                (Transform::Truncate(2), true),
59                (Transform::Bucket(4), false),
60                (Transform::Void, false),
61                (Transform::Day, true),
62            ],
63            trans_types: vec![
64                (Primitive(Binary), Some(Primitive(Binary))),
65                (Primitive(Date), Some(Primitive(Date))),
66                (
67                    Primitive(Decimal {
68                        precision: 8,
69                        scale: 5,
70                    }),
71                    Some(Primitive(Decimal {
72                        precision: 8,
73                        scale: 5,
74                    })),
75                ),
76                (Primitive(Fixed(8)), Some(Primitive(Fixed(8)))),
77                (Primitive(Int), Some(Primitive(Int))),
78                (Primitive(Long), Some(Primitive(Long))),
79                (Primitive(StringType), Some(Primitive(StringType))),
80                (Primitive(Uuid), Some(Primitive(Uuid))),
81                (Primitive(Time), Some(Primitive(Time))),
82                (Primitive(Timestamp), Some(Primitive(Timestamp))),
83                (Primitive(Timestamptz), Some(Primitive(Timestamptz))),
84                (Primitive(TimestampNs), Some(Primitive(TimestampNs))),
85                (Primitive(TimestamptzNs), Some(Primitive(TimestamptzNs))),
86                (
87                    Struct(StructType::new(vec![
88                        NestedField::optional(1, "a", Primitive(Timestamp)).into(),
89                    ])),
90                    None,
91                ),
92            ],
93        };
94
95        fixture.assert_transform(trans);
96    }
97}