forked from awslabs/deequ
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Column Count Analyzer and Check (awslabs#555)
* Fix flaky KLL test * Move CustomSql state to CustomSql analyzer * Implement new Analyzer to count columns * Improve documentation, remove unused parameter, replace if/else with map --------- Co-authored-by: Yannis Mentekidis <[email protected]>
- Loading branch information
1 parent
5cf0837
commit b4d0f2c
Showing
8 changed files
with
142 additions
and
13 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/main/scala/com/amazon/deequ/analyzers/ColumnCount.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,63 @@ | ||
/* | ||
* 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() extends Analyzer[NumMatches, DoubleMetric] { | ||
|
||
val name = "ColumnCount" | ||
val instance = "*" | ||
val entity = Entity.Dataset | ||
|
||
/** | ||
* Compute the state (sufficient statistics) from the data | ||
* | ||
* @param data the input dataframe | ||
* @return the number of columns in the input | ||
*/ | ||
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 the computed state from [[computeStateFrom]] | ||
* @return a double metric indicating the number of columns for this analyzer | ||
*/ | ||
override def computeMetricFrom(state: Option[NumMatches]): DoubleMetric = { | ||
state | ||
.map(v => Analyzers.metricFromValue(v.metricValue(), name, instance, entity)) | ||
.getOrElse(Analyzers.metricFromEmpty(this, name, instance, entity)) | ||
} | ||
|
||
/** | ||
* 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) | ||
} | ||
} |
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
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
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
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
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
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
45 changes: 45 additions & 0 deletions
45
src/test/scala/com/amazon/deequ/analyzers/ColumnCountTest.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,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) | ||
} | ||
} | ||
} |