-
Notifications
You must be signed in to change notification settings - Fork 34
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
String Length Check #14
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f7648a
String Length Check
d360227
Update README.md
samratmitra-0812 5087f20
Update README.md
samratmitra-0812 d34bc09
Update README.md
samratmitra-0812 534734d
Removed unused imports and variables
7e8ab17
Changes to README
50a6416
Unit Test Fix:Check that exactly one of the 2 errors are present
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
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
126 changes: 126 additions & 0 deletions
126
src/main/scala/com/target/data_validator/validator/StringLengthCheck.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,126 @@ | ||
package com.target.data_validator.validator | ||
|
||
import com.target.data_validator.{JsonEncoders, ValidatorError, VarSubstitution} | ||
import com.target.data_validator.JsonUtils.debugJson | ||
import com.target.data_validator.validator.ValidatorBase._ | ||
import com.typesafe.scalalogging.LazyLogging | ||
import io.circe.{DecodingFailure, HCursor, Json} | ||
import io.circe.syntax._ | ||
import org.apache.spark.sql.DataFrame | ||
import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.types.{DataType, IntegerType, StringType, StructType} | ||
|
||
case class StringLengthCheck( | ||
column: String, | ||
minValue: Option[Json], | ||
maxValue: Option[Json] | ||
) extends RowBased { | ||
|
||
override def substituteVariables(dict: VarSubstitution): ValidatorBase = { | ||
|
||
val ret = StringLengthCheck( | ||
getVarSub(column, "column", dict), | ||
minValue.map(getVarSubJson(_, "minValue", dict)), | ||
maxValue.map(getVarSubJson(_, "maxValue", dict)) | ||
) | ||
getEvents.foreach(ret.addEvent) | ||
ret | ||
} | ||
|
||
private def cmpExpr(colExpr: Expression, | ||
value: Option[Json], | ||
cmp: (Expression, Expression) => Expression | ||
): Option[Expression] = { | ||
value.map { v => cmp(colExpr, createLiteralOrUnresolvedAttribute(IntegerType, v)) } | ||
} | ||
|
||
override def colTest(schema: StructType, dict: VarSubstitution): Expression = { | ||
|
||
val colExp = Length(UnresolvedAttribute(column)) | ||
|
||
val minValueExpression = cmpExpr(colExp, minValue, LessThan) | ||
val maxValueExpression = cmpExpr(colExp, maxValue, GreaterThan) | ||
|
||
val ret = (minValueExpression, maxValueExpression) match { | ||
case (Some(x), None) => x | ||
case (None, Some(y)) => y | ||
case (Some(x), Some(y)) => Or(x, y) | ||
case _ => throw new RuntimeException("Must define min or max value.") | ||
} | ||
logger.debug(s"Expr: $ret") | ||
ret | ||
} | ||
|
||
private def checkMinLessThanOrEqualToMax(values: List[Json]): Unit = { | ||
|
||
if (values.forall(_.isNumber)) { | ||
values.flatMap(_.asNumber) match { | ||
case mv :: xv :: Nil if mv.toDouble > xv.toDouble => | ||
addEvent(ValidatorError(s"min: ${minValue.get} must be less than or equal to max: ${maxValue.get}")) | ||
case _ => | ||
} | ||
} else if (values.forall(_.isString)) { | ||
values.flatMap(_.asString) match { | ||
case mv :: xv :: Nil if mv == xv => | ||
addEvent(ValidatorError(s"Min[String]: $mv must be less than max[String]: $xv")) | ||
case _ => | ||
} | ||
} else { | ||
// Not Strings or Numbers | ||
addEvent(ValidatorError(s"Unsupported type in ${values.map(debugJson).mkString(", ")}")) | ||
} | ||
} | ||
|
||
override def configCheck(df: DataFrame): Boolean = { | ||
|
||
// Verify if at least one of min or max is specified. | ||
val values = (minValue::maxValue::Nil).flatten | ||
if (values.isEmpty) { | ||
addEvent(ValidatorError("Must define minValue or maxValue or both.")) | ||
} | ||
|
||
// Verify that min is less than max | ||
checkMinLessThanOrEqualToMax(values) | ||
|
||
// Verify that the data type of the specified column is a String. | ||
val colType = findColumnInDataFrame(df, column) | ||
if (colType.isDefined) { | ||
val dataType = colType.get.dataType | ||
if (!(dataType.isInstanceOf[StringType])) { | ||
addEvent(ValidatorError(s"Data type of column '$column' must be String, but was found to be $dataType")) | ||
} | ||
} | ||
|
||
failed | ||
} | ||
|
||
override def toJson: Json = { | ||
import JsonEncoders.eventEncoder | ||
val fields = Seq( | ||
("type", Json.fromString("stringLengthCheck")), | ||
("column", Json.fromString(column)) | ||
) ++ | ||
minValue.map(mv => ("minValue", mv)) ++ | ||
maxValue.map(mv => ("maxValue", mv)) ++ | ||
Seq( | ||
("events", getEvents.asJson) | ||
) | ||
Json.obj(fields: _*) | ||
} | ||
} | ||
|
||
object StringLengthCheck extends LazyLogging { | ||
def fromJson(c: HCursor): Either[DecodingFailure, ValidatorBase] = { | ||
val column = c.downField("column").as[String].right.get | ||
val minValueJ = c.downField("minValue").as[Json].right.toOption | ||
val maxValueJ = c.downField("maxValue").as[Json].right.toOption | ||
|
||
logger.debug(s"column: $column") | ||
logger.debug(s"minValue: $minValueJ type: ${minValueJ.getClass.getCanonicalName}") | ||
logger.debug(s"maxValue: $maxValueJ type: ${maxValueJ.getClass.getCanonicalName}") | ||
|
||
c.focus.foreach {f => logger.info(s"StringLengthCheckJson: ${f.spaces2}")} | ||
scala.util.Right(StringLengthCheck(column, minValueJ, maxValueJ)) | ||
} | ||
} |
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.
@samratmitra-0812
DataType
is an unused import