Skip to content

Commit f74be74

Browse files
committed
[SPARK-8149][SQL] Break ExpressionEvaluationSuite down to multiple files
Also moved a few files in expressions package around to match test suites. Author: Reynold Xin <[email protected]> Closes #6693 from rxin/expr-refactoring and squashes the following commits: 857599f [Reynold Xin] Fixed style violation. c0eb74b [Reynold Xin] Fixed compilation. b3a40f8 [Reynold Xin] Refactored expression test suites.
1 parent 5e7b6b6 commit f74be74

23 files changed

+2340
-1958
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
161161
try Timestamp.valueOf(n) catch { case _: java.lang.IllegalArgumentException => null }
162162
})
163163
case BooleanType =>
164-
buildCast[Boolean](_, b => new Timestamp((if (b) 1 else 0)))
164+
buildCast[Boolean](_, b => new Timestamp(if (b) 1 else 0))
165165
case LongType =>
166166
buildCast[Long](_, l => new Timestamp(l))
167167
case IntegerType =>

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Expression.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ abstract class Expression extends TreeNode[Expression] {
8181
val objectTerm = ctx.freshName("obj")
8282
s"""
8383
/* expression: ${this} */
84-
Object ${objectTerm} = expressions[${ctx.references.size - 1}].eval(i);
85-
boolean ${ev.isNull} = ${objectTerm} == null;
84+
Object $objectTerm = expressions[${ctx.references.size - 1}].eval(i);
85+
boolean ${ev.isNull} = $objectTerm == null;
8686
${ctx.javaType(this.dataType)} ${ev.primitive} = ${ctx.defaultValue(this.dataType)};
8787
if (!${ev.isNull}) {
88-
${ev.primitive} = (${ctx.boxedType(this.dataType)})${objectTerm};
88+
${ev.primitive} = (${ctx.boxedType(this.dataType)}) $objectTerm;
8989
}
9090
"""
9191
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -322,102 +322,6 @@ case class Remainder(left: Expression, right: Expression) extends BinaryArithmet
322322
}
323323
}
324324

