Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8579c97
[SPARK-36702][SQL] ArrayUnion handle duplicated Double.NaN and Float.Nan
AngersZhuuuu Sep 10, 2021
d6e4f39
follow comment
AngersZhuuuu Sep 10, 2021
a481196
Update collectionOperations.scala
AngersZhuuuu Sep 10, 2021
1857988
Update collectionOperations.scala
AngersZhuuuu Sep 10, 2021
4e533fd
Update collectionOperations.scala
AngersZhuuuu Sep 11, 2021
119679c
update
AngersZhuuuu Sep 13, 2021
45d1fee
Update SQLOpenHashSet.scala
AngersZhuuuu Sep 13, 2021
08cc967
update
AngersZhuuuu Sep 13, 2021
8d0e4a9
Merge branch 'SPARK-36702-WrapOpenHashSet' of https://github.com/Ange…
AngersZhuuuu Sep 13, 2021
36758d1
update
AngersZhuuuu Sep 13, 2021
5b077a5
Update collectionOperations.scala
AngersZhuuuu Sep 13, 2021
89a4263
Update collectionOperations.scala
AngersZhuuuu Sep 13, 2021
1ac5ab3
Update collectionOperations.scala
AngersZhuuuu Sep 13, 2021
08da413
Update collectionOperations.scala
AngersZhuuuu Sep 13, 2021
0da6ea5
Update collectionOperations.scala
AngersZhuuuu Sep 13, 2021
3059ea1
Update collectionOperations.scala
AngersZhuuuu Sep 13, 2021
991fddd
Update sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expr…
AngersZhuuuu Sep 13, 2021
db8159e
trigger
AngersZhuuuu Sep 13, 2021
eb1f028
Update SQLOpenHashSet.scala
AngersZhuuuu Sep 13, 2021
f59c0a8
Merge branch 'master' into SPARK-36702-WrapOpenHashSet
AngersZhuuuu Sep 14, 2021
f27c4e1
Update SQLOpenHashSet.scala
AngersZhuuuu Sep 14, 2021
affd31a
Revert "Update SQLOpenHashSet.scala"
AngersZhuuuu Sep 14, 2021
fe407c9
Update SQLOpenHashSet.scala
AngersZhuuuu Sep 14, 2021
4e5e085
trigger
AngersZhuuuu Sep 14, 2021
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 @@ -34,6 +34,7 @@ import org.apache.spark.sql.catalyst.util.DateTimeUtils._
import org.apache.spark.sql.errors.QueryExecutionErrors
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._
import org.apache.spark.sql.util.SQLOpenHashSet
import org.apache.spark.unsafe.UTF8StringBuilder
import org.apache.spark.unsafe.array.ByteArrayMethods
import org.apache.spark.unsafe.array.ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH
Expand Down Expand Up @@ -3575,24 +3576,31 @@ case class ArrayUnion(left: Expression, right: Expression) extends ArrayBinaryLi
if (TypeUtils.typeWithProperEquals(elementType)) {
(array1, array2) =>
val arrayBuffer = new scala.collection.mutable.ArrayBuffer[Any]
val hs = new OpenHashSet[Any]
var foundNullElement = false
val hs = new SQLOpenHashSet[Any]()
val isNaN = SQLOpenHashSet.isNaN(elementType)
Seq(array1, array2).foreach { array =>
var i = 0
while (i < array.numElements()) {
if (array.isNullAt(i)) {
if (!foundNullElement) {
if (!hs.containsNull) {
hs.addNull
arrayBuffer += null
foundNullElement = true
}
} else {
val elem = array.get(i, elementType)
if (!hs.contains(elem)) {
if (arrayBuffer.size > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
ArrayBinaryLike.throwUnionLengthOverflowException(arrayBuffer.size)
if (isNaN(elem)) {
if (!hs.containsNaN) {
arrayBuffer += elem
Copy link
Member

Choose a reason for hiding this comment

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

Ur, BTW, there are multiple NaN values which has different bytes from Double.NaN. So, this new semantic is adding the first NaN value into the result, right?

@cloud-fan and @AngersZhuuuu . Do we need to normalize the NaN value by adding Double.NaN or Float.NaN always?

Copy link
Contributor

Choose a reason for hiding this comment

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

good point!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, @cloud-fan and @AngersZhuuuu .

hs.addNaN
}
} else {
if (!hs.contains(elem)) {
if (arrayBuffer.size > ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH) {
ArrayBinaryLike.throwUnionLengthOverflowException(arrayBuffer.size)
}
arrayBuffer += elem
hs.add(elem)
}
arrayBuffer += elem
hs.add(elem)
}
}
i += 1
Expand Down Expand Up @@ -3649,13 +3657,12 @@ case class ArrayUnion(left: Expression, right: Expression) extends ArrayBinaryLi
val ptName = CodeGenerator.primitiveTypeName(jt)

nullSafeCodeGen(ctx, ev, (array1, array2) => {
val foundNullElement = ctx.freshName("foundNullElement")
val nullElementIndex = ctx.freshName("nullElementIndex")
val builder = ctx.freshName("builder")
val array = ctx.freshName("array")
val arrays = ctx.freshName("arrays")
val arrayDataIdx = ctx.freshName("arrayDataIdx")
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 @@ -3665,9 +3672,9 @@ case class ArrayUnion(left: Expression, right: Expression) extends ArrayBinaryLi
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 @@ -3679,22 +3686,42 @@ case class ArrayUnion(left: Expression, right: Expression) extends ArrayBinaryLi
body
}

val processArray = withArrayNullAssignment(
def withNaNCheck(body: String): String = {
(elementType match {
case DoubleType => Some(s"java.lang.Double.isNaN((double)$value)")
case FloatType => Some(s"java.lang.Float.isNaN((float)$value)")
case _ => None
}).map { isNaN =>
s"""
|if ($isNaN) {
| if (!$hashSet.containsNaN()) {
| $size++;
| $hashSet.addNaN();
| $builder.$$plus$$eq($value);
| }
|} 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))

// Only need to track null element index when result array's element is nullable.
val declareNullTrackVariables = if (dataType.asInstanceOf[ArrayType].containsNull) {
s"""
|boolean $foundNullElement = false;
|int $nullElementIndex = -1;
""".stripMargin
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.util

import scala.reflect._

import org.apache.spark.annotation.Private
import org.apache.spark.sql.types.{DataType, DoubleType, FloatType}
import org.apache.spark.util.collection.OpenHashSet

// A wrap of OpenHashSet that can handle null, Double.NaN and Float.NaN w.r.t. the SQL semantic.
@Private
class SQLOpenHashSet[@specialized(Long, Int, Double, Float) T: ClassTag](
initialCapacity: Int,
loadFactor: Double) {

def this(initialCapacity: Int) = this(initialCapacity, 0.7)

def this() = this(64)

private val hashSet = new OpenHashSet[T](initialCapacity, loadFactor)

private var containNull = false
private var containNaN = false

def addNull(): Unit = {
containNull = true
}

def addNaN(): Unit = {
containNaN = true
}

def add(k: T): Unit = {
hashSet.add(k)
}

def contains(k: T): Boolean = {
hashSet.contains(k)
}

def containsNull(): Boolean = containNull

def containsNaN(): Boolean = containNaN
}

object SQLOpenHashSet {
def isNaN(dataType: DataType): Any => Boolean = {
dataType match {
case DoubleType =>
(value: Any) => java.lang.Double.isNaN(value.asInstanceOf[java.lang.Double])
case FloatType =>
(value: Any) => java.lang.Float.isNaN(value.asInstanceOf[java.lang.Float])
case _ => (_: Any) => false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2309,4 +2309,21 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
}
}
}

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