-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathUCDeltaStreamingEdgeDataReadTest.java
More file actions
305 lines (273 loc) · 12.1 KB
/
Copy pathUCDeltaStreamingEdgeDataReadTest.java
File metadata and controls
305 lines (273 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Copyright (2026) The Delta Lake Project Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sparkuctest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.streaming.StreamingQuery;
import org.apache.spark.sql.streaming.Trigger;
import org.junit.jupiter.api.io.TempDir;
/**
* Reproduces the doc-claimed MANAGED-specific edge-data streaming bugs (NPE in
* OnHeapColumnVector.putNotNulls for null columns / boolean nulls / complex types) over the Unity
* Catalog catalog path, alongside the EXTERNAL counterparts.
*
* <p>Task D (`V2StreamingEdgeDataReadTest`) already proved these don't fire on EXTERNAL via
* file-path access (`dsv2.delta.<path>`). This suite exercises the same shapes through Unity
* Catalog's `unity.default.<table>` access path for both `EXTERNAL` and `MANAGED` table types.
*
* <p>Each test runs twice via `@TestAllTableTypes`. The MANAGED variant is the relevant repro
* target; the EXTERNAL variant via UC catalog is a useful control (UC catalog read path may differ
* from raw file-path read path even for external tables).
*/
public class UCDeltaStreamingEdgeDataReadTest extends UCDeltaTableIntegrationBaseTest {
@TempDir private Path tempDir;
private int checkpointCount;
/**
* Allocates a fresh local checkpoint directory. Checkpoints must be on local FS since UC server
* holds the cloud credentials, not Spark.
*/
private String checkpoint() throws IOException {
Path ckDir = tempDir.resolve("ck-" + checkpointCount++);
Files.createDirectory(ckDir);
return ckDir.toString();
}
// -------------------------------------------------------------------------
// Case 2: All-null INT column
// Doc claim: NPE in OnHeapColumnVector.putNotNulls (this.nulls uninitialized)
// on MANAGED. EXTERNAL was clean (Task D).
// -------------------------------------------------------------------------
@TestAllTableTypes
public void testManagedNullsInColumns(TableType tableType) throws Exception {
withNewTable(
"edge_all_null_col",
"v INT",
tableType,
tableName -> {
sql("INSERT INTO %s VALUES (NULL), (NULL), (NULL)", tableName);
String queryName =
"edge_all_null_" + tableType.name().toLowerCase() + "_" + checkpointCount;
Dataset<Row> input = spark().readStream().format("delta").table(tableName);
// AvailableNow gives a deterministic single batch over the existing 3 nulls.
input
.writeStream()
.format("memory")
.queryName(queryName)
.outputMode("append")
.trigger(Trigger.AvailableNow())
.option("checkpointLocation", checkpoint())
.start()
.awaitTermination();
List<Row> rows = spark().sql("SELECT * FROM " + queryName).collectAsList();
assertEquals(3, rows.size(), "expected 3 null rows; got: " + rows);
for (Row r : rows) {
assertEquals(true, r.isNullAt(0), "expected null value, got: " + r);
}
spark().sql("DROP VIEW IF EXISTS " + queryName);
});
}
// -------------------------------------------------------------------------
// Case 3: BOOLEAN column with nulls
// Doc claim: same NPE through bit-packing path on MANAGED.
// -------------------------------------------------------------------------
@TestAllTableTypes
public void testManagedBooleanNulls(TableType tableType) throws Exception {
withNewTable(
"edge_bool_nulls",
"b BOOLEAN",
tableType,
tableName -> {
sql("INSERT INTO %s VALUES (true), (false), (NULL), (true), (NULL)", tableName);
String queryName =
"edge_bool_nulls_" + tableType.name().toLowerCase() + "_" + checkpointCount;
spark()
.readStream()
.format("delta")
.table(tableName)
.writeStream()
.format("memory")
.queryName(queryName)
.outputMode("append")
.trigger(Trigger.AvailableNow())
.option("checkpointLocation", checkpoint())
.start()
.awaitTermination();
List<Row> rows = spark().sql("SELECT * FROM " + queryName).collectAsList();
assertEquals(5, rows.size(), "expected 5 rows; got: " + rows);
long nullCount = rows.stream().filter(r -> r.isNullAt(0)).count();
assertEquals(2L, nullCount, "expected 2 null booleans; got: " + rows);
spark().sql("DROP VIEW IF EXISTS " + queryName);
});
}
// -------------------------------------------------------------------------
// Case 4a: Complex types initial-snapshot (ARRAY / MAP / STRUCT)
// Sanity: Task D showed EXTERNAL passes initial-snapshot. Confirm same on UC.
// -------------------------------------------------------------------------
@TestAllTableTypes
public void testManagedComplexTypesInitialSnapshot(TableType tableType) throws Exception {
withNewTable(
"edge_complex_initial",
"id INT, arr ARRAY<INT>, mp MAP<STRING,INT>, st STRUCT<a: INT, b: STRING>",
tableType,
tableName -> {
sql(
"INSERT INTO %s VALUES "
+ "(1, array(1, 2, 3), map('k1', 10), named_struct('a', 1, 'b', 'foo')), "
+ "(2, array(), map(), named_struct('a', 2, 'b', 'bar')), "
+ "(3, NULL, NULL, NULL)",
tableName);
String queryName =
"edge_complex_init_" + tableType.name().toLowerCase() + "_" + checkpointCount;
spark()
.readStream()
.format("delta")
.table(tableName)
.writeStream()
.format("memory")
.queryName(queryName)
.outputMode("append")
.trigger(Trigger.AvailableNow())
.option("checkpointLocation", checkpoint())
.start()
.awaitTermination();
List<Row> rows = spark().sql("SELECT * FROM " + queryName).collectAsList();
assertEquals(3, rows.size(), "expected 3 rows; got: " + rows);
spark().sql("DROP VIEW IF EXISTS " + queryName);
});
}
// -------------------------------------------------------------------------
// Case 4b: Complex types INCREMENTAL (commits mid-stream)
// Doc claim: NPE in SparkMicroBatchStreaming for INCREMENTAL on MANAGED.
// -------------------------------------------------------------------------
@TestAllTableTypes
public void testManagedComplexTypesIncremental(TableType tableType) throws Exception {
withNewTable(
"edge_complex_incr",
"id INT, arr ARRAY<INT>, mp MAP<STRING,INT>, st STRUCT<a: INT, b: STRING>",
tableType,
tableName -> {
// Seed one initial row.
sql(
"INSERT INTO %s VALUES (1, array(1,2), map('a',1), named_struct('a',1,'b','x'))",
tableName);
String queryName =
"edge_complex_incr_" + tableType.name().toLowerCase() + "_" + checkpointCount;
StreamingQuery query = null;
try {
// Continuous stream: process initial commit, then commit two more rows mid-stream
// to exercise the INCREMENTAL micro-batch path the doc flagged.
query =
spark()
.readStream()
.format("delta")
.table(tableName)
.writeStream()
.format("memory")
.queryName(queryName)
.outputMode("append")
.option("checkpointLocation", checkpoint())
.start();
query.processAllAvailable();
sql(
"INSERT INTO %s VALUES (2, array(3,4,5), map('b',2,'c',3), "
+ "named_struct('a',2,'b','y'))",
tableName);
query.processAllAvailable();
sql("INSERT INTO %s VALUES (3, NULL, NULL, NULL)", tableName);
query.processAllAvailable();
List<Row> rows = spark().sql("SELECT * FROM " + queryName).collectAsList();
assertEquals(3, rows.size(), "expected 3 rows; got: " + rows);
} finally {
if (query != null) {
query.processAllAvailable();
query.stop();
query.awaitTermination(10000);
}
spark().sql("DROP VIEW IF EXISTS " + queryName);
}
});
}
// -------------------------------------------------------------------------
// Bonus 1: Empty table — initial snapshot
// Task D showed EXTERNAL passes; confirm UC catalog (both types) is also clean.
// -------------------------------------------------------------------------
@TestAllTableTypes
public void testManagedEmptyTableInitialSnapshot(TableType tableType) throws Exception {
withNewTable(
"edge_empty_table",
"v INT",
tableType,
tableName -> {
// No data inserted (table is empty) -- but for MANAGED we need at least the
// catalogManaged metadata commit, which CREATE TABLE produces.
String queryName = "edge_empty_" + tableType.name().toLowerCase() + "_" + checkpointCount;
spark()
.readStream()
.format("delta")
.table(tableName)
.writeStream()
.format("memory")
.queryName(queryName)
.outputMode("append")
.trigger(Trigger.AvailableNow())
.option("checkpointLocation", checkpoint())
.start()
.awaitTermination();
List<Row> rows = spark().sql("SELECT * FROM " + queryName).collectAsList();
assertEquals(0, rows.size(), "empty table should yield 0 rows; got: " + rows);
spark().sql("DROP VIEW IF EXISTS " + queryName);
});
}
// -------------------------------------------------------------------------
// Bonus 2: NULL partition value (HIVE_DEFAULT_PARTITION sentinel)
// Task D's external file-path test passed; UC catalog's path-style table read may differ.
// -------------------------------------------------------------------------
@TestAllTableTypes
public void testManagedNullPartitionValue(TableType tableType) throws Exception {
withNewTable(
"edge_null_part",
"id INT, part STRING",
"part",
tableType,
null,
tableName -> {
sql("INSERT INTO %s VALUES (1, 'a'), (2, NULL), (3, 'c')", tableName);
String queryName =
"edge_null_part_" + tableType.name().toLowerCase() + "_" + checkpointCount;
spark()
.readStream()
.format("delta")
.table(tableName)
.writeStream()
.format("memory")
.queryName(queryName)
.outputMode("append")
.trigger(Trigger.AvailableNow())
.option("checkpointLocation", checkpoint())
.start()
.awaitTermination();
List<Row> rows = spark().sql("SELECT * FROM " + queryName).collectAsList();
assertEquals(3, rows.size(), "expected 3 rows; got: " + rows);
long nullParts = rows.stream().filter(r -> r.isNullAt(r.fieldIndex("part"))).count();
assertEquals(1L, nullParts, "expected 1 null partition value; got: " + rows);
spark().sql("DROP VIEW IF EXISTS " + queryName);
});
}
}