Search in sources :

Example 36 with ConstantObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector in project hive by apache.

the class AccumuloRangeGenerator method processExpression.

protected Object processExpression(ExprNodeGenericFuncDesc func, Object[] nodeOutputs) throws SemanticException {
    // a binary operator (gt, lt, ge, le, eq, ne)
    GenericUDF genericUdf = func.getGenericUDF();
    // Find the argument to the operator which is a constant
    ExprNodeConstantDesc constantDesc = null;
    ExprNodeColumnDesc columnDesc = null;
    ExprNodeDesc leftHandNode = null;
    for (Object nodeOutput : nodeOutputs) {
        if (nodeOutput instanceof ExprNodeConstantDesc) {
            // Ordering of constant and column in expression is important in correct range generation
            if (null == leftHandNode) {
                leftHandNode = (ExprNodeDesc) nodeOutput;
            }
            constantDesc = (ExprNodeConstantDesc) nodeOutput;
        } else if (nodeOutput instanceof ExprNodeColumnDesc) {
            // Ordering of constant and column in expression is important in correct range generation
            if (null == leftHandNode) {
                leftHandNode = (ExprNodeDesc) nodeOutput;
            }
            columnDesc = (ExprNodeColumnDesc) nodeOutput;
        }
    }
    // we can generate ranges from e.g. rowid > (4 + 5)
    if (null == constantDesc || null == columnDesc) {
        return null;
    }
    ConstantObjectInspector objInspector = constantDesc.getWritableObjectInspector();
    // Reject any clauses that are against a column that isn't the rowId mapping or indexed
    if (!this.hiveRowIdColumnName.equals(columnDesc.getColumn())) {
        if (this.indexScanner != null && this.indexScanner.isIndexed(columnDesc.getColumn())) {
            return getIndexedRowIds(genericUdf, leftHandNode, columnDesc.getColumn(), objInspector);
        }
        return null;
    }
    Text constText = getConstantText(objInspector);
    return getRange(genericUdf, leftHandNode, constText);
}
Also used : ExprNodeConstantDesc(org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc) GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) ExprNodeColumnDesc(org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc) Text(org.apache.hadoop.io.Text) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 37 with ConstantObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector in project hive by apache.

the class StatsUtils method getAvgColLenOf.

/**
 * Get the raw data size of variable length data types
 * @param conf
 *          - hive conf
 * @param oi
 *          - object inspector
 * @param colType
 *          - column type
 * @return raw data size
 */
public static long getAvgColLenOf(HiveConf conf, ObjectInspector oi, String colType) {
    long configVarLen = HiveConf.getIntVar(conf, HiveConf.ConfVars.HIVE_STATS_MAX_VARIABLE_LENGTH);
    String colTypeLowCase = colType.toLowerCase();
    if (colTypeLowCase.equals(serdeConstants.STRING_TYPE_NAME)) {
        // constant string projection Ex: select "hello" from table
        if (oi instanceof ConstantObjectInspector) {
            ConstantObjectInspector coi = (ConstantObjectInspector) oi;
            // if writable constant is null then return size 0
            Object constantValue = coi.getWritableConstantValue();
            return constantValue == null ? 0 : constantValue.toString().length();
        } else if (oi instanceof StringObjectInspector) {
            // return the variable length from config
            return configVarLen;
        }
    } else if (colTypeLowCase.startsWith(serdeConstants.VARCHAR_TYPE_NAME)) {
        // constant varchar projection
        if (oi instanceof ConstantObjectInspector) {
            ConstantObjectInspector coi = (ConstantObjectInspector) oi;
            // if writable constant is null then return size 0
            Object constantValue = coi.getWritableConstantValue();
            return constantValue == null ? 0 : constantValue.toString().length();
        } else if (oi instanceof HiveVarcharObjectInspector) {
            VarcharTypeInfo type = (VarcharTypeInfo) ((HiveVarcharObjectInspector) oi).getTypeInfo();
            return type.getLength();
        }
    } else if (colTypeLowCase.startsWith(serdeConstants.CHAR_TYPE_NAME)) {
        // constant char projection
        if (oi instanceof ConstantObjectInspector) {
            ConstantObjectInspector coi = (ConstantObjectInspector) oi;
            // if writable constant is null then return size 0
            Object constantValue = coi.getWritableConstantValue();
            return constantValue == null ? 0 : constantValue.toString().length();
        } else if (oi instanceof HiveCharObjectInspector) {
            CharTypeInfo type = (CharTypeInfo) ((HiveCharObjectInspector) oi).getTypeInfo();
            return type.getLength();
        }
    } else if (colTypeLowCase.equals(serdeConstants.BINARY_TYPE_NAME)) {
        // constant byte arrays
        if (oi instanceof ConstantObjectInspector) {
            ConstantObjectInspector coi = (ConstantObjectInspector) oi;
            // if writable constant is null then return size 0
            BytesWritable constantValue = (BytesWritable) coi.getWritableConstantValue();
            return constantValue == null ? 0 : constantValue.getLength();
        } else if (oi instanceof BinaryObjectInspector) {
            // return the variable length from config
            return configVarLen;
        }
    } else {
        // complex types (map, list, struct, union)
        return getSizeOfComplexTypes(conf, oi);
    }
    throw new IllegalArgumentException("Size requested for unknown type: " + colType + " OI: " + oi.getTypeName());
}
Also used : VarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo) HiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveVarcharObjectInspector) CharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo) WritableBinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableBinaryObjectInspector) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) BytesWritable(org.apache.hadoop.io.BytesWritable) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) WritableStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableStringObjectInspector) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector)

