|
| 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.datasources.orc; |
| 19 | + |
| 20 | +import java.math.BigDecimal; |
| 21 | + |
| 22 | +import org.apache.orc.storage.ql.exec.vector.*; |
| 23 | + |
| 24 | +import org.apache.spark.sql.types.DataType; |
| 25 | +import org.apache.spark.sql.types.Decimal; |
| 26 | +import org.apache.spark.unsafe.types.UTF8String; |
| 27 | + |
| 28 | +/** |
| 29 | + * A column vector class wrapping Hive's ColumnVector. Because Spark ColumnarBatch only accepts |
| 30 | + * Spark's vectorized.ColumnVector, this column vector is used to adapt Hive ColumnVector with |
| 31 | + * Spark ColumnarVector. |
| 32 | + */ |
| 33 | +public class OrcColumnVector extends org.apache.spark.sql.vectorized.ColumnVector { |
| 34 | + private ColumnVector baseData; |
| 35 | + private LongColumnVector longData; |
| 36 | + private DoubleColumnVector doubleData; |
| 37 | + private BytesColumnVector bytesData; |
| 38 | + private DecimalColumnVector decimalData; |
| 39 | + |
| 40 | + private int batchSize; |
| 41 | + |
| 42 | + public OrcColumnVector(DataType type, ColumnVector vector) { |
| 43 | + super(type); |
| 44 | + |
| 45 | + baseData = vector; |
| 46 | + if (vector instanceof LongColumnVector) { |
| 47 | + longData = (LongColumnVector) vector; |
| 48 | + } else if (vector instanceof DoubleColumnVector) { |
| 49 | + doubleData = (DoubleColumnVector) vector; |
| 50 | + } else if (vector instanceof BytesColumnVector) { |
| 51 | + bytesData = (BytesColumnVector) vector; |
| 52 | + } else if (vector instanceof DecimalColumnVector) { |
| 53 | + decimalData = (DecimalColumnVector) vector; |
| 54 | + } else { |
| 55 | + throw new UnsupportedOperationException(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public void setBatchSize(int batchSize) { |
| 60 | + this.batchSize = batchSize; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void close() { |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public int numNulls() { |
| 70 | + if (baseData.isRepeating) { |
| 71 | + if (baseData.isNull[0]) { |
| 72 | + return batchSize; |
| 73 | + } else { |
| 74 | + return 0; |
| 75 | + } |
| 76 | + } else if (baseData.noNulls) { |
| 77 | + return 0; |
| 78 | + } else { |
| 79 | + int count = 0; |
| 80 | + for (int i = 0; i < batchSize; i++) { |
| 81 | + if (baseData.isNull[i]) count++; |
| 82 | + } |
| 83 | + return count; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /* A helper method to get the row index in a column. */ |
| 88 | + private int getRowIndex(int rowId) { |
| 89 | + return baseData.isRepeating ? 0 : rowId; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean isNullAt(int rowId) { |
| 94 | + return baseData.isNull[getRowIndex(rowId)]; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public boolean getBoolean(int rowId) { |
| 99 | + return longData.vector[getRowIndex(rowId)] == 1; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean[] getBooleans(int rowId, int count) { |
| 104 | + boolean[] res = new boolean[count]; |
| 105 | + for (int i = 0; i < count; i++) { |
| 106 | + res[i] = getBoolean(rowId + i); |
| 107 | + } |
| 108 | + return res; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public byte getByte(int rowId) { |
| 113 | + return (byte) longData.vector[getRowIndex(rowId)]; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public byte[] getBytes(int rowId, int count) { |
| 118 | + byte[] res = new byte[count]; |
| 119 | + for (int i = 0; i < count; i++) { |
| 120 | + res[i] = getByte(rowId + i); |
| 121 | + } |
| 122 | + return res; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public short getShort(int rowId) { |
| 127 | + return (short) longData.vector[getRowIndex(rowId)]; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public short[] getShorts(int rowId, int count) { |
| 132 | + short[] res = new short[count]; |
| 133 | + for (int i = 0; i < count; i++) { |
| 134 | + res[i] = getShort(rowId + i); |
| 135 | + } |
| 136 | + return res; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public int getInt(int rowId) { |
| 141 | + return (int) longData.vector[getRowIndex(rowId)]; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public int[] getInts(int rowId, int count) { |
| 146 | + int[] res = new int[count]; |
| 147 | + for (int i = 0; i < count; i++) { |
| 148 | + res[i] = getInt(rowId + i); |
| 149 | + } |
| 150 | + return res; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public long getLong(int rowId) { |
| 155 | + return longData.vector[getRowIndex(rowId)]; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public long[] getLongs(int rowId, int count) { |
| 160 | + long[] res = new long[count]; |
| 161 | + for (int i = 0; i < count; i++) { |
| 162 | + res[i] = getLong(rowId + i); |
| 163 | + } |
| 164 | + return res; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public float getFloat(int rowId) { |
| 169 | + return (float) doubleData.vector[getRowIndex(rowId)]; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public float[] getFloats(int rowId, int count) { |
| 174 | + float[] res = new float[count]; |
| 175 | + for (int i = 0; i < count; i++) { |
| 176 | + res[i] = getFloat(rowId + i); |
| 177 | + } |
| 178 | + return res; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public double getDouble(int rowId) { |
| 183 | + return doubleData.vector[getRowIndex(rowId)]; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public double[] getDoubles(int rowId, int count) { |
| 188 | + double[] res = new double[count]; |
| 189 | + for (int i = 0; i < count; i++) { |
| 190 | + res[i] = getDouble(rowId + i); |
| 191 | + } |
| 192 | + return res; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public int getArrayLength(int rowId) { |
| 197 | + throw new UnsupportedOperationException(); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public int getArrayOffset(int rowId) { |
| 202 | + throw new UnsupportedOperationException(); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public Decimal getDecimal(int rowId, int precision, int scale) { |
| 207 | + BigDecimal data = decimalData.vector[getRowIndex(rowId)].getHiveDecimal().bigDecimalValue(); |
| 208 | + return Decimal.apply(data, precision, scale); |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public UTF8String getUTF8String(int rowId) { |
| 213 | + int index = getRowIndex(rowId); |
| 214 | + BytesColumnVector col = bytesData; |
| 215 | + return UTF8String.fromBytes(col.vector[index], col.start[index], col.length[index]); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public byte[] getBinary(int rowId) { |
| 220 | + int index = getRowIndex(rowId); |
| 221 | + byte[] binary = new byte[bytesData.length[index]]; |
| 222 | + System.arraycopy(bytesData.vector[index], bytesData.start[index], binary, 0, binary.length); |
| 223 | + return binary; |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public org.apache.spark.sql.vectorized.ColumnVector arrayData() { |
| 228 | + throw new UnsupportedOperationException(); |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + public org.apache.spark.sql.vectorized.ColumnVector getChildColumn(int ordinal) { |
| 233 | + throw new UnsupportedOperationException(); |
| 234 | + } |
| 235 | +} |
0 commit comments