spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cloud-fan <...@git.apache.org>
Subject [GitHub] spark pull request #13680: [SPARK-15962][SQL] Introduce implementation with ...
Date Tue, 20 Sep 2016 09:16:41 GMT
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13680#discussion_r79565586
  
    --- Diff: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/util/UnsafeArraySuite.scala
---
    @@ -18,27 +18,187 @@
     package org.apache.spark.sql.catalyst.util
     
     import org.apache.spark.SparkFunSuite
    +import org.apache.spark.sql.catalyst.encoders.{ExpressionEncoder, RowEncoder}
     import org.apache.spark.sql.catalyst.expressions.UnsafeArrayData
    +import org.apache.spark.sql.Row
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.{CalendarInterval, UTF8String}
     
     class UnsafeArraySuite extends SparkFunSuite {
     
    -  test("from primitive int array") {
    -    val array = Array(1, 10, 100)
    -    val unsafe = UnsafeArrayData.fromPrimitiveArray(array)
    -    assert(unsafe.numElements == 3)
    -    assert(unsafe.getSizeInBytes == 4 + 4 * 3 + 4 * 3)
    -    assert(unsafe.getInt(0) == 1)
    -    assert(unsafe.getInt(1) == 10)
    -    assert(unsafe.getInt(2) == 100)
    +  val booleanArray = Array(false, true)
    +  val shortArray = Array(1.toShort, 10.toShort, 100.toShort)
    +  val intArray = Array(1, 10, 100)
    +  val longArray = Array(1.toLong, 10.toLong, 100.toLong)
    +  val floatArray = Array(1.1.toFloat, 2.2.toFloat, 3.3.toFloat)
    +  val doubleArray = Array(1.1, 2.2, 3.3)
    +  val stringArray = Array("1", "10", "100")
    +  val dateArray = Array(
    +    DateTimeUtils.stringToDate(UTF8String.fromString("1970-1-1")).get,
    +    DateTimeUtils.stringToDate(UTF8String.fromString("2016-7-26")).get)
    +  val timestampArray = Array(
    +    DateTimeUtils.stringToTimestamp(UTF8String.fromString("1970-1-1 00:00:00")).get,
    +    DateTimeUtils.stringToTimestamp(UTF8String.fromString("2016-7-26 00:00:00")).get)
    +  val decimalArray4_1 = Array(
    +    BigDecimal("123.4").setScale(1, BigDecimal.RoundingMode.FLOOR),
    +    BigDecimal("567.8").setScale(1, BigDecimal.RoundingMode.FLOOR))
    +  val decimalArray20_20 = Array(
    +    BigDecimal("1.2345678901234567890123456").setScale(21, BigDecimal.RoundingMode.FLOOR),
    +    BigDecimal("2.3456789012345678901234567").setScale(21, BigDecimal.RoundingMode.FLOOR))
    +
    +  val calenderintervalArray = Array(new CalendarInterval(3, 321), new CalendarInterval(1,
123))
    +
    +  val intMultiDimArray = Array(Array(1), Array(2, 20), Array(3, 30, 300))
    +  val doubleMultiDimArray = Array(
    +    Array(1.1, 11.1), Array(2.2, 22.2, 222.2), Array(3.3, 33.3, 333.3, 3333.3))
    +
    +  test("read array") {
    +    val unsafeBoolean = ExpressionEncoder[Array[Boolean]].resolveAndBind().
    +      toRow(booleanArray).getArray(0)
    +    assert(unsafeBoolean.isInstanceOf[UnsafeArrayData])
    +    assert(unsafeBoolean.numElements == booleanArray.length)
    +    booleanArray.zipWithIndex.map { case (e, i) =>
    +      assert(unsafeBoolean.getBoolean(i) == e)
    +    }
    +
    +    val unsafeShort = ExpressionEncoder[Array[Short]].resolveAndBind().
    +      toRow(shortArray).getArray(0)
    +    assert(unsafeShort.isInstanceOf[UnsafeArrayData])
    +    assert(unsafeShort.numElements == shortArray.length)
    +    shortArray.zipWithIndex.map { case (e, i) =>
    +      assert(unsafeShort.getShort(i) == e)
    +    }
    +
    +    val unsafeInt = ExpressionEncoder[Array[Int]].resolveAndBind().
    +      toRow(intArray).getArray(0)
    +    assert(unsafeInt.isInstanceOf[UnsafeArrayData])
    +    assert(unsafeInt.numElements == intArray.length)
    +    intArray.zipWithIndex.map { case (e, i) =>
    +      assert(unsafeInt.getInt(i) == e)
    +    }
    +
    +    val unsafeLong = ExpressionEncoder[Array[Long]].resolveAndBind().
    +      toRow(longArray).getArray(0)
    +    assert(unsafeLong.isInstanceOf[UnsafeArrayData])
    +    assert(unsafeLong.numElements == longArray.length)
    +    longArray.zipWithIndex.map { case (e, i) =>
    +      assert(unsafeLong.getLong(i) == e)
    +    }
    +
    +    val unsafeFloat = ExpressionEncoder[Array[Float]].resolveAndBind().
    +      toRow(floatArray).getArray(0)
    +    assert(unsafeFloat.isInstanceOf[UnsafeArrayData])
    +    assert(unsafeFloat.numElements == floatArray.length)
    +    floatArray.zipWithIndex.map { case (e, i) =>
    +      assert(unsafeFloat.getFloat(i) == e)
    +    }
    +
    +    val unsafeDouble = ExpressionEncoder[Array[Double]].resolveAndBind().
    +      toRow(doubleArray).getArray(0)
    +    assert(unsafeDouble.isInstanceOf[UnsafeArrayData])
    +    assert(unsafeDouble.numElements == doubleArray.length)
    +    doubleArray.zipWithIndex.map { case (e, i) =>
    +      assert(unsafeDouble.getDouble(i) == e)
    +    }
    +
    +    val unsafeString = ExpressionEncoder[Array[String]].resolveAndBind().
    +      toRow(stringArray).getArray(0)
    +    assert(unsafeString.isInstanceOf[UnsafeArrayData])
    +    assert(unsafeString.numElements == stringArray.length)
    +    stringArray.zipWithIndex.map { case (e, i) =>
    +      assert(unsafeString.getUTF8String(i).toString().equals(e))
    +    }
    +
    +    val unsafeDate = ExpressionEncoder[Array[Int]].resolveAndBind().
    +      toRow(dateArray).getArray(0)
    +    assert(unsafeDate.isInstanceOf[UnsafeArrayData])
    +    assert(unsafeDate.numElements == dateArray.length)
    +    dateArray.zipWithIndex.map { case (e, i) =>
    +      assert(unsafeDate.get(i, DateType) == e)
    +    }
    +
    +    val unsafeTimestamp = ExpressionEncoder[Array[Long]].resolveAndBind().
    +      toRow(timestampArray).getArray(0)
    +    assert(unsafeTimestamp.isInstanceOf[UnsafeArrayData])
    +    assert(unsafeTimestamp.numElements == timestampArray.length)
    +    timestampArray.zipWithIndex.map { case (e, i) =>
    +      assert(unsafeTimestamp.get(i, TimestampType) == e)
    +    }
    +
    +    Seq(decimalArray4_1, decimalArray20_20).map { decimalArray =>
    +      val decimal = decimalArray(0)
    +      val schema = new StructType().add(
    +        "array", ArrayType(DecimalType(decimal.precision, decimal.scale)))
    +      val encoder = RowEncoder(schema).resolveAndBind()
    +      val externalRow = Row(decimalArray)
    +      val ir = encoder.toRow(externalRow)
    +
    +      val unsafeDecimal = ir.getArray(0)
    +      assert(unsafeDecimal.isInstanceOf[UnsafeArrayData])
    +      assert(unsafeDecimal.numElements == decimalArray.length)
    +      decimalArray.zipWithIndex.map { case (e, i) =>
    +        assert(unsafeDecimal.getDecimal(i, e.precision, e.scale).toBigDecimal == e)
    +      }
    +    }
    +
    +    val unsafeInterval = ExpressionEncoder[Array[CalendarInterval]].resolveAndBind().
    --- End diff --
    
    `CalendarInterval` is not external, let's follow the way we test decimal, i.e. use `RowEncoder`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message