|
3 | 3 |
|
4 | 4 | package dev.vortex.spark; |
5 | 5 |
|
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
6 | 8 | import static org.junit.jupiter.api.Assertions.assertThrows; |
7 | 9 | import static org.junit.jupiter.api.Assertions.assertTrue; |
8 | 10 |
|
9 | 11 | import java.nio.file.Path; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.List; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import org.apache.spark.sql.Column; |
| 16 | +import org.apache.spark.sql.Dataset; |
| 17 | +import org.apache.spark.sql.Row; |
| 18 | +import org.apache.spark.sql.SaveMode; |
10 | 19 | import org.apache.spark.sql.SparkSession; |
| 20 | +import org.apache.spark.sql.functions; |
| 21 | +import org.apache.spark.sql.types.DataTypes; |
| 22 | +import org.apache.spark.sql.types.StructType; |
11 | 23 | import org.junit.jupiter.api.AfterAll; |
12 | 24 | import org.junit.jupiter.api.BeforeAll; |
13 | 25 | import org.junit.jupiter.api.DisplayName; |
14 | 26 | import org.junit.jupiter.api.Test; |
15 | 27 | import org.junit.jupiter.api.TestInstance; |
16 | 28 | import org.junit.jupiter.api.io.TempDir; |
17 | 29 |
|
18 | | -/** Tests for schema inference failure reporting in {@link VortexDataSourceV2}. */ |
| 30 | +/** Tests for the schema {@link VortexDataSourceV2} reads tables with. */ |
19 | 31 | @TestInstance(TestInstance.Lifecycle.PER_CLASS) |
20 | 32 | public final class VortexDataSourceInferSchemaTest { |
21 | 33 |
|
@@ -56,6 +68,130 @@ public void inferSchemaFailureNamesThePath() { |
56 | 68 | assertTrue(allMessages.contains(emptyDir), "error should name the offending path, got: " + allMessages); |
57 | 69 | } |
58 | 70 |
|
| 71 | + @Test |
| 72 | + @DisplayName("asNullable makes fields nullable at every depth, keeping names and types") |
| 73 | + public void asNullableReachesEveryDepth() { |
| 74 | + StructType key = new StructType().add("k", DataTypes.IntegerType, false); |
| 75 | + StructType element = new StructType().add("e", DataTypes.LongType, false); |
| 76 | + StructType schema = new StructType() |
| 77 | + .add("id", DataTypes.LongType, false) |
| 78 | + .add("arr", DataTypes.createArrayType(element, false), false) |
| 79 | + .add( |
| 80 | + "m", |
| 81 | + DataTypes.createMapType(key, DataTypes.createArrayType(DataTypes.StringType, false), false), |
| 82 | + false); |
| 83 | + |
| 84 | + StructType nullable = VortexDataSourceV2.asNullable(schema); |
| 85 | + |
| 86 | + assertEquals( |
| 87 | + "struct<id:bigint,arr:array<struct<e:bigint>>,m:map<struct<k:int>,array<string>>>", |
| 88 | + nullable.simpleString()); |
| 89 | + assertNoNonNullableFields(nullable); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @DisplayName("A null struct reads correctly when schema inference picks a file without nulls") |
| 94 | + public void inferFromFileWithoutNulls() { |
| 95 | + assertFilesDifferingInNullabilityRead(false); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + @DisplayName("A null struct reads correctly when schema inference picks the file with nulls") |
| 100 | + public void inferFromFileWithNulls() { |
| 101 | + assertFilesDifferingInNullabilityRead(true); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + @DisplayName("A null struct reads correctly under a user schema that declares it non-nullable") |
| 106 | + public void userSchemaDeclaringNonNullable() { |
| 107 | + Column s = functions.struct( |
| 108 | + functions.col("id").plus(100).alias("a"), |
| 109 | + functions.col("id").plus(200).alias("b")); |
| 110 | + StructType nonNullable = |
| 111 | + spark.range(0, 1).select(functions.col("id"), s.alias("s")).schema(); |
| 112 | + String withNull = write( |
| 113 | + spark.range(2, 4) |
| 114 | + .select( |
| 115 | + functions.col("id"), |
| 116 | + functions |
| 117 | + .when(functions.col("id").equalTo(3), functions.lit(null)) |
| 118 | + .otherwise(s) |
| 119 | + .alias("s")), |
| 120 | + "user-schema"); |
| 121 | + |
| 122 | + Dataset<Row> table = spark.read().schema(nonNullable).format("vortex").load(withNull); |
| 123 | + |
| 124 | + assertEquals( |
| 125 | + "struct<id:bigint,s:struct<a:bigint,b:bigint>>", table.schema().simpleString()); |
| 126 | + assertNoNonNullableFields(table.schema()); |
| 127 | + assertEquals( |
| 128 | + Arrays.asList(Arrays.asList(2L, 202L, false), Arrays.asList(3L, null, true)), |
| 129 | + rowsOf(table.select( |
| 130 | + functions.col("id"), |
| 131 | + functions.col("s.b"), |
| 132 | + functions.col("s").isNull()))); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Reads two files that differ only in whether struct {@code s} has nulls. Schema inference reads the last path, so |
| 137 | + * {@code inferFromNulls} picks the file it infers from. |
| 138 | + */ |
| 139 | + private void assertFilesDifferingInNullabilityRead(boolean inferFromNulls) { |
| 140 | + Column s = functions.struct( |
| 141 | + functions.col("id").plus(100).alias("a"), |
| 142 | + functions.col("id").plus(200).alias("b")); |
| 143 | + String noNulls = |
| 144 | + write(spark.range(0, 2).select(functions.col("id"), s.alias("s")), "no-nulls-" + inferFromNulls); |
| 145 | + String withNull = write( |
| 146 | + spark.range(2, 4) |
| 147 | + .select( |
| 148 | + functions.col("id"), |
| 149 | + functions |
| 150 | + .when(functions.col("id").equalTo(3), functions.lit(null)) |
| 151 | + .otherwise(s) |
| 152 | + .alias("s")), |
| 153 | + "with-null-" + inferFromNulls); |
| 154 | + |
| 155 | + Dataset<Row> table = inferFromNulls |
| 156 | + ? spark.read().format("vortex").load(noNulls, withNull) |
| 157 | + : spark.read().format("vortex").load(withNull, noNulls); |
| 158 | + |
| 159 | + assertEquals( |
| 160 | + "struct<id:bigint,s:struct<a:bigint,b:bigint>>", table.schema().simpleString()); |
| 161 | + assertNoNonNullableFields(table.schema()); |
| 162 | + assertEquals( |
| 163 | + Arrays.asList( |
| 164 | + Arrays.asList(0L, 200L, false), |
| 165 | + Arrays.asList(1L, 201L, false), |
| 166 | + Arrays.asList(2L, 202L, false), |
| 167 | + Arrays.asList(3L, null, true)), |
| 168 | + rowsOf(table.select( |
| 169 | + functions.col("id"), |
| 170 | + functions.col("s.b"), |
| 171 | + functions.col("s").isNull()))); |
| 172 | + } |
| 173 | + |
| 174 | + /** Asserts that no field, array element or map value in {@code schema} is non-nullable. */ |
| 175 | + static void assertNoNonNullableFields(StructType schema) { |
| 176 | + String json = schema.json(); |
| 177 | + for (String flag : |
| 178 | + new String[] {"\"nullable\":false", "\"containsNull\":false", "\"valueContainsNull\":false"}) { |
| 179 | + assertFalse(json.contains(flag), "expected every field to be nullable: " + json); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private String write(Dataset<Row> rows, String name) { |
| 184 | + String path = tempDir.resolve(name).toUri().toString(); |
| 185 | + rows.coalesce(1).write().format("vortex").mode(SaveMode.Overwrite).save(path); |
| 186 | + return path; |
| 187 | + } |
| 188 | + |
| 189 | + private static List<List<Object>> rowsOf(Dataset<Row> rows) { |
| 190 | + return rows.orderBy("id").collectAsList().stream() |
| 191 | + .map(row -> Arrays.asList(row.get(0), row.get(1), row.get(2))) |
| 192 | + .collect(Collectors.toList()); |
| 193 | + } |
| 194 | + |
59 | 195 | /** Concatenates the messages of the whole cause chain, since Spark may wrap data source exceptions. */ |
60 | 196 | private static String messagesOf(Throwable thrown) { |
61 | 197 | StringBuilder sb = new StringBuilder(); |
|
0 commit comments