Skip to content
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

allow nullable matcher pattern for hasPattern (#342) #425

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion src/main/scala/com/amazon/deequ/analyzers/PatternMatch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ import scala.util.matching.Regex
* @param pattern The regular expression to check for
* @param where Additional filter to apply before the analyzer is run.
*/
case class PatternMatch(column: String, pattern: Regex, where: Option[String] = None)
case class PatternMatch(
column: String,
pattern: Regex,
where: Option[String] = None,
isNullAllowed: Boolean = false)
extends StandardScanShareableAnalyzer[NumMatchesAndCount]("PatternMatch", column)
with FilterableAnalyzer {

Expand All @@ -48,6 +52,7 @@ case class PatternMatch(column: String, pattern: Regex, where: Option[String] =
override def aggregationFunctions(): Seq[Column] = {

val expression = when(regexp_extract(col(column), pattern.toString(), 0) =!= lit(""), 1)
.when(lit(isNullAllowed) && col(column).isNull, 1)
.otherwise(0)

val summation = sum(conditionalSelection(expression, where).cast(IntegerType))
Expand Down
12 changes: 10 additions & 2 deletions src/main/scala/com/amazon/deequ/checks/Check.scala
Original file line number Diff line number Diff line change
Expand Up @@ -691,11 +691,19 @@ case class Check(
pattern: Regex,
assertion: Double => Boolean = Check.IsOne,
name: Option[String] = None,
hint: Option[String] = None)
hint: Option[String] = None,
isNullAllowed: Boolean = false)
: CheckWithLastConstraintFilterable = {

addFilterableConstraint { filter =>
Constraint.patternMatchConstraint(column, pattern, assertion, filter, name, hint)
Constraint.patternMatchConstraint(
column,
pattern,
assertion,
filter,
name,
hint,
isNullAllowed)
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/com/amazon/deequ/constraints/Constraint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,11 @@ object Constraint {
assertion: Double => Boolean,
where: Option[String] = None,
name: Option[String] = None,
hint: Option[String] = None)
hint: Option[String] = None,
isNullAllowed: Boolean = false)
: Constraint = {

val patternMatch = PatternMatch(column, pattern, where)
val patternMatch = PatternMatch(column, pattern, where, isNullAllowed)

val constraint = AnalysisBasedConstraint[NumMatchesAndCount, Double, Double](
patternMatch, assertion, hint = hint)
Expand Down
21 changes: 21 additions & 0 deletions src/test/scala/com/amazon/deequ/checks/CheckTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,27 @@ class CheckTest extends AnyWordSpec with Matchers with SparkContextSpec with Fix
assertSuccess(baseCheck.hasMaxLength("att1", _ == 4.0), context)
}

"ignore null values for hasPattern constraints" in withSparkSession { spark =>
import spark.implicits._

val df = Seq(
("123", 1),
(null, 2),
("456", 1)
).toDF("nullable", "id")

val check = Check(CheckLevel.Error, "some description")
.hasPattern("nullable", "\\d{3,3}".r, _ == 1.0)
val checkWithNullAllowed = Check(CheckLevel.Error, "some description")
.hasPattern("nullable", "\\d{3,3}".r, _ == 1.0, isNullAllowed = true)

val context = runChecks(df, check)
val nullAllowedContext = runChecks(df, checkWithNullAllowed)

assertEvaluatesTo(check, context, CheckStatus.Error)
assertEvaluatesTo(checkWithNullAllowed, nullAllowedContext, CheckStatus.Success)
}

"work on regular expression patterns for E-Mails" in withSparkSession { sparkSession =>
val col = "some"
val df = dataFrameWithColumn(col, StringType, sparkSession, Row("[email protected]"),
Expand Down