|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/expression/term.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | + |
| 24 | +#include <gtest/gtest.h> |
| 25 | + |
| 26 | +#include "iceberg/expression/expressions.h" |
| 27 | +#include "iceberg/schema.h" |
| 28 | +#include "iceberg/test/matchers.h" |
| 29 | +#include "iceberg/transform.h" |
| 30 | +#include "iceberg/util/checked_cast.h" |
| 31 | + |
| 32 | +namespace iceberg { |
| 33 | +class TermTest : public ::testing::Test { |
| 34 | + protected: |
| 35 | + void SetUp() override { |
| 36 | + field1_ = std::make_unique<SchemaField>(1, "x", int32(), true); |
| 37 | + field2_ = std::make_unique<SchemaField>(2, "y", string(), true); |
| 38 | + field3_ = std::make_unique<SchemaField>(3, "time", timestamp(), true); |
| 39 | + |
| 40 | + schema_ = std::make_unique<Schema>( |
| 41 | + std::vector<SchemaField>{*field1_, *field2_, *field3_}, 1); |
| 42 | + |
| 43 | + term1_ = Expressions::Ref("x"); |
| 44 | + term2_ = Expressions::Ref("y"); |
| 45 | + term3_ = Expressions::Bucket("x", 10); |
| 46 | + term4_ = Expressions::Day("time"); |
| 47 | + } |
| 48 | + |
| 49 | + std::unique_ptr<Schema> schema_; |
| 50 | + std::unique_ptr<SchemaField> field1_; |
| 51 | + std::unique_ptr<SchemaField> field2_; |
| 52 | + std::unique_ptr<SchemaField> field3_; |
| 53 | + |
| 54 | + // NamedReference |
| 55 | + std::shared_ptr<Term> term1_; |
| 56 | + std::shared_ptr<Term> term2_; |
| 57 | + |
| 58 | + // UnboundTransform |
| 59 | + std::shared_ptr<Term> term3_; |
| 60 | + std::shared_ptr<Term> term4_; |
| 61 | +}; |
| 62 | + |
| 63 | +TEST_F(TermTest, NamedReference) { |
| 64 | + auto nr1 = internal::checked_pointer_cast<NamedReference>(term1_); |
| 65 | + auto bound_ref = nr1->Bind(*schema_, true); |
| 66 | + ASSERT_THAT(bound_ref, IsOk()); |
| 67 | + ASSERT_EQ(bound_ref.value()->field(), *field1_); |
| 68 | + |
| 69 | + auto nr2 = internal::checked_pointer_cast<NamedReference>(term2_); |
| 70 | + auto bound_ref2 = nr2->Bind(*schema_, true); |
| 71 | + ASSERT_THAT(bound_ref2, IsOk()); |
| 72 | + ASSERT_EQ(bound_ref2.value()->field(), *field2_); |
| 73 | +} |
| 74 | + |
| 75 | +TEST_F(TermTest, UnboundTransform) { |
| 76 | + auto ut1 = internal::checked_pointer_cast<UnboundTransform>(term3_); |
| 77 | + auto bound_transform = ut1->Bind(*schema_, true); |
| 78 | + ASSERT_THAT(bound_transform, IsOk()); |
| 79 | + ASSERT_EQ(bound_transform.value()->reference()->field(), *field1_); |
| 80 | + ASSERT_EQ(*bound_transform.value()->transform(), *Transform::Bucket(10)); |
| 81 | + |
| 82 | + auto ut2 = internal::checked_pointer_cast<UnboundTransform>(term4_); |
| 83 | + auto bound_transform2 = ut2->Bind(*schema_, true); |
| 84 | + ASSERT_THAT(bound_transform2, IsOk()); |
| 85 | + ASSERT_EQ(bound_transform2.value()->reference()->field(), *field3_); |
| 86 | + ASSERT_EQ(*bound_transform2.value()->transform(), *Transform::Day()); |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace iceberg |
0 commit comments