aboutsummaryrefslogtreecommitdiff
path: root/exec/java-exec/src/main/codegen/templates/SqlAccessors.java
blob: 7e5e99ba994486d53c16cf56dd32a7d448c32c0d (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * 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.
 */
import java.lang.Override;

<@pp.dropOutputFile />
<#list vv.types as type>
<#list type.minor as minor>
<#list ["", "Nullable"] as mode>
<#assign name = mode + minor.class?cap_first />
<#assign javaType = (minor.javaType!type.javaType) />
<#assign friendlyType = (minor.friendlyType!minor.boxedType!type.boxedType) />
<#-- Class returned by ResultSet.getObject(...): -->
<#assign jdbcObjectClass = minor.jdbcObjectClass ! friendlyType />

<@pp.changeOutputFile name="/org/apache/drill/exec/vector/accessor/${name}Accessor.java" />
<#include "/@includes/license.ftl" />

package org.apache.drill.exec.vector.accessor;

<#include "/@includes/vv_imports.ftl" />

/*
 * This class is generated using freemarker and the ${.template_name} template.
 */
@SuppressWarnings("unused")
public class ${name}Accessor extends AbstractSqlAccessor {
 <#if mode == "Nullable">
  private static final MajorType TYPE = Types.optional(MinorType.${minor.class?upper_case});
 <#else>
  private static final MajorType TYPE = Types.required(MinorType.${minor.class?upper_case});
 </#if>

  private final ${name}Vector.Accessor ac;

  public ${name}Accessor(${name}Vector vector) {
    this.ac = vector.getAccessor();
  }

  @Override
  public MajorType getType() {
    return TYPE;
  };

  @Override
  public boolean isNull(int index) {
   <#if mode == "Nullable">
    return ac.isNull(index);
   <#else>
    return false;
   </#if>
  }

 <#if minor.class != "VarChar" && minor.class != "TimeStamp"
   && minor.class != "Time" && minor.class != "Date">
  <#-- Types whose class for JDBC getObject(...) is same as class from getObject
       on vector. -->

  @Override
  public Class<?> getObjectClass() {
    return ${jdbcObjectClass}.class;
  }

  @Override
  public Object getObject(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
    return ac.getObject(index);
  }
 </#if>

 <#if type.major == "VarLen">

  @Override
  public InputStream getStream(int index) {
    <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
    ${name}Holder h = new ${name}Holder();
    ac.get(index, h);
    return new ByteBufInputStream(h.buffer.slice(h.start, h.end));
  }

  @Override
  public byte[] getBytes(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
    return ac.get(index);
  }

  <#switch minor.class>

    <#case "VarBinary">

    @Override
    public String getString(int index) {
     <#if mode == "Nullable">
      if (ac.isNull(index)) {
        return null;
      }
     </#if>
      byte [] b = ac.get(index);
      return DrillStringUtils.toBinaryString(b);
    }
      <#break>

    <#case "VarDecimal">

    @Override
    public String getString(int index) {
      <#if mode == "Nullable">
      if (ac.isNull(index)) {
        return null;
      }
      </#if>
      BigDecimal bd = getBigDecimal(index);
      return bd.toString();
    }

    @Override
    public BigDecimal getBigDecimal(int index) {
    <#if mode == "Nullable">
      if (ac.isNull(index)) {
        return null;
      }
    </#if>
      return ac.getObject(index);
    }
      <#break>

    <#case "VarChar">

    @Override
    public Class<?> getObjectClass() {
      return String.class;
    }

    @Override
    public String getObject(int index) {
     <#if mode == "Nullable">
       if (ac.isNull(index)) {
         return null;
       }
     </#if>
       return getString(index);
    }

    @Override
    public InputStreamReader getReader(int index) {
     <#if mode == "Nullable">
      if (ac.isNull(index)) {
        return null;
      }
     </#if>
      return new InputStreamReader(getStream(index), Charsets.UTF_8);
    }

    @Override
    public String getString(int index) {
     <#if mode == "Nullable">
      if (ac.isNull(index)) {
        return null;
      }
     </#if>
      return new String(getBytes(index), Charsets.UTF_8);
    }
      <#break>

    <#case "Var16Char">

    @Override
    public InputStreamReader getReader(int index) {
     <#if mode == "Nullable">
      if (ac.isNull(index)) {
        return null;
      }
     </#if>
      return new InputStreamReader(getStream(index), Charsets.UTF_16);
    }

    @Override
    public String getString(int index) {
     <#if mode == "Nullable">
      if (ac.isNull(index)) {
        return null;
      }
     </#if>
      return new String(getBytes(index), Charsets.UTF_16);
    }
      <#break>

    <#default>
    This is uncompilable code

  </#switch>

 <#else> <#-- VarLen -->

  <#if minor.class == "TimeStampTZ">

  @Override
  public Class<?> getObjectClass() {
    return Timestamp.class;
  }

  @Override
  public Object getObject(int index) {
    return getTimestamp(index);
  }

  @Override
  public Timestamp getTimestamp(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
    return new Timestamp(ac.getObject(index).getMillis());
  }

  <#elseif minor.class == "Interval" || minor.class == "IntervalDay" || minor.class == "IntervalYear">

  @Override
  public String getString(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
    return String.valueOf(ac.getAsStringBuilder(index));
  }

  <#elseif minor.class.contains("Decimal")>

  @Override
  public BigDecimal getBigDecimal(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
      return ac.getObject(index);
  }
  <#elseif minor.class == "Date">

  @Override
  public Class<?> getObjectClass() {
    return Date.class;
  }

  @Override
  public Object getObject(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
    return getDate(index);
  }

  @Override
  public Date getDate(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
    org.joda.time.LocalDate date = new org.joda.time.LocalDate(ac.get(index), org.joda.time.DateTimeZone.UTC);
    // Use "toDate()" to get java.util.Date object with exactly the same year the same year, month and day as Joda date.
    // See more in Javadoc for "LocalDate#toDate()"
    return new Date(date.toDate().getTime());
  }

  <#elseif minor.class == "TimeStamp">

  @Override
  public Class<?> getObjectClass() {
    return Timestamp.class;
  }

  @Override
  public Object getObject(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
    return getTimestamp(index);
  }

  @Override
  public Timestamp getTimestamp(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
    org.joda.time.LocalDateTime dateTime = new org.joda.time.LocalDateTime(ac.get(index), org.joda.time.DateTimeZone.UTC);
    // use "toDate()" to get java.util.Date object with exactly the same fields as this Joda date-time.
    // See more in Javadoc for "LocalDateTime#toDate()"
    return new Timestamp(dateTime.toDate().getTime());
  }

  <#elseif minor.class == "Time">

  @Override
  public Class<?> getObjectClass() {
    return Time.class;
  }

  @Override
  public Object getObject(int index) {
    return getTime(index);
  }

  @Override
  public Time getTime(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return null;
    }
   </#if>
    org.joda.time.LocalTime time = new org.joda.time.LocalTime(ac.get(index), org.joda.time.DateTimeZone.UTC);
    // use "toDateTimeToday()"  and "getMillis()" to get the local milliseconds from the Java epoch of 1970-01-01T00:00:00
    return new TimePrintMillis(time.toDateTimeToday().getMillis());
  }

  <#else>

  @Override
  public ${javaType} get${javaType?cap_first}(int index) {
    return ac.get(index);
  }
  </#if>

  <#if minor.class == "Bit" >
  public boolean getBoolean(int index) {
   <#if mode == "Nullable">
    if (ac.isNull(index)) {
      return false;
    }
   </#if>
   return 1 == ac.get(index);
  }
 </#if>
 </#if> <#-- not VarLen -->
}
</#list>
</#list>
</#list>