|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.execution.vectorized |
| 19 | + |
| 20 | +import org.scalatest.BeforeAndAfterEach |
| 21 | + |
| 22 | +import org.apache.spark.SparkFunSuite |
| 23 | +import org.apache.spark.sql.catalyst.util.ArrayData |
| 24 | +import org.apache.spark.sql.types._ |
| 25 | +import org.apache.spark.unsafe.types.UTF8String |
| 26 | + |
| 27 | +class ColumnVectorSuite extends SparkFunSuite with BeforeAndAfterEach { |
| 28 | + |
| 29 | + var testVector: WritableColumnVector = _ |
| 30 | + |
| 31 | + private def allocate(capacity: Int, dt: DataType): WritableColumnVector = { |
| 32 | + new OnHeapColumnVector(capacity, dt) |
| 33 | + } |
| 34 | + |
| 35 | + override def afterEach(): Unit = { |
| 36 | + testVector.close() |
| 37 | + } |
| 38 | + |
| 39 | + test("boolean") { |
| 40 | + testVector = allocate(10, BooleanType) |
| 41 | + (0 until 10).foreach { i => |
| 42 | + testVector.appendBoolean(i % 2 == 0) |
| 43 | + } |
| 44 | + |
| 45 | + val array = new ColumnVector.Array(testVector) |
| 46 | + |
| 47 | + (0 until 10).foreach { i => |
| 48 | + assert(array.get(i, BooleanType) === (i % 2 == 0)) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + test("byte") { |
| 53 | + testVector = allocate(10, ByteType) |
| 54 | + (0 until 10).foreach { i => |
| 55 | + testVector.appendByte(i.toByte) |
| 56 | + } |
| 57 | + |
| 58 | + val array = new ColumnVector.Array(testVector) |
| 59 | + |
| 60 | + (0 until 10).foreach { i => |
| 61 | + assert(array.get(i, ByteType) === (i.toByte)) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + test("short") { |
| 66 | + testVector = allocate(10, ShortType) |
| 67 | + (0 until 10).foreach { i => |
| 68 | + testVector.appendShort(i.toShort) |
| 69 | + } |
| 70 | + |
| 71 | + val array = new ColumnVector.Array(testVector) |
| 72 | + |
| 73 | + (0 until 10).foreach { i => |
| 74 | + assert(array.get(i, ShortType) === (i.toShort)) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + test("int") { |
| 79 | + testVector = allocate(10, IntegerType) |
| 80 | + (0 until 10).foreach { i => |
| 81 | + testVector.appendInt(i) |
| 82 | + } |
| 83 | + |
| 84 | + val array = new ColumnVector.Array(testVector) |
| 85 | + |
| 86 | + (0 until 10).foreach { i => |
| 87 | + assert(array.get(i, IntegerType) === i) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + test("long") { |
| 92 | + testVector = allocate(10, LongType) |
| 93 | + (0 until 10).foreach { i => |
| 94 | + testVector.appendLong(i) |
| 95 | + } |
| 96 | + |
| 97 | + val array = new ColumnVector.Array(testVector) |
| 98 | + |
| 99 | + (0 until 10).foreach { i => |
| 100 | + assert(array.get(i, LongType) === i) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + test("float") { |
| 105 | + testVector = allocate(10, FloatType) |
| 106 | + (0 until 10).foreach { i => |
| 107 | + testVector.appendFloat(i.toFloat) |
| 108 | + } |
| 109 | + |
| 110 | + val array = new ColumnVector.Array(testVector) |
| 111 | + |
| 112 | + (0 until 10).foreach { i => |
| 113 | + assert(array.get(i, FloatType) === i.toFloat) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + test("double") { |
| 118 | + testVector = allocate(10, DoubleType) |
| 119 | + (0 until 10).foreach { i => |
| 120 | + testVector.appendDouble(i.toDouble) |
| 121 | + } |
| 122 | + |
| 123 | + val array = new ColumnVector.Array(testVector) |
| 124 | + |
| 125 | + (0 until 10).foreach { i => |
| 126 | + assert(array.get(i, DoubleType) === i.toDouble) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + test("string") { |
| 131 | + testVector = allocate(10, StringType) |
| 132 | + (0 until 10).map { i => |
| 133 | + val utf8 = s"str$i".getBytes("utf8") |
| 134 | + testVector.appendByteArray(utf8, 0, utf8.length) |
| 135 | + } |
| 136 | + |
| 137 | + val array = new ColumnVector.Array(testVector) |
| 138 | + |
| 139 | + (0 until 10).foreach { i => |
| 140 | + assert(array.get(i, StringType) === UTF8String.fromString(s"str$i")) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + test("binary") { |
| 145 | + testVector = allocate(10, BinaryType) |
| 146 | + (0 until 10).map { i => |
| 147 | + val utf8 = s"str$i".getBytes("utf8") |
| 148 | + testVector.appendByteArray(utf8, 0, utf8.length) |
| 149 | + } |
| 150 | + |
| 151 | + val array = new ColumnVector.Array(testVector) |
| 152 | + |
| 153 | + (0 until 10).foreach { i => |
| 154 | + val utf8 = s"str$i".getBytes("utf8") |
| 155 | + assert(array.get(i, BinaryType) === utf8) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + test("array") { |
| 160 | + val arrayType = ArrayType(IntegerType, true) |
| 161 | + testVector = allocate(10, arrayType) |
| 162 | + |
| 163 | + val data = testVector.arrayData() |
| 164 | + var i = 0 |
| 165 | + while (i < 6) { |
| 166 | + data.putInt(i, i) |
| 167 | + i += 1 |
| 168 | + } |
| 169 | + |
| 170 | + // Populate it with arrays [0], [1, 2], [], [3, 4, 5] |
| 171 | + testVector.putArray(0, 0, 1) |
| 172 | + testVector.putArray(1, 1, 2) |
| 173 | + testVector.putArray(2, 3, 0) |
| 174 | + testVector.putArray(3, 3, 3) |
| 175 | + |
| 176 | + val array = new ColumnVector.Array(testVector) |
| 177 | + |
| 178 | + assert(array.get(0, arrayType).asInstanceOf[ArrayData].toIntArray() === Array(0)) |
| 179 | + assert(array.get(1, arrayType).asInstanceOf[ArrayData].toIntArray() === Array(1, 2)) |
| 180 | + assert(array.get(2, arrayType).asInstanceOf[ArrayData].toIntArray() === Array.empty[Int]) |
| 181 | + assert(array.get(3, arrayType).asInstanceOf[ArrayData].toIntArray() === Array(3, 4, 5)) |
| 182 | + } |
| 183 | + |
| 184 | + test("struct") { |
| 185 | + val schema = new StructType().add("int", IntegerType).add("double", DoubleType) |
| 186 | + testVector = allocate(10, schema) |
| 187 | + val c1 = testVector.getChildColumn(0) |
| 188 | + val c2 = testVector.getChildColumn(1) |
| 189 | + c1.putInt(0, 123) |
| 190 | + c2.putDouble(0, 3.45) |
| 191 | + c1.putInt(1, 456) |
| 192 | + c2.putDouble(1, 5.67) |
| 193 | + |
| 194 | + val array = new ColumnVector.Array(testVector) |
| 195 | + |
| 196 | + assert(array.get(0, schema).asInstanceOf[ColumnarBatch.Row].get(0, IntegerType) === 123) |
| 197 | + assert(array.get(0, schema).asInstanceOf[ColumnarBatch.Row].get(1, DoubleType) === 3.45) |
| 198 | + assert(array.get(1, schema).asInstanceOf[ColumnarBatch.Row].get(0, IntegerType) === 456) |
| 199 | + assert(array.get(1, schema).asInstanceOf[ColumnarBatch.Row].get(1, DoubleType) === 5.67) |
| 200 | + } |
| 201 | +} |
0 commit comments