|
| 1 | +package io.github.dfa1.vortex.integration; |
| 2 | + |
| 3 | +import dev.hardwood.InputFile; |
| 4 | +import dev.hardwood.reader.ParquetFileReader; |
| 5 | +import dev.hardwood.reader.RowReader; |
| 6 | +import io.github.dfa1.vortex.core.DType; |
| 7 | +import io.github.dfa1.vortex.core.array.LongArray; |
| 8 | +import io.github.dfa1.vortex.core.array.VarBinArray; |
| 9 | +import io.github.dfa1.vortex.io.VortexReader; |
| 10 | +import io.github.dfa1.vortex.parquet.ParquetImporter; |
| 11 | +import io.github.dfa1.vortex.scan.ScanIterator; |
| 12 | +import io.github.dfa1.vortex.scan.ScanOptions; |
| 13 | +import io.github.dfa1.vortex.scan.ScanResult; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.io.TempDir; |
| 16 | + |
| 17 | +import java.net.URI; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.nio.file.StandardCopyOption; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 26 | + |
| 27 | +/// Round-trip: Parquet (via Hardwood) → Vortex (via ParquetImporter) → VortexReader. |
| 28 | +/// |
| 29 | +/// Fixture: delta_encoding_optional_column.parquet from apache/parquet-testing. |
| 30 | +/// TPC-DS customer table, 100 rows, INT64 + STRING columns, all nullable. |
| 31 | +class ParquetImportIntegrationTest { |
| 32 | + |
| 33 | + private static final String FIXTURE_URL = |
| 34 | + "https://raw.githubusercontent.com/apache/parquet-testing/master/data/" |
| 35 | + + "delta_encoding_optional_column.parquet"; |
| 36 | + |
| 37 | + private static final int EXPECTED_ROWS = 100; |
| 38 | + |
| 39 | + @Test |
| 40 | + void rowCountAndColumnNamesMatch(@TempDir Path tmp) throws Exception { |
| 41 | + // Given |
| 42 | + assumeNetworkAvailable(); |
| 43 | + Path parquetFile = download(tmp, "delta_encoding_optional_column.parquet"); |
| 44 | + Path vortexFile = tmp.resolve("out.vortex"); |
| 45 | + |
| 46 | + // When |
| 47 | + ParquetImporter.importParquet(parquetFile, vortexFile); |
| 48 | + |
| 49 | + // Then |
| 50 | + List<String> parquetColumns = parquetColumnNames(parquetFile); |
| 51 | + try (VortexReader reader = VortexReader.open(vortexFile)) { |
| 52 | + assertThat(reader.dtype()).isInstanceOf(DType.Struct.class); |
| 53 | + DType.Struct schema = (DType.Struct) reader.dtype(); |
| 54 | + assertThat(schema.fieldNames()).containsExactlyElementsOf(parquetColumns); |
| 55 | + |
| 56 | + long vortexRows = countRows(reader); |
| 57 | + assertThat(vortexRows).isEqualTo(EXPECTED_ROWS); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void longColumnValuesMatch(@TempDir Path tmp) throws Exception { |
| 63 | + // Given |
| 64 | + assumeNetworkAvailable(); |
| 65 | + Path parquetFile = download(tmp, "delta_encoding_optional_column.parquet"); |
| 66 | + Path vortexFile = tmp.resolve("out.vortex"); |
| 67 | + |
| 68 | + // When |
| 69 | + ParquetImporter.importParquet(parquetFile, vortexFile); |
| 70 | + |
| 71 | + // Then — c_customer_sk (INT64, nullable): first 3 values are 100, 99, 98 |
| 72 | + try (VortexReader reader = VortexReader.open(vortexFile); |
| 73 | + ScanIterator iter = reader.scan(ScanOptions.all())) { |
| 74 | + assertThat(iter.hasNext()).isTrue(); |
| 75 | + ScanResult first = iter.next(); |
| 76 | + LongArray col = first.column("c_customer_sk"); |
| 77 | + assertThat(col.getLong(0)).isEqualTo(100L); |
| 78 | + assertThat(col.getLong(1)).isEqualTo(99L); |
| 79 | + assertThat(col.getLong(2)).isEqualTo(98L); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void stringColumnValuesMatch(@TempDir Path tmp) throws Exception { |
| 85 | + // Given |
| 86 | + assumeNetworkAvailable(); |
| 87 | + Path parquetFile = download(tmp, "delta_encoding_optional_column.parquet"); |
| 88 | + Path vortexFile = tmp.resolve("out.vortex"); |
| 89 | + |
| 90 | + // When |
| 91 | + ParquetImporter.importParquet(parquetFile, vortexFile); |
| 92 | + |
| 93 | + // Then — c_first_name (STRING, nullable): first 3 values |
| 94 | + try (VortexReader reader = VortexReader.open(vortexFile); |
| 95 | + ScanIterator iter = reader.scan(ScanOptions.all())) { |
| 96 | + assertThat(iter.hasNext()).isTrue(); |
| 97 | + ScanResult first = iter.next(); |
| 98 | + VarBinArray col = first.column("c_first_name"); |
| 99 | + assertThat(col.getString(0)).isEqualTo("Jeannette"); |
| 100 | + assertThat(col.getString(1)).isEqualTo("Austin"); |
| 101 | + assertThat(col.getString(2)).isEqualTo("David"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void parquetAndVortexRowCountsAreEqual(@TempDir Path tmp) throws Exception { |
| 107 | + // Given |
| 108 | + assumeNetworkAvailable(); |
| 109 | + Path parquetFile = download(tmp, "delta_encoding_optional_column.parquet"); |
| 110 | + Path vortexFile = tmp.resolve("out.vortex"); |
| 111 | + |
| 112 | + // When |
| 113 | + ParquetImporter.importParquet(parquetFile, vortexFile); |
| 114 | + |
| 115 | + // Then — same row count when reading both with their native reader |
| 116 | + long parquetRows = countParquetRows(parquetFile); |
| 117 | + long vortexRows; |
| 118 | + try (VortexReader reader = VortexReader.open(vortexFile)) { |
| 119 | + vortexRows = countRows(reader); |
| 120 | + } |
| 121 | + assertThat(vortexRows).isEqualTo(parquetRows); |
| 122 | + } |
| 123 | + |
| 124 | + // ── helpers ────────────────────────────────────────────────────────────── |
| 125 | + |
| 126 | + private static long countRows(VortexReader reader) { |
| 127 | + long total = 0; |
| 128 | + try (ScanIterator iter = reader.scan(ScanOptions.all())) { |
| 129 | + while (iter.hasNext()) { |
| 130 | + total += iter.next().rowCount(); |
| 131 | + } |
| 132 | + } |
| 133 | + return total; |
| 134 | + } |
| 135 | + |
| 136 | + private static long countParquetRows(Path path) throws Exception { |
| 137 | + long total = 0; |
| 138 | + try (ParquetFileReader parquet = ParquetFileReader.open(InputFile.of(path)); |
| 139 | + RowReader rowReader = parquet.rowReader()) { |
| 140 | + while (rowReader.hasNext()) { |
| 141 | + rowReader.next(); |
| 142 | + total++; |
| 143 | + } |
| 144 | + } |
| 145 | + return total; |
| 146 | + } |
| 147 | + |
| 148 | + private static List<String> parquetColumnNames(Path path) throws Exception { |
| 149 | + try (ParquetFileReader parquet = ParquetFileReader.open(InputFile.of(path))) { |
| 150 | + List<String> names = new ArrayList<>(); |
| 151 | + parquet.getFileSchema().getColumns() |
| 152 | + .forEach(col -> names.add(col.name())); |
| 153 | + return names; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private static Path download(Path tmp, String name) throws Exception { |
| 158 | + Path cached = Path.of("/tmp/parquet-fixtures", name); |
| 159 | + if (Files.exists(cached)) { |
| 160 | + return cached; |
| 161 | + } |
| 162 | + Path dest = tmp.resolve(name); |
| 163 | + try (var in = URI.create(FIXTURE_URL).toURL().openStream()) { |
| 164 | + Files.copy(in, dest, StandardCopyOption.REPLACE_EXISTING); |
| 165 | + } |
| 166 | + return dest; |
| 167 | + } |
| 168 | + |
| 169 | + private static void assumeNetworkAvailable() { |
| 170 | + try { |
| 171 | + URI.create("https://raw.githubusercontent.com").toURL().openStream().close(); |
| 172 | + } catch (Exception e) { |
| 173 | + assumeTrue(false, "no network"); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments