Skip to content

Commit

Permalink
[SPARK-22893][SQL] Unified the data type mismatch message
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
wangyum authored and gatorsmile committed Dec 25, 2017
1 parent fba0313 commit 33ae243
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
TypeCheckResult.TypeCheckSuccess
} else {
TypeCheckResult.TypeCheckFailure(
s"cannot cast ${child.dataType} to $dataType")
s"cannot cast ${child.dataType.simpleString} to ${dataType.simpleString}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ case class ApproximatePercentile(
case TimestampType => value.asInstanceOf[Long].toDouble
case n: NumericType => n.numeric.toDouble(value.asInstanceOf[n.InternalType])
case other: DataType =>
throw new UnsupportedOperationException(s"Unexpected data type $other")
throw new UnsupportedOperationException(s"Unexpected data type ${other.simpleString}")
}
buffer.add(doubleValue)
}
Expand All @@ -157,7 +157,7 @@ case class ApproximatePercentile(
case DoubleType => doubleResult
case _: DecimalType => doubleResult.map(Decimal(_))
case other: DataType =>
throw new UnsupportedOperationException(s"Unexpected data type $other")
throw new UnsupportedOperationException(s"Unexpected data type ${other.simpleString}")
}
if (result.length == 0) {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ case class If(predicate: Expression, trueValue: Expression, falseValue: Expressi
override def checkInputDataTypes(): TypeCheckResult = {
if (predicate.dataType != BooleanType) {
TypeCheckResult.TypeCheckFailure(
s"type of predicate expression in If should be boolean, not ${predicate.dataType}")
"type of predicate expression in If should be boolean, " +
s"not ${predicate.dataType.simpleString}")
} else if (!trueValue.dataType.sameType(falseValue.dataType)) {
TypeCheckResult.TypeCheckFailure(s"differing types in '$sql' " +
s"(${trueValue.dataType.simpleString} and ${falseValue.dataType.simpleString}).")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ case class Stack(children: Seq[Expression]) extends Generator {
val j = (i - 1) % numFields
if (children(i).dataType != elementSchema.fields(j).dataType) {
return TypeCheckResult.TypeCheckFailure(
s"Argument ${j + 1} (${elementSchema.fields(j).dataType}) != " +
s"Argument $i (${children(i).dataType})")
s"Argument ${j + 1} (${elementSchema.fields(j).dataType.simpleString}) != " +
s"Argument $i (${children(i).dataType.simpleString})")
}
}
TypeCheckResult.TypeCheckSuccess
Expand Down Expand Up @@ -249,7 +249,8 @@ abstract class ExplodeBase extends UnaryExpression with CollectionGenerator with
TypeCheckResult.TypeCheckSuccess
case _ =>
TypeCheckResult.TypeCheckFailure(
s"input to function explode should be array or map type, not ${child.dataType}")
"input to function explode should be array or map type, " +
s"not ${child.dataType.simpleString}")
}

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

override def elementSchema: StructType = child.dataType match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ case class In(value: Expression, list: Seq[Expression]) extends Predicate {
}
case _ =>
TypeCheckResult.TypeCheckFailure(s"Arguments must be same type but were: " +
s"${value.dataType} != ${mismatchOpt.get.dataType}")
s"${value.dataType.simpleString} != ${mismatchOpt.get.dataType.simpleString}")
}
} else {
TypeUtils.checkForOrderingExpr(value.dataType, s"function $prettyName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ case class WindowSpecDefinition(
case f: SpecifiedWindowFrame if f.frameType == RangeFrame && f.isValueBound &&
!isValidFrameType(f.valueBoundary.head.dataType) =>
TypeCheckFailure(
s"The data type '${orderSpec.head.dataType}' used in the order specification does " +
s"not match the data type '${f.valueBoundary.head.dataType}' which is used in the " +
"range frame.")
s"The data type '${orderSpec.head.dataType.simpleString}' used in the order " +
"specification does not match the data type " +
s"'${f.valueBoundary.head.dataType.simpleString}' which is used in the range frame.")
case _ => TypeCheckSuccess
}
}
Expand Down Expand Up @@ -251,7 +251,7 @@ case class SpecifiedWindowFrame(
TypeCheckFailure(s"Window frame $location bound '$e' is not a literal.")
case e: Expression if !frameType.inputType.acceptsType(e.dataType) =>
TypeCheckFailure(
s"The data type of the $location bound '${e.dataType}' does not match " +
s"The data type of the $location bound '${e.dataType.simpleString}' does not match " +
s"the expected data type '${frameType.inputType.simpleString}'.")
case _ => TypeCheckSuccess
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SELECT cast(1 as binary) = '1' FROM t
struct<>
-- !query 1 output
org.apache.spark.sql.AnalysisException
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast IntegerType to BinaryType; line 1 pos 7
cannot resolve 'CAST(1 AS BINARY)' due to data type mismatch: cannot cast int to binary; line 1 pos 7


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


-- !query 25
Expand Down
Loading

0 comments on commit 33ae243

Please sign in to comment.