Skip to content

Commit

Permalink
Use enum instead of bitwise masks
Browse files Browse the repository at this point in the history
  • Loading branch information
suminb committed Oct 17, 2019
1 parent 59b6dbe commit a9cb26b
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/main/java/com/pinterest/secor/util/orc/VectorColumnFiller.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ public void convert(JsonElement value, ColumnVector vect, int row) {
*/
static class UnionColumnConverter implements JsonConverter {

private static final byte BOOLEAN_MASK = 0x01;
private static final byte NUMBER_MASK = 0x02;
private static final byte STRING_MASK = 0x04;
private enum JsonType {
NULL, BOOLEAN, NUMBER, STRING, ARRAY, OBJECT
}

// TODO: Could we come up with a better name?
private class ConverterInfo {
Expand All @@ -243,50 +243,50 @@ public JsonConverter getConverter() {
* and it is represented by multiple child columns under UnionColumnVector.
* Thus we need converters for each type.
*/
private Map<Byte, ConverterInfo> childConverters = new HashMap<>();
private Map<JsonType, ConverterInfo> childConverters = new HashMap<>();

public UnionColumnConverter(TypeDescription schema) {
List<TypeDescription> children = schema.getChildren();
int index = 0;
for (TypeDescription childType : children) {
byte mask = getTypeMask(childType.getCategory());
JsonType jsonType = getJsonType(childType.getCategory());
JsonConverter converter = createConverter(childType);
// FIXME: Handle cases where childConverters is pre-occupied with the same mask
childConverters.put(mask, new ConverterInfo(index++, converter));
childConverters.put(jsonType, new ConverterInfo(index++, converter));
}
}

private byte getTypeMask(TypeDescription.Category category) {
private JsonType getJsonType(TypeDescription.Category category) {
switch (category) {
case BOOLEAN:
return BOOLEAN_MASK;
return JsonType.BOOLEAN;
case BYTE:
case SHORT:
case INT:
case LONG:
case FLOAT:
case DOUBLE:
case DECIMAL:
return NUMBER_MASK;
return JsonType.NUMBER;
case CHAR:
case VARCHAR:
case STRING:
return STRING_MASK;
return JsonType.STRING;
default:
throw new UnsupportedOperationException();
}
}

private byte getTypeMask(JsonPrimitive value) {
byte mask = 0;

mask |= value.isBoolean() ? BOOLEAN_MASK : 0;
mask |= value.isNumber() ? NUMBER_MASK : 0;
mask |= value.isString() ? STRING_MASK : 0;

// FIXME: How should we handle isArray() and isObject()?

return mask;
private JsonType getJsonType(JsonPrimitive value) {
if (value.isBoolean()) {
return JsonType.BOOLEAN;
} else if (value.isNumber()) {
return JsonType.NUMBER;
} else if (value.isString()) {
return JsonType.STRING;
} else {
throw new UnsupportedOperationException();
}
}

public void convert(JsonElement value, ColumnVector vect, int row) {
Expand All @@ -297,8 +297,8 @@ public void convert(JsonElement value, ColumnVector vect, int row) {
UnionColumnVector vector = (UnionColumnVector) vect;
JsonPrimitive primitive = value.getAsJsonPrimitive();

byte mask = getTypeMask(primitive);
ConverterInfo converterInfo = childConverters.get(mask);
JsonType jsonType = getJsonType(primitive);
ConverterInfo converterInfo = childConverters.get(jsonType);
if (converterInfo == null) {
String message = String.format("Unable to infer type for '%s'", primitive);
throw new IllegalArgumentException(message);
Expand Down

0 comments on commit a9cb26b

Please sign in to comment.