use of org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector in project hive by apache.
the class GenericUDFIndex method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 2) {
throw new UDFArgumentLengthException("The function INDEX accepts exactly 2 arguments.");
}
if (arguments[0] instanceof MapObjectInspector) {
// index into a map
mapOI = (MapObjectInspector) arguments[0];
listOI = null;
} else if (arguments[0] instanceof ListObjectInspector) {
// index into a list
listOI = (ListObjectInspector) arguments[0];
mapOI = null;
} else {
throw new UDFArgumentTypeException(0, "\"" + Category.MAP.toString().toLowerCase() + "\" or \"" + Category.LIST.toString().toLowerCase() + "\" is expected at function INDEX, but \"" + arguments[0].getTypeName() + "\" is found");
}
// index has to be a primitive
if (!(arguments[1] instanceof PrimitiveObjectInspector)) {
throw new UDFArgumentTypeException(1, "Primitive Type is expected but " + arguments[1].getTypeName() + "\" is found");
}
PrimitiveObjectInspector inputOI = (PrimitiveObjectInspector) arguments[1];
ObjectInspector returnOI;
ObjectInspector indexOI;
if (mapOI != null) {
indexOI = ObjectInspectorConverters.getConvertedOI(inputOI, mapOI.getMapKeyObjectInspector());
returnOI = mapOI.getMapValueObjectInspector();
} else {
indexOI = PrimitiveObjectInspectorFactory.writableIntObjectInspector;
returnOI = listOI.getListElementObjectInspector();
}
converter = ObjectInspectorConverters.getConverter(inputOI, indexOI);
return returnOI;
}
use of org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector in project hive by apache.
the class GenericUDFConcatWS method evaluate.
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
if (arguments[0].get() == null) {
return null;
}
String separator = PrimitiveObjectInspectorUtils.getString(arguments[0].get(), (PrimitiveObjectInspector) argumentOIs[0]);
StringBuilder sb = new StringBuilder();
boolean first = true;
for (int i = 1; i < arguments.length; i++) {
if (arguments[i].get() != null) {
if (first) {
first = false;
} else {
sb.append(separator);
}
if (argumentOIs[i].getCategory().equals(Category.LIST)) {
Object strArray = arguments[i].get();
ListObjectInspector strArrayOI = (ListObjectInspector) argumentOIs[i];
boolean strArrayFirst = true;
for (int j = 0; j < strArrayOI.getListLength(strArray); j++) {
if (strArrayFirst) {
strArrayFirst = false;
} else {
sb.append(separator);
}
sb.append(strArrayOI.getListElement(strArray, j));
}
} else {
sb.append(PrimitiveObjectInspectorUtils.getString(arguments[i].get(), (PrimitiveObjectInspector) argumentOIs[i]));
}
}
}
resultText.set(sb.toString());
return resultText;
}
use of org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector in project hive by apache.
the class GenericUDFEWAHBitmapEmpty method evaluate.
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
assert (arguments.length == 1);
Object b = arguments[0].get();
ListObjectInspector lloi = (ListObjectInspector) bitmapOI;
int length = lloi.getListLength(b);
ArrayList<LongWritable> bitmapArray = new ArrayList<LongWritable>();
for (int i = 0; i < length; i++) {
long l = PrimitiveObjectInspectorUtils.getLong(lloi.getListElement(b, i), (PrimitiveObjectInspector) lloi.getListElementObjectInspector());
bitmapArray.add(new LongWritable(l));
}
BitmapObjectInput bitmapObjIn = new BitmapObjectInput(bitmapArray);
EWAHCompressedBitmap bitmap = new EWAHCompressedBitmap();
try {
bitmap.readExternal(bitmapObjIn);
} catch (IOException e) {
throw new RuntimeException(e);
}
// Add return true only if bitmap is all zeros.
return new BooleanWritable(!bitmap.iterator().hasNext());
}
use of org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector in project hive by apache.
the class HCatTypeCheckHive method getJavaObject.
private Object getJavaObject(Object o, ObjectInspector oi, List<Category> categories) {
if (categories != null) {
categories.add(oi.getCategory());
}
if (oi.getCategory() == ObjectInspector.Category.LIST) {
List<?> l = ((ListObjectInspector) oi).getList(o);
List<Object> result = new ArrayList<Object>();
ObjectInspector elemOI = ((ListObjectInspector) oi).getListElementObjectInspector();
for (Object lo : l) {
result.add(getJavaObject(lo, elemOI, categories));
}
return result;
} else if (oi.getCategory() == ObjectInspector.Category.MAP) {
Map<?, ?> m = ((MapObjectInspector) oi).getMap(o);
Map<String, String> result = new HashMap<String, String>();
ObjectInspector koi = ((MapObjectInspector) oi).getMapKeyObjectInspector();
ObjectInspector voi = ((MapObjectInspector) oi).getMapValueObjectInspector();
for (Entry<?, ?> e : m.entrySet()) {
result.put((String) getJavaObject(e.getKey(), koi, null), (String) getJavaObject(e.getValue(), voi, null));
}
return result;
} else if (oi.getCategory() == ObjectInspector.Category.STRUCT) {
List<Object> s = ((StructObjectInspector) oi).getStructFieldsDataAsList(o);
List<? extends StructField> sf = ((StructObjectInspector) oi).getAllStructFieldRefs();
List<Object> result = new ArrayList<Object>();
for (int i = 0; i < s.size(); i++) {
result.add(getJavaObject(s.get(i), sf.get(i).getFieldObjectInspector(), categories));
}
return result;
} else if (oi.getCategory() == ObjectInspector.Category.PRIMITIVE) {
return ((PrimitiveObjectInspector) oi).getPrimitiveJavaObject(o);
}
throw new RuntimeException("Unexpected error!");
}
use of org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector in project hive by apache.
the class PTFTranslator method translate.
public PTFDesc translate(WindowingSpec wdwSpec, SemanticAnalyzer semAly, HiveConf hCfg, RowResolver inputRR, UnparseTranslator unparseT) throws SemanticException {
init(semAly, hCfg, inputRR, unparseT);
windowingSpec = wdwSpec;
ptfDesc = new PTFDesc();
ptfDesc.setCfg(hCfg);
ptfDesc.setLlInfo(llInfo);
WindowTableFunctionDef wdwTFnDef = new WindowTableFunctionDef();
ptfDesc.setFuncDef(wdwTFnDef);
PTFQueryInputSpec inpSpec = new PTFQueryInputSpec();
inpSpec.setType(PTFQueryInputType.WINDOWING);
wdwTFnDef.setInput(translate(inpSpec, 0));
ShapeDetails inpShape = wdwTFnDef.getInput().getOutputShape();
WindowingTableFunctionResolver tFn = (WindowingTableFunctionResolver) FunctionRegistry.getTableFunctionResolver(FunctionRegistry.WINDOWING_TABLE_FUNCTION);
if (tFn == null) {
throw new SemanticException(String.format("Internal Error: Unknown Table Function %s", FunctionRegistry.WINDOWING_TABLE_FUNCTION));
}
wdwTFnDef.setName(FunctionRegistry.WINDOWING_TABLE_FUNCTION);
wdwTFnDef.setResolverClassName(tFn.getClass().getName());
wdwTFnDef.setAlias("ptf_" + 1);
wdwTFnDef.setExpressionTreeString(null);
wdwTFnDef.setTransformsRawInput(false);
tFn.initialize(hCfg, ptfDesc, wdwTFnDef);
TableFunctionEvaluator tEval = tFn.getEvaluator();
wdwTFnDef.setTFunction(tEval);
wdwTFnDef.setCarryForwardNames(tFn.carryForwardNames());
wdwTFnDef.setRawInputShape(inpShape);
PartitioningSpec partiSpec = wdwSpec.getQueryPartitioningSpec();
if (partiSpec == null) {
throw new SemanticException("Invalid use of Windowing: there is no Partitioning associated with Windowing");
}
PartitionDef partDef = translate(inpShape, wdwSpec.getQueryPartitionSpec());
OrderDef ordDef = translate(inpShape, wdwSpec.getQueryOrderSpec(), partDef);
wdwTFnDef.setPartition(partDef);
wdwTFnDef.setOrder(ordDef);
/*
* process Wdw functions
*/
ArrayList<WindowFunctionDef> windowFunctions = new ArrayList<WindowFunctionDef>();
if (wdwSpec.getWindowExpressions() != null) {
for (WindowExpressionSpec expr : wdwSpec.getWindowExpressions()) {
if (expr instanceof WindowFunctionSpec) {
WindowFunctionDef wFnDef = translate(wdwTFnDef, (WindowFunctionSpec) expr);
windowFunctions.add(wFnDef);
}
}
wdwTFnDef.setWindowFunctions(windowFunctions);
}
/*
* set outputFromWdwFnProcessing
*/
ArrayList<String> aliases = new ArrayList<String>();
ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
for (WindowFunctionDef wFnDef : windowFunctions) {
aliases.add(wFnDef.getAlias());
if (wFnDef.isPivotResult()) {
fieldOIs.add(((ListObjectInspector) wFnDef.getOI()).getListElementObjectInspector());
} else {
fieldOIs.add(wFnDef.getOI());
}
}
PTFTranslator.addInputColumnsToList(inpShape, aliases, fieldOIs);
StructObjectInspector wdwOutOI = ObjectInspectorFactory.getStandardStructObjectInspector(aliases, fieldOIs);
tFn.setWdwProcessingOutputOI(wdwOutOI);
RowResolver wdwOutRR = buildRowResolverForWindowing(wdwTFnDef);
ShapeDetails wdwOutShape = setupShape(wdwOutOI, null, wdwOutRR);
wdwTFnDef.setOutputShape(wdwOutShape);
tFn.setupOutputOI();
PTFDeserializer.alterOutputOIForStreaming(ptfDesc);
return ptfDesc;
}
Aggregations