-
Notifications
You must be signed in to change notification settings - Fork 542
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
CustomAggregator #572
Merged
Merged
CustomAggregator #572
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0ba95ac
Add support for EntityTypes dqdl rule
4118c50
Add support for Conditional Aggregation Analyzer
cbd2a06
Add support for ConditionalAggregationAnalyzer
e120ec5
Add support for ConditionalAggregationAnalyzer
7cc655c
Merge branch 'entityTypes' of https://github.com/joshuazexter/deequ i…
25a8705
Add support for CustomAggregator analyzer
d336fe4
Merge branch 'entityTypes' of https://github.com/joshuazexter/deequ i…
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
69 changes: 69 additions & 0 deletions
69
src/main/scala/com/amazon/deequ/analyzers/ConditionalAggregationAnalyzer.scala
This file contains 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 @@ | ||
/** | ||
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License | ||
* is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.deequ.analyzers | ||
|
||
import com.amazon.deequ.metrics.AttributeDoubleMetric | ||
import com.amazon.deequ.metrics.Entity | ||
import org.apache.spark.sql.DataFrame | ||
|
||
import scala.util.Failure | ||
import scala.util.Success | ||
import scala.util.Try | ||
|
||
// Define a custom state to hold aggregation results | ||
case class AggregatedMetricState(counts: Map[String, Int], totalRows: Int) | ||
extends DoubleValuedState[AggregatedMetricState] { | ||
|
||
def sum(other: AggregatedMetricState): AggregatedMetricState = { | ||
val combinedCounts = counts ++ other | ||
.counts | ||
.map { case (k, v) => k -> (v + counts.getOrElse(k, 0)) } | ||
AggregatedMetricState(combinedCounts, totalRows + other.totalRows) | ||
} | ||
|
||
def metricValue(): Double = counts.values.sum.toDouble / totalRows | ||
} | ||
|
||
// Define the analyzer | ||
case class ConditionalAggregationAnalyzer(aggregatorFunc: DataFrame => AggregatedMetricState, | ||
metricName: String, | ||
instance: String) | ||
extends Analyzer[AggregatedMetricState, AttributeDoubleMetric] { | ||
|
||
def computeStateFrom(data: DataFrame, filterCondition: Option[String] = None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add the |
||
: Option[AggregatedMetricState] = { | ||
Try(aggregatorFunc(data)) match { | ||
case Success(state) => Some(state) | ||
case Failure(_) => None | ||
} | ||
} | ||
|
||
def computeMetricFrom(state: Option[AggregatedMetricState]): AttributeDoubleMetric = { | ||
state match { | ||
case Some(detState) => | ||
val metrics = detState.counts.map { case (key, count) => | ||
key -> (count.toDouble / detState.totalRows) | ||
} | ||
AttributeDoubleMetric(Entity.Column, metricName, instance, Success(metrics)) | ||
case None => | ||
AttributeDoubleMetric(Entity.Column, metricName, instance, | ||
Failure(new RuntimeException("Metric computation failed"))) | ||
} | ||
} | ||
|
||
override private[deequ] def toFailureMetric(failure: Exception): AttributeDoubleMetric = { | ||
AttributeDoubleMetric(Entity.Column, metricName, instance, Failure(failure)) | ||
} | ||
} |
This file contains 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
116 changes: 116 additions & 0 deletions
116
src/test/scala/com/amazon/deequ/analyzers/ConditionalAggregationAnalyzerTest.scala
This file contains 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,116 @@ | ||
/** | ||
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not | ||
* use this file except in compliance with the License. A copy of the License | ||
* is located at | ||
* | ||
* http://aws.amazon.com/apache2.0/ | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.deequ.analyzers | ||
|
||
import com.amazon.deequ.SparkContextSpec | ||
import com.amazon.deequ.utils.FixtureSupport | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import scala.util.Failure | ||
import scala.util.Success | ||
|
||
import org.apache.spark.sql.SparkSession | ||
import org.apache.spark.sql.DataFrame | ||
|
||
import com.amazon.deequ.metrics.AttributeDoubleMetric | ||
|
||
class ConditionalAggregationAnalyzerTest | ||
extends AnyWordSpec with Matchers with SparkContextSpec with FixtureSupport { | ||
|
||
"ConditionalAggregationAnalyzerTest" should { | ||
|
||
"""Example use: return correct counts | ||
|for product sales in different categories""".stripMargin in withSparkSession | ||
{ session => | ||
val data = getDfWithIdColumn(session) | ||
val mockLambda: DataFrame => AggregatedMetricState = _ => | ||
AggregatedMetricState(Map("ProductA" -> 50, "ProductB" -> 45), 100) | ||
|
||
val analyzer = ConditionalAggregationAnalyzer(mockLambda, "ProductSales", "category") | ||
|
||
val state = analyzer.computeStateFrom(data) | ||
val metric: AttributeDoubleMetric = analyzer.computeMetricFrom(state) | ||
|
||
metric.value.isSuccess shouldBe true | ||
metric.value.get should contain ("ProductA" -> 0.5) | ||
metric.value.get should contain ("ProductB" -> 0.45) | ||
} | ||
|
||
"handle scenarios with no data points effectively" in withSparkSession { session => | ||
val data = getDfWithIdColumn(session) | ||
val mockLambda: DataFrame => AggregatedMetricState = _ => | ||
AggregatedMetricState(Map.empty[String, Int], 100) | ||
|
||
val analyzer = ConditionalAggregationAnalyzer(mockLambda, "WebsiteTraffic", "page") | ||
|
||
val state = analyzer.computeStateFrom(data) | ||
val metric: AttributeDoubleMetric = analyzer.computeMetricFrom(state) | ||
|
||
metric.value.isSuccess shouldBe true | ||
metric.value.get shouldBe empty | ||
} | ||
|
||
"return a failure metric when the lambda function fails" in withSparkSession { session => | ||
val data = getDfWithIdColumn(session) | ||
val failingLambda: DataFrame => AggregatedMetricState = | ||
_ => throw new RuntimeException("Test failure") | ||
|
||
val analyzer = ConditionalAggregationAnalyzer(failingLambda, "ProductSales", "category") | ||
|
||
val state = analyzer.computeStateFrom(data) | ||
val metric = analyzer.computeMetricFrom(state) | ||
|
||
metric.value.isFailure shouldBe true | ||
metric.value match { | ||
case Success(_) => fail("Should have failed due to lambda function failure") | ||
case Failure(exception) => exception.getMessage shouldBe "Metric computation failed" | ||
} | ||
} | ||
|
||
"return a failure metric if there are no rows in DataFrame" in withSparkSession { session => | ||
val emptyData = session.createDataFrame( | ||
session.sparkContext.emptyRDD[org.apache.spark.sql.Row], | ||
getDfWithIdColumn(session).schema) | ||
val mockLambda: DataFrame => AggregatedMetricState = df => | ||
if (df.isEmpty) throw new RuntimeException("No data to analyze") | ||
else AggregatedMetricState(Map("ProductA" -> 0, "ProductB" -> 0), 0) | ||
|
||
val analyzer = ConditionalAggregationAnalyzer(mockLambda, | ||
"ProductSales", | ||
"category") | ||
|
||
val state = analyzer.computeStateFrom(emptyData) | ||
val metric = analyzer.computeMetricFrom(state) | ||
|
||
metric.value.isFailure shouldBe true | ||
metric.value match { | ||
case Success(_) => fail("Should have failed due to no data") | ||
case Failure(exception) => exception.getMessage should include("Metric computation failed") | ||
} | ||
} | ||
} | ||
|
||
def getDfWithIdColumn(session: SparkSession): DataFrame = { | ||
import session.implicits._ | ||
Seq( | ||
("ProductA", "North"), | ||
("ProductA", "South"), | ||
("ProductB", "East"), | ||
("ProductA", "West") | ||
).toDF("product", "region") | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are running the aggregator on the entire dataframe, we can probably use
Dataset
for the instance (like how we do in other analyzers like rowcount). That way, we do not need to ask for this parameter from the user. We should keep the public facing API as simple as possible.