Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add test.
  • Loading branch information
viirya committed Apr 9, 2018
commit 962d048f8ad02003d83d67cd5105167ab11ac277
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,15 @@ class ArrayDataIndexedSeq[T](arrayData: ArrayData, dataType: DataType) extends I
case _ => (idx: Int) => arrayData.get(idx, dataType)
}

override def apply(idx: Int): T = if (idx < arrayData.numElements()) {
accessor(idx).asInstanceOf[T]
override def apply(idx: Int): T = if (0 <= idx && idx < arrayData.numElements()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

NITish: Can you put the if statement on a separate line? This is kinda hard to read.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok.

if (arrayData.isNullAt(idx)) {
null.asInstanceOf[T]
Copy link
Member Author

@viirya viirya Apr 10, 2018

Choose a reason for hiding this comment

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

For primitive ArrayData, this seems to be an issue for null elements, if the caller asks for a primitive type sequence like Seq[Int].

} else {
accessor(idx).asInstanceOf[T]
}
} else {
throw new IndexOutOfBoundsException(s"Index $idx is greater than the length of the ArrayData.")
throw new IndexOutOfBoundsException(
s"Index $idx must be between 0 and the length of the ArrayData.")
}

override def length: Int = arrayData.numElements()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.catalyst.util

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
import org.apache.spark.sql.catalyst.expressions.UnsafeArrayData
import org.apache.spark.sql.types.StringType
import org.apache.spark.unsafe.types.UTF8String

class ArrayDataIndexedSeqSuite extends SparkFunSuite {
def utf8(str: String): UTF8String = UTF8String.fromString(str)
val stringArray = Array("1", "10", "100", null)

private def testArrayData(arrayData: ArrayData): Unit = {
assert(arrayData.numElements == stringArray.length)
stringArray.zipWithIndex.map { case (e, i) =>
if (e != null) {
assert(arrayData.getUTF8String(i).toString().equals(e))
} else {
assert(arrayData.isNullAt(i))
}
}

val seq = arrayData.toSeq[UTF8String](StringType)
stringArray.zipWithIndex.map { case (e, i) =>
if (e != null) {
assert(seq(i).toString().equals(e))
} else {
assert(seq(i) == null)
}
}

intercept[IndexOutOfBoundsException] {
seq(-1)
}.getMessage().contains("must be between 0 and the length of the ArrayData.")

intercept[IndexOutOfBoundsException] {
seq(seq.length)
}.getMessage().contains("must be between 0 and the length of the ArrayData.")
}

test("ArrayDataIndexedSeq can work on GenericArrayData") {
val arrayData = new GenericArrayData(stringArray.map(utf8(_)))
testArrayData(arrayData)
}

test("ArrayDataIndexedSeq can work on UnsafeArrayData") {
val unsafeArrayData = ExpressionEncoder[Array[String]].resolveAndBind().
toRow(stringArray).getArray(0)
assert(unsafeArrayData.isInstanceOf[UnsafeArrayData])
testArrayData(unsafeArrayData)
Copy link
Member Author

Choose a reason for hiding this comment

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

This test may need polish to test again all possible types.

Copy link
Member

Choose a reason for hiding this comment

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

+1 for all type test case. :)

}
}