Skip to content

Commit 33ae243

Browse files
wangyumgatorsmile
authored andcommitted
[SPARK-22893][SQL] Unified the data type mismatch message
## What changes were proposed in this pull request? We should use `dataType.simpleString` to unified the data type mismatch message: Before: ``` spark-sql> select cast(1 as binary); Error in query: cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7; ``` After: ``` park-sql> select cast(1 as binary); Error in query: cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7; ``` ## How was this patch tested? Exist test. Author: Yuming Wang <[email protected]> Closes #20064 from wangyum/SPARK-22893.
1 parent fba0313 commit 33ae243

File tree

12 files changed

+189
-186
lines changed

12 files changed

+189
-186
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
181181
TypeCheckResult.TypeCheckSuccess
182182
} else {
183183
TypeCheckResult.TypeCheckFailure(
184-
s"cannot cast ${child.dataType} to $dataType")
184+
s"cannot cast ${child.dataType.simpleString} to ${dataType.simpleString}")
185185
}
186186
}
187187

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/ApproximatePercentile.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ case class ApproximatePercentile(
132132
case TimestampType => value.asInstanceOf[Long].toDouble
133133
case n: NumericType => n.numeric.toDouble(value.asInstanceOf[n.InternalType])
134134
case other: DataType =>
135-
throw new UnsupportedOperationException(s"Unexpected data type $other")
135+
throw new UnsupportedOperationException(s"Unexpected data type ${other.simpleString}")
136136
}
137137
buffer.add(doubleValue)
138138
}
@@ -157,7 +157,7 @@ case class ApproximatePercentile(
157157
case DoubleType => doubleResult
158158
case _: DecimalType => doubleResult.map(Decimal(_))
159159
case other: DataType =>
160-
throw new UnsupportedOperationException(s"Unexpected data type $other")
160+
throw new UnsupportedOperationException(s"Unexpected data type ${other.simpleString}")
161161
}
162162
if (result.length == 0) {
163163
null

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ case class If(predicate: Expression, trueValue: Expression, falseValue: Expressi
4040
override def checkInputDataTypes(): TypeCheckResult = {
4141
if (predicate.dataType != BooleanType) {
4242
TypeCheckResult.TypeCheckFailure(
43-
s"type of predicate expression in If should be boolean, not ${predicate.dataType}")
43+
"type of predicate expression in If should be boolean, " +
44+
s"not ${predicate.dataType.simpleString}")
4445
} else if (!trueValue.dataType.sameType(falseValue.dataType)) {
4546
TypeCheckResult.TypeCheckFailure(s"differing types in '$sql' " +
4647
s"(${trueValue.dataType.simpleString} and ${falseValue.dataType.simpleString}).")

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/generators.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ case class Stack(children: Seq[Expression]) extends Generator {
155155
val j = (i - 1) % numFields
156156
if (children(i).dataType != elementSchema.fields(j).dataType) {
157157
return TypeCheckResult.TypeCheckFailure(
158-
s"Argument ${j + 1} (${elementSchema.fields(j).dataType}) != " +
159-
s"Argument $i (${children(i).dataType})")
158+
s"Argument ${j + 1} (${elementSchema.fields(j).dataType.simpleString}) != " +
159+
s"Argument $i (${children(i).dataType.simpleString})")
160160
}
161161
}
162162
TypeCheckResult.TypeCheckSuccess
@@ -249,7 +249,8 @@ abstract class ExplodeBase extends UnaryExpression with CollectionGenerator with
249249
TypeCheckResult.TypeCheckSuccess
250250
case _ =>
251251
TypeCheckResult.TypeCheckFailure(
252-
s"input to function explode should be array or map type, not ${child.dataType}")
252+
"input to function explode should be array or map type, " +
253+
s"not ${child.dataType.simpleString}")
253254
}
254255

255256
// hive-compatible default alias for explode function ("col" for array, "key", "value" for map)
@@ -378,7 +379,8 @@ case class Inline(child: Expression) extends UnaryExpression with CollectionGene
378379
TypeCheckResult.TypeCheckSuccess
379380
case _ =>
380381
TypeCheckResult.TypeCheckFailure(
381-
s"input to function $prettyName should be array of struct type, not ${child.dataType}")
382+
s"input to function $prettyName should be array of struct type, " +
383+
s"not ${child.dataType.simpleString}")
382384
}
383385

