PHILO-HE commented on code in PR #5276: URL: https://github.com/apache/incubator-gluten/pull/5276#discussion_r1554807763
########## gluten-core/src/main/scala/org/apache/gluten/extension/columnar/heuristic/HeuristicApplier.scala: ########## @@ -0,0 +1,173 @@ +/* + * 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.heuristic + +import org.apache.gluten.GlutenConfig +import org.apache.gluten.backendsapi.BackendsApiManager +import org.apache.gluten.extension.columnar._ +import org.apache.gluten.extension.columnar.MiscColumnarRules.{RemoveGlutenTableCacheColumnarToRow, RemoveTopmostColumnarToRow, TransformPostOverrides, TransformPreOverrides} +import org.apache.gluten.extension.columnar.util.AdaptiveContext +import org.apache.gluten.metrics.GlutenTimeMetric +import org.apache.gluten.utils.{LogLevelUtil, PhysicalPlanSelector} + +import org.apache.spark.internal.Logging +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.catalyst.rules.{PlanChangeLogger, Rule} +import org.apache.spark.sql.execution.{ColumnarCollapseTransformStages, GlutenFallbackReporter, SparkPlan} +import org.apache.spark.util.SparkRuleUtil + +class HeuristicApplier(session: SparkSession) + extends ColumnarRuleApplier + with Logging + with LogLevelUtil { + + private lazy val transformPlanLogLevel = GlutenConfig.getConf.transformPlanLogLevel + private lazy val planChangeLogger = new PlanChangeLogger[SparkPlan]() + + private val adaptiveContext = AdaptiveContext(session) + + override def apply(plan: SparkPlan, outputsColumnar: Boolean): SparkPlan = + withTransformRules(transformRules(outputsColumnar)).apply(plan) + + // Visible for testing. + def withTransformRules(transformRules: List[SparkSession => Rule[SparkPlan]]): Rule[SparkPlan] = + plan => + PhysicalPlanSelector.maybe(session, plan) { + val finalPlan = prepareFallback(plan) { + p => + val suggestedPlan = transformPlan(transformRules, p, "transform") + transformPlan(fallbackPolicies(), suggestedPlan, "fallback") match { + case FallbackNode(fallbackPlan) => + // we should use vanilla c2r rather than native c2r, + // and there should be no `GlutenPlan` any more, + // so skip the `postRules()`. + fallbackPlan + case plan => + transformPlan(postRules(), plan, "post") + } + } + transformPlan(finalRules(), finalPlan, "final") + } + + private def transformPlan( + getRules: List[SparkSession => Rule[SparkPlan]], + plan: SparkPlan, + step: String) = GlutenTimeMetric.withMillisTime { + logOnLevel( + transformPlanLogLevel, + s"${step}ColumnarTransitions preOverridden plan:\n${plan.toString}") + val overridden = getRules.foldLeft(plan) { + (p, getRule) => + val rule = getRule(session) + val newPlan = rule(p) + planChangeLogger.logRule(rule.ruleName, p, newPlan) + newPlan + } + logOnLevel( + transformPlanLogLevel, + s"${step}ColumnarTransitions afterOverridden plan:\n${overridden.toString}") + overridden + }(t => logOnLevel(transformPlanLogLevel, s"${step}Transform SparkPlan took: $t ms.")) + + private def prepareFallback[T](plan: SparkPlan)(f: SparkPlan => T): T = { + adaptiveContext.setAdaptiveContext() + adaptiveContext.setOriginalPlan(plan) + try { + f(plan) + } finally { + adaptiveContext.resetOriginalPlan() + adaptiveContext.resetAdaptiveContext() + } + } + + /** + * Rules to let planner create a suggested Gluten plan being sent to `fallbackPolicies` in which + * the plan will be breakdown and decided to be fallen back or not. + */ + private def transformRules(outputsColumnar: Boolean): List[SparkSession => Rule[SparkPlan]] = { Review Comment: Looks there are few difference between the rule sets (`transformRules`, `fallbackPolicies`, `postRules`, `finalRules`) of `HeuristicApplier` & `EnumeratedApplier`. Will some more differences be introduced in the future development? If not, may be better to put common rules in some common place or super class? ########## gluten-core/src/main/scala/org/apache/gluten/extension/columnar/enumerated/EnumeratedApplier.scala: ########## @@ -0,0 +1,173 @@ +/* + * 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.enumerated + +import org.apache.gluten.GlutenConfig +import org.apache.gluten.backendsapi.BackendsApiManager +import org.apache.gluten.extension.columnar._ +import org.apache.gluten.extension.columnar.MiscColumnarRules.{RemoveGlutenTableCacheColumnarToRow, RemoveTopmostColumnarToRow, TransformPostOverrides, TransformPreOverrides} +import org.apache.gluten.extension.columnar.util.AdaptiveContext +import org.apache.gluten.metrics.GlutenTimeMetric +import org.apache.gluten.utils.{LogLevelUtil, PhysicalPlanSelector} + +import org.apache.spark.internal.Logging +import org.apache.spark.sql.SparkSession +import org.apache.spark.sql.catalyst.rules.{PlanChangeLogger, Rule} +import org.apache.spark.sql.execution.{ColumnarCollapseTransformStages, GlutenFallbackReporter, SparkPlan} +import org.apache.spark.util.SparkRuleUtil + +class EnumeratedApplier(session: SparkSession) Review Comment: Better to add some comments for this class. Same for HeuristicApplier. -- 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]
