jianzhenwu commented on code in PR #12009: URL: https://github.com/apache/gluten/pull/12009#discussion_r3569944402
########## gluten-substrait/src/main/scala/org/apache/gluten/extension/columnar/rewrite/AlignExpandOutputTypes.scala: ########## @@ -0,0 +1,84 @@ +/* + * 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.gluten.extension.columnar.rewrite + +import org.apache.spark.sql.catalyst.expressions._ +import org.apache.spark.sql.execution.{ExpandExec, SparkPlan} +import org.apache.spark.sql.types.DataType +import org.apache.spark.util.SparkVersionUtil + +/** + * Spark 3.2 and 3.3 may produce Expand projections whose expression output types do not exactly + * match the corresponding Expand output attributes. Spark's row path tolerates this, but native + * Expand conversion requires each projection column to have a consistent type. + * + * This rule rewrites each projection column: null literals are replaced with a typed null matching + * the output type; non-matching expressions are wrapped in a Cast to the output type. + */ +object AlignExpandOutputTypes extends RewriteSingleNode { + override def isRewritable(plan: SparkPlan): Boolean = { + ExpandOutputTypeAlignment.isSpark32Or33 && plan.isInstanceOf[ExpandExec] + } + + override def rewrite(plan: SparkPlan): SparkPlan = plan match { + case expand: ExpandExec if isRewritable(expand) => + val alignedProjections = ExpandOutputTypeAlignment.alignProjections( + expand.projections, + expand.output, + expand.child.output) + if (alignedProjections == expand.projections) { + expand + } else { + val newExpand = expand.copy(projections = alignedProjections) + newExpand.copyTagsFrom(expand) + newExpand + } + case _ => plan + } +} + +private[gluten] object ExpandOutputTypeAlignment { + val isSpark32Or33: Boolean = Review Comment: Thank you for your suggestion. I have updated the code. Could you please review it again? -- 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]