384386
override def elementSchema: StructType = child.dataType match {

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/predicates.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ case class In(value: Expression, list: Seq[Expression]) extends Predicate {
195195
}
196196
case _ =>
197197
TypeCheckResult.TypeCheckFailure(s"Arguments must be same type but were: " +
198-
s"${value.dataType} != ${mismatchOpt.get.dataType}")
198+
s"${value.dataType.simpleString} != ${mismatchOpt.get.dataType.simpleString}")
199199
}
200200
} else {
201201
TypeUtils.checkForOrderingExpr(value.dataType, s"function $prettyName")

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/windowExpressions.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ case class WindowSpecDefinition(
7070
case f: SpecifiedWindowFrame if f.frameType == RangeFrame && f.isValueBound &&
7171
!isValidFrameType(f.valueBoundary.head.dataType) =>
7272
TypeCheckFailure(
73-
s"The data type '${orderSpec.head.dataType}' used in the order specification does " +
74-
s"not match the data type '${f.valueBoundary.head.dataType}' which is used in the " +
75-
"range frame.")
73+
s"The data type '${orderSpec.head.dataType.simpleString}' used in the order " +
74+
"specification does not match the data type " +
75+
s"'${f.valueBoundary.head.dataType.simpleString}' which is used in the range frame.")
7676
case _ => TypeCheckSuccess
7777
}
7878
}
@@ -251,7 +251,7 @@ case class SpecifiedWindowFrame(
251251
TypeCheckFailure(s"Window frame $location bound '$e' is not a literal.")
252252
case e: Expression if !frameType.inputType.acceptsType(e.dataType) =>
253253
TypeCheckFailure(
254-
s"The data type of the $location bound '${e.dataType}' does not match " +
254+
s"The data type of the $location bound '${e.dataType.simpleString}' does not match " +
255255
s"the expected data type '${frameType.inputType.simpleString}'.")
256256
case _ => TypeCheckSuccess
257257
}

sql/core/src/test/resources/sql-tests/results/typeCoercion/native/binaryComparison.sql.out

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SELECT cast(1 as binary) = '1' FROM t
1616
struct<>
1717
-- !query 1 output
1818
org.apache.spark.sql.AnalysisException
19-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
19+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
2020

2121

2222
-- !query 2
@@ -25,7 +25,7 @@ SELECT cast(1 as binary) > '2' FROM t
2525
struct<>
2626
-- !query 2 output
2727
org.apache.spark.sql.AnalysisException
28-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
28+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
2929

3030

3131
-- !query 3
@@ -34,7 +34,7 @@ SELECT cast(1 as binary) >= '2' FROM t
3434
struct<>
3535
-- !query 3 output
3636
org.apache.spark.sql.AnalysisException
37-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
37+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
3838

3939

4040
-- !query 4
@@ -43,7 +43,7 @@ SELECT cast(1 as binary) < '2' FROM t
4343
struct<>
4444
-- !query 4 output
4545
org.apache.spark.sql.AnalysisException
46-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
46+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
4747

4848

4949
-- !query 5
@@ -52,7 +52,7 @@ SELECT cast(1 as binary) <= '2' FROM t
5252
struct<>
5353
-- !query 5 output
5454
org.apache.spark.sql.AnalysisException
55-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
55+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
5656

5757

5858
-- !query 6
@@ -61,7 +61,7 @@ SELECT cast(1 as binary) <> '2' FROM t
6161
struct<>
6262
-- !query 6 output
6363
org.apache.spark.sql.AnalysisException
64-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
64+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
6565

6666

6767
-- !query 7
@@ -70,7 +70,7 @@ SELECT cast(1 as binary) = cast(null as string) FROM t
7070
struct<>
7171
-- !query 7 output
7272
org.apache.spark.sql.AnalysisException
73-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
73+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
7474

7575

7676
-- !query 8
@@ -79,7 +79,7 @@ SELECT cast(1 as binary) > cast(null as string) FROM t
7979
struct<>
8080
-- !query 8 output
8181
org.apache.spark.sql.AnalysisException
82-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
82+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
8383

8484

8585
-- !query 9
@@ -88,7 +88,7 @@ SELECT cast(1 as binary) >= cast(null as string) FROM t
8888
struct<>
8989
-- !query 9 output
9090
org.apache.spark.sql.AnalysisException
91-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
91+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
9292

