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-50612][SQL][FOLLOWUP] Put normalization of inner project lists under a flag #49285

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 @@ -23,8 +23,8 @@ import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression
import org.apache.spark.sql.catalyst.plans.logical._

object NormalizePlan extends PredicateHelper {
def apply(plan: LogicalPlan): LogicalPlan =
normalizePlan(normalizeExprIds(plan))
def apply(plan: LogicalPlan, normalizeInnerProjectListOrder: Boolean = false): LogicalPlan =
normalizePlan(normalizeExprIds(plan), normalizeInnerProjectListOrder)

/**
* Since attribute references are given globally unique ids during analysis,
Expand Down Expand Up @@ -69,7 +69,9 @@ object NormalizePlan extends PredicateHelper {
* - Sample the seed will replaced by 0L.
* - Join conditions will be resorted by hashCode.
*/
def normalizePlan(plan: LogicalPlan): LogicalPlan = {
def normalizePlan(
plan: LogicalPlan,
normalizeInnerProjectListOrder: Boolean = false): LogicalPlan = {
plan transform {
case Filter(condition: Expression, child: LogicalPlan) =>
Filter(
Expand All @@ -95,7 +97,7 @@ object NormalizePlan extends PredicateHelper {
.sortBy(_.hashCode())
.reduce(And)
Join(left, right, newJoinType, Some(newCondition), hint)
case Project(outerProjectList, innerProject: Project) =>
case Project(outerProjectList, innerProject: Project) if normalizeInnerProjectListOrder =>
val normalizedInnerProjectList = normalizeProjectList(innerProject.projectList)
val orderedInnerProjectList = normalizedInnerProjectList.sortBy(_.name)
val newInnerProject =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.plans.logical.LocalRelation

class NormalizePlanSuite extends SparkFunSuite{
class NormalizePlanSuite extends SparkFunSuite {

test("Normalize Project") {
val baselineCol1 = $"col1".int
Expand All @@ -31,7 +31,11 @@ class NormalizePlanSuite extends SparkFunSuite{
val testPlan = LocalRelation(testCol1).select(testCol1)

assert(baselinePlan != testPlan)
assert(NormalizePlan(baselinePlan) == NormalizePlan(testPlan))
assert(
NormalizePlan(baselinePlan)
==
NormalizePlan(testPlan)
)
}

test("Normalize ordering in a project list of an inner Project") {
Expand All @@ -41,6 +45,17 @@ class NormalizePlanSuite extends SparkFunSuite{
LocalRelation($"col1".int, $"col2".string).select($"col2", $"col1").select($"col1")

assert(baselinePlan != testPlan)
assert(NormalizePlan(baselinePlan) == NormalizePlan(testPlan))

assert(
NormalizePlan(baselinePlan, normalizeInnerProjectListOrder = false)
!=
NormalizePlan(testPlan, normalizeInnerProjectListOrder = false)
)

assert(
NormalizePlan(baselinePlan, normalizeInnerProjectListOrder = true)
==
NormalizePlan(testPlan, normalizeInnerProjectListOrder = true)
)
}
}
Loading