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-32258][SQL] NormalizeFloatingNumbers directly normalizes IF/CaseWhen/Coalesce child expressions #29061

Closed
wants to merge 5 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 @@ -17,7 +17,7 @@

package org.apache.spark.sql.catalyst.optimizer

import org.apache.spark.sql.catalyst.expressions.{Alias, And, ArrayTransform, CreateArray, CreateMap, CreateNamedStruct, CreateStruct, EqualTo, ExpectsInputTypes, Expression, GetStructField, If, IsNull, KnownFloatingPointNormalized, LambdaFunction, Literal, NamedLambdaVariable, UnaryExpression}
import org.apache.spark.sql.catalyst.expressions.{Alias, And, ArrayTransform, CaseWhen, CreateArray, CreateMap, CreateNamedStruct, CreateStruct, EqualTo, ExpectsInputTypes, Expression, GetStructField, If, IsNull, KnownFloatingPointNormalized, LambdaFunction, Literal, NamedLambdaVariable, UnaryExpression}
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode}
import org.apache.spark.sql.catalyst.planning.ExtractEquiJoinKeys
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Subquery, Window}
Expand Down Expand Up @@ -116,6 +116,12 @@ object NormalizeFloatingNumbers extends Rule[LogicalPlan] {
case CreateMap(children, useStringTypeWhenEmpty) =>
CreateMap(children.map(normalize), useStringTypeWhenEmpty)

case If(cond, trueValue, falseValue) =>
dongjoon-hyun marked this conversation as resolved.
Show resolved Hide resolved
If(cond, normalize(trueValue), normalize(falseValue))

case CaseWhen(branches, elseVale) =>
dongjoon-hyun marked this conversation as resolved.
Show resolved Hide resolved
CaseWhen(branches.map(br => (br._1, normalize(br._2))), elseVale.map(normalize))

dongjoon-hyun marked this conversation as resolved.
Show resolved Hide resolved
case _ if expr.dataType == FloatType || expr.dataType == DoubleType =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we put these new cases after this case? The main goal of this optimization is to avoid constructing a new CreateStruct during normalization. If it's just a float/double type If/CashWhen/Coalesce, it's actually an overhead to duplicate the normalization work in each child.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay.

KnownFloatingPointNormalized(NormalizeNaNAndZero(expr))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.spark.sql.catalyst.optimizer

import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions.{And, IsNull, KnownFloatingPointNormalized}
import org.apache.spark.sql.catalyst.expressions.{And, CaseWhen, If, IsNull, KnownFloatingPointNormalized}
viirya marked this conversation as resolved.
Show resolved Hide resolved
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.rules.RuleExecutor
Expand Down Expand Up @@ -91,5 +91,38 @@ class NormalizeFloatingPointNumbersSuite extends PlanTest {

comparePlans(doubleOptimized, correctAnswer)
}

test("normalize the children of If") {
viirya marked this conversation as resolved.
Show resolved Hide resolved
val cond = If(a > 0.1D, a, a + 0.2D) === b
val query = testRelation1.join(testRelation2, condition = Some(cond))
val optimized = Optimize.execute(query)
val doubleOptimized = Optimize.execute(optimized)

val joinCond = If(a > 0.1D,
KnownFloatingPointNormalized(NormalizeNaNAndZero(a)),
KnownFloatingPointNormalized(NormalizeNaNAndZero(a + 0.2D))) ===
KnownFloatingPointNormalized(NormalizeNaNAndZero(b))
val correctAnswer = testRelation1.join(testRelation2, condition = Some(joinCond))

comparePlans(doubleOptimized, correctAnswer)
}

test("normalize the children of CaseWhen") {
val cond = CaseWhen(
Seq((a > 0.1D, a), (a > 0.2D, a + 0.2D)),
Some(a + 0.3D)) === b
val query = testRelation1.join(testRelation2, condition = Some(cond))
val optimized = Optimize.execute(query)
val doubleOptimized = Optimize.execute(optimized)

val joinCond = CaseWhen(
Seq((a > 0.1D, KnownFloatingPointNormalized(NormalizeNaNAndZero(a))),
(a > 0.2D, KnownFloatingPointNormalized(NormalizeNaNAndZero(a + 0.2D)))),
Some(KnownFloatingPointNormalized(NormalizeNaNAndZero(a + 0.3D)))) ===
KnownFloatingPointNormalized(NormalizeNaNAndZero(b))
val correctAnswer = testRelation1.join(testRelation2, condition = Some(joinCond))

comparePlans(doubleOptimized, correctAnswer)
}
}