aboutsummaryrefslogtreecommitdiff
path: root/exec/java-exec/src/main/codegen/templates/ParquetTypeHelper.java
blob: d2f27cca941d26ac668556cebbbfb11e52b87523 (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
/*
 * 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/parquet/ParquetTypeHelper.java" />
<#include "/@includes/license.ftl" />

package org.apache.drill.exec.store.parquet;

import org.apache.drill.common.types.TypeProtos.DataMode;
import org.apache.drill.common.types.TypeProtos.MinorType;
import org.apache.drill.exec.planner.types.DrillRelDataTypeSystem;
import org.apache.drill.exec.record.MaterializedField;
import org.apache.parquet.schema.OriginalType;
import org.apache.parquet.schema.DecimalMetadata;
import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName;
import org.apache.parquet.schema.Type.Repetition;

import java.util.HashMap;
import java.util.Map;

/*
 * This class is generated using freemarker and the ${.template_name} template.
 */

public class ParquetTypeHelper {
  private static Map<MinorType,PrimitiveTypeName> typeMap;
  private static Map<DataMode,Repetition> modeMap;
  private static Map<MinorType,OriginalType> originalTypeMap;

  static {
    typeMap = new HashMap();

    <#list vv.types as type>
    <#list type.minor as minor>
    <#if    minor.class == "TinyInt" ||
            minor.class == "UInt1" ||
            minor.class == "UInt2" ||
            minor.class == "SmallInt" ||
            minor.class == "Int" ||
            minor.class == "Time" ||
            minor.class == "Decimal9" ||
            minor.class == "Date" ||
            minor.class == "UInt4">
                    typeMap.put(MinorType.${minor.class?upper_case}, PrimitiveTypeName.INT32);
    <#elseif
            minor.class == "Float4">
                    typeMap.put(MinorType.${minor.class?upper_case}, PrimitiveTypeName.FLOAT);
    <#elseif
            minor.class == "BigInt" ||
            minor.class == "Decimal18" ||
            minor.class == "TimeStamp" ||
            minor.class == "UInt8">
                    typeMap.put(MinorType.${minor.class?upper_case}, PrimitiveTypeName.INT64);
    <#elseif
            minor.class == "Float8">
                    typeMap.put(MinorType.${minor.class?upper_case}, PrimitiveTypeName.DOUBLE);
    <#elseif
            minor.class == "Bit">
                    typeMap.put(MinorType.${minor.class?upper_case}, PrimitiveTypeName.BOOLEAN);
    <#elseif
            minor.class == "TimeTZ" ||
            minor.class == "IntervalDay" ||
            minor.class == "IntervalYear" ||
            minor.class == "Interval" ||
            minor.class == "Decimal28Dense" ||
            minor.class == "Decimal38Dense" ||
            minor.class == "Decimal28Sparse" ||
            minor.class == "Decimal38Sparse" ||
            minor.class == "VarDecimal">
                    typeMap.put(MinorType.${minor.class?upper_case}, PrimitiveTypeName.FIXED_LEN_BYTE_ARRAY);
    <#elseif
            minor.class == "VarChar" ||
            minor.class == "Var16Char" ||
            minor.class == "VarBinary" >
                    typeMap.put(MinorType.${minor.class?upper_case}, PrimitiveTypeName.BINARY);
    </#if>
    </#list>
    </#list>

    modeMap = new HashMap();

    modeMap.put(DataMode.REQUIRED, Repetition.REQUIRED);
    modeMap.put(DataMode.OPTIONAL, Repetition.OPTIONAL);
    modeMap.put(DataMode.REPEATED, Repetition.REPEATED);

    originalTypeMap = new HashMap();

    <#list vv.types as type>
    <#list type.minor as minor>
            <#if minor.class.contains("Decimal")>
            originalTypeMap.put(MinorType.${minor.class?upper_case}, OriginalType.DECIMAL);
            </#if>
    </#list>
    </#list>
            originalTypeMap.put(MinorType.VARCHAR, OriginalType.UTF8);
            originalTypeMap.put(MinorType.DATE, OriginalType.DATE);
            originalTypeMap.put(MinorType.TIME, OriginalType.TIME_MILLIS);
            originalTypeMap.put(MinorType.TIMESTAMP, OriginalType.TIMESTAMP_MILLIS);
            originalTypeMap.put(MinorType.INTERVALDAY, OriginalType.INTERVAL);
            originalTypeMap.put(MinorType.INTERVALYEAR, OriginalType.INTERVAL);
            originalTypeMap.put(MinorType.INTERVAL, OriginalType.INTERVAL);
//            originalTypeMap.put(MinorType.TIMESTAMPTZ, OriginalType.TIMESTAMPTZ);
  }

  public static PrimitiveTypeName getPrimitiveTypeNameForMinorType(MinorType minorType) {
    return typeMap.get(minorType);
  }

  public static Repetition getRepetitionForDataMode(DataMode dataMode) {
    return modeMap.get(dataMode);
  }

  public static OriginalType getOriginalTypeForMinorType(MinorType minorType) {
    return originalTypeMap.get(minorType);
  }

  public static DecimalMetadata getDecimalMetadataForField(MaterializedField field) {
    switch(field.getType().getMinorType()) {
      case DECIMAL9:
      case DECIMAL18:
      case DECIMAL28SPARSE:
      case DECIMAL28DENSE:
      case DECIMAL38SPARSE:
      case DECIMAL38DENSE:
      case VARDECIMAL:
        return new DecimalMetadata(field.getPrecision(), field.getScale());
      default:
        return null;
    }
  }

  public static int getLengthForMinorType(MinorType minorType) {
    switch(minorType) {
      case INTERVALDAY:
      case INTERVALYEAR:
      case INTERVAL:
        return 12;
      case DECIMAL28SPARSE:
        return 12;
      case DECIMAL38SPARSE:
      case VARDECIMAL:
        return 16;
      default:
        return 0;
    }
  }

  public static int getMaxPrecisionForPrimitiveType(PrimitiveTypeName type) {
    switch(type) {
      case INT32:
        return 9;
      case INT64:
        return 18;
      case FIXED_LEN_BYTE_ARRAY:
        return DrillRelDataTypeSystem.DRILL_REL_DATATYPE_SYSTEM.getMaxNumericPrecision();
      default:
        throw new UnsupportedOperationException(String.format(
          "Specified PrimitiveTypeName %s cannot be used to determine max precision",
          type));
    }
  }

}