Search in sources :

Example 1 with ReturnObjectInspectorResolver

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver in project hive by apache.

the class GenericUDTFStack method initialize.

@Override
public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException {
    if (args.length < 2) {
        throw new UDFArgumentException("STACK() expects at least two arguments.");
    }
    if (!(args[0] instanceof ConstantObjectInspector)) {
        throw new UDFArgumentException("The first argument to STACK() must be a constant integer (got " + args[0].getTypeName() + " instead).");
    }
    numRows = (IntWritable) ((ConstantObjectInspector) args[0]).getWritableConstantValue();
    if (numRows == null || numRows.get() < 1) {
        throw new UDFArgumentException("STACK() expects its first argument to be >= 1.");
    }
    // Divide and round up.
    numCols = (args.length - 1 + numRows.get() - 1) / numRows.get();
    for (int jj = 0; jj < numCols; ++jj) {
        returnOIResolvers.add(new ReturnObjectInspectorResolver());
        for (int ii = 0; ii < numRows.get(); ++ii) {
            int index = ii * numCols + jj + 1;
            if (index < args.length && !returnOIResolvers.get(jj).update(args[index])) {
                throw new UDFArgumentException("Argument " + (jj + 1) + "'s type (" + args[jj + 1].getTypeName() + ") should be equal to argument " + index + "'s type (" + args[index].getTypeName() + ")");
            }
        }
    }
    forwardObj = new Object[numCols];
    for (int ii = 0; ii < args.length; ++ii) {
        argOIs.add(args[ii]);
    }
    ArrayList<String> fieldNames = new ArrayList<String>();
    ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
    for (int ii = 0; ii < numCols; ++ii) {
        fieldNames.add("col" + ii);
        fieldOIs.add(returnOIResolvers.get(ii).get());
    }
    return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) WritableConstantIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ReturnObjectInspectorResolver(org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver) ArrayList(java.util.ArrayList) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 2 with ReturnObjectInspectorResolver

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver in project hive by apache.

the class GenericUDFIn method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 2) {
        throw new UDFArgumentLengthException("The function IN requires at least two arguments, got " + arguments.length);
    }
    argumentOIs = arguments;
    // We want to use the ReturnObjectInspectorResolver because otherwise
    // ObjectInspectorUtils.compare() will return != for two objects that have
    // different object inspectors, e.g. 238 and "238". The ROIR will help convert
    // both values to a common type so that they can be compared reasonably.
    conversionHelper = new GenericUDFUtils.ReturnObjectInspectorResolver(true);
    for (ObjectInspector oi : arguments) {
        if (!conversionHelper.update(oi)) {
            StringBuilder sb = new StringBuilder();
            sb.append("The arguments for IN should be the same type! Types are: {");
            sb.append(arguments[0].getTypeName());
            sb.append(" IN (");
            for (int i = 1; i < arguments.length; i++) {
                if (i != 1) {
                    sb.append(", ");
                }
                sb.append(arguments[i].getTypeName());
            }
            sb.append(")}");
            throw new UDFArgumentException(sb.toString());
        }
    }
    compareOI = conversionHelper.get();
    checkIfInSetConstant();
    return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
}
Also used : ReturnObjectInspectorResolver(org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver) UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)

Example 3 with ReturnObjectInspectorResolver

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver in project hive by apache.

the class UnionOperator method initializeOp.

/**
   * UnionOperator will transform the input rows if the inputObjInspectors from
   * different parents are different. If one parent has exactly the same
   * ObjectInspector as the output ObjectInspector, then we don't need to do
   * transformation for that parent. This information is recorded in
   * needsTransform[].
   */
@Override
protected void initializeOp(Configuration hconf) throws HiveException {
    super.initializeOp(hconf);
    int parents = parentOperators.size();
    parentObjInspectors = new StructObjectInspector[parents];
    parentFields = new List[parents];
    int columns = 0;
    for (int p = 0; p < parents; p++) {
        parentObjInspectors[p] = (StructObjectInspector) inputObjInspectors[p];
        parentFields[p] = parentObjInspectors[p].getAllStructFieldRefs();
        if (p == 0 || parentFields[p].size() < columns) {
            columns = parentFields[p].size();
        }
    }
    // Get columnNames from the first parent
    ArrayList<String> columnNames = new ArrayList<String>(columns);
    for (int c = 0; c < columns; c++) {
        columnNames.add(parentFields[0].get(c).getFieldName());
    }
    // Get outputFieldOIs
    columnTypeResolvers = new ReturnObjectInspectorResolver[columns];
    for (int c = 0; c < columns; c++) {
        columnTypeResolvers[c] = new ReturnObjectInspectorResolver(true);
    }
    for (int p = 0; p < parents; p++) {
        //When columns is 0, the union operator is empty.
        assert (columns == 0 || parentFields[p].size() == columns);
        for (int c = 0; c < columns; c++) {
            if (!columnTypeResolvers[c].updateForUnionAll(parentFields[p].get(c).getFieldObjectInspector())) {
                // checked in SemanticAnalyzer. Should not happen
                throw new HiveException("Incompatible types for union operator");
            }
        }
    }
    ArrayList<ObjectInspector> outputFieldOIs = new ArrayList<ObjectInspector>(columns);
    for (int c = 0; c < columns; c++) {
        // can be null for void type
        ObjectInspector fieldOI = parentFields[0].get(c).getFieldObjectInspector();
        outputFieldOIs.add(columnTypeResolvers[c].get(fieldOI));
    }
    // create output row ObjectInspector
    outputObjInspector = ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, outputFieldOIs);
    outputRow = new ArrayList<Object>(columns);
    for (int c = 0; c < columns; c++) {
        outputRow.add(null);
    }
    // whether we need to do transformation for each parent
    needsTransform = new boolean[parents];
    for (int p = 0; p < parents; p++) {
        // Testing using != is good enough, because we use ObjectInspectorFactory
        // to
        // create ObjectInspectors.
        needsTransform[p] = (inputObjInspectors[p] != outputObjInspector);
        if (isLogInfoEnabled && needsTransform[p]) {
            LOG.info("Union Operator needs to transform row from parent[" + p + "] from " + inputObjInspectors[p] + " to " + outputObjInspector);
        }
    }
}
Also used : StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) ReturnObjectInspectorResolver(org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver) ArrayList(java.util.ArrayList)

Aggregations

ReturnObjectInspectorResolver (org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver)3 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)3 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)3 ArrayList (java.util.ArrayList)2 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)2 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)2 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)1 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)1 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)1 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)1 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)1 WritableConstantIntObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector)1