Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-39476][SQL] Disable Unwrap cast optimize when casting from Long to Float/ Double or from Integer to Float #36873

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -358,8 +358,16 @@ object UnwrapCastInBinaryComparison extends Rule[LogicalPlan] {
toType.sameType(literalType) &&
!fromExp.foldable &&
toType.isInstanceOf[NumericType] &&
((fromExp.dataType.isInstanceOf[NumericType] && Cast.canUpCast(fromExp.dataType, toType)) ||
fromExp.dataType.isInstanceOf[BooleanType])
canUnwrapCast(fromExp.dataType, toType)
}

private def canUnwrapCast(from: DataType, to: DataType): Boolean = (from, to) match {
case (BooleanType, _) => true
case (IntegerType, FloatType) => false
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add some code comments to explain the reason?

case (LongType, FloatType) => false
case (LongType, DoubleType) => false
case _ if from.isInstanceOf[NumericType] => Cast.canUpCast(from, to)
case _ => false
}

private[optimizer] def getRange(dt: DataType): Option[(Any, Any)] = dt match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,36 @@ class UnwrapCastInComparisonEndToEndSuite extends QueryTest with SharedSparkSess
}
}

test("SPARK-39476: Should not unwrap cast from Long to Double/Float") {
withTable(t) {
Seq((6470759586864300301L))
.toDF("c1").write.saveAsTable(t)
val df = spark.table(t)

checkAnswer(
df.where("cast(c1 as double) == cast(6470759586864300301L as double)")
.select("c1"),
Row(6470759586864300301L))

checkAnswer(
df.where("cast(c1 as float) == cast(6470759586864300301L as float)")
.select("c1"),
Row(6470759586864300301L))
}
}

test("SPARK-39476: Should not unwrap cast from Integer to Float") {
withTable(t) {
Seq((33554435))
.toDF("c1").write.saveAsTable(t)
val df = spark.table(t)

checkAnswer(
df.where("cast(c1 as float) == cast(33554435 as float)")
.select("c1"),
Row(33554435))
}
}

private def decimal(v: BigDecimal): Decimal = Decimal(v, 5, 2)
}