Github user marmbrus commented on a diff in the pull request:

    https://github.com/apache/spark/pull/224#discussion_r11002751
  
    --- Diff: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ExpressionEvaluationSuite.scala
 ---
    @@ -26,7 +26,186 @@ import org.apache.spark.sql.catalyst.types._
     /* Implicit conversions */
     import org.apache.spark.sql.catalyst.dsl.expressions._
     
    -class ExpressionEvaluationSuite extends FunSuite {
    +import org.apache.spark.sql.catalyst.types._
    +import org.apache.spark.sql.catalyst.dsl._
    +
    +
    +/**
    + * Root class of expression evaluation test
    + */
    +trait ExprEvalTest {
    +  type Execution = (Row => Row)
    +
    +  def engine: Execution
    +}
    +
    +case class InterpretExprEvalTest(exprs: Seq[Expression]) extends 
ExprEvalTest {
    +  override def engine: Execution = new Projection(exprs)
    +}
    +
    +class InterpretExpressionEvaluationSuite extends ExpressionEvaluationSuite 
{
    +  override def executor(exprs: Seq[Expression]) = 
InterpretExprEvalTest(exprs)
    +}
    +
    +trait ExpressionEvaluationSuite extends FunSuite {
    +  /**
    +   * The sub classes need to create the ExprEvalTest object 
    +   */
    +  def executor(exprs: Seq[Expression]): ExprEvalTest
    +  
    +  val data: Row = new GenericRow(Array(1, null, 1.0, true, 4, 5, null, 
"abcccd", "a%"))
    +
    +  // TODO add to DSL
    +  val c1 = BoundReference(0, AttributeReference("a", IntegerType)())
    +  val c2 = BoundReference(1, AttributeReference("b", IntegerType)())
    +  val c3 = BoundReference(2, AttributeReference("c", DoubleType)())
    +  val c4 = BoundReference(3, AttributeReference("d", BooleanType)())
    +  val c5 = BoundReference(4, AttributeReference("e", IntegerType)())
    +  val c6 = BoundReference(5, AttributeReference("f", IntegerType)())
    +  val c7 = BoundReference(6, AttributeReference("g", StringType)())
    +  val c8 = BoundReference(7, AttributeReference("h", StringType)())
    +  val c9 = BoundReference(8, AttributeReference("i", StringType)())
    +  
    +  /**
    +   * Compare each of the field if it equals the expected value.
    +   * 
    +   * expected is a sequence of (Any, Any), 
    +   * and the first element indicates:
    +   *   true:  the expected value is field is null
    +   *   false: the expected value is not null
    +   *   Exception Class: the expected exception class while computing the 
value 
    +   * the second element is the real value when first element equals 
false(not null)
    +   */
    +  def verify(expected: Seq[(Any, Any)], result: Row, input: Row) {
    +    Seq.tabulate(expected.size) { i =>
    +      expected(i) match {
    +        case (false, expected) => {
    +          assert(result.isNullAt(i) == false, 
    +            s"Input:($input), Output field:$i shouldn't be null")
    +
    +          val real = result.apply(i)
    +          assert(real == expected, 
    +            s"Input:($input), Output field:$i is expected as $expected, 
but got $real")
    +        }
    +        case (true, _) => {
    +          assert(result.isNullAt(i) == true, s"Input:($input), Output 
field:$i is expected as null")
    +        }
    +        case (exception: Class[_], _) => {
    +          assert(result.isNullAt(i) == false, 
    +            s"Input:($input), Output field:$i should be exception")
    +
    +          val real = result.apply(i).getClass.getName
    +          val expect = exception.getName
    +          assert(real == expect, 
    +            s"Input:($input), Output field:$i expect exception $expect, 
but got $real")
    +        }
    +      }
    +    }
    +  }
    +
    +  def verify(expecteds: Seq[Seq[(Any, Any)]], results: Seq[Row], inputs: 
Seq[Row]) {
    +    Range(0, expecteds.length).foreach { i =>
    +      verify(expecteds(i), results(i), inputs(i))
    +    }
    +  }
    +  
    +  def proc(tester: ExprEvalTest, input: Row): Row = {
    +    try {
    +      tester.engine.apply(input)
    +    } catch {
    +      case x: Any => {
    +        new GenericRow(Array(x.asInstanceOf[Any]))
    +      }
    +    }
    +  }
    +  
    +  def run(exprs: Seq[Expression], expected: Seq[(Any, Any)], input: Row) {
    +    val tester = executor(exprs)
    +    
    +    verify(expected, proc(tester,input), input)
    +  }
    +  
    +  def run(exprs: Seq[Expression], expecteds: Seq[Seq[(Any, Any)]], inputs: 
Seq[Row]) {
    +    val tester = executor(exprs)
    +    
    +    verify(expecteds, inputs.map(proc(tester,_)), inputs)
    +  }
    +  
    +  test("logical") {
    +    val expected = Seq[(Boolean, Any)](
    +        (false, false), 
    +        (true, -1), 
    +        (false, true), 
    +        (false, true), 
    +        (false, false))
    +
    +    val exprs = Seq[Expression](And(LessThan(Cast(c1, DoubleType), c3), 
LessThan(c1, c2)), 
    +      Or(LessThan(Cast(c1, DoubleType), c3), LessThan(c1, c2)),
    +      IsNull(c2),
    +      IsNotNull(c3),
    +      Not(c4))
    +    
    +    run(exprs, expected, data)
    +  }
    +  
    +  test("arithmetic") {
    +    val exprs = Array[Expression](
    +      Add(c1, c2),
    +      Add(c1, c5),
    +      Divide(c1, c5),
    +      Subtract(c1, c5),
    +      Multiply(c1, c5),
    +      Remainder(c1, c5),
    +      UnaryMinus(c1)
    +    )
    +    val expecteds = Seq[(Boolean, Any)](
    +        (true, 0), 
    +        (false, 5), 
    +        (false, 0), 
    +        (false, -3), 
    +        (false, 4),
    +        (false, 1),
    +        (false, -1))
    +
    +    run(exprs, expecteds, data)
    +  }
    +
    +  test("string like / rlike") {
    +    val exprs = Seq(
    +      Like(c7, Literal("a", StringType)),
    --- End diff --
    
    The way these are structured the answer for a given test case ends up being 
16 lines away from the test case itself, making it harder to read than it needs 
to be.  I think sequences of tuples makes sense for something like a truth 
table, but in this case I think something much simpler would be much easier for 
people to understand.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to