|
16 | 16 |
|
17 | 17 | package za.co.absa.spark.commons.implicits |
18 | 18 |
|
19 | | -import org.apache.spark.sql.functions.lit |
| 19 | +import org.apache.spark.sql.functions.{array, lit, struct} |
| 20 | +import org.apache.spark.sql.types._ |
20 | 21 | import org.apache.spark.sql.{AnalysisException, DataFrame} |
21 | 22 | import org.scalatest.funsuite.AnyFunSuite |
22 | 23 | import za.co.absa.spark.commons.test.SparkTestBase |
@@ -260,6 +261,197 @@ class DataFrameImplicitsTest extends AnyFunSuite with SparkTestBase with JsonTes |
260 | 261 | } |
261 | 262 | } |
262 | 263 |
|
| 264 | + test("cast NullTypes to corresponding types by enforceTypeOnNullTypeFields") { |
| 265 | + val dfWithComplexTypes = spark.read.json(Seq(jsonF).toDS) |
| 266 | + .withColumn("nullShouldBeString", lit(null)) |
| 267 | + .withColumn("nullShouldBeInteger", lit(null)) |
| 268 | + .withColumn("nullShouldBeArrayOfIntegers", lit(null)) |
| 269 | + .withColumn("nullShouldBeArrayOfArraysOfIntegers", lit(null)) |
| 270 | + .withColumn("nullShouldBeArrayOfStructs", lit(null)) |
| 271 | + .withColumn("nullShouldBeStruct", lit(null)) |
| 272 | + .withColumn("shouldIgnoreNonNullTypeMismatch", lit("abc")) |
| 273 | + .withColumn("arrayOfNullShouldBeArrayOfIntegers", array(lit(null), lit(null))) |
| 274 | + .withColumn( |
| 275 | + "arrayOfArrayOfNullShouldBeArrayOfArrayOfStrings", |
| 276 | + array(array(lit(null), lit(null))) |
| 277 | + ) |
| 278 | + .withColumn( |
| 279 | + "arrayOfStructs", |
| 280 | + array( |
| 281 | + struct( |
| 282 | + lit(null).as("nullShouldBeString"), |
| 283 | + lit(null).as("nullShouldBeInteger"), |
| 284 | + lit("abc").as("shouldIgnoreNonNullTypeMismatch"), |
| 285 | + array(lit(null), lit(null)).as("arrayOfNullShouldBeArrayOfIntegers") |
| 286 | + ) |
| 287 | + ) |
| 288 | + ) |
| 289 | + .withColumn( |
| 290 | + "complexStruct", |
| 291 | + struct( |
| 292 | + lit(null).as("nullShouldBeString"), |
| 293 | + lit(null).as("nullShouldBeInteger"), |
| 294 | + lit("abc").as("shouldIgnoreNonNullTypeMismatch"), |
| 295 | + array(lit(1), lit(2), lit(3)).as("shouldIgnoreNonNullArrayTypeMismatch"), |
| 296 | + struct( |
| 297 | + lit(null).as("nullShouldBeString"), |
| 298 | + lit(null).as("nullShouldBeInteger"), |
| 299 | + lit("abc").as("shouldIgnoreNonNullTypeMismatch") |
| 300 | + ).as("nestedStruct"), |
| 301 | + array(lit(null), lit(null)).as("arrayOfNullShouldBeArrayOfIntegers") |
| 302 | + ) |
| 303 | + ) |
| 304 | + |
| 305 | + val targetSchema = StructType( |
| 306 | + Seq( |
| 307 | + StructField("id", LongType), |
| 308 | + // nullShouldBeInteger and nullShouldBeString are swapped comparing to dfWithComplexTypes |
| 309 | + // to ensure enforceTypeOnNullTypeFields converts by name |
| 310 | + StructField("nullShouldBeInteger", IntegerType), |
| 311 | + StructField("nullShouldBeString", StringType), |
| 312 | + // checks case-insensitivity |
| 313 | + StructField("nullShouldBeArrayOfINTEGERS", ArrayType(IntegerType)), |
| 314 | + StructField("nullShouldBeArrayOfArraysOfIntegers", ArrayType(ArrayType(IntegerType))), |
| 315 | + StructField( |
| 316 | + "nullShouldBeArrayOfStructs", |
| 317 | + ArrayType( |
| 318 | + StructType( |
| 319 | + Seq(StructField("a", StringType), StructField("b", DecimalType(28, 8))) |
| 320 | + ) |
| 321 | + ) |
| 322 | + ), |
| 323 | + StructField( |
| 324 | + "nullShouldBeStruct", |
| 325 | + StructType( |
| 326 | + Seq(StructField("a", StringType), StructField("b", DecimalType(28, 8))) |
| 327 | + ) |
| 328 | + ), |
| 329 | + StructField("shouldIgnoreNonNullTypeMismatch", IntegerType, false), |
| 330 | + StructField("arrayOfNullShouldBeArrayOfIntegers", ArrayType(IntegerType), false), |
| 331 | + StructField("arrayOfArrayOfNullShouldBeArrayOfArrayOfStrings", ArrayType(ArrayType(StringType), false), false), |
| 332 | + StructField( |
| 333 | + "arrayOfStructs", |
| 334 | + ArrayType( |
| 335 | + StructType( |
| 336 | + Seq( |
| 337 | + // nullShouldBeInteger and nullShouldBeString are swapped comparing to dfWithComplexTypes |
| 338 | + // to ensure enforceTypeOnNullTypeFields converts by name |
| 339 | + StructField("nullShouldBeInteger", IntegerType), |
| 340 | + StructField("nullShouldBeString", StringType), |
| 341 | + StructField("shouldIgnoreNonNullTypeMismatch", IntegerType, false), |
| 342 | + StructField("arrayOfNullShouldBeArrayOfIntegers", ArrayType(IntegerType), false) |
| 343 | + ) |
| 344 | + ) |
| 345 | + ), |
| 346 | + false |
| 347 | + ), |
| 348 | + StructField( |
| 349 | + "complexStruct", |
| 350 | + StructType( |
| 351 | + Seq( |
| 352 | + // nullShouldBeInteger and nullShouldBeString are swapped comparing to dfWithComplexTypes |
| 353 | + // to ensure enforceTypeOnNullTypeFields converts by name |
| 354 | + StructField("nullShouldBeInteger", IntegerType), |
| 355 | + StructField("nullShouldBeString", StringType), |
| 356 | + // checks case-insensitivity |
| 357 | + StructField("shouldIgnoreNonNullTypeMISMATCH", IntegerType, false), |
| 358 | + StructField("shouldIgnoreNonNullArrayTypeMismatch", ArrayType(StringType, false), false), |
| 359 | + StructField( |
| 360 | + "nestedStruct", |
| 361 | + StructType( |
| 362 | + Seq( |
| 363 | + // nullShouldBeInteger and nullShouldBeString are swapped comparing to dfWithComplexTypes |
| 364 | + // to ensure enforceTypeOnNullTypeFields converts by name |
| 365 | + StructField("nullShouldBeInteger", IntegerType), |
| 366 | + // checks case-insensitivity |
| 367 | + StructField("nullSHOULDBeString", StringType), |
| 368 | + StructField("shouldIgnoreNonNullTypeMismatch", IntegerType, false) |
| 369 | + ) |
| 370 | + ), |
| 371 | + false |
| 372 | + ), |
| 373 | + StructField("arrayOfNullShouldBeArrayOfIntegers", ArrayType(IntegerType), false) |
| 374 | + ) |
| 375 | + ), |
| 376 | + false |
| 377 | + ) |
| 378 | + ) |
| 379 | + ) |
| 380 | + |
| 381 | + val actual = dfWithComplexTypes.enforceTypeOnNullTypeFields(targetSchema) |
| 382 | + |
| 383 | + assert(actual.count() === dfWithComplexTypes.count()) |
| 384 | + |
| 385 | + val actualSchema = actual.schema |
| 386 | + val expectedSchema = StructType( |
| 387 | + Seq( |
| 388 | + StructField("id", LongType), |
| 389 | + StructField("nullShouldBeString", StringType), |
| 390 | + StructField("nullShouldBeInteger", IntegerType), |
| 391 | + StructField("nullShouldBeArrayOfIntegers", ArrayType(IntegerType)), |
| 392 | + StructField("nullShouldBeArrayOfArraysOfIntegers", ArrayType(ArrayType(IntegerType))), |
| 393 | + StructField( |
| 394 | + "nullShouldBeArrayOfStructs", |
| 395 | + ArrayType( |
| 396 | + StructType( |
| 397 | + Seq(StructField("a", StringType), StructField("b", DecimalType(28, 8))) |
| 398 | + ) |
| 399 | + ) |
| 400 | + ), |
| 401 | + StructField( |
| 402 | + "nullShouldBeStruct", |
| 403 | + StructType( |
| 404 | + Seq(StructField("a", StringType), StructField("b", DecimalType(28, 8))) |
| 405 | + ) |
| 406 | + ), |
| 407 | + StructField("shouldIgnoreNonNullTypeMismatch", StringType, false), |
| 408 | + StructField("arrayOfNullShouldBeArrayOfIntegers", ArrayType(IntegerType), false), |
| 409 | + StructField("arrayOfArrayOfNullShouldBeArrayOfArrayOfStrings", ArrayType(ArrayType(StringType), false), false), |
| 410 | + StructField( |
| 411 | + "arrayOfStructs", |
| 412 | + ArrayType( |
| 413 | + StructType( |
| 414 | + Seq( |
| 415 | + StructField("nullShouldBeString", StringType), |
| 416 | + StructField("nullShouldBeInteger", IntegerType), |
| 417 | + StructField("shouldIgnoreNonNullTypeMismatch", StringType, false), |
| 418 | + StructField("arrayOfNullShouldBeArrayOfIntegers", ArrayType(IntegerType), false) |
| 419 | + ) |
| 420 | + ), |
| 421 | + false |
| 422 | + ), |
| 423 | + false |
| 424 | + ), |
| 425 | + StructField( |
| 426 | + "complexStruct", |
| 427 | + StructType( |
| 428 | + Seq( |
| 429 | + StructField("nullShouldBeString", StringType), |
| 430 | + StructField("nullShouldBeInteger", IntegerType), |
| 431 | + StructField("shouldIgnoreNonNullTypeMismatch", StringType, false), |
| 432 | + StructField("shouldIgnoreNonNullArrayTypeMismatch", ArrayType(IntegerType, false), false), |
| 433 | + StructField( |
| 434 | + "nestedStruct", |
| 435 | + StructType( |
| 436 | + Seq( |
| 437 | + StructField("nullShouldBeString", StringType), |
| 438 | + StructField("nullShouldBeInteger", IntegerType), |
| 439 | + StructField("shouldIgnoreNonNullTypeMismatch", StringType, false) |
| 440 | + ) |
| 441 | + ), |
| 442 | + false |
| 443 | + ), |
| 444 | + StructField("arrayOfNullShouldBeArrayOfIntegers", ArrayType(IntegerType), false) |
| 445 | + ) |
| 446 | + ), |
| 447 | + false |
| 448 | + ) |
| 449 | + ) |
| 450 | + ) |
| 451 | + |
| 452 | + assert(actualSchema === expectedSchema) |
| 453 | + } |
| 454 | + |
263 | 455 | test("Check that cacheIfNotCachedYet caches the data") { |
264 | 456 | //Verify check test procedure |
265 | 457 | val dfA = spark.read.json(Seq(jsonA).toDS) |
|
0 commit comments