coderfender commented on code in PR #2906:
URL: https://github.com/apache/datafusion-comet/pull/2906#discussion_r2617426301


##########
spark/src/main/scala/org/apache/comet/cost/CometCostModel.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.comet.cost
+
+import org.apache.spark.sql.catalyst.expressions.{BinaryArithmetic, Expression}
+import org.apache.spark.sql.comet.{CometColumnarToRowExec, CometPlan, 
CometProjectExec}
+import org.apache.spark.sql.comet.execution.shuffle.{CometColumnarShuffle, 
CometNativeShuffle, CometShuffleExchangeExec}
+import org.apache.spark.sql.execution.SparkPlan
+
+import org.apache.comet.DataTypeSupport
+
+case class CometCostEstimate(acceleration: Double)
+
+trait CometCostModel {
+
+  /** Estimate the relative cost of one operator */
+  def estimateCost(plan: SparkPlan): CometCostEstimate
+}
+
+class DefaultCometCostModel extends CometCostModel {
+
+  // optimistic default of 2x acceleration
+  private val defaultAcceleration = 2.0
+
+  override def estimateCost(plan: SparkPlan): CometCostEstimate = {
+    // Walk the entire plan tree and accumulate costs
+    var totalAcceleration = 0.0
+    var operatorCount = 0
+
+    def collectOperatorCosts(node: SparkPlan): Unit = {
+      val operatorCost = estimateOperatorCost(node)
+      totalAcceleration += operatorCost.acceleration
+      operatorCount += 1
+
+      // Recursively process children
+      node.children.foreach(collectOperatorCosts)

Review Comment:
   Perhaps we could remove usage of vars and let the function return the 
totalAcceleration and operator count itself ? 
   
   Something like : 
   ```
   def countItems(list: List[Any], accumulator: Int = 0): Int = {
     list match {
       case head :: tail => countItems(tail, accumulator + 1)
       case Nil => accumulator
     }
   }
   
   val myList = List(1, 2, 3, 4)
   val count = countItems(myList)  // result: 4
   ```



-- 
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]

Reply via email to