-
Notifications
You must be signed in to change notification settings - Fork 539
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
Column Count Analyzer and Check #555
Changes from 3 commits
20064c3
5feaaaf
5d5d8d4
5c91225
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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.DoubleMetric | ||
import com.amazon.deequ.metrics.Entity | ||
import org.apache.spark.sql.DataFrame | ||
|
||
case class ColumnCount(where: Option[String] = None) extends Analyzer[NumMatches, DoubleMetric] { | ||
|
||
val name = "ColumnCount" | ||
val instance = "*" | ||
val entity = Entity.Dataset | ||
|
||
|
||
mentekid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Compute the state (sufficient statistics) from the data | ||
* | ||
* @param data data frame | ||
* @return | ||
*/ | ||
override def computeStateFrom(data: DataFrame, filterCondition: Option[String]): Option[NumMatches] = { | ||
if (filterCondition.isDefined) { | ||
throw new IllegalArgumentException("ColumnCount does not accept a filter condition") | ||
} else { | ||
val numColumns = data.columns.size | ||
Some(NumMatches(numColumns)) | ||
} | ||
} | ||
|
||
/** | ||
* Compute the metric from the state (sufficient statistics) | ||
* | ||
* @param state wrapper holding a state of type S (required due to typing issues...) | ||
* @return | ||
mentekid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
override def computeMetricFrom(state: Option[NumMatches]): DoubleMetric = { | ||
if (state.isDefined) { | ||
Analyzers.metricFromValue(state.get.metricValue(), name, instance, entity) | ||
} else { | ||
Analyzers.metricFromEmpty(this, name, instance, entity) | ||
} | ||
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. How about:
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. it's |
||
} | ||
|
||
/** | ||
* Compute the metric from a failure - reports the exception thrown while trying to count columns | ||
*/ | ||
override private[deequ] def toFailureMetric(failure: Exception): DoubleMetric = { | ||
Analyzers.metricFromFailure(failure, name, instance, entity) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,17 @@ import scala.util.Failure | |
import scala.util.Success | ||
import scala.util.Try | ||
|
||
case class CustomSqlState(stateOrError: Either[Double, String]) extends DoubleValuedState[CustomSqlState] { | ||
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. Either is right biased in Scala, which means the right sided value is what you would operate on when doing 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. I'm just moving this from Size.scala to its own class, I can address that in a different PR |
||
lazy val state = stateOrError.left.get | ||
lazy val error = stateOrError.right.get | ||
|
||
override def sum(other: CustomSqlState): CustomSqlState = { | ||
CustomSqlState(Left(state + other.state)) | ||
} | ||
|
||
override def metricValue(): Double = state | ||
} | ||
|
||
case class CustomSql(expression: String) extends Analyzer[CustomSqlState, DoubleMetric] { | ||
/** | ||
* Compute the state (sufficient statistics) from the data | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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.apache.spark.sql.Row | ||
import org.apache.spark.sql.types.StructType | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import scala.util.Failure | ||
import scala.util.Success | ||
|
||
class ColumnCountTest extends AnyWordSpec with Matchers with SparkContextSpec with FixtureSupport { | ||
"ColumnCount" should { | ||
"return column count for a dataset" in withSparkSession { session => | ||
val data = getDfWithStringColumns(session) | ||
val colCount = ColumnCount() | ||
|
||
val state = colCount.computeStateFrom(data) | ||
state.isDefined shouldBe true | ||
state.get.metricValue() shouldBe 5.0 | ||
|
||
val metric = colCount.computeMetricFrom(state) | ||
metric.fullColumn shouldBe None | ||
metric.value shouldBe Success(5.0) | ||
} | ||
} | ||
} |
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.
Can we remove
where
? Or is that a requirement fromAnalyzer
?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.
removed