Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/DataFrame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,9 @@ class DataFrame private[sql](
*/
private def withCallback[T](name: String, df: DataFrame)(action: DataFrame => T) = {
try {
df.queryExecution.executedPlan.foreach { plan =>
plan.metrics.valuesIterator.foreach(_.reset())
}
val start = System.nanoTime()
val result = action(df)
val end = System.nanoTime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import org.apache.spark.{Accumulable, AccumulableParam, SparkContext}
*/
private[sql] abstract class SQLMetric[R <: SQLMetricValue[T], T](
name: String, val param: SQLMetricParam[R, T])
extends Accumulable[R, T](param.zero, param, Some(name), true)
extends Accumulable[R, T](param.zero, param, Some(name), true) {

def reset(): Unit = {
this.value = param.zero
}
}

/**
* Create a layer for specialized metric. We cannot add `@specialized` to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,24 @@ class DataFrameCallbackSuite extends QueryTest with SharedSQLContext {
assert(metrics(0)._2.analyzed.isInstanceOf[Project])
assert(metrics(0)._3.getMessage == e.getMessage)
}

test("get metrics by callback") {
val metrics = ArrayBuffer.empty[Long]
val listener = new QueryExecutionListener {
// Only test successful case here, so no need to implement `onFailure`
override def onFailure(funcName: String, qe: QueryExecution, exception: Exception): Unit = {}

override def onSuccess(funcName: String, qe: QueryExecution, duration: Long): Unit = {
metrics += qe.executedPlan.longMetric("numInputRows").value.value
}
}
sqlContext.listenerManager.register(listener)

Seq(1 -> "a").toDF("i", "j").groupBy("i").count().collect()
Seq(1 -> "a", 2 -> "a").toDF("i", "j").groupBy("i").count().collect()

assert(metrics.length == 2)
assert(metrics(0) == 1)
assert(metrics(1) == 2)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we run the same plan physical plan multiple times to make sure metrics are good?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. Just one last thing. I think we need to call unregister at the end of every test, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

}