Search in sources :

Example 1 with Const

use of org.apache.pig.Expression.Const in project hive by apache.

the class HCatLoader method getHCatComparisonString.

private String getHCatComparisonString(Expression expr) {
    if (expr instanceof BinaryExpression) {
        // call getHCatComparisonString on lhs and rhs, and and join the
        // results with OpType string
        // we can just use OpType.toString() on all Expression types except
        // Equal, NotEqualt since Equal has '==' in toString() and
        // we need '='
        String opStr = null;
        switch(expr.getOpType()) {
            case OP_EQ:
                opStr = " = ";
                break;
            default:
                opStr = expr.getOpType().toString();
        }
        BinaryExpression be = (BinaryExpression) expr;
        if (be.getRhs() instanceof Const) {
            // If the expr is column op const, will try to cast the const to string
            // according to the data type of the column
            UDFContext udfContext = UDFContext.getUDFContext();
            Properties udfProps = udfContext.getUDFProperties(this.getClass(), new String[] { signature });
            HCatSchema hcatTableSchema = (HCatSchema) udfProps.get(HCatConstants.HCAT_TABLE_SCHEMA);
            HCatFieldSchema fs = null;
            try {
                fs = hcatTableSchema.get(be.getLhs().toString());
            } catch (HCatException e) {
            // Shall never happen
            }
            if (fs != null) {
                return "(" + getHCatComparisonString(be.getLhs()) + opStr + getHCatConstString((Const) be.getRhs(), fs.getType()) + ")";
            }
        }
        return "(" + getHCatComparisonString(be.getLhs()) + opStr + getHCatComparisonString(be.getRhs()) + ")";
    } else {
        // should be a constant or column
        return expr.toString();
    }
}
Also used : BinaryExpression(org.apache.pig.Expression.BinaryExpression) HCatSchema(org.apache.hive.hcatalog.data.schema.HCatSchema) Const(org.apache.pig.Expression.Const) HCatException(org.apache.hive.hcatalog.common.HCatException) UDFContext(org.apache.pig.impl.util.UDFContext) Properties(java.util.Properties) HCatFieldSchema(org.apache.hive.hcatalog.data.schema.HCatFieldSchema)

Aggregations

Properties (java.util.Properties)1 HCatException (org.apache.hive.hcatalog.common.HCatException)1 HCatFieldSchema (org.apache.hive.hcatalog.data.schema.HCatFieldSchema)1 HCatSchema (org.apache.hive.hcatalog.data.schema.HCatSchema)1 BinaryExpression (org.apache.pig.Expression.BinaryExpression)1 Const (org.apache.pig.Expression.Const)1 UDFContext (org.apache.pig.impl.util.UDFContext)1