|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Amazon Athena Query Federation SDK |
| 4 | + * %% |
| 5 | + * Copyright (C) 2019 - 2025 Amazon Web Services |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package com.amazonaws.athena.connector.lambda.data; |
| 21 | + |
| 22 | +import com.amazonaws.athena.connector.lambda.exceptions.AthenaConnectorException; |
| 23 | +import org.apache.arrow.vector.types.DateUnit; |
| 24 | +import org.apache.arrow.vector.types.FloatingPointPrecision; |
| 25 | +import org.apache.arrow.vector.types.IntervalUnit; |
| 26 | +import org.apache.arrow.vector.types.TimeUnit; |
| 27 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 28 | + |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import java.math.BigDecimal; |
| 33 | +import java.time.LocalDateTime; |
| 34 | +import java.time.ZoneOffset; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +import static org.junit.Assert.assertEquals; |
| 39 | +import static org.junit.Assert.assertNotEquals; |
| 40 | +import static org.junit.Assert.assertThrows; |
| 41 | + |
| 42 | +public class ArrowTypeComparatorTest { |
| 43 | + |
| 44 | + private static final String stringABC = "abc"; |
| 45 | + private static final String stringXYZ = "xyz"; |
| 46 | + private static final BigDecimal decimal1 = new BigDecimal("123.45"); |
| 47 | + private static final BigDecimal decimal2 = new BigDecimal("678.90"); |
| 48 | + private static final byte[] binary1 = {0x01, 0x02}; |
| 49 | + private static final byte[] binary2 = {0x01, 0x03}; |
| 50 | + private static final LocalDateTime date1 = LocalDateTime.of(2023, 1, 1, 12, 0); |
| 51 | + private static final LocalDateTime date2 = LocalDateTime.of(2023, 1, 2, 12, 0); |
| 52 | + |
| 53 | + private Map<String, Object> struct1; |
| 54 | + private Map<String, Object> struct2; |
| 55 | + |
| 56 | + private static final ArrowType intType = new ArrowType.Int(32, true); |
| 57 | + private static final ArrowType tinyIntType = new ArrowType.Int(8, true); |
| 58 | + private static final ArrowType smallIntType = new ArrowType.Int(16, true); |
| 59 | + private static final ArrowType uint2Type = new ArrowType.Int(16, false); |
| 60 | + private static final ArrowType bigIntType = new ArrowType.Int(64, true); |
| 61 | + private static final ArrowType float8Type = new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE); |
| 62 | + private static final ArrowType float4Type = new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE); |
| 63 | + private static final ArrowType varcharType = new ArrowType.Utf8(); |
| 64 | + private static final ArrowType varbinaryType = new ArrowType.Binary(); |
| 65 | + private static final ArrowType decimalType = new ArrowType.Decimal(10, 2, 128); |
| 66 | + private static final ArrowType bitType = new ArrowType.Bool(); |
| 67 | + private static final ArrowType dateMilliType = new ArrowType.Timestamp(TimeUnit.MILLISECOND, ZoneOffset.UTC.getId()); |
| 68 | + private static final ArrowType dateDayType = new ArrowType.Date(DateUnit.DAY); |
| 69 | + private static final ArrowType structType = new ArrowType.Struct(); |
| 70 | + private static final ArrowType unsupportedType = new ArrowType.Interval(IntervalUnit.YEAR_MONTH); |
| 71 | + |
| 72 | + @Before |
| 73 | + public void setUp() { |
| 74 | + struct1 = new HashMap<>(); |
| 75 | + struct1.put("key1", "value1"); |
| 76 | + struct1.put("key2", 42); |
| 77 | + |
| 78 | + struct2 = new HashMap<>(); |
| 79 | + struct2.put("key1", "value1"); |
| 80 | + struct2.put("key2", 43); // Different value for "key2" |
| 81 | + } |
| 82 | + |
| 83 | + private void assertComparison(int expected, ArrowType arrowType, Object lhs, Object rhs) { |
| 84 | + assertEquals(expected, ArrowTypeComparator.compare(arrowType, lhs, rhs)); |
| 85 | + } |
| 86 | + |
| 87 | + private void assertStructComparison(int expected, Map<String, Object> lhs, Map<String, Object> rhs) { |
| 88 | + assertEquals(expected, ArrowTypeComparator.compare(structType, lhs, rhs)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testCompareIntegers() { |
| 93 | + assertComparison(-1, intType, 5, 10); |
| 94 | + assertComparison(1, intType, 10, 5); |
| 95 | + assertComparison(0, intType, 5, 5); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testCompareTinyInt() { |
| 100 | + assertComparison(-1, tinyIntType, (byte) 1, (byte) 2); |
| 101 | + assertComparison(1, tinyIntType, (byte) 2, (byte) 1); |
| 102 | + assertComparison(0, tinyIntType, (byte) 1, (byte) 1); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testCompareSmallInt() { |
| 107 | + assertComparison(-100, smallIntType, (short) 100, (short) 200); |
| 108 | + assertComparison(100, smallIntType, (short) 200, (short) 100); |
| 109 | + assertComparison(0, smallIntType, (short) 100, (short) 100); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testCompareUint2() { |
| 114 | + assertComparison(-1, uint2Type, 'A', 'B'); |
| 115 | + assertComparison(1, uint2Type, 'B', 'A'); |
| 116 | + assertComparison(0, uint2Type, 'A', 'A'); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testCompareBigInt() { |
| 121 | + assertComparison(-1, bigIntType, 1000L, 2000L); |
| 122 | + assertComparison(1, bigIntType, 2000L, 1000L); |
| 123 | + assertComparison(0, bigIntType, 1000L, 1000L); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testCompareFloat8() { |
| 128 | + assertComparison(-1, float8Type, 123.45, 678.90); |
| 129 | + assertComparison(1, float8Type, 678.90, 123.45); |
| 130 | + assertComparison(0, float8Type, 123.45, 123.45); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testCompareFloat4() { |
| 135 | + assertComparison(-1, float4Type, 123.45f, 678.90f); |
| 136 | + assertComparison(1, float4Type, 678.90f, 123.45f); |
| 137 | + assertComparison(0, float4Type, 123.45f, 123.45f); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testCompareVarchar() { |
| 142 | + assertComparison(-23, varcharType, stringABC, stringXYZ); |
| 143 | + assertComparison(23, varcharType, stringXYZ, stringABC); |
| 144 | + assertComparison(0, varcharType, stringABC, stringABC); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testCompareVarbinary() { |
| 149 | + assertComparison(-1, varbinaryType, binary1, binary2); |
| 150 | + assertComparison(1, varbinaryType, binary2, binary1); |
| 151 | + assertComparison(0, varbinaryType, binary1, binary1); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testCompareDecimal() { |
| 156 | + assertComparison(-1, decimalType, decimal1, decimal2); |
| 157 | + assertComparison(1, decimalType, decimal2, decimal1); |
| 158 | + assertComparison(0, decimalType, decimal1, decimal1); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void testCompareBit() { |
| 163 | + assertComparison(1, bitType, true, false); |
| 164 | + assertComparison(-1, bitType, false, true); |
| 165 | + assertComparison(0, bitType, true, true); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testCompareDateMilli() { |
| 170 | + assertComparison(-1, dateMilliType, date1, date2); |
| 171 | + assertComparison(1, dateMilliType, date2, date1); |
| 172 | + assertComparison(0, dateMilliType, date1, date1); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void testCompareDateDay() { |
| 177 | + assertComparison(-1, dateDayType, 1, 2); |
| 178 | + assertComparison(1, dateDayType, 2, 1); |
| 179 | + assertComparison(0, dateDayType, 1, 1); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testCompareStructEqual() { |
| 184 | + struct2.put("key2", 42); |
| 185 | + |
| 186 | + assertStructComparison(0, struct1, struct2); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void testCompareStructNotEqual() { |
| 191 | + assertStructComparison(1, struct1, struct2); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void testCompareStructWithNull() { |
| 196 | + // Arrange: Set one struct to null |
| 197 | + struct2 = null; |
| 198 | + |
| 199 | + // Act & Assert: Expect struct1 (non-null) to be greater than struct2 (null) |
| 200 | + assertStructComparison(-1, struct1, struct2); |
| 201 | + |
| 202 | + // Act & Assert: Reverse comparison |
| 203 | + assertStructComparison(1, struct2, struct1); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + public void testCompareStructBothNull() { |
| 208 | + // Arrange: Set both structs to null |
| 209 | + struct1 = null; |
| 210 | + struct2 = null; |
| 211 | + |
| 212 | + // Act & Assert: Expect equality |
| 213 | + assertStructComparison(0, struct1, struct2); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void testCompareUnsupportedStructType() { |
| 218 | + // Act & Assert: Expect AthenaConnectorException |
| 219 | + AthenaConnectorException exception = assertThrows( |
| 220 | + AthenaConnectorException.class, |
| 221 | + () -> ArrowTypeComparator.compare(unsupportedType, struct1, struct2) |
| 222 | + ); |
| 223 | + assertEquals("Unknown type INTERVALYEAR object: class java.util.HashMap", exception.getMessage()); |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + public void testUnsupportedType() { |
| 228 | + AthenaConnectorException exception = assertThrows( |
| 229 | + AthenaConnectorException.class, |
| 230 | + () -> ArrowTypeComparator.compare(unsupportedType, "lhs", "rhs") |
| 231 | + ); |
| 232 | + assertEquals("Unknown type INTERVALYEAR object: class java.lang.String", exception.getMessage()); |
| 233 | + } |
| 234 | + |
| 235 | + @Test |
| 236 | + public void testCompareNulls() { |
| 237 | + assertComparison(0, intType, null, null); |
| 238 | + assertComparison(1, intType, null, 10); |
| 239 | + assertComparison(-1, intType, 10, null); |
| 240 | + } |
| 241 | +} |
0 commit comments