|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <gtest/gtest.h> |
| 18 | + |
| 19 | +#include "velox/connectors/hive/iceberg/IcebergPartitionPath.h" |
| 20 | +#include "velox/type/Type.h" |
| 21 | + |
| 22 | +namespace facebook::velox::connector::hive::iceberg { |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +template <typename T> |
| 27 | +std::string test(TransformType transform, T value, const TypePtr& type) { |
| 28 | + return IcebergPartitionPath(transform).toPartitionString(value, type); |
| 29 | +} |
| 30 | + |
| 31 | +TEST(IcebergPartitionPathTest, identityInt32) { |
| 32 | + EXPECT_EQ(test(TransformType::kIdentity, 100, INTEGER()), "100"); |
| 33 | + EXPECT_EQ(test(TransformType::kIdentity, -100, INTEGER()), "-100"); |
| 34 | + EXPECT_EQ(test(TransformType::kIdentity, 0, INTEGER()), "0"); |
| 35 | +} |
| 36 | + |
| 37 | +TEST(IcebergPartitionPathTest, identityDate) { |
| 38 | + EXPECT_EQ(test(TransformType::kIdentity, 18'262, DATE()), "2020-01-01"); |
| 39 | + EXPECT_EQ(test(TransformType::kIdentity, 0, DATE()), "1970-01-01"); |
| 40 | + EXPECT_EQ(test(TransformType::kIdentity, -1, DATE()), "1969-12-31"); |
| 41 | + EXPECT_EQ(test(TransformType::kIdentity, 2'932'897, DATE()), "10000-01-01"); |
| 42 | +} |
| 43 | + |
| 44 | +TEST(IcebergPartitionPathTest, identityBoolean) { |
| 45 | + EXPECT_EQ(test(TransformType::kIdentity, true, BOOLEAN()), "true"); |
| 46 | + EXPECT_EQ(test(TransformType::kIdentity, false, BOOLEAN()), "false"); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(IcebergPartitionPathTest, identityVarchar) { |
| 50 | + EXPECT_EQ( |
| 51 | + test(TransformType::kIdentity, StringView("a/b/c=d"), VARCHAR()), |
| 52 | + "a/b/c=d"); |
| 53 | + EXPECT_EQ(test(TransformType::kIdentity, StringView(""), VARCHAR()), ""); |
| 54 | +} |
| 55 | + |
| 56 | +TEST(IcebergPartitionPathTest, identityVarbinary) { |
| 57 | + EXPECT_EQ( |
| 58 | + test( |
| 59 | + TransformType::kIdentity, |
| 60 | + StringView("\x48\x65\x6c\x6c\x6f"), |
| 61 | + VARBINARY()), |
| 62 | + "SGVsbG8="); |
| 63 | + EXPECT_EQ( |
| 64 | + test(TransformType::kIdentity, StringView("\x1\x2\x3"), VARBINARY()), |
| 65 | + "AQID"); |
| 66 | + EXPECT_EQ(test(TransformType::kIdentity, StringView(""), VARBINARY()), ""); |
| 67 | +} |
| 68 | + |
| 69 | +TEST(IcebergPartitionPathTest, identityTimestamp) { |
| 70 | + EXPECT_EQ( |
| 71 | + test(TransformType::kIdentity, Timestamp(0, 0), TIMESTAMP()), |
| 72 | + "1970-01-01T00:00:00"); |
| 73 | + EXPECT_EQ( |
| 74 | + test( |
| 75 | + TransformType::kIdentity, |
| 76 | + Timestamp(1609459200, 999000000), |
| 77 | + TIMESTAMP()), |
| 78 | + "2021-01-01T00:00:00.999"); |
| 79 | + EXPECT_EQ( |
| 80 | + test( |
| 81 | + TransformType::kIdentity, |
| 82 | + Timestamp(1640995200, 500000000), |
| 83 | + TIMESTAMP()), |
| 84 | + "2022-01-01T00:00:00.5"); |
| 85 | + EXPECT_EQ( |
| 86 | + test(TransformType::kIdentity, Timestamp(-1, 999000000), TIMESTAMP()), |
| 87 | + "1969-12-31T23:59:59.999"); |
| 88 | + EXPECT_EQ( |
| 89 | + test( |
| 90 | + TransformType::kIdentity, |
| 91 | + Timestamp(253402300800, 100000000), |
| 92 | + TIMESTAMP()), |
| 93 | + "+10000-01-01T00:00:00.1"); |
| 94 | + EXPECT_EQ( |
| 95 | + test(TransformType::kIdentity, Timestamp(-62170000000, 0), TIMESTAMP()), |
| 96 | + "-0001-11-29T19:33:20"); |
| 97 | + EXPECT_EQ( |
| 98 | + test(TransformType::kIdentity, Timestamp(-62167219199, 0), TIMESTAMP()), |
| 99 | + "0000-01-01T00:00:01"); |
| 100 | +} |
| 101 | + |
| 102 | +TEST(IcebergPartitionPathTest, year) { |
| 103 | + EXPECT_EQ(test(TransformType::kYear, 0, INTEGER()), "1970"); |
| 104 | + EXPECT_EQ(test(TransformType::kYear, 1, INTEGER()), "1971"); |
| 105 | + EXPECT_EQ(test(TransformType::kYear, 8030, INTEGER()), "10000"); |
| 106 | + EXPECT_EQ(test(TransformType::kYear, -1, INTEGER()), "1969"); |
| 107 | + EXPECT_EQ(test(TransformType::kYear, -50, INTEGER()), "1920"); |
| 108 | +} |
| 109 | + |
| 110 | +TEST(IcebergPartitionPathTest, month) { |
| 111 | + EXPECT_EQ(test(TransformType::kMonth, 0, INTEGER()), "1970-01"); |
| 112 | + EXPECT_EQ(test(TransformType::kMonth, 1, INTEGER()), "1970-02"); |
| 113 | + EXPECT_EQ(test(TransformType::kMonth, 11, INTEGER()), "1970-12"); |
| 114 | + EXPECT_EQ(test(TransformType::kMonth, 612, INTEGER()), "2021-01"); |
| 115 | + EXPECT_EQ(test(TransformType::kMonth, -1, INTEGER()), "1969-12"); |
| 116 | + EXPECT_EQ(test(TransformType::kMonth, -13, INTEGER()), "1968-12"); |
| 117 | +} |
| 118 | + |
| 119 | +TEST(IcebergPartitionPathTest, day) { |
| 120 | + EXPECT_EQ(test(TransformType::kDay, 0, DATE()), "1970-01-01"); |
| 121 | + EXPECT_EQ(test(TransformType::kDay, 1, DATE()), "1970-01-02"); |
| 122 | + EXPECT_EQ(test(TransformType::kDay, 18'262, DATE()), "2020-01-01"); |
| 123 | + EXPECT_EQ(test(TransformType::kDay, -1, DATE()), "1969-12-31"); |
| 124 | +} |
| 125 | + |
| 126 | +TEST(IcebergPartitionPathTest, hour) { |
| 127 | + EXPECT_EQ(test(TransformType::kHour, 0, INTEGER()), "1970-01-01-00"); |
| 128 | + EXPECT_EQ(test(TransformType::kHour, 1, INTEGER()), "1970-01-01-01"); |
| 129 | + EXPECT_EQ(test(TransformType::kHour, 24, INTEGER()), "1970-01-02-00"); |
| 130 | + EXPECT_EQ(test(TransformType::kHour, 438'288, INTEGER()), "2020-01-01-00"); |
| 131 | + EXPECT_EQ(test(TransformType::kHour, -1, INTEGER()), "1969-12-31-23"); |
| 132 | +} |
| 133 | + |
| 134 | +TEST(IcebergPartitionPathTest, bucket) { |
| 135 | + EXPECT_EQ(test(TransformType::kBucket, 0, INTEGER()), "0"); |
| 136 | + EXPECT_EQ(test(TransformType::kBucket, 1, INTEGER()), "1"); |
| 137 | + EXPECT_EQ(test(TransformType::kBucket, 128, INTEGER()), "128"); |
| 138 | +} |
| 139 | + |
| 140 | +TEST(IcebergPartitionPathTest, truncateInteger) { |
| 141 | + EXPECT_EQ(test(TransformType::kTruncate, 0, INTEGER()), "0"); |
| 142 | + EXPECT_EQ(test(TransformType::kTruncate, 10, INTEGER()), "10"); |
| 143 | + EXPECT_EQ(test(TransformType::kTruncate, 100, INTEGER()), "100"); |
| 144 | + EXPECT_EQ(test(TransformType::kTruncate, -10, INTEGER()), "-10"); |
| 145 | + EXPECT_EQ(test(TransformType::kTruncate, -100, INTEGER()), "-100"); |
| 146 | + EXPECT_EQ(test(TransformType::kTruncate, -1000, INTEGER()), "-1000"); |
| 147 | +} |
| 148 | + |
| 149 | +TEST(IcebergPartitionPathTest, truncateString) { |
| 150 | + EXPECT_EQ( |
| 151 | + test(TransformType::kTruncate, StringView("abc"), VARCHAR()), "abc"); |
| 152 | + EXPECT_EQ(test(TransformType::kTruncate, StringView(""), VARCHAR()), ""); |
| 153 | + EXPECT_EQ( |
| 154 | + test(TransformType::kTruncate, StringView("\x1\x2\x3"), VARBINARY()), |
| 155 | + "AQID"); |
| 156 | +} |
| 157 | + |
| 158 | +} // namespace |
| 159 | + |
| 160 | +} // namespace facebook::velox::connector::hive::iceberg |
0 commit comments