dongjoon-hyun commented on code in PR #58227: URL: https://github.com/apache/spark/pull/58227#discussion_r3839486670
########## common/utils/src/test/scala/org/apache/spark/util/SparkCollectionUtilsSuite.scala: ########## @@ -0,0 +1,64 @@ +/* + * 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.util + +import org.scalatest.funsuite.AnyFunSuite // scalastyle:ignore funsuite + +class SparkCollectionUtilsSuite extends AnyFunSuite { // scalastyle:ignore funsuite + + test("toMapWithIndex maps each key to its position, matching zipWithIndex.toMap") { + assert(SparkCollectionUtils.toMapWithIndex(Seq("a", "b", "c")) === + Map("a" -> 0, "b" -> 1, "c" -> 2)) + assert(SparkCollectionUtils.toMapWithIndex(Seq.empty[String]) === Map.empty[String, Int]) + // Duplicate keys keep the last index, matching zipWithIndex.toMap. + assert(SparkCollectionUtils.toMapWithIndex(Seq("a", "a")) === Map("a" -> 1)) + val keys = Seq(10, 20, 30, 40) + assert(SparkCollectionUtils.toMapWithIndex(keys) === keys.zipWithIndex.toMap) + } + + test("isEmpty and isNotEmpty handle null, empty and non-empty maps") { + val nullMap: java.util.Map[String, Int] = null + assert(SparkCollectionUtils.isEmpty(nullMap)) + assert(!SparkCollectionUtils.isNotEmpty(nullMap)) + + val empty = new java.util.HashMap[String, Int]() + assert(SparkCollectionUtils.isEmpty(empty)) + assert(!SparkCollectionUtils.isNotEmpty(empty)) + + val nonEmpty = new java.util.HashMap[String, Int]() + nonEmpty.put("a", 1) + assert(!SparkCollectionUtils.isEmpty(nonEmpty)) + assert(SparkCollectionUtils.isNotEmpty(nonEmpty)) + } + + test("createArray fills primitive-typed arrays with the default value") { + assert(SparkCollectionUtils.createArray(3, 7).sameElements(Array(7, 7, 7))) + assert(SparkCollectionUtils.createArray(2, true).sameElements(Array(true, true))) + assert(SparkCollectionUtils.createArray(2, 1L).sameElements(Array(1L, 1L))) + assert(SparkCollectionUtils.createArray(2, 2.5d).sameElements(Array(2.5d, 2.5d))) + assert(SparkCollectionUtils.createArray(2, 1.5f).sameElements(Array(1.5f, 1.5f))) + assert(SparkCollectionUtils.createArray(2, 3.toByte).sameElements(Array(3.toByte, 3.toByte))) + assert( + SparkCollectionUtils.createArray(2, 4.toShort).sameElements(Array(4.toShort, 4.toShort))) Review Comment: `Char` is missing from this list, and it is the one primitive that `createArray` gets wrong today. `SparkCollectionUtils.createArray` dispatches on Boolean/Byte/Short/Int/Long/Float/Double only, so `Char` falls through to `case _` and runs `arr.asInstanceOf[Array[AnyRef]]` against a `char[]`. Adding ```scala assert(SparkCollectionUtils.createArray(2, 'x').sameElements(Array('x', 'x'))) ``` fails with ``` java.lang.ClassCastException: class [C cannot be cast to class [Ljava.lang.Object; ``` I verified this locally against current master via `build/sbt 'common-utils/testOnly ...'`, and confirmed that adding the missing case makes it pass: ```scala case c if c == classOf[Char] => Arrays.fill(arr.asInstanceOf[Array[Char]], defaultValue.asInstanceOf[Char]) ``` Since the stated goal here is to guard the `ClassTag` dispatch against regressions, covering 7 of 8 primitives gives a false sense of safety for the 8th. Could you either include the one-line source fix along with a `Char` assertion (the PR title would then need to drop `[TEST]`), or file a follow-up JIRA for it? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
