|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.fluss.lake.iceberg.source; |
| 20 | + |
| 21 | +import org.apache.iceberg.Schema; |
| 22 | +import org.apache.iceberg.data.GenericRecord; |
| 23 | +import org.apache.iceberg.data.Record; |
| 24 | +import org.apache.iceberg.types.Types; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import java.math.BigDecimal; |
| 29 | +import java.nio.ByteBuffer; |
| 30 | +import java.time.LocalDateTime; |
| 31 | +import java.time.OffsetDateTime; |
| 32 | +import java.time.ZoneOffset; |
| 33 | + |
| 34 | +import static org.apache.iceberg.types.Types.NestedField.optional; |
| 35 | +import static org.apache.iceberg.types.Types.NestedField.required; |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | + |
| 38 | +/** Unit tests for {@link IcebergRecordAsFlussRow}. */ |
| 39 | +class IcebergRecordAsFlussRowTest { |
| 40 | + |
| 41 | + private IcebergRecordAsFlussRow icebergRecordAsFlussRow; |
| 42 | + private Record record; |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void setUp() { |
| 46 | + icebergRecordAsFlussRow = new IcebergRecordAsFlussRow(); |
| 47 | + |
| 48 | + // Create a schema with various data types |
| 49 | + Schema schema = |
| 50 | + new Schema( |
| 51 | + required(1, "id", Types.LongType.get()), |
| 52 | + optional(2, "name", Types.StringType.get()), |
| 53 | + optional(3, "age", Types.IntegerType.get()), |
| 54 | + optional(4, "salary", Types.DoubleType.get()), |
| 55 | + optional(5, "is_active", Types.BooleanType.get()), |
| 56 | + optional(6, "tiny_int", Types.IntegerType.get()), |
| 57 | + optional(7, "small_int", Types.IntegerType.get()), |
| 58 | + optional(8, "float_val", Types.FloatType.get()), |
| 59 | + optional(9, "decimal_val", Types.DecimalType.of(10, 2)), |
| 60 | + optional(10, "timestamp_ntz", Types.TimestampType.withoutZone()), |
| 61 | + optional(11, "timestamp_ltz", Types.TimestampType.withZone()), |
| 62 | + optional(12, "binary_data", Types.BinaryType.get()), |
| 63 | + optional(13, "char_data", Types.StringType.get()), |
| 64 | + // System columns |
| 65 | + required(14, "__bucket", Types.IntegerType.get()), |
| 66 | + required(15, "__offset", Types.LongType.get()), |
| 67 | + required(16, "__timestamp", Types.TimestampType.withZone())); |
| 68 | + |
| 69 | + record = GenericRecord.create(schema); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testGetFieldCount() { |
| 74 | + // Set up record with data |
| 75 | + record.setField("id", 1L); |
| 76 | + record.setField("name", "John"); |
| 77 | + record.setField("age", 30); |
| 78 | + record.setField("__bucket", 1); |
| 79 | + record.setField("__offset", 100L); |
| 80 | + record.setField("__timestamp", OffsetDateTime.now(ZoneOffset.UTC)); |
| 81 | + |
| 82 | + icebergRecordAsFlussRow.setIcebergRecord(record); |
| 83 | + |
| 84 | + // Should return count excluding system columns (3 system columns) |
| 85 | + assertThat(icebergRecordAsFlussRow.getFieldCount()).isEqualTo(13); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testIsNullAt() { |
| 90 | + record.setField("id", 1L); |
| 91 | + record.setField("name", null); // null value |
| 92 | + record.setField("age", 30); |
| 93 | + |
| 94 | + icebergRecordAsFlussRow.setIcebergRecord(record); |
| 95 | + |
| 96 | + assertThat(icebergRecordAsFlussRow.isNullAt(0)).isFalse(); // id |
| 97 | + assertThat(icebergRecordAsFlussRow.isNullAt(1)).isTrue(); // name |
| 98 | + assertThat(icebergRecordAsFlussRow.isNullAt(2)).isFalse(); // age |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void testAllDataTypes() { |
| 103 | + // Set up all data types with test values |
| 104 | + record.setField("id", 12345L); |
| 105 | + record.setField("name", "John Doe"); |
| 106 | + record.setField("age", 30); |
| 107 | + record.setField("salary", 50000.50); |
| 108 | + record.setField("is_active", true); |
| 109 | + record.setField("tiny_int", 127); |
| 110 | + record.setField("small_int", 32767); |
| 111 | + record.setField("float_val", 3.14f); |
| 112 | + record.setField("decimal_val", new BigDecimal("123.45")); |
| 113 | + record.setField("timestamp_ntz", LocalDateTime.of(2023, 12, 25, 10, 30, 45)); |
| 114 | + record.setField( |
| 115 | + "timestamp_ltz", OffsetDateTime.of(2023, 12, 25, 10, 30, 45, 0, ZoneOffset.UTC)); |
| 116 | + record.setField("binary_data", ByteBuffer.wrap("Hello World".getBytes())); |
| 117 | + record.setField("char_data", "Hello"); |
| 118 | + // System columns |
| 119 | + record.setField("__bucket", 1); |
| 120 | + record.setField("__offset", 100L); |
| 121 | + record.setField("__timestamp", OffsetDateTime.now(ZoneOffset.UTC)); |
| 122 | + |
| 123 | + icebergRecordAsFlussRow.setIcebergRecord(record); |
| 124 | + |
| 125 | + // Test all data type conversions |
| 126 | + assertThat(icebergRecordAsFlussRow.getLong(0)).isEqualTo(12345L); // id |
| 127 | + assertThat(icebergRecordAsFlussRow.getString(1).toString()).isEqualTo("John Doe"); // name |
| 128 | + assertThat(icebergRecordAsFlussRow.getInt(2)).isEqualTo(30); // age |
| 129 | + assertThat(icebergRecordAsFlussRow.getDouble(3)).isEqualTo(50000.50); // salary |
| 130 | + assertThat(icebergRecordAsFlussRow.getBoolean(4)).isTrue(); // is_active |
| 131 | + assertThat(icebergRecordAsFlussRow.getByte(5)).isEqualTo((byte) 127); // tiny_int |
| 132 | + assertThat(icebergRecordAsFlussRow.getShort(6)).isEqualTo((short) 32767); // small_int |
| 133 | + assertThat(icebergRecordAsFlussRow.getFloat(7)).isEqualTo(3.14f); // float_val |
| 134 | + assertThat(icebergRecordAsFlussRow.getDecimal(8, 10, 2).toBigDecimal()) |
| 135 | + .isEqualTo(new BigDecimal("123.45")); // decimal_val |
| 136 | + assertThat(icebergRecordAsFlussRow.getTimestampNtz(9, 3).toLocalDateTime()) |
| 137 | + .isEqualTo(LocalDateTime.of(2023, 12, 25, 10, 30, 45)); // timestamp_ntz |
| 138 | + assertThat(icebergRecordAsFlussRow.getTimestampLtz(10, 3).toInstant()) |
| 139 | + .isEqualTo( |
| 140 | + OffsetDateTime.of(2023, 12, 25, 10, 30, 45, 0, ZoneOffset.UTC) |
| 141 | + .toInstant()); // timestamp_ltz |
| 142 | + assertThat(icebergRecordAsFlussRow.getBytes(11)) |
| 143 | + .isEqualTo("Hello World".getBytes()); // binary_data |
| 144 | + assertThat(icebergRecordAsFlussRow.getChar(12, 10).toString()) |
| 145 | + .isEqualTo("Hello"); // char_data |
| 146 | + |
| 147 | + // Test field count (excluding system columns) |
| 148 | + assertThat(icebergRecordAsFlussRow.getFieldCount()).isEqualTo(13); |
| 149 | + } |
| 150 | +} |
0 commit comments