Skip to content

Commit

Permalink
Allow selectExpr to accept multiple columns
Browse files Browse the repository at this point in the history
  • Loading branch information
pflooky committed Dec 4, 2023
1 parent 9eb092f commit 88ebb73
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,16 @@ case class ValidationBuilder(validation: Validation = ExpressionValidation()) {
* SQL expression used to apply to columns before running validations.
*
* For example,
* {{{validation.selectExpr("PERCENTILE(amount, 0.5) AS median_amount, *")}}}
* {{{validation.selectExpr("PERCENTILE(amount, 0.5) AS median_amount", "*")}}}
*
* @param expr SQL expression
* @param expr SQL expressions
* @return ValidationBuilder
* @see <a href="https://spark.apache.org/docs/latest/api/sql/">SQL expressions</a>
*/
def selectExpr(expr: String): ValidationBuilder = {
@varargs def selectExpr(expr: String*): ValidationBuilder = {
validation match {
case expressionValidation: ExpressionValidation =>
val withExpr = expressionValidation.modify(_.selectExpr).setTo(expr)
val withExpr = expressionValidation.modify(_.selectExpr).setTo(expr.toList)
copyWithDescAndThreshold(withExpr)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait Validation {

case class ExpressionValidation(
whereExpr: String = "true",
selectExpr: String = "*"
selectExpr: List[String] = List("*")
) extends Validation

case class GroupByValidation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ class ValidationBuilderSerializer extends JsonSerializer[ValidationBuilder] {
gen.writeStartObject()
validation match {
case ExpressionValidation(expr, selectExpr) =>
gen.writeArrayFieldStart("selectExpr")
selectExpr.foreach(gen.writeObject)
gen.writeEndArray()
gen.writeStringField("whereExpr", expr)
gen.writeStringField("selectExpr", selectExpr)
case GroupByValidation(groupByCols, aggCol, aggType, expr) =>
gen.writeArrayFieldStart("groupByCols")
groupByCols.foreach(gen.writeObject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ class ResultHtmlWriter {
val options = validation match {
case ExpressionValidation(expr, selectExpr) =>
List(
List("selectExpr", selectExpr),
List("selectExpr", selectExpr.mkString(", ")),
List("whereExpr", expr),
List("errorThreshold", validation.errorThreshold.getOrElse(0.0).toString)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object ValidationHelper {

class ExpressionValidationOps(expressionValidation: ExpressionValidation) extends ValidationOps(expressionValidation) {
override def validate(df: DataFrame, dfCount: Long): ValidationResult = {
val dfWithSelectExpr = df.selectExpr(expressionValidation.selectExpr)
val dfWithSelectExpr = df.selectExpr(expressionValidation.selectExpr: _*)
validateWithExpression(dfWithSelectExpr, dfCount, expressionValidation.whereExpr)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ValidationOperationsTest extends SparkSuite {
}

test("Can define select expression to run before where expression") {
val validation = ExpressionValidation("median_amount < 1000", "PERCENTILE(amount, 0.5) AS median_amount")
val validation = ExpressionValidation("median_amount < 1000", List("PERCENTILE(amount, 0.5) AS median_amount"))
val result = new ExpressionValidationOps(validation).validate(df, 4)

assert(result.isSuccess)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
groupId=io.github.data-catering
version=0.5.4
version=0.5.5

scalaVersion=2.12
scalaSpecificVersion=2.12.15
Expand Down

0 comments on commit 88ebb73

Please sign in to comment.