-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23875][SQL] Add IndexedSeq wrapper for ArrayData #20984
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
Changes from 1 commit
27eddd7
962d048
ac8d5b4
a77128f
878c995
77640c4
8b5de0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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()) { | ||
| if (arrayData.isNullAt(idx)) { | ||
| null.asInstanceOf[T] | ||
|
||
| } 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() | ||
|
|
||
| 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) | ||
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.