@@ -41,7 +41,6 @@ import org.apache.spark.sql.functions._
41
41
import org .apache .spark .sql .internal .SQLConf
42
42
import org .apache .spark .sql .test .SharedSparkSession
43
43
import org .apache .spark .sql .types ._
44
- import org .apache .spark .util .Utils
45
44
46
45
class SchemaUtilsSuite extends QueryTest
47
46
with SharedSparkSession
@@ -70,29 +69,26 @@ class SchemaUtilsSuite extends QueryTest
70
69
s " Error message ' ${e.getMessage}' didn't contain the patterns: $shouldContainPatterns" )
71
70
}
72
71
73
- private def expectErrorClassAndCause (errorClass : String )( shouldContain : String * )
72
+ private def expectAnalysisErrorClass (errorClass : String , params : Map [ String , String ] )
74
73
(f : => Unit ): Unit = {
75
74
val e = intercept[AnalysisException ] {
76
75
f
77
76
}
78
- val msg = Utils .exceptionString(e).toLowerCase(Locale .ROOT )
79
- assert(e.getErrorClass == errorClass)
80
- assert(
81
- shouldContain.map(_.toLowerCase(Locale .ROOT )).forall(msg.contains),
82
- s " Error cause didn't contain: $shouldContain"
83
- )
84
- }
85
77
86
- private def expectErrorClassAndCausePattern (errorClass : String )
87
- (shouldContainPatterns : String * )(f : => Unit ): Unit = {
88
- val e = intercept[AnalysisException ] {
89
- f
78
+ @ tailrec
79
+ def getError (ex : Throwable ): Option [DeltaAnalysisException ] = ex match {
80
+ case e : DeltaAnalysisException if e.getErrorClass() == errorClass => Some (e)
81
+ case e : AnalysisException => getError(e.getCause)
82
+ case _ => None
90
83
}
91
- assert(e.getErrorClass == errorClass)
92
- val patterns =
93
- shouldContainPatterns.map(regex => Pattern .compile(regex, Pattern .CASE_INSENSITIVE ))
94
- assert(patterns.forall(_.matcher(Utils .exceptionString(e)).find()),
95
- s " Error cause didn't contain the patterns: $shouldContainPatterns" )
84
+
85
+ val err = getError(e)
86
+ assert(err.isDefined, " exception with the error class not found" )
87
+ checkError(
88
+ exception = err.get,
89
+ errorClass = errorClass,
90
+ parameters = params,
91
+ matchPVals = true )
96
92
}
97
93
98
94
// ///////////////////////////
@@ -2185,10 +2181,12 @@ class SchemaUtilsSuite extends QueryTest
2185
2181
.add(" b" , DecimalType (18 , 10 ))))
2186
2182
.add(" map" , MapType (StringType , StringType ))
2187
2183
2188
- expectErrorClassAndCause(" DELTA_FAILED_TO_MERGE_FIELDS" )(" StringType" , " IntegerType" ) {
2184
+ expectAnalysisErrorClass(" DELTA_MERGE_INCOMPATIBLE_DATATYPE" ,
2185
+ Map (" currentDataType" -> " StringType" , " updateDataType" -> " IntegerType" )) {
2189
2186
mergeSchemas(base, new StructType ().add(" top" , IntegerType ))
2190
2187
}
2191
- expectErrorClassAndCause(" DELTA_FAILED_TO_MERGE_FIELDS" )(" IntegerType" , " DateType" ) {
2188
+ expectAnalysisErrorClass(" DELTA_MERGE_INCOMPATIBLE_DATATYPE" ,
2189
+ Map (" currentDataType" -> " IntegerType" , " updateDataType" -> " DateType" )) {
2192
2190
mergeSchemas(base, new StructType ()
2193
2191
.add(" struct" , new StructType ().add(" a" , DateType )))
2194
2192
}
@@ -2197,37 +2195,39 @@ class SchemaUtilsSuite extends QueryTest
2197
2195
// `StructType(StructField(a,IntegerType,true))`.
2198
2196
// - In Scala 2.13, it extends `scala.collection.immutable.Seq` which returns
2199
2197
// `Seq(StructField(a,IntegerType,true))`.
2200
- expectErrorClassAndCausePattern( " DELTA_FAILED_TO_MERGE_FIELDS " )(
2201
- " 'struct' " , " StructType|Seq\\ (" , " MapType" ) {
2198
+ expectAnalysisErrorClass( " DELTA_MERGE_INCOMPATIBLE_DATATYPE " ,
2199
+ Map ( " currentDataType " -> " ( StructType|Seq) \\ (.* " , " updateDataType " -> " MapType\\ (.* " ) ) {
2202
2200
mergeSchemas(base, new StructType ()
2203
2201
.add(" struct" , MapType (StringType , IntegerType )))
2204
2202
}
2205
- expectErrorClassAndCause( " DELTA_FAILED_TO_MERGE_FIELDS " )(
2206
- " 'array' " , " DecimalType" , " DoubleType" ) {
2203
+ expectAnalysisErrorClass( " DELTA_MERGE_INCOMPATIBLE_DATATYPE " ,
2204
+ Map ( " currentDataType " -> " DecimalType\\ (.* " , " updateDataType " -> " DoubleType" ) ) {
2207
2205
mergeSchemas(base, new StructType ()
2208
2206
.add(" array" , ArrayType (new StructType ().add(" b" , DoubleType ))))
2209
2207
}
2210
- expectErrorClassAndCause(" DELTA_FAILED_TO_MERGE_FIELDS" )(" 'array'" , " scale" ) {
2208
+ expectAnalysisErrorClass(" DELTA_MERGE_INCOMPATIBLE_DECIMAL_TYPE" ,
2209
+ Map (" decimalRanges" -> " scale.*" )) {
2211
2210
mergeSchemas(base, new StructType ()
2212
2211
.add(" array" , ArrayType (new StructType ().add(" b" , DecimalType (18 , 12 )))))
2213
2212
}
2214
- expectErrorClassAndCause(" DELTA_FAILED_TO_MERGE_FIELDS" )(" 'array'" , " precision" ) {
2213
+ expectAnalysisErrorClass(" DELTA_MERGE_INCOMPATIBLE_DECIMAL_TYPE" ,
2214
+ Map (" decimalRanges" -> " precision.*" )) {
2215
2215
mergeSchemas(base, new StructType ()
2216
2216
.add(" array" , ArrayType (new StructType ().add(" b" , DecimalType (16 , 10 )))))
2217
2217
}
2218
2218
// See the above comment about `StructType`
2219
- expectErrorClassAndCausePattern( " DELTA_FAILED_TO_MERGE_FIELDS " )(
2220
- " 'map' " , " MapType" , " StructType|Seq\\ (" ) {
2219
+ expectAnalysisErrorClass( " DELTA_MERGE_INCOMPATIBLE_DATATYPE " ,
2220
+ Map ( " currentDataType " -> " MapType\\ (.* " , " updateDataType " -> " ( StructType|Seq) \\ (.* " ) ) {
2221
2221
mergeSchemas(base, new StructType ()
2222
2222
.add(" map" , new StructType ().add(" b" , StringType )))
2223
2223
}
2224
- expectErrorClassAndCause( " DELTA_FAILED_TO_MERGE_FIELDS " )(
2225
- " 'map' " , " StringType" , " IntegerType" ) {
2224
+ expectAnalysisErrorClass( " DELTA_MERGE_INCOMPATIBLE_DATATYPE " ,
2225
+ Map ( " currentDataType " -> " StringType" , " updateDataType " -> " IntegerType" ) ) {
2226
2226
mergeSchemas(base, new StructType ()
2227
2227
.add(" map" , MapType (StringType , IntegerType )))
2228
2228
}
2229
- expectErrorClassAndCause( " DELTA_FAILED_TO_MERGE_FIELDS " )(
2230
- " 'map' " , " StringType" , " IntegerType" ) {
2229
+ expectAnalysisErrorClass( " DELTA_MERGE_INCOMPATIBLE_DATATYPE " ,
2230
+ Map ( " currentDataType " -> " StringType" , " updateDataType " -> " IntegerType" ) ) {
2231
2231
mergeSchemas(base, new StructType ()
2232
2232
.add(" map" , MapType (IntegerType , StringType )))
2233
2233
}
@@ -2299,9 +2299,11 @@ class SchemaUtilsSuite extends QueryTest
2299
2299
val e = intercept[DeltaAnalysisException ] {
2300
2300
mergeSchemas(longType, sourceType)
2301
2301
}
2302
- assert(e.getErrorClass == " DELTA_FAILED_TO_MERGE_FIELDS" )
2303
- assert(Utils .exceptionString(e).contains(
2304
- s " Failed to merge incompatible data types LongType and ${sourceType.head.dataType}" ))
2302
+ checkError(
2303
+ exception = e.getCause.asInstanceOf [AnalysisException ],
2304
+ errorClass = " DELTA_MERGE_INCOMPATIBLE_DATATYPE" ,
2305
+ parameters = Map (" currentDataType" -> " LongType" ,
2306
+ " updateDataType" -> sourceType.head.dataType.toString))
2305
2307
}
2306
2308
}
2307
2309
0 commit comments