9393

9494
-- !query 10
@@ -97,7 +97,7 @@ SELECT cast(1 as binary) < cast(null as string) FROM t
9797
struct<>
9898
-- !query 10 output
9999
org.apache.spark.sql.AnalysisException
100-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
100+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
101101

102102

103103
-- !query 11
@@ -106,7 +106,7 @@ SELECT cast(1 as binary) <= cast(null as string) FROM t
106106
struct<>
107107
-- !query 11 output
108108
org.apache.spark.sql.AnalysisException
109-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
109+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
110110

111111

112112
-- !query 12
@@ -115,7 +115,7 @@ SELECT cast(1 as binary) <> cast(null as string) FROM t
115115
struct<>
116116
-- !query 12 output
117117
org.apache.spark.sql.AnalysisException
118-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
118+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7
119119

120120

121121
-- !query 13
@@ -124,7 +124,7 @@ SELECT '1' = cast(1 as binary) FROM t
124124
struct<>
125125
-- !query 13 output
126126
org.apache.spark.sql.AnalysisException
127-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 13
127+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 13
128128

129129

130130
-- !query 14
@@ -133,7 +133,7 @@ SELECT '2' > cast(1 as binary) FROM t
133133
struct<>
134134
-- !query 14 output
135135
org.apache.spark.sql.AnalysisException
136-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 13
136+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 13
137137

138138

139139
-- !query 15
@@ -142,7 +142,7 @@ SELECT '2' >= cast(1 as binary) FROM t
142142
struct<>
143143
-- !query 15 output
144144
org.apache.spark.sql.AnalysisException
145-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 14
145+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 14
146146

147147

148148
-- !query 16
@@ -151,7 +151,7 @@ SELECT '2' < cast(1 as binary) FROM t
151151
struct<>
152152
-- !query 16 output
153153
org.apache.spark.sql.AnalysisException
154-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 13
154+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 13
155155

156156

157157
-- !query 17
@@ -160,7 +160,7 @@ SELECT '2' <= cast(1 as binary) FROM t
160160
struct<>
161161
-- !query 17 output
162162
org.apache.spark.sql.AnalysisException
163-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 14
163+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 14
164164

165165

166166
-- !query 18
@@ -169,7 +169,7 @@ SELECT '2' <> cast(1 as binary) FROM t
169169
struct<>
170170
-- !query 18 output
171171
org.apache.spark.sql.AnalysisException
172-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 14
172+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 14
173173

174174

175175
-- !query 19
@@ -178,7 +178,7 @@ SELECT cast(null as string) = cast(1 as binary) FROM t
178178
struct<>
179179
-- !query 19 output
180180
org.apache.spark.sql.AnalysisException
181-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 30
181+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 30
182182

183183

184184
-- !query 20
@@ -187,7 +187,7 @@ SELECT cast(null as string) > cast(1 as binary) FROM t
187187
struct<>
188188
-- !query 20 output
189189
org.apache.spark.sql.AnalysisException
190-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 30
190+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 30
191191

192192

193193
-- !query 21
@@ -196,7 +196,7 @@ SELECT cast(null as string) >= cast(1 as binary) FROM t
196196
struct<>
197197
-- !query 21 output
198198
org.apache.spark.sql.AnalysisException
199-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 31
199+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 31
200200

201201

202202
-- !query 22
@@ -205,7 +205,7 @@ SELECT cast(null as string) < cast(1 as binary) FROM t
205205
struct<>
206206
-- !query 22 output
207207
org.apache.spark.sql.AnalysisException
208-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 30
208+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 30
209209

210210

211211
-- !query 23
@@ -214,7 +214,7 @@ SELECT cast(null as string) <= cast(1 as binary) FROM t
214214
struct<>
215215
-- !query 23 output
216216
org.apache.spark.sql.AnalysisException
217-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 31
217+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 31
218218

219219

220220
-- !query 24
@@ -223,7 +223,7 @@ SELECT cast(null as string) <> cast(1 as binary) FROM t
223223
struct<>
224224
-- !query 24 output
225225
org.apache.spark.sql.AnalysisException
226-
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 31
226+
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 31
227227

228228

229229
-- !query 25

0 commit comments

Comments
 (0)