|
17 | 17 | import com.clickhouse.data.ClickHouseFormat; |
18 | 18 | import com.clickhouse.data.ClickHouseVersion; |
19 | 19 | import org.apache.commons.lang3.RandomStringUtils; |
| 20 | +import org.testng.Assert; |
20 | 21 | import org.testng.annotations.BeforeMethod; |
| 22 | +import org.testng.annotations.DataProvider; |
21 | 23 | import org.testng.annotations.Test; |
22 | 24 |
|
23 | 25 | import java.io.IOException; |
@@ -254,6 +256,86 @@ public void writeMissingFieldsTest() throws Exception { |
254 | 256 | } |
255 | 257 |
|
256 | 258 |
|
| 259 | + @Test (groups = { "integration" }) |
| 260 | + public void writeEnumZeroLikeValuesTest() throws Exception { |
| 261 | + String tableName = "rowBinaryFormatWriterTest_enumZeroLike_" + UUID.randomUUID().toString().replace('-', '_'); |
| 262 | + String tableCreate = "CREATE TABLE \"" + tableName + "\" " + |
| 263 | + " (id Int32, " + |
| 264 | + " e8 Enum8('' = 0, 'a' = 1, 'neg' = -5), " + |
| 265 | + " e16 Enum16('zero' = 0, 'big' = 30000, 'nb' = -20000), " + |
| 266 | + " tail Float64" + |
| 267 | + " ) Engine = MergeTree ORDER BY id"; |
| 268 | + |
| 269 | + Field[][] rows = new Field[][] { |
| 270 | + // Zero-like written by enum name: an empty-string name and a named zero both map to 0. |
| 271 | + {new Field("id", 1), new Field("e8", ""), new Field("e16", "zero"), new Field("tail", 1.5)}, |
| 272 | + // The same zero-like members written by their underlying int 0. |
| 273 | + {new Field("id", 2), new Field("e8", 0).set(""), new Field("e16", 0).set("zero"), new Field("tail", 2.5)}, |
| 274 | + // Negative members (Enum8/Enum16 are signed) written by name. |
| 275 | + {new Field("id", 3), new Field("e8", "neg"), new Field("e16", "nb"), new Field("tail", 3.5)}, |
| 276 | + // The same negative members written by their underlying int. |
| 277 | + {new Field("id", 4), new Field("e8", -5).set("neg"), new Field("e16", -20000).set("nb"), new Field("tail", 4.5)}, |
| 278 | + // Ordinary positive members. |
| 279 | + {new Field("id", 5), new Field("e8", "a"), new Field("e16", "big"), new Field("tail", 5.5)}, |
| 280 | + }; |
| 281 | + |
| 282 | + writeTest(tableName, tableCreate, rows); |
| 283 | + } |
| 284 | + |
| 285 | + |
| 286 | + // A null element in a non-nullable Enum sub-column reaches serializeEnumData directly (no |
| 287 | + // per-element null preamble is written for non-nullable nested columns), which is the path |
| 288 | + // that used to throw an opaque NullPointerException. Covers every container seam that routes |
| 289 | + // an element through serializeEnumData: Array (level 1), Tuple, Map value, and a nested Array. |
| 290 | + @DataProvider(name = "nullEnumContainers") |
| 291 | + private Object[][] nullEnumContainers() { |
| 292 | + return new Object[][] { |
| 293 | + {"Array(Enum8('a' = 1, 'b' = 2))", Arrays.asList("a", null)}, |
| 294 | + {"Tuple(Enum8('a' = 1, 'b' = 2), Int32)", Arrays.asList(null, 7)}, |
| 295 | + {"Map(String, Enum16('a' = 1, 'b' = 2))", singleEntryMap("k", null)}, |
| 296 | + {"Array(Array(Enum8('a' = 1, 'b' = 2)))", Arrays.asList(Arrays.asList("a", null))}, |
| 297 | + }; |
| 298 | + } |
| 299 | + |
| 300 | + @Test (groups = { "integration" }, dataProvider = "nullEnumContainers") |
| 301 | + public void writeNullEnumInContainerThrowsTest(String columnType, Object valueWithNullEnum) throws Exception { |
| 302 | + String tableName = "rowBinaryFormatWriterTest_enumContainerNull_" + UUID.randomUUID().toString().replace('-', '_'); |
| 303 | + initTable(tableName, |
| 304 | + "CREATE TABLE \"" + tableName + "\" (id Int32, c " + columnType + ") Engine = MergeTree ORDER BY id", |
| 305 | + new CommandSettings()); |
| 306 | + TableSchema schema = client.getTableSchema(tableName); |
| 307 | + ClickHouseFormat format = ClickHouseFormat.RowBinaryWithDefaults; |
| 308 | + |
| 309 | + Exception thrown = null; |
| 310 | + try (InsertResponse response = client.insert(tableName, out -> { |
| 311 | + RowBinaryFormatWriter w = new RowBinaryFormatWriter(out, schema, format); |
| 312 | + w.setValue(schema.nameToColumnIndex("id"), 1); |
| 313 | + w.setValue(schema.nameToColumnIndex("c"), valueWithNullEnum); |
| 314 | + w.commitRow(); |
| 315 | + }, format, settings).get(EXECUTE_CMD_TIMEOUT, TimeUnit.SECONDS)) { |
| 316 | + // The insert must not succeed: a null Enum element in a non-nullable container is invalid. |
| 317 | + } catch (Exception e) { |
| 318 | + thrown = e; |
| 319 | + } |
| 320 | + |
| 321 | + Assert.assertNotNull(thrown, "Expected the insert to fail for a null Enum element in " + columnType); |
| 322 | + boolean clearMessage = false; |
| 323 | + for (Throwable t = thrown; t != null; t = t.getCause()) { |
| 324 | + if (t.getMessage() != null && t.getMessage().contains("Cannot write NULL into non-nullable Enum column")) { |
| 325 | + clearMessage = true; |
| 326 | + break; |
| 327 | + } |
| 328 | + } |
| 329 | + Assert.assertTrue(clearMessage, "Expected a clear non-nullable Enum error for " + columnType + ", but got: " + thrown); |
| 330 | + } |
| 331 | + |
| 332 | + private static Map<String, Object> singleEntryMap(String key, Object value) { |
| 333 | + Map<String, Object> map = new HashMap<>(); |
| 334 | + map.put(key, value); |
| 335 | + return map; |
| 336 | + } |
| 337 | + |
| 338 | + |
257 | 339 | @Test (groups = { "integration" }) |
258 | 340 | public void writeNumbersTest() throws Exception { |
259 | 341 | String tableName = "rowBinaryFormatWriterTest_writeNumbersTest_" + UUID.randomUUID().toString().replace('-', '_'); |
|
0 commit comments