Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,12 @@ trait CheckAnalysis extends PredicateHelper {
// Check if the data types match.
dataTypes(child).zip(ref).zipWithIndex.foreach { case ((dt1, dt2), ci) =>
// SPARK-18058: we shall not care about the nullability of columns
if (!dt1.sameType(dt2)) {
if (TypeCoercion.findWiderTypeForTwo(dt1.asNullable, dt2.asNullable).isEmpty) {
failAnalysis(
s"""
|${operator.nodeName} can only be performed on tables with the compatible
|column types. $dt1 <> $dt2 at the ${ordinalNumber(ci)} column of
|the ${ordinalNumber(ti + 1)} table
|column types. ${dt1.catalogString} <> ${dt2.catalogString} at the
|${ordinalNumber(ci)} column of the ${ordinalNumber(ti + 1)} table
""".stripMargin.replace("\n", " ").trim())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,19 @@ object TypeCoercion {
* i.e. the main difference with [[findTightestCommonType]] is that here we allow some
* loss of precision when widening decimal and double, and promotion to string.
*/
private def findWiderTypeForTwo(t1: DataType, t2: DataType): Option[DataType] = (t1, t2) match {
case (t1: DecimalType, t2: DecimalType) =>
Some(DecimalPrecision.widerDecimalType(t1, t2))
case (t: IntegralType, d: DecimalType) =>
Some(DecimalPrecision.widerDecimalType(DecimalType.forType(t), d))
case (d: DecimalType, t: IntegralType) =>
Some(DecimalPrecision.widerDecimalType(DecimalType.forType(t), d))
case (_: FractionalType, _: DecimalType) | (_: DecimalType, _: FractionalType) =>
Some(DoubleType)
case _ =>
findTightestCommonTypeToString(t1, t2)
private[analysis] def findWiderTypeForTwo(t1: DataType, t2: DataType): Option[DataType] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @HyukjinKwon .
private[analysis]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Added)

(t1, t2) match {
case (t1: DecimalType, t2: DecimalType) =>
Some(DecimalPrecision.widerDecimalType(t1, t2))
case (t: IntegralType, d: DecimalType) =>
Some(DecimalPrecision.widerDecimalType(DecimalType.forType(t), d))
case (d: DecimalType, t: IntegralType) =>
Some(DecimalPrecision.widerDecimalType(DecimalType.forType(t), d))
case (_: FractionalType, _: DecimalType) | (_: DecimalType, _: FractionalType) =>
Some(DoubleType)
case _ =>
findTightestCommonTypeToString(t1, t2)
}
}

private def findWiderCommonType(types: Seq[DataType]) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,31 @@ class AnalysisErrorSuite extends AnalysisTest {
testRelation.union(nestedRelation),
"union" :: "the compatible column types" :: Nil)

errorTest(
"union with a incompatible column type and compatible column types",
testRelation3.union(testRelation4),
"union" :: "the compatible column types" :: "map" :: "decimal" :: Nil)

errorTest(
"intersect with incompatible column types",
testRelation.intersect(nestedRelation),
"intersect" :: "the compatible column types" :: Nil)

errorTest(
"intersect with a incompatible column type and compatible column types",
testRelation3.intersect(testRelation4),
"intersect" :: "the compatible column types" :: "map" :: "decimal" :: Nil)

errorTest(
"except with incompatible column types",
testRelation.except(nestedRelation),
"except" :: "the compatible column types" :: Nil)

errorTest(
"except with a incompatible column type and compatible column types",
testRelation3.except(testRelation4),
"except" :: "the compatible column types" :: "map" :: "decimal" :: Nil)

errorTest(
"SPARK-9955: correct error message for aggregate",
// When parse SQL string, we will wrap aggregate expressions with UnresolvedAlias.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ object TestRelations {
AttributeReference("g", DoubleType)(),
AttributeReference("h", DecimalType(10, 2))())

// This is the same with `testRelation3` but only `h` is incompatible type.
val testRelation4 = LocalRelation(
AttributeReference("e", StringType)(),
AttributeReference("f", StringType)(),
AttributeReference("g", StringType)(),
AttributeReference("h", MapType(IntegerType, IntegerType))())

val nestedRelation = LocalRelation(
AttributeReference("top", StructType(
StructField("duplicateField", StringType) ::
Expand Down