325-
/**
326-
* A function that calculates bitwise and(&) of two numbers.
327-
*/
328-
case class BitwiseAnd(left: Expression, right: Expression) extends BinaryArithmetic {
329-
override def symbol: String = "&"
330-
331-
protected def checkTypesInternal(t: DataType) =
332-
TypeUtils.checkForBitwiseExpr(t, "operator " + symbol)
333-
334-
private lazy val and: (Any, Any) => Any = dataType match {
335-
case ByteType =>
336-
((evalE1: Byte, evalE2: Byte) => (evalE1 & evalE2).toByte).asInstanceOf[(Any, Any) => Any]
337-
case ShortType =>
338-
((evalE1: Short, evalE2: Short) => (evalE1 & evalE2).toShort).asInstanceOf[(Any, Any) => Any]
339-
case IntegerType =>
340-
((evalE1: Int, evalE2: Int) => evalE1 & evalE2).asInstanceOf[(Any, Any) => Any]
341-
case LongType =>
342-
((evalE1: Long, evalE2: Long) => evalE1 & evalE2).asInstanceOf[(Any, Any) => Any]
343-
}
344-
345-
protected override def evalInternal(evalE1: Any, evalE2: Any) = and(evalE1, evalE2)
346-
}
347-
348-
/**
349-
* A function that calculates bitwise or(|) of two numbers.
350-
*/
351-
case class BitwiseOr(left: Expression, right: Expression) extends BinaryArithmetic {
352-
override def symbol: String = "|"
353-
354-
protected def checkTypesInternal(t: DataType) =
355-
TypeUtils.checkForBitwiseExpr(t, "operator " + symbol)
356-
357-
private lazy val or: (Any, Any) => Any = dataType match {
358-
case ByteType =>
359-
((evalE1: Byte, evalE2: Byte) => (evalE1 | evalE2).toByte).asInstanceOf[(Any, Any) => Any]
360-
case ShortType =>
361-
((evalE1: Short, evalE2: Short) => (evalE1 | evalE2).toShort).asInstanceOf[(Any, Any) => Any]
362-
case IntegerType =>
363-
((evalE1: Int, evalE2: Int) => evalE1 | evalE2).asInstanceOf[(Any, Any) => Any]
364-
case LongType =>
365-
((evalE1: Long, evalE2: Long) => evalE1 | evalE2).asInstanceOf[(Any, Any) => Any]
366-
}
367-
368-
protected override def evalInternal(evalE1: Any, evalE2: Any) = or(evalE1, evalE2)
369-
}
370-
371-
/**
372-
* A function that calculates bitwise xor of two numbers.
373-
*/
374-
case class BitwiseXor(left: Expression, right: Expression) extends BinaryArithmetic {
375-
override def symbol: String = "^"
376-
377-
protected def checkTypesInternal(t: DataType) =
378-
TypeUtils.checkForBitwiseExpr(t, "operator " + symbol)
379-
380-
private lazy val xor: (Any, Any) => Any = dataType match {
381-
case ByteType =>
382-
((evalE1: Byte, evalE2: Byte) => (evalE1 ^ evalE2).toByte).asInstanceOf[(Any, Any) => Any]
383-
case ShortType =>
384-
((evalE1: Short, evalE2: Short) => (evalE1 ^ evalE2).toShort).asInstanceOf[(Any, Any) => Any]
385-
case IntegerType =>
386-
((evalE1: Int, evalE2: Int) => evalE1 ^ evalE2).asInstanceOf[(Any, Any) => Any]
387-
case LongType =>
388-
((evalE1: Long, evalE2: Long) => evalE1 ^ evalE2).asInstanceOf[(Any, Any) => Any]
389-
}
390-
391-
protected override def evalInternal(evalE1: Any, evalE2: Any): Any = xor(evalE1, evalE2)
392-
}
393-
394-
/**
395-
* A function that calculates bitwise not(~) of a number.
396-
*/
397-
case class BitwiseNot(child: Expression) extends UnaryArithmetic {
398-
override def toString: String = s"~$child"
399-
400-
override def checkInputDataTypes(): TypeCheckResult =
401-
TypeUtils.checkForBitwiseExpr(child.dataType, "operator ~")
402-
403-
private lazy val not: (Any) => Any = dataType match {
404-
case ByteType =>
405-
((evalE: Byte) => (~evalE).toByte).asInstanceOf[(Any) => Any]
406-
case ShortType =>
407-
((evalE: Short) => (~evalE).toShort).asInstanceOf[(Any) => Any]
408-
case IntegerType =>
409-
((evalE: Int) => ~evalE).asInstanceOf[(Any) => Any]
410-
case LongType =>
411-
((evalE: Long) => ~evalE).asInstanceOf[(Any) => Any]
412-
}
413-
414-
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): Code = {
415-
defineCodeGen(ctx, ev, c => s"(${ctx.javaType(dataType)})~($c)")
416-
}
417-
418-
protected override def evalInternal(evalE: Any) = not(evalE)
419-
}
420-
421325
case class MaxOf(left: Expression, right: Expression) extends BinaryArithmetic {
422326
override def nullable: Boolean = left.nullable && right.nullable
423327

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.catalyst.expressions
19+
20+
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
21+
import org.apache.spark.sql.catalyst.expressions.codegen._
22+
import org.apache.spark.sql.catalyst.util.TypeUtils
23+
import org.apache.spark.sql.types._
24+
25+
26+
/**
27+
* A function that calculates bitwise and(&) of two numbers.
28+
*/
29+
case class BitwiseAnd(left: Expression, right: Expression) extends BinaryArithmetic {
30+
override def symbol: String = "&"
31+
32+
protected def checkTypesInternal(t: DataType) =
33+
TypeUtils.checkForBitwiseExpr(t, "operator " + symbol)
34+
35+
private lazy val and: (Any, Any) => Any = dataType match {
36+
case ByteType =>
37+
((evalE1: Byte, evalE2: Byte) => (evalE1 & evalE2).toByte).asInstanceOf[(Any, Any) => Any]
38+
case ShortType =>
39+
((evalE1: Short, evalE2: Short) => (evalE1 & evalE2).toShort).asInstanceOf[(Any, Any) => Any]
40+
case IntegerType =>
41+
((evalE1: Int, evalE2: Int) => evalE1 & evalE2).asInstanceOf[(Any, Any) => Any]
42+
case LongType =>
43+
((evalE1: Long, evalE2: Long) => evalE1 & evalE2).asInstanceOf[(Any, Any) => Any]
44+
}
45+
46+
protected override def evalInternal(evalE1: Any, evalE2: Any) = and(evalE1, evalE2)
47+
}
48+
49+
/**
50+
* A function that calculates bitwise or(|) of two numbers.
51+
*/
52+
case class BitwiseOr(left: Expression, right: Expression) extends BinaryArithmetic {
53+
override def symbol: String = "|"
54+
55+
protected def checkTypesInternal(t: DataType) =
56+
TypeUtils.checkForBitwiseExpr(t, "operator " + symbol)
57+
58+
private lazy val or: (Any, Any) => Any = dataType match {
59+
case ByteType =>
60+
((evalE1: Byte, evalE2: Byte) => (evalE1 | evalE2).toByte).asInstanceOf[(Any, Any) => Any]
61+
case ShortType =>
62+
((evalE1: Short, evalE2: Short) => (evalE1 | evalE2).toShort).asInstanceOf[(Any, Any) => Any]
63+
case IntegerType =>
64+
((evalE1: Int, evalE2: Int) => evalE1 | evalE2).asInstanceOf[(Any, Any) => Any]
65+
case LongType =>
66+
((evalE1: Long, evalE2: Long) => evalE1 | evalE2).asInstanceOf[(Any, Any) => Any]
67+
}
68+
69+
protected override def evalInternal(evalE1: Any, evalE2: Any) = or(evalE1, evalE2)
70+
}
71+
72+
/**
73+
* A function that calculates bitwise xor of two numbers.
74+
*/
75+
case class BitwiseXor(left: Expression, right: Expression) extends BinaryArithmetic {
76+
override def symbol: String = "^"
77+
78+
protected def checkTypesInternal(t: DataType) =
79+
TypeUtils.checkForBitwiseExpr(t, "operator " + symbol)
80+
81+
private lazy val xor: (Any, Any) => Any = dataType match {
82+
case ByteType =>
83+
((evalE1: Byte, evalE2: Byte) => (evalE1 ^ evalE2).toByte).asInstanceOf[(Any, Any) => Any]
84+
case ShortType =>
85+
((evalE1: Short, evalE2: Short) => (evalE1 ^ evalE2).toShort).asInstanceOf[(Any, Any) => Any]
86+
case IntegerType =>
87+
((evalE1: Int, evalE2: Int) => evalE1 ^ evalE2).asInstanceOf[(Any, Any) => Any]
88+
case LongType =>
89+
((evalE1: Long, evalE2: Long) => evalE1 ^ evalE2).asInstanceOf[(Any, Any) => Any]
90+
}
91+
92+
protected override def evalInternal(evalE1: Any, evalE2: Any): Any = xor(evalE1, evalE2)
93+
}
94+
95+
/**
96+
* A function that calculates bitwise not(~) of a number.
97+
*/
98+
case class BitwiseNot(child: Expression) extends UnaryArithmetic {
99+
override def toString: String = s"~$child"
100+
101+
override def checkInputDataTypes(): TypeCheckResult =
102+
TypeUtils.checkForBitwiseExpr(child.dataType, "operator ~")
103+
104+
private lazy val not: (Any) => Any = dataType match {
105+
case ByteType =>
106+
((evalE: Byte) => (~evalE).toByte).asInstanceOf[(Any) => Any]
107+
case ShortType =>
108+
((evalE: Short) => (~evalE).toShort).asInstanceOf[(Any) => Any]
109+
case IntegerType =>
110+
((evalE: Int) => ~evalE).asInstanceOf[(Any) => Any]
111+
case LongType =>
112+
((evalE: Long) => ~evalE).asInstanceOf[(Any) => Any]
113+
}
114+
115+
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): Code = {
116+
defineCodeGen(ctx, ev, c => s"(${ctx.javaType(dataType)})~($c)")
117+
}
118+
119+
protected override def evalInternal(evalE: Any) = not(evalE)
120+
}

0 commit comments

Comments
 (0)