EnricoMi commented on a change in pull request #31905:
URL: https://github.com/apache/spark/pull/31905#discussion_r650684696



##########
File path: sql/core/src/main/scala/org/apache/spark/sql/Observation.scala
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.spark.sql
+
+import java.util.UUID
+import java.util.concurrent.TimeUnit
+import java.util.concurrent.locks.{Condition, Lock, ReentrantLock}
+
+import org.apache.spark.sql.execution.QueryExecution
+import org.apache.spark.sql.util.QueryExecutionListener
+
+/**
+ * Not thread-safe.
+ * @param name
+ * @param sparkSession
+ */
+case class Observation(name: String) {

Review comment:
       I have turned this into a `class` now.

##########
File path: sql/core/src/main/scala/org/apache/spark/sql/Observation.scala
##########
@@ -0,0 +1,184 @@
+/*
+ * 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.spark.sql
+
+import java.util.UUID
+import java.util.concurrent.TimeUnit
+import java.util.concurrent.locks.{Condition, Lock, ReentrantLock}
+
+import org.apache.spark.sql.execution.QueryExecution
+import org.apache.spark.sql.util.QueryExecutionListener
+
+/**
+ * Not thread-safe.
+ * @param name
+ * @param sparkSession
+ */
+case class Observation(name: String) {

Review comment:
       Good point, I have turned this into a `class` now.

##########
File path: sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala
##########
@@ -2374,6 +2376,78 @@ class DataFrameSuite extends QueryTest
     }
   }
 
+  test("SPARK-34806: observation on datasets") {
+    val namedObservation = Observation("named")
+    val unnamedObservation = Observation()
+
+    try {
+      val df = spark
+        .range(100)
+        .observe(
+          namedObservation,
+          min($"id").as("min_val"),
+          max($"id").as("max_val"),
+          sum($"id").as("sum_val"),
+          count(when($"id" % 2 === 0, 1)).as("num_even")
+        )
+        .observe(
+          unnamedObservation,
+          avg($"id").cast("int").as("avg_val")
+        )
+
+      def checkMetrics(namedMetric: Row, unnamedMetric: Row): Unit = {
+        assert(namedMetric === Row(0L, 99L, 4950L, 50L))
+        assert(unnamedMetric === Row(49))
+      }
+
+      // Before first run observation times out
+      assert(namedObservation.waitCompleted(100, TimeUnit.MILLISECONDS) === 
false)
+      assert(unnamedObservation.waitCompleted(100, TimeUnit.MILLISECONDS) === 
false)
+
+      // First run
+      df.collect()
+      assert(namedObservation.waitCompleted(1, TimeUnit.SECONDS))
+      assert(unnamedObservation.waitCompleted(1, TimeUnit.SECONDS))
+      checkMetrics(namedObservation.get, unnamedObservation.get)
+      namedObservation.reset()
+      unnamedObservation.reset()

Review comment:
       If you start a second run and then retrieve the result, you are not 
guaranteed to get the second run's result, or still the first one's. The latter 
is more likely as the method will instantly return the currently known result. 
So only with calling `reset`, the observation waits for a new result.
   
   I have reworded the docstring of that method to better reflect that.




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

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