|
| 1 | +// |
| 2 | +// Copyright (c) 2016-2024 Deephaven Data Labs and Patent Pending |
| 3 | +// |
| 4 | +package io.deephaven.iceberg; |
| 5 | + |
| 6 | +import io.deephaven.engine.table.ColumnDefinition; |
| 7 | +import io.deephaven.engine.table.Table; |
| 8 | +import io.deephaven.engine.table.TableDefinition; |
| 9 | +import io.deephaven.engine.testutil.TstUtils; |
| 10 | +import io.deephaven.engine.util.TableTools; |
| 11 | +import io.deephaven.iceberg.sqlite.DbResource; |
| 12 | +import io.deephaven.iceberg.util.IcebergCatalogAdapter; |
| 13 | +import io.deephaven.iceberg.util.IcebergTableAdapter; |
| 14 | +import org.apache.iceberg.PartitionField; |
| 15 | +import org.apache.iceberg.PartitionSpec; |
| 16 | +import org.apache.iceberg.Snapshot; |
| 17 | +import org.apache.iceberg.catalog.Namespace; |
| 18 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Tag; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.time.LocalDateTime; |
| 24 | +import java.net.URISyntaxException; |
| 25 | +import java.util.List; |
| 26 | +import static io.deephaven.util.QueryConstants.NULL_DOUBLE; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +/** |
| 31 | + * This test shows that we can integrate with data written by <a href="https://py.iceberg.apache.org/">pyiceberg</a>. |
| 32 | + * See TESTING.md and generate-pyiceberg-2.py for more details. |
| 33 | + */ |
| 34 | +@Tag("security-manager-allow") |
| 35 | +class Pyiceberg2Test { |
| 36 | + private static final Namespace NAMESPACE = Namespace.of("trading"); |
| 37 | + private static final TableIdentifier TRADING_DATA = TableIdentifier.of(NAMESPACE, "data"); |
| 38 | + |
| 39 | + // This will need to be updated if the data is regenerated |
| 40 | + private static final long SNAPSHOT_1_ID = 2806418501596315192L; |
| 41 | + |
| 42 | + private static final TableDefinition TABLE_DEFINITION = TableDefinition.of( |
| 43 | + ColumnDefinition.fromGenericType("datetime", LocalDateTime.class), |
| 44 | + ColumnDefinition.ofString("symbol").withPartitioning(), |
| 45 | + ColumnDefinition.ofDouble("bid"), |
| 46 | + ColumnDefinition.ofDouble("ask")); |
| 47 | + |
| 48 | + private IcebergCatalogAdapter catalogAdapter; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void setUp() throws URISyntaxException { |
| 52 | + catalogAdapter = DbResource.openCatalog("pyiceberg-2"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void catalogInfo() { |
| 57 | + assertThat(catalogAdapter.listNamespaces()).containsExactly(NAMESPACE); |
| 58 | + assertThat(catalogAdapter.listTables(NAMESPACE)).containsExactly(TRADING_DATA); |
| 59 | + |
| 60 | + final IcebergTableAdapter tableAdapter = catalogAdapter.loadTable(TRADING_DATA); |
| 61 | + final List<Snapshot> snapshots = tableAdapter.listSnapshots(); |
| 62 | + assertThat(snapshots).hasSize(1); |
| 63 | + { |
| 64 | + final Snapshot snapshot = snapshots.get(0); |
| 65 | + assertThat(snapshot.parentId()).isNull(); |
| 66 | + assertThat(snapshot.schemaId()).isEqualTo(0); |
| 67 | + assertThat(snapshot.sequenceNumber()).isEqualTo(1L); |
| 68 | + assertThat(snapshot.snapshotId()).isEqualTo(SNAPSHOT_1_ID); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testDefinition() { |
| 74 | + final IcebergTableAdapter tableAdapter = catalogAdapter.loadTable(TRADING_DATA); |
| 75 | + final TableDefinition td = tableAdapter.definition(); |
| 76 | + assertThat(td).isEqualTo(TABLE_DEFINITION); |
| 77 | + |
| 78 | + // Check the partition spec |
| 79 | + final PartitionSpec partitionSpec = tableAdapter.icebergTable().spec(); |
| 80 | + assertThat(partitionSpec.fields().size()).isEqualTo(2); |
| 81 | + final PartitionField firstPartitionField = partitionSpec.fields().get(0); |
| 82 | + assertThat(firstPartitionField.name()).isEqualTo("datetime_day"); |
| 83 | + assertThat(firstPartitionField.transform().toString()).isEqualTo("day"); |
| 84 | + |
| 85 | + final PartitionField secondPartitionField = partitionSpec.fields().get(1); |
| 86 | + assertThat(secondPartitionField.name()).isEqualTo("symbol"); |
| 87 | + assertThat(secondPartitionField.transform().toString()).isEqualTo("identity"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void testData() { |
| 92 | + final IcebergTableAdapter tableAdapter = catalogAdapter.loadTable(TRADING_DATA); |
| 93 | + final Table fromIceberg = tableAdapter.table(); |
| 94 | + assertThat(fromIceberg.size()).isEqualTo(5); |
| 95 | + final Table expectedData = TableTools.newTable(TABLE_DEFINITION, |
| 96 | + TableTools.col("datetime", |
| 97 | + LocalDateTime.of(2024, 11, 27, 10, 0, 0), |
| 98 | + LocalDateTime.of(2024, 11, 27, 10, 0, 0), |
| 99 | + LocalDateTime.of(2024, 11, 26, 10, 1, 0), |
| 100 | + LocalDateTime.of(2024, 11, 26, 10, 2, 0), |
| 101 | + LocalDateTime.of(2024, 11, 28, 10, 3, 0)), |
| 102 | + TableTools.stringCol("symbol", "AAPL", "MSFT", "GOOG", "AMZN", "MSFT"), |
| 103 | + TableTools.doubleCol("bid", 150.25, 150.25, 2800.75, 3400.5, NULL_DOUBLE), |
| 104 | + TableTools.doubleCol("ask", 151.0, 151.0, 2810.5, 3420.0, 250.0)); |
| 105 | + TstUtils.assertTableEquals(expectedData.sort("datetime", "symbol"), |
| 106 | + fromIceberg.sort("datetime", "symbol")); |
| 107 | + } |
| 108 | +} |
0 commit comments