com.facebook.hive.orc.OrcStruct Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hive-dwrf Show documentation
Show all versions of hive-dwrf Show documentation
DWRF file format for Hive
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
/**
* 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.
*/
package com.facebook.hive.orc;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.SettableListObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.SettableMapObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StructField;
import org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
import org.apache.hadoop.io.Writable;
import com.facebook.hive.orc.lazy.OrcLazyObjectInspectorUtils;
public final class OrcStruct implements Writable {
private Object[] fields;
private List fieldNames;
public OrcStruct() {
this.fields = new Object[0];
this.fieldNames = new ArrayList();
}
public OrcStruct(List fieldNames) {
this.fields = new Object[fieldNames.size()];
this.fieldNames = fieldNames;
}
public Object getFieldValue(int fieldIndex) {
return fields[fieldIndex];
}
public void setFieldValue(int fieldIndex, Object value) {
fields[fieldIndex] = value;
}
public int getNumFields() {
return fields.length;
}
public List getFieldNames() {
return fieldNames;
}
/**
* Change the names and number of fields in the struct. No effect if the number of
* fields is the same. The old field values are copied to the new array.
* @param numFields the new number of fields
*/
public void setFieldNames(List fieldNames) {
this.fieldNames = fieldNames;
if (fields.length != fieldNames.size()) {
Object[] oldFields = fields;
fields = new Object[fieldNames.size()];
System.arraycopy(oldFields, 0, fields, 0,
Math.min(oldFields.length, fieldNames.size()));
}
}
public void appendField(String fieldName) {
fieldNames.add(fieldName);
setFieldNames(fieldNames);
}
@Override
public void write(DataOutput dataOutput) throws IOException {
throw new UnsupportedOperationException("write unsupported");
}
@Override
public void readFields(DataInput dataInput) throws IOException {
throw new UnsupportedOperationException("readFields unsupported");
}
@Override
public boolean equals(Object other) {
if (other == null || other.getClass() != OrcStruct.class) {
return false;
} else {
OrcStruct oth = (OrcStruct) other;
if (fields.length != oth.fields.length) {
return false;
}
for(int i=0; i < fields.length; ++i) {
if (fields[i] == null) {
if (oth.fields[i] != null) {
return false;
}
} else {
if (!fields[i].equals(oth.fields[i])) {
return false;
}
}
}
return true;
}
}
@Override
public int hashCode() {
int result = fields.length;
for(Object field: fields) {
if (field != null) {
result ^= field.hashCode();
}
}
return result;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append("{");
for(int i=0; i < fields.length; ++i) {
if (i != 0) {
buffer.append(", ");
}
buffer.append(fields[i]);
}
buffer.append("}");
return buffer.toString();
}
public static class Field implements StructField {
private final String name;
private final ObjectInspector inspector;
private final int offset;
public Field(String name, ObjectInspector inspector, int offset) {
this.name = name;
this.inspector = inspector;
this.offset = offset;
}
@Override
public String getFieldName() {
return name;
}
@Override
public ObjectInspector getFieldObjectInspector() {
return inspector;
}
@Override
public String getFieldComment() {
return null;
}
public int getOffset() {
return offset;
}
}
public static class OrcStructInspector extends SettableStructObjectInspector {
private final List fields;
public OrcStructInspector(StructTypeInfo info) {
ArrayList fieldNames = info.getAllStructFieldNames();
ArrayList fieldTypes = info.getAllStructFieldTypeInfos();
fields = new ArrayList(fieldNames.size());
for(int i=0; i < fieldNames.size(); ++i) {
fields.add(new Field(fieldNames.get(i),
OrcLazyObjectInspectorUtils.createWritableObjectInspector(fieldTypes.get(i)), i));
}
}
public OrcStructInspector(int columnId, List types) {
OrcProto.Type type = types.get(columnId);
int fieldCount = type.getSubtypesCount();
fields = new ArrayList(fieldCount);
for(int i=0; i < fieldCount; ++i) {
int fieldType = type.getSubtypes(i);
fields.add(new Field(type.getFieldNames(i),
OrcLazyObjectInspectorUtils.createWritableObjectInspector(fieldType, types), i));
}
}
@Override
public List getAllStructFieldRefs() {
return fields;
}
@Override
public StructField getStructFieldRef(String s) {
for(StructField field: fields) {
if (field.getFieldName().equals(s)) {
return field;
}
}
return null;
}
@Override
public Object getStructFieldData(Object object, StructField field) {
int offset = ((Field) field).offset;
OrcStruct struct = (OrcStruct) object;
if (offset >= struct.fields.length) {
return null;
}
return struct.fields[offset];
}
@Override
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy