Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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 @@ -305,12 +305,22 @@ object HiveTypeCoercion {

/**
* Convert all expressions in in() list to the left operator type
* except when the left operator type is NullType. In case when left hand
* operator type is NullType create a Literal(Null).
*/
object InConversion extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan resolveExpressions {
// Skip nodes who's children have not been resolved yet.
case e if !e.childrenResolved => e

case i @ In(a, b) if (a.dataType == NullType) =>
var inTypes : Seq[DataType] = Seq.empty
b.foreach(e => inTypes = inTypes ++ Seq(e.dataType))
findWiderCommonType(inTypes) match {
case Some(finalDataType) => Literal.create(null, BooleanType)
case None => i
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

instead of returning literal null, I think we should just wider the types, as it does fix the bug(we can add a rule in Optimizer to return null for this case).

the code can be:

case i @ In(a, b) if b.exists(_.dataType != a.dataType) =>
  findWiderCommonType(i.children.map(_.dataType)) match {
    case Some(finalDataType) => i.withNewChildren(i.children.map(Cast(_, finalDataType)))
    case None => i
  }


case i @ In(a, b) if b.exists(_.dataType != a.dataType) =>
i.makeCopy(Array(a, b.map(Cast(_, a.dataType))))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,26 @@ class AnalysisSuite extends AnalysisTest {
plan = testRelation.select(CreateStructUnsafe(Seq(a, (a + 1).as("a+1"))).as("col"))
checkAnalysis(plan, plan)
}

test("SPARK-8654: invalid CAST in NULL IN(...) expression") {
val plan = Project(Alias(In(Literal(null), Seq(Literal(1), Literal(2))), "a")() :: Nil,
LocalRelation()
)
assertAnalysisSuccess(plan)
}

test("SPARK-8654: different types in inlist but can be converted to a commmon type") {
val plan = Project(Alias(In(Literal(null), Seq(Literal(1), Literal(1.2345))), "a")() :: Nil,
LocalRelation()
)
assertAnalysisSuccess(plan)
}

test("SPARK-8654: check type compatibility error") {
val plan = Project(Alias(In(Literal(null), Seq(Literal(true), Literal(1))), "a")() :: Nil,
LocalRelation()
)
assertAnalysisError(plan, Seq("cannot resolve 'null IN (true,1)' due to data " +
"type mismatch: Arguments must be same type"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This can be simplified to assertAnalysisError(plan, Seq("data type mismatch: Arguments must be same type")

}
}