Skip to content

Commit

Permalink
[SPARK-38748][SQL][TESTS] Test the error class: PIVOT_VALUE_DATA_TYPE…
Browse files Browse the repository at this point in the history
…_MISMATCH

## What changes were proposed in this pull request?
This PR aims to add a test for the error class PIVOT_VALUE_DATA_TYPE_MISMATCH to `QueryCompilationErrorsSuite`.

### Why are the changes needed?
The changes improve test coverage, and document expected error messages in tests.

### Does this PR introduce any user-facing change?
No

### How was this patch tested?
By running new test:
```
$ build/sbt "sql/testOnly *QueryCompilationErrorsSuite*"
```

Closes apache#36400 from panbingkun/SPARK-38748.

Authored-by: panbingkun <[email protected]>
Signed-off-by: Max Gekk <[email protected]>
  • Loading branch information
panbingkun authored and MaxGekk committed Apr 29, 2022
1 parent af016d9 commit 30a2d9b
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark.sql.errors
import org.apache.spark.sql.{AnalysisException, IntegratedUDFTestUtils, QueryTest, Row}
import org.apache.spark.sql.api.java.{UDF1, UDF2, UDF23Test}
import org.apache.spark.sql.expressions.SparkUserDefinedFunction
import org.apache.spark.sql.functions.{grouping, grouping_id, sum, udf}
import org.apache.spark.sql.functions.{grouping, grouping_id, lit, struct, sum, udf}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{IntegerType, MapType, StringType, StructField, StructType}

Expand Down Expand Up @@ -491,6 +491,28 @@ class QueryCompilationErrorsSuite
msg = "Field name c.X is ambiguous and has 2 matching fields in the struct.; line 1 pos 0")
}
}

test("PIVOT_VALUE_DATA_TYPE_MISMATCH: can't cast pivot value data type (struct) " +
"to pivot column data type (int)") {
val df = Seq(
("dotNET", 2012, 10000),
("Java", 2012, 20000),
("dotNET", 2012, 5000),
("dotNET", 2013, 48000),
("Java", 2013, 30000)
).toDF("course", "year", "earnings")

checkErrorClass(
exception = intercept[AnalysisException] {
df.groupBy(df("course")).pivot(df("year"), Seq(
struct(lit("dotnet"), lit("Experts")),
struct(lit("java"), lit("Dummies")))).
agg(sum($"earnings")).collect()
},
errorClass = "PIVOT_VALUE_DATA_TYPE_MISMATCH",
msg = "Invalid pivot value 'struct(col1, dotnet, col2, Experts)': value data type " +
"struct<col1:string,col2:string> does not match pivot column data type int")
}
}

class MyCastToString extends SparkUserDefinedFunction(
Expand Down

0 comments on commit 30a2d9b

Please sign in to comment.