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
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ object ApproximatePercentile {
*
* @param summaries underlying probabilistic data structure [[QuantileSummaries]].
*/
class PercentileDigest(private var summaries: QuantileSummaries) {
class PercentileDigest(private var summaries: QuantileSummaries) extends Serializable {
Copy link
Member

Choose a reason for hiding this comment

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

hmm, there are special methods for (de-)serialization of PercentileDigest:

override def serialize(obj: PercentileDigest): Array[Byte] = {
ApproximatePercentile.serializer.serialize(obj)
}
override def deserialize(bytes: Array[Byte]): PercentileDigest = {
ApproximatePercentile.serializer.deserialize(bytes)
}

Do you know why the methods are used instead of extending of Serializable?

cc @cloud-fan

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 just add the proper logic to observe? Adding serialization will fix this particular problem, but it won't work for other aggregates.

Copy link
Member

Choose a reason for hiding this comment

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

I agree if that's possible - I couldn't tell if this was something observe() should never have to pull back and does accidentally, or whether it is necessary to return metrics about percentile_approx somehow

Copy link
Contributor

Choose a reason for hiding this comment

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

I probably forgot to add it...

Copy link
Contributor Author

@beliefer beliefer Nov 4, 2021

Choose a reason for hiding this comment

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

@MaxGekk @hvanhovell @srowen I updated the code and it will fix the issue for AggregatingAccumulator takes any TypedImperativeAggregate.


def this(relativeError: Double) = {
this(new QuantileSummaries(defaultCompressThreshold, relativeError, compressed = true))
Expand Down Expand Up @@ -276,7 +276,7 @@ object ApproximatePercentile {
}

/**
* Serializer for class [[PercentileDigest]]
* Serializer for class [[PercentileDigest]]
*
* This class is thread safe.
*/
Expand Down
11 changes: 11 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/DatasetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,17 @@ class DatasetSuite extends QueryTest
assert(err2.getMessage.contains("Name must not be empty"))
}

test("SPARK-37203: Fix NotSerializableException when observe with percentile_approx") {
val namedObservation = Observation("named")

val df = spark.range(100)
val observed_df = df.observe(
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 test a DataFrame with no data?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

namedObservation, percentile_approx($"id", lit(0.5), lit(100)).as("percentile_approx_val"))

observed_df.collect()
assert(namedObservation.get === Map("percentile_approx_val" -> 49))
}

test("sample with replacement") {
val n = 100
val data = sparkContext.parallelize(1 to n, 2).toDS()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ class StreamingQueryListenerSuite extends StreamTest with BeforeAndAfter {
min($"value").as("min_val"),
max($"value").as("max_val"),
sum($"value").as("sum_val"),
count(when($"value" % 2 === 0, 1)).as("num_even"))
count(when($"value" % 2 === 0, 1)).as("num_even"),
percentile_approx($"value", lit(0.5), lit(100)).as("percentile_approx_val"))
.observe(
name = "other_event",
avg($"value").cast("int").as("avg_val"))
Expand All @@ -444,15 +445,15 @@ class StreamingQueryListenerSuite extends StreamTest with BeforeAndAfter {
AddData(inputData, 1, 2),
AdvanceManualClock(100),
checkMetrics { metrics =>
assert(metrics.get("my_event") === Row(1, 2, 3L, 1L))
assert(metrics.get("my_event") === Row(1, 2, 3L, 1L, 1))
assert(metrics.get("other_event") === Row(1))
},

// Batch 2
AddData(inputData, 10, 30, -10, 5),
AdvanceManualClock(100),
checkMetrics { metrics =>
assert(metrics.get("my_event") === Row(-10, 30, 35L, 3L))
assert(metrics.get("my_event") === Row(-10, 30, 35L, 3L, 5))
assert(metrics.get("other_event") === Row(8))
},

Expand Down