aboutsummaryrefslogtreecommitdiff
path: root/exec/vector/src/main/java/org/apache/drill/exec/vector/ValueHolderHelper.java
blob: 15a2a2d02335ab0497b64b82dcfefe4a18b11a9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * 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.drill.exec.vector;

import io.netty.buffer.DrillBuf;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.drill.common.types.TypeProtos;
import org.apache.drill.exec.expr.holders.BigIntHolder;
import org.apache.drill.exec.expr.holders.BitHolder;
import org.apache.drill.exec.expr.holders.DateHolder;
import org.apache.drill.exec.expr.holders.Decimal18Holder;
import org.apache.drill.exec.expr.holders.Decimal28SparseHolder;
import org.apache.drill.exec.expr.holders.Decimal38SparseHolder;
import org.apache.drill.exec.expr.holders.Decimal9Holder;
import org.apache.drill.exec.expr.holders.Float4Holder;
import org.apache.drill.exec.expr.holders.Float8Holder;
import org.apache.drill.exec.expr.holders.IntHolder;
import org.apache.drill.exec.expr.holders.IntervalDayHolder;
import org.apache.drill.exec.expr.holders.IntervalYearHolder;
import org.apache.drill.exec.expr.holders.NullableBitHolder;
import org.apache.drill.exec.expr.holders.TimeHolder;
import org.apache.drill.exec.expr.holders.TimeStampHolder;
import org.apache.drill.exec.expr.holders.VarCharHolder;
import org.apache.drill.exec.expr.holders.VarDecimalHolder;
import org.apache.drill.exec.memory.BufferAllocator;
import org.apache.drill.exec.util.DecimalUtility;

import org.apache.drill.shaded.guava.com.google.common.base.Charsets;