Example 38 with ConstantObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector in project hive by apache.

the class GenericUDF method initializeAndFoldConstants.

/**
 * Initialize this GenericUDF.  Additionally, if the arguments are constant
 * and the function is eligible to be folded, then the constant value
 * returned by this UDF will be computed and stored in the
 * ConstantObjectInspector returned.  Otherwise, the function behaves exactly
 * like initialize().
 */
public ObjectInspector initializeAndFoldConstants(ObjectInspector[] arguments) throws UDFArgumentException {
    ObjectInspector oi = initialize(arguments);
    // resources may not be available at compile time.
    if (getRequiredFiles() != null || getRequiredJars() != null) {
        return oi;
    }
    boolean allConstant = true;
    for (int ii = 0; ii < arguments.length; ++ii) {
        if (!ObjectInspectorUtils.isConstantObjectInspector(arguments[ii])) {
            allConstant = false;
            break;
        }
    }
    if (allConstant && !ObjectInspectorUtils.isConstantObjectInspector(oi) && FunctionRegistry.isConsistentWithinQuery(this) && ObjectInspectorUtils.supportsConstantObjectInspector(oi)) {
        DeferredObject[] argumentValues = new DeferredJavaObject[arguments.length];
        for (int ii = 0; ii < arguments.length; ++ii) {
            argumentValues[ii] = new DeferredJavaObject(((ConstantObjectInspector) arguments[ii]).getWritableConstantValue());
        }
        try {
            Object constantValue = evaluate(argumentValues);
            oi = ObjectInspectorUtils.getConstantObjectInspector(oi, constantValue);
        } catch (HiveException e) {
            throw new UDFArgumentException(e);
        }
    }
    return oi;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) BooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 39 with ConstantObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector in project hive by apache.

the class GenericUDFGrouping method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 2) {
        throw new UDFArgumentLengthException("grouping() requires at least 2 argument, got " + arguments.length);
    }
    if (arguments[0].getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "The first argument to grouping() must be primitive");
    }
    PrimitiveObjectInspector arg1OI = (PrimitiveObjectInspector) arguments[0];
    // INT can happen in cases where grouping() is used without grouping sets, in all other cases it should be LONG.
    if (!(arg1OI.getPrimitiveCategory() == PrimitiveCategory.INT || arg1OI.getPrimitiveCategory() == PrimitiveCategory.LONG)) {
        throw new UDFArgumentTypeException(0, "The first argument to grouping() must be an int/long. Got: " + arg1OI.getPrimitiveCategory());
    }
    groupingIdOI = arg1OI;
    indices = new int[arguments.length - 1];
    for (int i = 1; i < arguments.length; i++) {
        PrimitiveObjectInspector arg2OI = (PrimitiveObjectInspector) arguments[i];
        if (!(arg2OI instanceof ConstantObjectInspector)) {
            throw new UDFArgumentTypeException(i, "Must be a constant. Got: " + arg2OI.getClass().getSimpleName());
        }
        indices[i - 1] = PrimitiveObjectInspectorUtils.getInt(((ConstantObjectInspector) arguments[i]).getWritableConstantValue(), arg2OI);
    }
    return PrimitiveObjectInspectorFactory.writableLongObjectInspector;
}
Also used : UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 40 with ConstantObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector in project hive by apache.

the class GenericUDFLikeAny method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 2) {
        throw new UDFArgumentLengthException("The like any operator requires at least one pattern for matching, got " + (arguments.length - 1));
    }
    inputTypes = new PrimitiveCategory[arguments.length];
    converters = new Converter[arguments.length];
    /**
     *expects string and null arguments
     */
    for (int idx = 0; idx < arguments.length; idx++) {
        checkArgPrimitive(arguments, idx);
        checkArgGroups(arguments, idx, inputTypes, PrimitiveGrouping.STRING_GROUP, PrimitiveGrouping.VOID_GROUP);
        PrimitiveCategory inputType = ((PrimitiveObjectInspector) arguments[idx]).getPrimitiveCategory();
        if (arguments[idx] instanceof ConstantObjectInspector && idx != 0) {
            Object constValue = ((ConstantObjectInspector) arguments[idx]).getWritableConstantValue();
            if (!isConstantNullPatternContain && constValue == null) {
                isConstantNullPatternContain = true;
            }
        } else if (idx != 0 && isAllPatternsConstant) {
            isAllPatternsConstant = false;
        }
        converters[idx] = ObjectInspectorConverters.getConverter(arguments[idx], getOutputOI(inputType));
        inputTypes[idx] = inputType;
    }
    return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
}
Also used : UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)

Aggregations

ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)36 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)25 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)19 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)14 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)12 ExprNodeConstantDesc (org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc)7 ArrayList (java.util.ArrayList)6 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)6 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)5 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)4 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)4 IntWritable (org.apache.hadoop.io.IntWritable)4 Text (org.apache.hadoop.io.Text)4 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)3 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)3 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)3 BytesWritable (org.apache.hadoop.io.BytesWritable)3 LongWritable (org.apache.hadoop.io.LongWritable)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 List (java.util.List)2