Repository navigation
[GLUTEN-9003][CORE] Carry native counter counts into SQL metrics #13358
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
Open
malinjawi
wants to merge
4
commits into
apache:main
Choose a base branch
from
malinjawi:feat/native-expression-stats-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+257
−1
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
312b6aa
[GLUTEN-9003][CORE] Carry native counter counts into SQL metrics
malinjawi 5a02cc7
[GLUTEN-9003][CORE] Test the expressionStats parsing and explain Incr…
malinjawi c04e7fa
Merge remote-tracking branch 'origin/main' into feat/native-expressio…
malinjawi 43179ae
[GLUTEN-9003][CORE] Say why filters need no counter slot branch
malinjawi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
backends-velox/src/test/scala/org/apache/gluten/metrics/MetricsUtilSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * 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.metrics | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper | ||
| import org.scalatest.funsuite.AnyFunSuite | ||
|
|
||
| import java.util.{Arrays => JArrays} | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
|
|
||
| class MetricsUtilSuite extends AnyFunSuite { | ||
|
|
||
| private val mapper = new ObjectMapper() | ||
|
|
||
| private def expressionStats(json: String): Map[String, Long] = | ||
| MetricsUtil | ||
| .expressionStatsFromJson(mapper.readTree(json)) | ||
| .asScala | ||
| .map { case (name, rows) => name -> rows.longValue() } | ||
| .toMap | ||
|
|
||
| private def operatorMetrics(expressionStats: (String, Long)*): OperatorMetrics = { | ||
| val metrics = new OperatorMetrics() | ||
| expressionStats.foreach { | ||
| case (name, rows) => metrics.expressionStats.put(name, java.lang.Long.valueOf(rows)) | ||
| } | ||
| metrics | ||
| } | ||
|
|
||
| test("expression stats are read from an operator's metrics") { | ||
| val json = | ||
| """{"outputRows": 4, "expressionStats": {"increment_metric_0": 3, "increment_metric_1": 0}}""" | ||
| assert( | ||
| expressionStats(json) === Map("increment_metric_0" -> 3L, "increment_metric_1" -> 0L)) | ||
| } | ||
|
|
||
| test("an operator without expression stats has none") { | ||
| assert(expressionStats("""{"outputRows": 4}""").isEmpty) | ||
| assert(expressionStats("""{"outputRows": 4, "expressionStats": {}}""").isEmpty) | ||
| } | ||
|
|
||
| test("merging operator metrics sums the expression stats of each function") { | ||
| val merged = MetricsUtil.mergeMetrics( | ||
| JArrays.asList( | ||
| operatorMetrics("increment_metric_0" -> 3L, "increment_metric_1" -> 1L), | ||
| operatorMetrics("increment_metric_0" -> 2L), | ||
| operatorMetrics())) | ||
| assert( | ||
| merged.expressionStats.asScala.map { case (name, rows) => name -> rows.longValue() } === | ||
| Map("increment_metric_0" -> 5L, "increment_metric_1" -> 1L)) | ||
| // A concrete map, like the one every OperatorMetrics starts with. | ||
| assert(merged.expressionStats.isInstanceOf[java.util.HashMap[_, _]]) | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
backends-velox/src/test/scala/org/apache/gluten/metrics/ProjectMetricsUpdaterSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * 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.metrics | ||
|
|
||
| import org.apache.spark.sql.execution.metric.SQLMetric | ||
|
|
||
| import org.scalatest.funsuite.AnyFunSuite | ||
|
|
||
| class ProjectMetricsUpdaterSuite extends AnyFunSuite { | ||
|
|
||
| private def projectMetrics(): Map[String, SQLMetric] = Seq( | ||
| "numOutputRows", | ||
| "outputVectors", | ||
| "outputBytes", | ||
| "cpuCount", | ||
| "wallNanos", | ||
| "peakMemoryBytes", | ||
| "numMemoryAllocations", | ||
| "loadLazyVectorTime").map(_ -> new SQLMetric("sum")).toMap | ||
|
|
||
| private def operatorMetrics( | ||
| outputRows: Long, | ||
| expressionStats: (String, Long)*): OperatorMetrics = { | ||
| val metrics = new OperatorMetrics() | ||
| metrics.outputRows = outputRows | ||
| expressionStats.foreach { | ||
| case (name, rows) => metrics.expressionStats.put(name, java.lang.Long.valueOf(rows)) | ||
| } | ||
| metrics | ||
| } | ||
|
|
||
| test("a native counter is credited with the rows its function processed") { | ||
| val updated = new SQLMetric("sum") | ||
| val copied = new SQLMetric("sum") | ||
| val updater = new ProjectMetricsUpdater( | ||
| projectMetrics(), | ||
| Seq("increment_metric_0" -> updated, "increment_metric_1" -> copied)) | ||
| updater.updateNativeMetrics( | ||
| operatorMetrics( | ||
| 10, | ||
| "increment_metric_0" -> 3L, | ||
| "increment_metric_1" -> 7L, | ||
| "increment_metric_2" -> 10L)) | ||
| assert(updated.value === 3) | ||
| assert(copied.value === 7) | ||
| } | ||
|
|
||
| test("a native counter that never ran is credited with zero rows") { | ||
| val metric = new SQLMetric("sum") | ||
| val updater = new ProjectMetricsUpdater(projectMetrics(), Seq("increment_metric_0" -> metric)) | ||
| updater.updateNativeMetrics(operatorMetrics(10)) | ||
| assert(metric.value === 0) | ||
| } | ||
|
|
||
| test("a counter removed from the native plan is credited with the output rows") { | ||
| val metric = new SQLMetric("sum") | ||
| val updater = new ProjectMetricsUpdater(projectMetrics(), Seq("increment_metric" -> metric)) | ||
| updater.updateNativeMetrics(operatorMetrics(10, "increment_metric_0" -> 3L)) | ||
| assert(metric.value === 10) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
gluten-substrait/src/main/scala/org/apache/gluten/expression/IncrementMetricCall.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * 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.expression | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.{Expression, UnaryExpression, Unevaluable} | ||
| import org.apache.spark.sql.types.DataType | ||
|
|
||
| /** | ||
| * A native pass-through call that makes the backend count how many rows reached this point of an | ||
| * expression tree. Delta's transformers emit it in place of `IncrementMetric` when the backend | ||
| * supports native counting. `functionName` is one of the backend's fixed counter slots and is | ||
| * unique to one SQL metric within its projection: the backend keys its per-expression statistics by | ||
| * function name, and the metrics updater credits the metric from that entry. Never evaluated on | ||
| * Spark; it only exists in the expression list handed to the backend. | ||
| * | ||
| * Gluten maps it by class (`ExpressionNames.INCREMENT_METRIC`, which is also the name to put in the | ||
| * expression blacklist), and `getTransformer` sends `functionName` to the backend. Its `prettyName` | ||
| * is the slot name, never `increment_metric`, which is how Gluten recognizes Delta's own | ||
| * `IncrementMetric` without depending on Delta. | ||
| */ | ||
| case class IncrementMetricCall(child: Expression, functionName: String) | ||
| extends UnaryExpression | ||
| with Unevaluable | ||
| with Transformable { | ||
|
|
||
| override def dataType: DataType = child.dataType | ||
|
|
||
| override def nullable: Boolean = child.nullable | ||
|
|
||
| override def prettyName: String = functionName | ||
|
malinjawi marked this conversation as resolved.
Resolved
|
||
|
|
||
| override protected def withNewChildInternal(newChild: Expression): IncrementMetricCall = | ||
| copy(child = newChild) | ||
|
|
||
| override def getTransformer( | ||
| childrenTransformers: Seq[ExpressionTransformer]): ExpressionTransformer = | ||
| GenericExpressionTransformer(functionName, childrenTransformers, this) | ||
|
malinjawi marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| object IncrementMetricCall { | ||
|
|
||
| /** Prefix of every counter slot name. */ | ||
| val functionNamePrefix: String = "increment_metric_" | ||
|
|
||
| /** | ||
| * Number of counter slots the backend registers at startup (`increment_metric_0` to | ||
| * `increment_metric_31`). Mirrors `kIncrementMetricFunctionSlots` in the Velox backend. A | ||
| * projection with more distinct metrics than this falls back. | ||
| */ | ||
| val maxCounters: Int = 32 | ||
|
|
||
| /** Name of the counter slot `slot`, which must be below [[maxCounters]]. */ | ||
| def functionName(slot: Int): String = { | ||
| require(slot >= 0 && slot < maxCounters, s"counter slot $slot out of range") | ||
| functionNamePrefix + slot | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.