Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORC-236: Support UNION type in Java Convert tool #1025

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions java/tools/src/java/org/apache/orc/tools/convert/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.hadoop.hive.ql.exec.vector.MapColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.StructColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.TimestampColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.UnionColumnVector;
import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
import org.apache.orc.RecordReader;
import org.apache.orc.TypeDescription;
Expand Down Expand Up @@ -293,6 +294,32 @@ public void convert(JsonElement value, ColumnVector vect, int row) {
}
}

class UnionColumnConverter implements JsonConverter {
private JsonConverter[] childConverter;

UnionColumnConverter(TypeDescription schema) {
int size = schema.getChildren().size();
childConverter = new JsonConverter[size];
for (int i = 0; i < size; i++) {
childConverter[i] = createConverter(schema.getChildren().get(i));
}
}

@Override
public void convert(JsonElement value, ColumnVector vect, int row) {
if (value == null || value.isJsonNull()) {
vect.noNulls = false;
vect.isNull[row] = true;
} else {
UnionColumnVector vector = (UnionColumnVector) vect;
JsonObject obj = value.getAsJsonObject();
int tag = obj.get("tag").getAsInt();
vector.tags[row] = tag;
childConverter[tag].convert(obj.get("value"), vector.fields[tag], row);
}
}
}

JsonConverter createConverter(TypeDescription schema) {
switch (schema.getCategory()) {
case BYTE:
Expand Down Expand Up @@ -324,6 +351,8 @@ JsonConverter createConverter(TypeDescription schema) {
return new ListColumnConverter(schema);
case MAP:
return new MapColumnConverter(schema);
case UNION:
return new UnionColumnConverter(schema);
default:
throw new IllegalArgumentException("Unhandled type " + schema);
}
Expand Down