Skip to content

[SPARK-52233][SQL] Fix map_zip_with for Floating Point Types #50967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
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 @@ -21,6 +21,7 @@ import java.util.Comparator
import java.util.concurrent.atomic.{AtomicInteger, AtomicReference}

import scala.collection.mutable
import scala.jdk.CollectionConverters.MapHasAsScala

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.analysis.{TypeCheckResult, TypeCoercion, UnresolvedException}
Expand Down Expand Up @@ -1109,8 +1110,10 @@ case class MapZipWith(left: Expression, right: Expression, function: Expression)
*/
@transient private lazy val getKeysWithValueIndexes:
(ArrayData, ArrayData) => mutable.Iterable[(Any, Array[Option[Int]])] = {
if (TypeUtils.typeWithProperEquals(keyType)) {
getKeysWithIndexesFast
if (TypeUtils.typeWithProperEquals(keyType) && SQLConf.get.mapZipWithUsesJavaCollections) {
getKeysWithIndexesFastAsJava
} else if (TypeUtils.typeWithProperEquals(keyType)) {
getKeysWithIndexesFastUsingScala
} else {
getKeysWithIndexesBruteForce
}
Expand All @@ -1122,7 +1125,7 @@ case class MapZipWith(left: Expression, right: Expression, function: Expression)
}
}

private def getKeysWithIndexesFast(keys1: ArrayData, keys2: ArrayData) = {
private def getKeysWithIndexesFastUsingScala(keys1: ArrayData, keys2: ArrayData) = {
val hashMap = new mutable.LinkedHashMap[Any, Array[Option[Int]]]
for ((z, array) <- Array((0, keys1), (1, keys2))) {
var i = 0
Expand All @@ -1144,6 +1147,31 @@ case class MapZipWith(left: Expression, right: Expression, function: Expression)
hashMap
}

private def getKeysWithIndexesFastAsJava(
keys1: ArrayData,
keys2: ArrayData
): scala.collection.mutable.LinkedHashMap[Any, Array[Option[Int]]] = {
val hashMap = new java.util.LinkedHashMap[Any, Array[Option[Int]]]
for ((z, array) <- Array((0, keys1), (1, keys2))) {
var i = 0
while (i < array.numElements()) {
val key = array.get(i, keyType)
Option(hashMap.get(key)) match {
case Some(indexes) =>
if (indexes(z).isEmpty) {
indexes(z) = Some(i)
}
case None =>
val indexes = Array[Option[Int]](None, None)
indexes(z) = Some(i)
hashMap.put(key, indexes)
}
i += 1
}
}
scala.collection.mutable.LinkedHashMap(hashMap.asScala.toSeq: _*)
}

private def getKeysWithIndexesBruteForce(keys1: ArrayData, keys2: ArrayData) = {
val arrayBuffer = new mutable.ArrayBuffer[(Any, Array[Option[Int]])]
for ((z, array) <- Array((0, keys1), (1, keys2))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,14 @@ object SQLConf {
.stringConf
.createOptional

val MAP_ZIP_WITH_USES_JAVA_COLLECTIONS =
buildConf("spark.sql.mapZipWithUsesJavaCollections")
.doc("When true, the `map_zip_with` function uses Java collections instead of Scala " +
"collections. This is useful for avoiding NaN equality issues.")
.version("4.1.0")
.booleanConf
.createWithDefault(true)

val SUBEXPRESSION_ELIMINATION_ENABLED =
buildConf("spark.sql.subexpressionElimination.enabled")
.internal()
Expand Down Expand Up @@ -6367,6 +6375,9 @@ class SQLConf extends Serializable with Logging with SqlApiConf {
*/
def hintErrorHandler: HintErrorHandler = HintErrorLogger

def mapZipWithUsesJavaCollections: Boolean =
getConf(MAP_ZIP_WITH_USES_JAVA_COLLECTIONS)

def subexpressionEliminationEnabled: Boolean =
getConf(SUBEXPRESSION_ELIMINATION_ENABLED)

Expand Down
Loading