|
| 1 | +package com.target.data_validator.validator |
| 2 | + |
| 3 | +import com.target.data_validator.{JsonEncoders, ValidatorError, VarSubstitution} |
| 4 | +import com.target.data_validator.JsonUtils.debugJson |
| 5 | +import com.target.data_validator.validator.ValidatorBase._ |
| 6 | +import com.typesafe.scalalogging.LazyLogging |
| 7 | +import io.circe.{DecodingFailure, HCursor, Json} |
| 8 | +import io.circe.syntax._ |
| 9 | +import org.apache.spark.sql.DataFrame |
| 10 | +import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute |
| 11 | +import org.apache.spark.sql.catalyst.expressions._ |
| 12 | +import org.apache.spark.sql.types.{StringType, StructType} |
| 13 | + |
| 14 | +case class StringRegexCheck( |
| 15 | + column: String, |
| 16 | + regex: Option[Json], |
| 17 | + threshold: Option[String] |
| 18 | + ) extends RowBased { |
| 19 | + |
| 20 | + override def substituteVariables(dict: VarSubstitution): ValidatorBase = { |
| 21 | + |
| 22 | + val ret = StringRegexCheck( |
| 23 | + getVarSub(column, "column", dict), |
| 24 | + regex.map(getVarSubJson(_, "regex", dict)), |
| 25 | + threshold.map(getVarSub(_, "threshold", dict)) |
| 26 | + ) |
| 27 | + getEvents.foreach(ret.addEvent) |
| 28 | + ret |
| 29 | + } |
| 30 | + |
| 31 | + override def colTest(schema: StructType, dict: VarSubstitution): Expression = { |
| 32 | + |
| 33 | + val colExp = UnresolvedAttribute(column) |
| 34 | + |
| 35 | + val regexExpression = regex.map { r => RLike(colExp, createLiteralOrUnresolvedAttribute(StringType, r)) } |
| 36 | + |
| 37 | + val ret = regexExpression match { |
| 38 | + /* |
| 39 | + RLike returns false if the column value is null. |
| 40 | + To avoid counting null values as validation failures (like other validations), |
| 41 | + an explicit non null check on the column value is required. |
| 42 | + */ |
| 43 | + case Some(x) => And(Not(x), IsNotNull(colExp)) |
| 44 | + case _ => throw new RuntimeException("Must define a regex.") |
| 45 | + } |
| 46 | + logger.debug(s"Expr: $ret") |
| 47 | + ret |
| 48 | + } |
| 49 | + |
| 50 | + override def configCheck(df: DataFrame): Boolean = { |
| 51 | + |
| 52 | + // Verify if regex is specified. |
| 53 | + val values = (regex::Nil).flatten |
| 54 | + if (values.isEmpty) { |
| 55 | + addEvent(ValidatorError("Must define a regex.")) |
| 56 | + } |
| 57 | + |
| 58 | + // Verify that the data type of the specified column is a String. |
| 59 | + val colType = findColumnInDataFrame(df, column) |
| 60 | + if (colType.isDefined) { |
| 61 | + val dataType = colType.get.dataType |
| 62 | + if (!(dataType.isInstanceOf[StringType])) { |
| 63 | + addEvent(ValidatorError(s"Data type of column '$column' must be String, but was found to be $dataType")) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + failed |
| 68 | + } |
| 69 | + |
| 70 | + override def toJson: Json = { |
| 71 | + import JsonEncoders.eventEncoder |
| 72 | + val fields = Seq( |
| 73 | + ("type", Json.fromString("stringRegexCheck")), |
| 74 | + ("column", Json.fromString(column)) |
| 75 | + ) ++ |
| 76 | + regex.map(r => ("regex", r)) ++ |
| 77 | + Seq( |
| 78 | + ("events", getEvents.asJson) |
| 79 | + ) |
| 80 | + Json.obj(fields: _*) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +object StringRegexCheck extends LazyLogging { |
| 85 | + def fromJson(c: HCursor): Either[DecodingFailure, ValidatorBase] = { |
| 86 | + val column = c.downField("column").as[String].right.get |
| 87 | + val regex = c.downField("regex").as[Json].right.toOption |
| 88 | + val threshold = c.downField("threshold").as[String].right.toOption |
| 89 | + |
| 90 | + logger.debug(s"column: $column") |
| 91 | + logger.debug(s"regex: $regex type: ${regex.getClass.getCanonicalName}") |
| 92 | + logger.debug(s"threshold: $threshold type: ${threshold.getClass.getCanonicalName}") |
| 93 | + |
| 94 | + c.focus.foreach {f => logger.info(s"StringRegexCheckJson: ${f.spaces2}")} |
| 95 | + scala.util.Right(StringRegexCheck(column, regex, threshold)) |
| 96 | + } |
| 97 | +} |
0 commit comments