Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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 @@ -3410,32 +3410,64 @@ case class ArrayDistinct(child: Expression)
}

override def nullSafeEval(array: Any): Any = {
val data = array.asInstanceOf[ArrayData].toArray[AnyRef](elementType)
val data = array.asInstanceOf[ArrayData]
doEvaluation(data)
}

@transient private lazy val doEvaluation = if (TypeUtils.typeWithProperEquals(elementType)) {
(data: Array[AnyRef]) => new GenericArrayData(data.distinct.asInstanceOf[Array[Any]])
(array: ArrayData) =>
val arrayBuffer = new scala.collection.mutable.ArrayBuffer[Any]
val hs = new SQLOpenHashSet[Any]()
val isNaN = SQLOpenHashSet.isNaN(elementType)
val valueNaN = SQLOpenHashSet.valueNaN(elementType)
var i = 0
while (i < array.numElements()) {
if (array.isNullAt(i)) {
if (!hs.containsNull) {
hs.addNull
arrayBuffer += null
}
} else {
val elem = array.get(i, elementType)
if (isNaN(elem)) {
if (!hs.containsNaN) {
arrayBuffer += valueNaN
hs.addNaN
}
} else {
if (!hs.contains(elem)) {
if (arrayBuffer.size > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
ArrayBinaryLike.throwUnionLengthOverflowException(arrayBuffer.size)
}
arrayBuffer += elem
hs.add(elem)
}
}
}
i += 1
}
new GenericArrayData(arrayBuffer.toSeq)
} else {
(data: Array[AnyRef]) => {
(data: ArrayData) => {
val array = data.toArray[AnyRef](elementType)
val arrayBuffer = new scala.collection.mutable.ArrayBuffer[AnyRef]
var alreadyStoredNull = false
for (i <- 0 until data.length) {
if (data(i) != null) {
for (i <- 0 until array.length) {
if (array(i) != null) {
var found = false
var j = 0
while (!found && j < arrayBuffer.size) {
val va = arrayBuffer(j)
found = (va != null) && ordering.equiv(va, data(i))
found = (va != null) && ordering.equiv(va, array(i))
j += 1
}
if (!found) {
arrayBuffer += data(i)
arrayBuffer += array(i)
}
} else {
// De-duplicate the null values.
if (!alreadyStoredNull) {
arrayBuffer += data(i)
arrayBuffer += array(i)
alreadyStoredNull = true
}
}
Expand All @@ -3454,10 +3486,9 @@ case class ArrayDistinct(child: Expression)
val ptName = CodeGenerator.primitiveTypeName(jt)

nullSafeCodeGen(ctx, ev, (array) => {
val foundNullElement = ctx.freshName("foundNullElement")
val nullElementIndex = ctx.freshName("nullElementIndex")
val builder = ctx.freshName("builder")
val openHashSet = classOf[OpenHashSet[_]].getName
val openHashSet = classOf[SQLOpenHashSet[_]].getName
val classTag = s"scala.reflect.ClassTag$$.MODULE$$.$hsTypeName()"
val hashSet = ctx.freshName("hashSet")
val arrayBuilder = classOf[mutable.ArrayBuilder[_]].getName
Expand All @@ -3466,7 +3497,6 @@ case class ArrayDistinct(child: Expression)
// Only need to track null element index when array's element is nullable.
val declareNullTrackVariables = if (dataType.asInstanceOf[ArrayType].containsNull) {
s"""
|boolean $foundNullElement = false;
|int $nullElementIndex = -1;
""".stripMargin
} else {
Expand All @@ -3477,9 +3507,9 @@ case class ArrayDistinct(child: Expression)
if (dataType.asInstanceOf[ArrayType].containsNull) {
s"""
|if ($array.isNullAt($i)) {
| if (!$foundNullElement) {
| if (!$hashSet.containsNull()) {
| $nullElementIndex = $size;
| $foundNullElement = true;
| $hashSet.addNull();
| $size++;
| $builder.$$plus$$eq($nullValueHolder);
| }
Expand All @@ -3491,17 +3521,41 @@ case class ArrayDistinct(child: Expression)
body
}

val processArray = withArrayNullAssignment(
def withNaNCheck(body: String): String = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we move these codegen utils to SQLOpenHashSet as well to reduce duplicated code?

@AngersZhuuuu AngersZhuuuu Sep 15, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we move these codegen utils to SQLOpenHashSet as well to reduce duplicated code?

How about to do this after all done. Not only this can be refactored.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cloud-fan Updated, How about current?

(elementType match {
case DoubleType =>
Some((s"java.lang.Double.isNaN((double)$value)", "java.lang.Double.NaN"))
case FloatType =>
Some((s"java.lang.Float.isNaN((float)$value)", "java.lang.Float.NaN"))
case _ => None
}).map { case (isNaN, valueNaN) =>
s"""
|if ($isNaN) {
| if (!$hashSet.containsNaN()) {
| $size++;
| $hashSet.addNaN();
| $builder.$$plus$$eq($valueNaN);
| }
|} else {
| $body
|}
""".stripMargin
}
}.getOrElse(body)

val body =
s"""
|$jt $value = ${genGetValue(array, i)};
|if (!$hashSet.contains($hsValueCast$value)) {
| if (++$size > ${ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH}) {
| break;
| }
| $hashSet.add$hsPostFix($hsValueCast$value);
| $builder.$$plus$$eq($value);
|}
""".stripMargin)
""".stripMargin

val processArray = withArrayNullAssignment(
s"$jt $value = ${genGetValue(array, i)};" + withNaNCheck(body))

s"""
|$openHashSet $hashSet = new $openHashSet$hsPostFix($classTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2326,4 +2326,13 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
Literal.create(Seq(Float.NaN, null, 1f), ArrayType(FloatType))),
Seq(Float.NaN, null, 1f))
}

test("SPARK-36741: ArrayDistinct should handle duplicated Double.NaN and Float.Nan") {
checkEvaluation(ArrayDistinct(
Literal.create(Seq(Double.NaN, Double.NaN, null, null, 1d, 1d), ArrayType(DoubleType))),
Seq(Double.NaN, null, 1d))
checkEvaluation(ArrayDistinct(
Literal.create(Seq(Float.NaN, Float.NaN, null, null, 1f, 1f), ArrayType(FloatType))),
Seq(Float.NaN, null, 1f))
}
}