MaxGekk commented on code in PR #49230: URL: https://github.com/apache/spark/pull/49230#discussion_r1891673530
########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/NormalizePlanSuite.scala: ########## @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.plans + +import org.apache.spark.SparkFunSuite +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{ + + test("Normalize Project") { + val baselineCol1 = $"col1".int + val testCol1 = baselineCol1.newInstance() + val baselinePlan = LocalRelation(baselineCol1).select(baselineCol1) + val testPlan = LocalRelation(testCol1).select(testCol1) + + assert(baselinePlan != testPlan) + assert(NormalizePlan(baselinePlan) == NormalizePlan(testPlan)) + } + + test("Normalize ordering in a project list of an inner Project") { + val baselinePlan = + LocalRelation($"col1".int, $"col2".string).select($"col1", $"col2").select($"col1") + val testPlan = + LocalRelation($"col1".int, $"col2".string).select($"col2", $"col1").select($"col1") + + assert(baselinePlan != testPlan) + assert(NormalizePlan(baselinePlan) == (NormalizePlan(testPlan))) Review Comment: nit: ```suggestion assert(NormalizePlan(baselinePlan) == NormalizePlan(testPlan)) ``` ########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/NormalizePlan.scala: ########## @@ -95,19 +95,27 @@ object NormalizePlan extends PredicateHelper { .sortBy(_.hashCode()) .reduce(And) Join(left, right, newJoinType, Some(newCondition), hint) + case Project(outerProjectList, innerProject: Project) => + val normalizedInnerProjectList = normalizeProjectList(innerProject.projectList) + val orderedInnerProjectList = normalizedInnerProjectList.sortBy(_.name) + val newInnerProject = + Project(orderedInnerProjectList, innerProject.child) + Project(normalizeProjectList(outerProjectList), newInnerProject) case Project(projectList, child) => - val projList = projectList - .map { e => - e.transformUp { - case g: GetViewColumnByNameAndOrdinal => g.copy(viewDDL = None) - } - } - .asInstanceOf[Seq[NamedExpression]] - Project(projList, child) + Project(normalizeProjectList(projectList), child) case c: KeepAnalyzedQuery => c.storeAnalyzedQuery() } } + private def normalizeProjectList(projectList: Seq[NamedExpression]): Seq[NamedExpression] = Review Comment: Please, add curly braces around the method body, see https://github.com/databricks/scala-style-guide?tab=readme-ov-file#curly-braces ########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/NormalizePlanSuite.scala: ########## @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.plans + +import org.apache.spark.SparkFunSuite +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{ + + test("Normalize Project") { + val baselineCol1 = $"col1".int + val testCol1 = baselineCol1.newInstance() + val baselinePlan = LocalRelation(baselineCol1).select(baselineCol1) + val testPlan = LocalRelation(testCol1).select(testCol1) + + assert(baselinePlan != testPlan) + assert(NormalizePlan(baselinePlan) == NormalizePlan(testPlan)) + } + + test("Normalize ordering in a project list of an inner Project") { + val baselinePlan = + LocalRelation($"col1".int, $"col2".string).select($"col1", $"col2").select($"col1") + val testPlan = + LocalRelation($"col1".int, $"col2".string).select($"col2", $"col1").select($"col1") + + assert(baselinePlan != testPlan) + assert(NormalizePlan(baselinePlan) == (NormalizePlan(testPlan))) + } + Review Comment: ```suggestion ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
