|
| 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 | +package org.apache.spark.sql.execution.vectorized; |
| 18 | + |
| 19 | +import java.math.BigDecimal; |
| 20 | +import java.math.BigInteger; |
| 21 | + |
| 22 | +import org.apache.spark.sql.types.*; |
| 23 | +import org.apache.spark.sql.vectorized.ColumnVector; |
| 24 | +import org.apache.spark.sql.vectorized.ColumnarArray; |
| 25 | +import org.apache.spark.sql.vectorized.ColumnarMap; |
| 26 | +import org.apache.spark.unsafe.types.UTF8String; |
| 27 | + |
| 28 | +/** |
| 29 | + * This class adds the constant support to ColumnVector. |
| 30 | + * It supports all the types and contains `set` APIs, |
| 31 | + * which will set the exact same value to all rows. |
| 32 | + * |
| 33 | + * Capacity: The vector stores only one copy of the data. |
| 34 | + */ |
| 35 | +public class ConstantColumnVector extends ColumnVector { |
| 36 | + |
| 37 | + // The data stored in this ConstantColumnVector, the vector stores only one copy of the data. |
| 38 | + private byte nullData; |
| 39 | + private byte byteData; |
| 40 | + private short shortData; |
| 41 | + private int intData; |
| 42 | + private long longData; |
| 43 | + private float floatData; |
| 44 | + private double doubleData; |
| 45 | + private UTF8String stringData; |
| 46 | + private byte[] byteArrayData; |
| 47 | + private ConstantColumnVector[] childData; |
| 48 | + private ColumnarArray arrayData; |
| 49 | + private ColumnarMap mapData; |
| 50 | + |
| 51 | + private final int numRows; |
| 52 | + |
| 53 | + /** |
| 54 | + * @param numRows: The number of rows for this ConstantColumnVector |
| 55 | + * @param type: The data type of this ConstantColumnVector |
| 56 | + */ |
| 57 | + public ConstantColumnVector(int numRows, DataType type) { |
| 58 | + super(type); |
| 59 | + this.numRows = numRows; |
| 60 | + |
| 61 | + if (type instanceof StructType) { |
| 62 | + this.childData = new ConstantColumnVector[((StructType) type).fields().length]; |
| 63 | + } else if (type instanceof CalendarIntervalType) { |
| 64 | + // Three columns. Months as int. Days as Int. Microseconds as Long. |
| 65 | + this.childData = new ConstantColumnVector[3]; |
| 66 | + } else { |
| 67 | + this.childData = null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void close() { |
| 73 | + byteArrayData = null; |
| 74 | + for (int i = 0; i < childData.length; i++) { |
| 75 | + childData[i].close(); |
| 76 | + childData[i] = null; |
| 77 | + } |
| 78 | + childData = null; |
| 79 | + arrayData = null; |
| 80 | + mapData = null; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean hasNull() { |
| 85 | + return nullData == 1; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public int numNulls() { |
| 90 | + return hasNull() ? numRows : 0; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean isNullAt(int rowId) { |
| 95 | + return nullData == 1; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Sets all rows as `null` |
| 100 | + */ |
| 101 | + public void setNull() { |
| 102 | + nullData = (byte) 1; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Sets all rows as not `null` |
| 107 | + */ |
| 108 | + public void setNotNull() { |
| 109 | + nullData = (byte) 0; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean getBoolean(int rowId) { |
| 114 | + return byteData == 1; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Sets the boolean `value` for all rows |
| 119 | + */ |
| 120 | + public void setBoolean(boolean value) { |
| 121 | + byteData = (byte) ((value) ? 1 : 0); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public byte getByte(int rowId) { |
| 126 | + return byteData; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Sets the byte `value` for all rows |
| 131 | + */ |
| 132 | + public void setByte(byte value) { |
| 133 | + byteData = value; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public short getShort(int rowId) { |
| 138 | + return shortData; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Sets the short `value` for all rows |
| 143 | + */ |
| 144 | + public void setShort(short value) { |
| 145 | + shortData = value; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public int getInt(int rowId) { |
| 150 | + return intData; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Sets the int `value` for all rows |
| 155 | + */ |
| 156 | + public void setInt(int value) { |
| 157 | + intData = value; |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public long getLong(int rowId) { |
| 162 | + return longData; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Sets the long `value` for all rows |
| 167 | + */ |
| 168 | + public void setLong(long value) { |
| 169 | + longData = value; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public float getFloat(int rowId) { |
| 174 | + return floatData; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Sets the float `value` for all rows |
| 179 | + */ |
| 180 | + public void setFloat(float value) { |
| 181 | + floatData = value; |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public double getDouble(int rowId) { |
| 186 | + return doubleData; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Sets the double `value` for all rows |
| 191 | + */ |
| 192 | + public void setDouble(double value) { |
| 193 | + doubleData = value; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public ColumnarArray getArray(int rowId) { |
| 198 | + return arrayData; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Sets the `ColumnarArray` `value` for all rows |
| 203 | + */ |
| 204 | + public void setArray(ColumnarArray value) { |
| 205 | + arrayData = value; |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public ColumnarMap getMap(int ordinal) { |
| 210 | + return mapData; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Sets the `ColumnarMap` `value` for all rows |
| 215 | + */ |
| 216 | + public void setMap(ColumnarMap value) { |
| 217 | + mapData = value; |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public Decimal getDecimal(int rowId, int precision, int scale) { |
| 222 | + // copy and modify from WritableColumnVector |
| 223 | + if (precision <= Decimal.MAX_INT_DIGITS()) { |
| 224 | + return Decimal.createUnsafe(getInt(rowId), precision, scale); |
| 225 | + } else if (precision <= Decimal.MAX_LONG_DIGITS()) { |
| 226 | + return Decimal.createUnsafe(getLong(rowId), precision, scale); |
| 227 | + } else { |
| 228 | + byte[] bytes = getBinary(rowId); |
| 229 | + BigInteger bigInteger = new BigInteger(bytes); |
| 230 | + BigDecimal javaDecimal = new BigDecimal(bigInteger, scale); |
| 231 | + return Decimal.apply(javaDecimal, precision, scale); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Sets the `Decimal` `value` with the precision for all rows |
| 237 | + */ |
| 238 | + public void setDecimal(Decimal value, int precision) { |
| 239 | + // copy and modify from WritableColumnVector |
| 240 | + if (precision <= Decimal.MAX_INT_DIGITS()) { |
| 241 | + setInt((int) value.toUnscaledLong()); |
| 242 | + } else if (precision <= Decimal.MAX_LONG_DIGITS()) { |
| 243 | + setLong(value.toUnscaledLong()); |
| 244 | + } else { |
| 245 | + BigInteger bigInteger = value.toJavaBigDecimal().unscaledValue(); |
| 246 | + setByteArray(bigInteger.toByteArray()); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + public UTF8String getUTF8String(int rowId) { |
| 252 | + return stringData; |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * Sets the `UTF8String` `value` for all rows |
| 257 | + */ |
| 258 | + public void setUtf8String(UTF8String value) { |
| 259 | + stringData = value; |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * Sets the byte array `value` for all rows |
| 264 | + */ |
| 265 | + private void setByteArray(byte[] value) { |
| 266 | + byteArrayData = value; |
| 267 | + } |
| 268 | + |
| 269 | + @Override |
| 270 | + public byte[] getBinary(int rowId) { |
| 271 | + return byteArrayData; |
| 272 | + } |
| 273 | + |
| 274 | + /** |
| 275 | + * Sets the binary `value` for all rows |
| 276 | + */ |
| 277 | + public void setBinary(byte[] value) { |
| 278 | + setByteArray(value); |
| 279 | + } |
| 280 | + |
| 281 | + @Override |
| 282 | + public ColumnVector getChild(int ordinal) { |
| 283 | + return childData[ordinal]; |
| 284 | + } |
| 285 | + |
| 286 | + /** |
| 287 | + * Sets the child `ConstantColumnVector` `value` at the given ordinal for all rows |
| 288 | + */ |
| 289 | + public void setChild(int ordinal, ConstantColumnVector value) { |
| 290 | + childData[ordinal] = value; |
| 291 | + } |
| 292 | +} |
0 commit comments