|
| 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 <memory> |
| 21 | +#include <string> |
| 22 | + |
| 23 | +#include <gmock/gmock.h> |
| 24 | +#include <gtest/gtest.h> |
| 25 | + |
| 26 | +#include "iceberg/expression/literal.h" |
| 27 | +#include "iceberg/test/matchers.h" |
| 28 | +#include "iceberg/transform.h" |
| 29 | + |
| 30 | +namespace iceberg { |
| 31 | + |
| 32 | +struct HumanStringTestParam { |
| 33 | + std::string test_name; |
| 34 | + std::shared_ptr<Type> source_type; |
| 35 | + Literal literal; |
| 36 | + std::vector<std::string> expecteds; |
| 37 | +}; |
| 38 | + |
| 39 | +class IdentityTest : public ::testing::TestWithParam<HumanStringTestParam> { |
| 40 | + protected: |
| 41 | + std::vector<std::shared_ptr<Transform>> transforms_{{Transform::Identity()}}; |
| 42 | +}; |
| 43 | + |
| 44 | +TEST_P(IdentityTest, ToHumanString) { |
| 45 | + const auto& param = GetParam(); |
| 46 | + for (int32_t i = 0; i < transforms_.size(); ++i) { |
| 47 | + EXPECT_THAT(transforms_[i]->ToHumanString(param.literal), |
| 48 | + HasValue(::testing::Eq(param.expecteds[i]))); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +INSTANTIATE_TEST_SUITE_P( |
| 53 | + IdentityTestCases, IdentityTest, |
| 54 | + ::testing::Values( |
| 55 | + HumanStringTestParam{.test_name = "Null", |
| 56 | + .literal = Literal::Null(std::make_shared<IntType>()), |
| 57 | + .expecteds{"null"}}, |
| 58 | + HumanStringTestParam{.test_name = "Binary", |
| 59 | + .literal = Literal::Binary(std::vector<uint8_t>{1, 2, 3}), |
| 60 | + .expecteds{"AQID"}}, |
| 61 | + HumanStringTestParam{.test_name = "Fixed", |
| 62 | + .literal = Literal::Fixed(std::vector<uint8_t>{1, 2, 3}), |
| 63 | + .expecteds{"AQID"}}, |
| 64 | + HumanStringTestParam{.test_name = "Date", |
| 65 | + .literal = Literal::Date(17501), |
| 66 | + .expecteds{"2017-12-01"}}, |
| 67 | + HumanStringTestParam{.test_name = "Time", |
| 68 | + .literal = Literal::Time(36775038194), |
| 69 | + .expecteds{"10:12:55.038194"}}, |
| 70 | + HumanStringTestParam{.test_name = "TimestampWithZone", |
| 71 | + .literal = Literal::TimestampTz(1512151975038194), |
| 72 | + .expecteds{"2017-12-01T18:12:55.038194+00:00"}}, |
| 73 | + HumanStringTestParam{.test_name = "TimestampWithoutZone", |
| 74 | + .literal = Literal::Timestamp(1512123175038194), |
| 75 | + .expecteds{"2017-12-01T10:12:55.038194"}}, |
| 76 | + HumanStringTestParam{.test_name = "Long", |
| 77 | + .literal = Literal::Long(-1234567890000L), |
| 78 | + .expecteds{"-1234567890000"}}, |
| 79 | + HumanStringTestParam{.test_name = "String", |
| 80 | + .literal = Literal::String("a/b/c=d"), |
| 81 | + .expecteds{"a/b/c=d"}}), |
| 82 | + [](const ::testing::TestParamInfo<HumanStringTestParam>& info) { |
| 83 | + return info.param.test_name; |
| 84 | + }); |
| 85 | + |
| 86 | +class DateTest : public ::testing::TestWithParam<HumanStringTestParam> { |
| 87 | + protected: |
| 88 | + std::vector<std::shared_ptr<Transform>> transforms_{ |
| 89 | + Transform::Year(), Transform::Month(), Transform::Day()}; |
| 90 | +}; |
| 91 | + |
| 92 | +TEST_P(DateTest, ToHumanString) { |
| 93 | + const auto& param = GetParam(); |
| 94 | + |
| 95 | + for (uint32_t i = 0; i < transforms_.size(); i++) { |
| 96 | + ICEBERG_UNWRAP_OR_FAIL(auto trans_func, |
| 97 | + transforms_[i]->Bind(std::make_shared<DateType>())); |
| 98 | + ICEBERG_UNWRAP_OR_FAIL(auto literal, trans_func->Transform(param.literal)); |
| 99 | + EXPECT_THAT(transforms_[i]->ToHumanString(literal), |
| 100 | + HasValue(::testing::Eq(param.expecteds[i]))); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +INSTANTIATE_TEST_SUITE_P( |
| 105 | + DateTestCases, DateTest, |
| 106 | + ::testing::Values( |
| 107 | + HumanStringTestParam{.test_name = "Date", |
| 108 | + .literal = Literal::Date(17501), |
| 109 | + .expecteds = {"2017", "2017-12", "2017-12-01"}}, |
| 110 | + HumanStringTestParam{.test_name = "NegativeDate", |
| 111 | + .literal = Literal::Date(-2), |
| 112 | + .expecteds = {"1969", "1969-12", "1969-12-30"}}, |
| 113 | + HumanStringTestParam{.test_name = "DateLowerBound", |
| 114 | + .literal = Literal::Date(0), |
| 115 | + .expecteds = {"1970", "1970-01", "1970-01-01"}}, |
| 116 | + HumanStringTestParam{.test_name = "NegativeDateLowerBound", |
| 117 | + .literal = Literal::Date(-365), |
| 118 | + .expecteds = {"1969", "1969-01", "1969-01-01"}}, |
| 119 | + HumanStringTestParam{.test_name = "NegativeDateUpperBound", |
| 120 | + .literal = Literal::Date(-1), |
| 121 | + .expecteds = {"1969", "1969-12", "1969-12-31"}}, |
| 122 | + HumanStringTestParam{.test_name = "Null", |
| 123 | + .literal = Literal::Null(std::make_shared<DateType>()), |
| 124 | + .expecteds = {"null", "null", "null"}}), |
| 125 | + [](const ::testing::TestParamInfo<HumanStringTestParam>& info) { |
| 126 | + return info.param.test_name; |
| 127 | + }); |
| 128 | + |
| 129 | +class TimestampTest : public ::testing::TestWithParam<HumanStringTestParam> { |
| 130 | + protected: |
| 131 | + std::vector<std::shared_ptr<Transform>> transforms_{ |
| 132 | + Transform::Year(), Transform::Month(), Transform::Day(), Transform::Hour()}; |
| 133 | +}; |
| 134 | + |
| 135 | +TEST_P(TimestampTest, ToHumanString) { |
| 136 | + const auto& param = GetParam(); |
| 137 | + for (uint32_t i = 0; i < transforms_.size(); i++) { |
| 138 | + ICEBERG_UNWRAP_OR_FAIL(auto trans_func, transforms_[i]->Bind(param.source_type)); |
| 139 | + ICEBERG_UNWRAP_OR_FAIL(auto literal, trans_func->Transform(param.literal)); |
| 140 | + EXPECT_THAT(transforms_[i]->ToHumanString(literal), |
| 141 | + HasValue(::testing::Eq(param.expecteds[i]))); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +INSTANTIATE_TEST_SUITE_P( |
| 146 | + TimestampTestCases, TimestampTest, |
| 147 | + ::testing::Values( |
| 148 | + HumanStringTestParam{ |
| 149 | + .test_name = "Timestamp", |
| 150 | + .source_type = std::make_shared<TimestampType>(), |
| 151 | + .literal = Literal::Timestamp(1512123175038194), |
| 152 | + .expecteds = {"2017", "2017-12", "2017-12-01", "2017-12-01-10"}}, |
| 153 | + HumanStringTestParam{ |
| 154 | + .test_name = "NegativeTimestamp", |
| 155 | + .source_type = std::make_shared<TimestampType>(), |
| 156 | + .literal = Literal::Timestamp(-136024961806), |
| 157 | + .expecteds = {"1969", "1969-12", "1969-12-30", "1969-12-30-10"}}, |
| 158 | + HumanStringTestParam{ |
| 159 | + .test_name = "TimestampLowerBound", |
| 160 | + .source_type = std::make_shared<TimestampType>(), |
| 161 | + .literal = Literal::Timestamp(0), |
| 162 | + .expecteds = {"1970", "1970-01", "1970-01-01", "1970-01-01-00"}}, |
| 163 | + HumanStringTestParam{ |
| 164 | + .test_name = "NegativeTimestampLowerBound", |
| 165 | + .source_type = std::make_shared<TimestampType>(), |
| 166 | + .literal = Literal::Timestamp(-172800000000), |
| 167 | + .expecteds = {"1969", "1969-12", "1969-12-30", "1969-12-30-00"}, |
| 168 | + }, |
| 169 | + HumanStringTestParam{ |
| 170 | + .test_name = "NegativeTimestampUpperBound", |
| 171 | + .source_type = std::make_shared<TimestampType>(), |
| 172 | + .literal = Literal::Timestamp(-1), |
| 173 | + .expecteds = {"1969", "1969-12", "1969-12-31", "1969-12-31-23"}}, |
| 174 | + HumanStringTestParam{ |
| 175 | + .test_name = "TimestampTz", |
| 176 | + .source_type = std::make_shared<TimestampTzType>(), |
| 177 | + .literal = Literal::TimestampTz(1512151975038194), |
| 178 | + .expecteds = {"2017", "2017-12", "2017-12-01", "2017-12-01-18"}}, |
| 179 | + HumanStringTestParam{.test_name = "Null", |
| 180 | + .source_type = std::make_shared<TimestampType>(), |
| 181 | + .literal = Literal::Null(std::make_shared<TimestampType>()), |
| 182 | + .expecteds = {"null", "null", "null", "null"}}), |
| 183 | + [](const ::testing::TestParamInfo<HumanStringTestParam>& info) { |
| 184 | + return info.param.test_name; |
| 185 | + }); |
| 186 | + |
| 187 | +} // namespace iceberg |
0 commit comments