aboutsummaryrefslogtreecommitdiff
path: root/exec/java-exec/src/main/codegen/templates/JsonOutputRecordWriter.java
blob: b6b0e9c145695bb23d781f461d9756c7708ae9fe (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
/**
 * 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.
 */

<@pp.dropOutputFile />
<@pp.changeOutputFile name="org/apache/drill/exec/store/JSONOutputRecordWriter.java" />
<#include "/@includes/license.ftl" />

package org.apache.drill.exec.store;

import org.apache.drill.exec.store.EventBasedRecordWriter.FieldConverter;
import org.apache.drill.exec.vector.complex.reader.FieldReader;
import org.apache.drill.exec.vector.complex.fn.JsonOutput;

import java.io.IOException;
import java.lang.UnsupportedOperationException;
import java.util.List;

/**
 * Abstract implementation of RecordWriter interface which exposes interface:
 *    {@link #writeHeader(List)}
 *    {@link #addField(int,String)}
 * to output the data in string format instead of implementing addField for each type holder.
 *
 * This is useful for text format writers such as CSV, TSV etc.
 *
 * NB: Source code generated using FreeMarker template ${.template_name}
 */
public abstract class JSONOutputRecordWriter extends AbstractRecordWriter implements RecordWriter {

  protected JsonOutput gen;

<#list vv.types as type>
  <#list type.minor as minor>
    <#list vv.modes as mode>
  <#assign friendlyType = (minor.friendlyType!minor.boxedType!type.boxedType) />
  @Override
  public FieldConverter getNew${mode.prefix}${minor.class}Converter(int fieldId, String fieldName, FieldReader reader) {
    return new ${mode.prefix}${minor.class}JsonConverter(fieldId, fieldName, reader);
  }

  public class ${mode.prefix}${minor.class}JsonConverter extends FieldConverter {

    public ${mode.prefix}${minor.class}JsonConverter(int fieldId, String fieldName, FieldReader reader) {
      super(fieldId, fieldName, reader);
    }

    @Override
    public void startField() throws IOException {
      gen.writeFieldName(fieldName);
    }

    @Override
    public void writeField() throws IOException {
  <#if mode.prefix == "Repeated" >
    gen.writeStartArray();
    for (int i = 0; i < reader.size(); i++) {
  </#if>
  
  <#assign typeName = minor.class >
  
  <#switch minor.class>
  <#case "UInt1">
  <#case "UInt2">
  <#case "UInt4">
  <#case "UInt8">
    <#assign typeName = "unsupported">
    <#break>
    
  <#case "Decimal9">
  <#case "Decimal18">
  <#case "Decimal28Sparse">
  <#case "Decimal28Dense">
  <#case "Decimal38Dense">
  <#case "Decimal38Sparse">
    <#assign typeName = "Decimal">
    <#break>
  <#case "Float4">
    <#assign typeName = "Float">
    <#break>
  <#case "Float8">
    <#assign typeName = "Double">
    <#break>
    
  <#case "IntervalDay">
  <#case "IntervalYear">
    <#assign typeName = "Interval">
    <#break>
    
  <#case "Bit">
    <#assign typeName = "Boolean">
    <#break>  

  <#case "TimeStamp">
    <#assign typeName = "Timestamp">
    <#break>  
    
  <#case "VarBinary">
    <#assign typeName = "Binary">
    <#break>  
    
  </#switch>
  
  <#if typeName == "unsupported">
    throw new UnsupportedOperationException("Unable to currently write ${minor.class} type to JSON.");
  <#elseif mode.prefix == "Repeated" >
    gen.write${typeName}(i, reader);
  <#else>
    gen.write${typeName}(reader);
  </#if>

  <#if mode.prefix == "Repeated">
    }
      gen.writeEndArray();
  </#if>
    }
  }
    </#list>
  </#list>
</#list>

}