Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,13 @@ trait HiveTypeCoercion {
findTightestCommonTypeToString(left.dataType, right.dataType).map { widestType =>
val newLeft = if (left.dataType == widestType) left else Cast(left, widestType)
val newRight = if (right.dataType == widestType) right else Cast(right, widestType)
i.makeCopy(Array(pred, newLeft, newRight))
If(pred, newLeft, newRight)
}.getOrElse(i) // If there is no applicable conversion, leave expression unchanged.

// Convert If(null literal, _, _) into boolean type.
// In the optimizer, we should short-circuit this directly into false value.
case i @ If(pred, left, right) if pred.dataType == NullType =>
i.makeCopy(Array(Literal.create(null, BooleanType), left, right))
case If(pred, left, right) if pred.dataType == NullType =>
If(Literal.create(null, BooleanType), left, right)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.scalatest.Matchers._

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.types.{DoubleType, IntegerType}
import org.apache.spark.sql.types.{Decimal, DoubleType, IntegerType}


class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
Expand Down Expand Up @@ -75,6 +75,21 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
checkDoubleEvaluation(c3 % c2, (1.1 +- 0.001), row)
}

test("Abs") {
def testAbs(convert: (Int) => Any): Unit = {
checkEvaluation(Abs(Literal(convert(0))), convert(0))
checkEvaluation(Abs(Literal(convert(1))), convert(1))
checkEvaluation(Abs(Literal(convert(-1))), convert(1))
}
testAbs(_.toByte)
testAbs(_.toShort)
testAbs(identity)
testAbs(_.toLong)
testAbs(_.toFloat)
testAbs(_.toDouble)
testAbs(Decimal(_))
}

test("Divide") {
checkEvaluation(Divide(Literal(2), Literal(1)), 2)
checkEvaluation(Divide(Literal(1.0), Literal(2.0)), 0.5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,23 +369,6 @@ class ColumnExpressionSuite extends QueryTest {
)
}

test("abs") {
checkAnswer(
testData.select(abs('key)).orderBy('key.asc),
(1 to 100).map(n => Row(n))
)

checkAnswer(
negativeData.select(abs('key)).orderBy('key.desc),
(1 to 100).map(n => Row(n))
)

checkAnswer(
testData.select(abs(lit(null))),
(1 to 100).map(_ => Row(null))
)
}

test("upper") {
checkAnswer(
lowerCaseData.select(upper('l)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ class MathExpressionsSuite extends QueryTest {
testOneToOneNonNegativeMathFunction(log1p, math.log1p)
}

test("abs") {
val input =
Seq[(java.lang.Double, java.lang.Double)]((null, null), (0.0, 0.0), (1.5, 1.5), (-2.5, 2.5))
checkAnswer(
input.toDF("key", "value").select(abs($"key").alias("a")).sort("a"),
input.map(pair => Row(pair._2)))

checkAnswer(
input.toDF("key", "value").selectExpr("abs(key) a").sort("a"),
input.map(pair => Row(pair._2)))
}

test("log2") {
val df = Seq((1, 2)).toDF("a", "b")
checkAnswer(
Expand Down
12 changes: 0 additions & 12 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,6 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll with SQLTestUtils {
Seq(Row("1"), Row("2")))
}

test("SPARK-3176 Added Parser of SQL ABS()") {
checkAnswer(
sql("SELECT ABS(-1.3)"),
Row(1.3))
checkAnswer(
sql("SELECT ABS(0.0)"),
Row(0.0))
checkAnswer(
sql("SELECT ABS(2.5)"),
Row(2.5))
}

test("aggregation with codegen") {
val originalValue = sqlContext.conf.codegenEnabled
sqlContext.setConf(SQLConf.CODEGEN_ENABLED, "true")
Expand Down