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 @@ -236,6 +236,8 @@ trait HiveTypeCoercion {
case e if !e.childrenResolved => e
// No need to change EqualTo operators as that actually makes sense for boolean types.
case e: EqualTo => e
// No need to change the EqualNullSafe operators, too
case e: EqualNullSafe => e
// Otherwise turn them to Byte types so that there exists and ordering.
case p: BinaryComparison
if p.left.dataType == BooleanType && p.right.dataType == BooleanType =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ package object dsl {
def > (other: Expression) = GreaterThan(expr, other)
def >= (other: Expression) = GreaterThanOrEqual(expr, other)
def === (other: Expression) = EqualTo(expr, other)
def <=> (other: Expression) = EqualNullSafe(expr, other)
def !== (other: Expression) = Not(EqualTo(expr, other))

def in(list: Expression*) = In(expr, list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ case class EqualTo(left: Expression, right: Expression) extends BinaryComparison
}
}

case class EqualNullSafe(left: Expression, right: Expression) extends BinaryComparison {
def symbol = "<=>"
override def nullable = false
override def eval(input: Row): Any = {
val l = left.eval(input)
val r = right.eval(input)
if (l == null && r == null) {
true
} else if (l == null || r == null) {
false
} else {
l == r
}
}
}

case class LessThan(left: Expression, right: Expression) extends BinaryComparison {
def symbol = "<"
override def eval(input: Row): Any = c2(input, left, right, _.lt(_, _))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ object NullPropagation extends Rule[LogicalPlan] {
case e @ GetItem(Literal(null, _), _) => Literal(null, e.dataType)
case e @ GetItem(_, Literal(null, _)) => Literal(null, e.dataType)
case e @ GetField(Literal(null, _), _) => Literal(null, e.dataType)
case e @ EqualNullSafe(Literal(null, _), r) => IsNull(r)
case e @ EqualNullSafe(l, Literal(null, _)) => IsNull(l)

// For Coalesce, remove null literals.
case e @ Coalesce(children) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,13 @@ class ExpressionEvaluationSuite extends FunSuite {
}

test("BinaryComparison") {
val row = new GenericRow(Array[Any](1, 2, 3, null))
val row = new GenericRow(Array[Any](1, 2, 3, null, 3, null))
val c1 = 'a.int.at(0)
val c2 = 'a.int.at(1)
val c3 = 'a.int.at(2)
val c4 = 'a.int.at(3)
val c5 = 'a.int.at(4)
val c6 = 'a.int.at(5)

checkEvaluation(LessThan(c1, c4), null, row)
checkEvaluation(LessThan(c1, c2), true, row)
Expand All @@ -469,6 +471,12 @@ class ExpressionEvaluationSuite extends FunSuite {
checkEvaluation(c1 >= c2, false, row)
checkEvaluation(c1 === c2, false, row)
checkEvaluation(c1 !== c2, true, row)
checkEvaluation(c4 <=> c1, false, row)
checkEvaluation(c1 <=> c4, false, row)
checkEvaluation(c4 <=> c6, true, row)
checkEvaluation(c3 <=> c5, true, row)
checkEvaluation(Literal(true) <=> Literal(null, BooleanType), false, row)
checkEvaluation(Literal(null, BooleanType) <=> Literal(true), false, row)
}

test("StringComparison") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ class HiveCompatibilitySuite extends HiveQueryFileTest with BeforeAndAfter {

// Hive returns the results of describe as plain text. Comments with multiple lines
// introduce extra lines in the Hive results, which make the result comparison fail.
"describe_comment_indent"
"describe_comment_indent",

// Limit clause without a ordering, which causes failure.
"orc_predicate_pushdown"
)

/**
Expand Down Expand Up @@ -503,6 +506,7 @@ class HiveCompatibilitySuite extends HiveQueryFileTest with BeforeAndAfter {
"join_hive_626",
"join_map_ppr",
"join_nulls",
"join_nullsafe",
"join_rc",
"join_reorder2",
"join_reorder3",
Expand Down Expand Up @@ -734,6 +738,7 @@ class HiveCompatibilitySuite extends HiveQueryFileTest with BeforeAndAfter {
"udf_double",
"udf_E",
"udf_elt",
"udf_equal",
"udf_exp",
"udf_field",
"udf_find_in_set",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class HiveContext(sc: SparkContext) extends SQLContext(sc) {

protected val primitiveTypes =
Seq(StringType, IntegerType, LongType, DoubleType, FloatType, BooleanType, ByteType,
ShortType, DecimalType, TimestampType)
ShortType, DecimalType, TimestampType, BinaryType)

protected def toHiveString(a: (Any, DataType)): String = a match {
case (struct: Row, StructType(fields)) =>
Expand All @@ -269,6 +269,7 @@ class HiveContext(sc: SparkContext) extends SQLContext(sc) {
}.toSeq.sorted.mkString("{", ",", "}")
case (null, _) => "NULL"
case (t: Timestamp, TimestampType) => new TimestampWritable(t).toString
case (bin: Array[Byte], BinaryType) => new String(bin, "UTF-8")
case (other, tpe) if primitiveTypes contains tpe => other.toString
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ private[hive] object HiveQl {
/* Comparisons */
case Token("=", left :: right:: Nil) => EqualTo(nodeToExpr(left), nodeToExpr(right))
case Token("==", left :: right:: Nil) => EqualTo(nodeToExpr(left), nodeToExpr(right))
case Token("<=>", left :: right:: Nil) => EqualNullSafe(nodeToExpr(left), nodeToExpr(right))
case Token("!=", left :: right:: Nil) => Not(EqualTo(nodeToExpr(left), nodeToExpr(right)))
case Token("<>", left :: right:: Nil) => Not(EqualTo(nodeToExpr(left), nodeToExpr(right)))
case Token(">", left :: right:: Nil) => GreaterThan(nodeToExpr(left), nodeToExpr(right))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NULL 10 10 NULL NULL 10
100 100 100 100 100 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NULL NULL NULL NULL NULL NULL
NULL 10 10 NULL NULL 10
10 NULL NULL 10 10 NULL
100 100 100 100 100 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
NULL NULL NULL NULL
NULL NULL 10 NULL
NULL NULL 48 NULL
NULL 10 NULL NULL
NULL 10 10 NULL
NULL 10 48 NULL
NULL 35 NULL NULL
NULL 35 10 NULL
NULL 35 48 NULL
10 NULL NULL 10
48 NULL NULL NULL
100 100 100 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
NULL NULL NULL NULL
NULL NULL NULL 35
NULL NULL 10 NULL
NULL NULL 48 NULL
NULL 10 NULL NULL
NULL 10 10 NULL
NULL 10 48 NULL
NULL 35 NULL NULL
NULL 35 10 NULL
NULL 35 48 NULL
10 NULL NULL 10
100 100 100 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NULL NULL NULL NULL
NULL NULL NULL 35
NULL NULL 10 NULL
NULL NULL 48 NULL
NULL 10 NULL NULL
NULL 10 10 NULL
NULL 10 48 NULL
NULL 35 NULL NULL
NULL 35 10 NULL
NULL 35 48 NULL
10 NULL NULL 10
48 NULL NULL NULL
100 100 100 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NULL NULL NULL NULL
NULL NULL 10 NULL
NULL NULL 48 NULL
NULL 10 NULL NULL
NULL 10 10 NULL
NULL 10 48 NULL
NULL 35 NULL NULL
NULL 35 10 NULL
NULL 35 48 NULL
10 NULL NULL 10
100 100 100 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
NULL NULL NULL NULL
NULL NULL 10 NULL
NULL NULL 48 NULL
NULL 10 NULL NULL
NULL 10 10 NULL
NULL 10 48 NULL
NULL 35 NULL NULL
NULL 35 10 NULL
NULL 35 48 NULL
10 NULL NULL 10
100 100 100 100
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL 10
NULL NULL NULL 10
NULL NULL NULL 35
NULL NULL NULL 35
NULL NULL NULL 110
NULL NULL NULL 110
NULL NULL NULL 135
NULL NULL NULL 135
NULL 10 NULL NULL
NULL 10 NULL NULL
NULL 10 NULL 10
NULL 10 NULL 35
NULL 10 NULL 110
NULL 10 NULL 135
NULL 35 NULL NULL
NULL 35 NULL NULL
NULL 35 NULL 10
NULL 35 NULL 35
NULL 35 NULL 110
NULL 35 NULL 135
NULL 110 NULL NULL
NULL 110 NULL NULL
NULL 110 NULL 10
NULL 110 NULL 35
NULL 110 NULL 110
NULL 110 NULL 135
NULL 135 NULL NULL
NULL 135 NULL NULL
NULL 135 NULL 10
NULL 135 NULL 35
NULL 135 NULL 110
NULL 135 NULL 135
10 NULL 10 NULL
48 NULL 48 NULL
100 100 100 100
110 NULL 110 NULL
148 NULL 148 NULL
200 200 200 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL 10 NULL 10
NULL 35 NULL 35
NULL 110 NULL 110
NULL 135 NULL 135
10 NULL 10 NULL
48 NULL 48 NULL
100 100 100 100
110 NULL 110 NULL
148 NULL 148 NULL
200 200 200 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL 10
NULL NULL NULL 10
NULL NULL NULL 35
NULL NULL NULL 35
NULL NULL NULL 110
NULL NULL NULL 110
NULL NULL NULL 135
NULL NULL NULL 135
NULL 10 NULL NULL
NULL 10 NULL NULL
NULL 10 NULL 10
NULL 10 NULL 35
NULL 10 NULL 110
NULL 10 NULL 135
NULL 35 NULL NULL
NULL 35 NULL NULL
NULL 35 NULL 10
NULL 35 NULL 35
NULL 35 NULL 110
NULL 35 NULL 135
NULL 110 NULL NULL
NULL 110 NULL NULL
NULL 110 NULL 10
NULL 110 NULL 35
NULL 110 NULL 110
NULL 110 NULL 135
NULL 135 NULL NULL
NULL 135 NULL NULL
NULL 135 NULL 10
NULL 135 NULL 35
NULL 135 NULL 110
NULL 135 NULL 135
10 NULL 10 NULL
48 NULL 48 NULL
100 100 100 100
110 NULL 110 NULL
148 NULL 148 NULL
200 200 200 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL 10
NULL NULL NULL 10
NULL NULL NULL 35
NULL NULL NULL 35
NULL NULL NULL 110
NULL NULL NULL 110
NULL NULL NULL 135
NULL NULL NULL 135
NULL 10 NULL NULL
NULL 10 NULL NULL
NULL 10 NULL 10
NULL 10 NULL 35
NULL 10 NULL 110
NULL 10 NULL 135
NULL 35 NULL NULL
NULL 35 NULL NULL
NULL 35 NULL 10
NULL 35 NULL 35
NULL 35 NULL 110
NULL 35 NULL 135
NULL 110 NULL NULL
NULL 110 NULL NULL
NULL 110 NULL 10
NULL 110 NULL 35
NULL 110 NULL 110
NULL 110 NULL 135
NULL 135 NULL NULL
NULL 135 NULL NULL
NULL 135 NULL 10
NULL 135 NULL 35
NULL 135 NULL 110
NULL 135 NULL 135
10 NULL 10 NULL
48 NULL 48 NULL
100 100 100 100
110 NULL 110 NULL
148 NULL 148 NULL
200 200 200 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL NULL
NULL NULL NULL 10
NULL NULL NULL 10
NULL NULL NULL 35
NULL NULL NULL 35
NULL NULL NULL 110
NULL NULL NULL 110
NULL NULL NULL 135
NULL NULL NULL 135
NULL 10 NULL NULL
NULL 10 NULL NULL
NULL 10 NULL 10
NULL 10 NULL 35
NULL 10 NULL 110
NULL 10 NULL 135
NULL 35 NULL NULL
NULL 35 NULL NULL
NULL 35 NULL 10
NULL 35 NULL 35
NULL 35 NULL 110
NULL 35 NULL 135
NULL 110 NULL NULL
NULL 110 NULL NULL
NULL 110 NULL 10
NULL 110 NULL 35
NULL 110 NULL 110
NULL 110 NULL 135
NULL 135 NULL NULL
NULL 135 NULL NULL
NULL 135 NULL 10
NULL 135 NULL 35
NULL 135 NULL 110
NULL 135 NULL 135
10 NULL 10 NULL
48 NULL 48 NULL
100 100 100 100
110 NULL 110 NULL
148 NULL 148 NULL
200 200 200 200
Loading