public class ValueHolderHelper {
  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ValueHolderHelper.class);

  public static IntHolder getIntHolder(int value) {
    IntHolder holder = new IntHolder();
    holder.value = value;

    return holder;
  }

  public static BigIntHolder getBigIntHolder(long value) {
    BigIntHolder holder = new BigIntHolder();
    holder.value = value;

    return holder;
  }

  public static Float4Holder getFloat4Holder(float value) {
    Float4Holder holder = new Float4Holder();
    holder.value = value;

    return holder;
  }

  public static Float8Holder getFloat8Holder(double value) {
    Float8Holder holder = new Float8Holder();
    holder.value = value;

    return holder;
  }

  public static DateHolder getDateHolder(long value) {
    DateHolder holder = new DateHolder();
    holder.value = value;
    return holder;
  }

  public static TimeHolder getTimeHolder(int value) {
    TimeHolder holder = new TimeHolder();
    holder.value = value;
    return holder;
  }

  public static TimeStampHolder getTimeStampHolder(long value) {
    TimeStampHolder holder = new TimeStampHolder();
    holder.value = value;
    return holder;
  }

  public static BitHolder getBitHolder(int value) {
    BitHolder holder = new BitHolder();
    holder.value = value;

    return holder;
  }

  public static NullableBitHolder getNullableBitHolder(boolean isNull, int value) {
    NullableBitHolder holder = new NullableBitHolder();
    holder.isSet = isNull? 0 : 1;
    if (! isNull) {
      holder.value = value;
    }

    return holder;
  }

  public static VarCharHolder getVarCharHolder(DrillBuf buf, String s){
    VarCharHolder vch = new VarCharHolder();

    byte[] b = s.getBytes(Charsets.UTF_8);
    vch.start = 0;
    vch.end = b.length;
    vch.buffer = buf.reallocIfNeeded(b.length);
    vch.buffer.setBytes(0, b);
    return vch;
  }

  public static VarCharHolder getVarCharHolder(BufferAllocator a, String s){
    VarCharHolder vch = new VarCharHolder();

    byte[] b = s.getBytes(Charsets.UTF_8);
    vch.start = 0;
    vch.end = b.length;
    vch.buffer = a.buffer(b.length);
    vch.buffer.setBytes(0, b);
    return vch;
  }


  public static IntervalYearHolder getIntervalYearHolder(int intervalYear) {
    IntervalYearHolder holder = new IntervalYearHolder();

    holder.value = intervalYear;
    return holder;
  }

  public static IntervalDayHolder getIntervalDayHolder(int days, int millis) {
      IntervalDayHolder dch = new IntervalDayHolder();

      dch.days = days;
      dch.milliseconds = millis;
      return dch;
  }

  public static Decimal9Holder getDecimal9Holder(int decimal, int scale, int precision) {
    Decimal9Holder dch = new Decimal9Holder();

    dch.scale = scale;
    dch.precision = precision;
    dch.value = decimal;

    return dch;
  }

  public static Decimal18Holder getDecimal18Holder(long decimal, int scale, int precision) {
    Decimal18Holder dch = new Decimal18Holder();

    dch.scale = scale;
    dch.precision = precision;
    dch.value = decimal;

    return dch;
  }

  public static Decimal28SparseHolder getDecimal28Holder(DrillBuf buf, String decimal) {
    BigDecimal bigDecimal = new BigDecimal(decimal);

    return getDecimal28Holder(buf, bigDecimal);
  }

  public static Decimal28SparseHolder getDecimal28Holder(DrillBuf buf, BigDecimal bigDecimal) {
    Decimal28SparseHolder dch = new Decimal28SparseHolder();

    dch.scale = bigDecimal.scale();
    dch.precision = bigDecimal.precision();
    Decimal28SparseHolder.setSign(bigDecimal.signum() == -1, dch.start, dch.buffer);
    dch.start = 0;
    dch.buffer = buf.reallocIfNeeded(5 * DecimalUtility.INTEGER_SIZE);
    DecimalUtility
      .getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, Decimal28SparseHolder.nDecimalDigits);

    return dch;
  }

  public static Decimal38SparseHolder getDecimal38Holder(DrillBuf buf, String decimal) {
      BigDecimal bigDecimal = new BigDecimal(decimal);

      return getDecimal38Holder(buf, bigDecimal);
  }

  public static Decimal38SparseHolder getDecimal38Holder(DrillBuf buf, BigDecimal bigDecimal) {
    Decimal38SparseHolder dch = new Decimal38SparseHolder();

    dch.scale = bigDecimal.scale();
    dch.precision = bigDecimal.precision();
    Decimal38SparseHolder.setSign(bigDecimal.signum() == -1, dch.start, dch.buffer);
    dch.start = 0;
    dch.buffer = buf.reallocIfNeeded(Decimal38SparseHolder.maxPrecision * DecimalUtility.INTEGER_SIZE);
    DecimalUtility
      .getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, Decimal38SparseHolder.nDecimalDigits);

    return dch;
  }

  public static VarDecimalHolder getVarDecimalHolder(DrillBuf buf, String decimal) {
    BigDecimal bigDecimal = new BigDecimal(decimal);

    return getVarDecimalHolder(buf, bigDecimal);
  }

  public static VarDecimalHolder getVarDecimalHolder(DrillBuf buf, BigDecimal bigDecimal) {
    VarDecimalHolder dch = new VarDecimalHolder();

    byte[] bytes = bigDecimal.unscaledValue().toByteArray();
    int length = bytes.length;

    dch.scale = bigDecimal.scale();
    dch.precision = bigDecimal.precision();
    dch.start = 0;
    dch.end = length;
    dch.buffer = buf.reallocIfNeeded(length);
    dch.buffer.setBytes(0, bytes);

    return dch;
  }

  /**
   * Returns list of field names which belong to holder corresponding to the specified {@code TypeProtos.MajorType type}.
   *
   * @param type type of holder whose fields should be returned
   * @return list of field names which belong to holder corresponding to the specified {@code TypeProtos.MajorType type}.
   */
  public static List<String> getHolderParams(TypeProtos.MajorType type) {
    ArrayList<String> result = new ArrayList<>();
    switch (type.getMode()) {
      case OPTIONAL:
        result.add("isSet");
        // fall through
      case REQUIRED:
        switch (type.getMinorType()) {
          case BIGINT:
          case FLOAT4:
          case FLOAT8:
          case INT:
          case MONEY:
          case SMALLINT:
          case TINYINT:
          case UINT1:
          case UINT2:
          case UINT4:
          case UINT8:
          case INTERVALYEAR:
          case DATE:
          case TIME:
          case TIMESTAMP:
          case BIT:
          case DECIMAL9:
          case DECIMAL18:
            result.add("value");
            return result;
          case DECIMAL28DENSE:
          case DECIMAL28SPARSE:
          case DECIMAL38DENSE:
          case DECIMAL38SPARSE:
            result.add("start");
            result.add("buffer");
            result.add("scale");
            result.add("precision");
            return result;
          case INTERVAL: {
            result.add("months");
            result.add("days");
            result.add("milliseconds");
            return result;
          }
          case INTERVALDAY: {
            result.add("days");
            result.add("milliseconds");
            return result;
          }
          case VARDECIMAL:
            result.add("scale");
            result.add("precision");
            // fall through
          case VAR16CHAR:
          case VARBINARY:
          case VARCHAR:
            result.add("start");
            result.add("end");
            result.add("buffer");
            return result;
          case UNION:
            result.add("reader");
            return result;
        }
    }
    return result;
  }
}