Java Code Examples for org.apache.pig.data.DataType#isNumberType()

The following examples show how to use org.apache.pig.data.DataType#isNumberType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeCheckingExpVisitor.java    From spork with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(NegativeExpression negExp) throws FrontendException {
    byte type = negExp.getExpression().getType() ;
    if (DataType.isNumberType(type)) {
        //do nothing
    }
    else if (type == DataType.BYTEARRAY) {
        // cast bytearray to double
        insertCast(negExp, DataType.DOUBLE, negExp.getExpression());
    }
    else {
        int errCode = 1041;
        String msg = "NEG can be used with numbers or Bytearray only" ;
        msgCollector.collect(msg, MessageType.Error);
        throw new TypeCheckerException(negExp, msg, errCode, PigException.INPUT) ;
    }

}
 
Example 2
Source File: LogicalSchema.java    From spork with Apache License 2.0 4 votes vote down vote up
/**
 * Check if FieldSchema inFs is castable to outFs
 * @param inFs
 * @param outFs
 * @return true if it is castable
 */
public static boolean castable(LogicalFieldSchema inFs,
        LogicalFieldSchema outFs) {
    
    if(outFs == null && inFs == null) {
        return false;
    }
    
    if (outFs == null) {
        return false ;
    }
    
    if (inFs == null) {
        return false ;
    }
    byte inType = inFs.type;
    byte outType = outFs.type;
    
    if (DataType.isSchemaType(outFs.type)) {
        if(inType == DataType.BYTEARRAY) {
            // good
        } else if (inType == outType) {
            // Don't do the comparison if either input inner schema 
            // is null/empty or  both inner schemas are
            // null.  That will cause Schema.equals to return false,
            // even though we want to view that as true.
            if (!(inFs.schema == null || inFs.schema.size() == 0 || 
                    (outFs.schema == null && inFs.schema == null))) { 
                // compare recursively using schema
                if (!LogicalSchema.castable(inFs.schema, outFs.schema)) {
                    return false ;
                }
            }
        } else {
            return false;
        }
    } else {
        if (inType == outType) {
            // good
        }
        else if (inType == DataType.BOOLEAN && (outType == DataType.CHARARRAY
                || outType == DataType.BYTEARRAY || DataType.isNumberType(outType))) {
            // good
        }
        else if (DataType.isNumberType(inType) && (outType == DataType.CHARARRAY
                || outType == DataType.BYTEARRAY || DataType.isNumberType(outType))
                || outType == DataType.BOOLEAN) {
            // good
        }
        else if (inType == DataType.CHARARRAY && (outType == DataType.BYTEARRAY
                || DataType.isNumberType(outType)) || outType == DataType.BOOLEAN) {
            // good
        }
        else if (inType == DataType.BYTEARRAY) {
            // good
        }
        else {
            return false;
        }
    }
    
    return true ;
}
 
Example 3
Source File: TypeCheckingExpVisitor.java    From spork with Apache License 2.0 4 votes vote down vote up
/**
 * Add casts to promote numeric type to larger of two input numeric types of
 * the {@link BinaryExpression}  binOp . If one of the inputs is numeric
 * and other bytearray, cast the bytearray type to other numeric type.
 * If both inputs are bytearray, cast them to double.
 * @param binOp
 * @throws FrontendException
 */
private void addCastsToNumericBinExpression(BinaryExpression binOp)
throws FrontendException {
    LogicalExpression lhs = binOp.getLhs() ;
    LogicalExpression rhs = binOp.getRhs() ;

    byte lhsType = lhs.getType() ;
    byte rhsType = rhs.getType() ;

    if ( DataType.isNumberType(lhsType) &&
            DataType.isNumberType(rhsType) ) {

        // return the bigger type
        byte biggerType = lhsType > rhsType ? lhsType:rhsType ;

        // Cast smaller type to the bigger type
        if (lhsType != biggerType) {
            insertCast(binOp, biggerType, binOp.getLhs());
        }
        else if (rhsType != biggerType) {
            insertCast(binOp, biggerType, binOp.getRhs());
        }
    }
    else if ( (lhsType == DataType.BYTEARRAY) &&
            (DataType.isNumberType(rhsType)) ) {
        insertCast(binOp, rhsType, binOp.getLhs());
    }
    else if ( (rhsType == DataType.BYTEARRAY) &&
            (DataType.isNumberType(lhsType)) ) {
        insertCast(binOp, lhsType, binOp.getRhs());
    }
    else if ( (lhsType == DataType.BYTEARRAY) &&
            (rhsType == DataType.BYTEARRAY) ) {
        // Cast both operands to double
        insertCast(binOp, DataType.DOUBLE, binOp.getLhs());
        insertCast(binOp, DataType.DOUBLE, binOp.getRhs());
    }
    else {
        int errCode = 1039;
        String msg = generateIncompatibleTypesMessage(binOp);
        msgCollector.collect(msg, MessageType.Error);
        throw new TypeCheckerException(binOp, msg, errCode, PigException.INPUT) ;
    }

}
 
Example 4
Source File: TypeCheckingExpVisitor.java    From spork with Apache License 2.0 4 votes vote down vote up
private void addCastsToCompareBinaryExp(BinaryExpression binOp, boolean isEquality)
throws FrontendException {
    LogicalExpression lhs = binOp.getLhs() ;
    LogicalExpression rhs = binOp.getRhs() ;

    byte lhsType = lhs.getType() ;
    byte rhsType = rhs.getType() ;
    if ( DataType.isNumberType(lhsType) &&
            DataType.isNumberType(rhsType) ) {
        // If not the same type, we cast them to the same
        byte biggerType = lhsType > rhsType ? lhsType:rhsType ;

        // Cast smaller type to the bigger type
        if (lhsType != biggerType) {
            insertCast(binOp, biggerType, binOp.getLhs());
        }
        else if (rhsType != biggerType) {
            insertCast(binOp, biggerType, binOp.getRhs());
        }
    }
    else if ( (lhsType == DataType.DATETIME) &&
            (rhsType == DataType.DATETIME) ) {
        // good
    }
    else if ( (lhsType == DataType.CHARARRAY) &&
            (rhsType == DataType.CHARARRAY) ) {
        // good
    }
    else if ( (lhsType == DataType.BYTEARRAY) &&
            (rhsType == DataType.BYTEARRAY) ) {
        // good
    }
    else if ( (lhsType == DataType.BYTEARRAY) &&
            ( (rhsType == DataType.CHARARRAY) || (DataType.isNumberType(rhsType)) || (rhsType == DataType.BOOLEAN) || (rhsType == DataType.DATETIME))
    ) {
        // Cast byte array to the type on rhs
        insertCast(binOp, rhsType, binOp.getLhs());
    }
    else if ( (rhsType == DataType.BYTEARRAY) &&
            ( (lhsType == DataType.CHARARRAY) || (DataType.isNumberType(lhsType)) || (lhsType == DataType.BOOLEAN) || (lhsType == DataType.DATETIME))
    ) {
        // Cast byte array to the type on lhs
        insertCast(binOp, lhsType, binOp.getRhs());
    }else if (isEquality){

        //in case of equality condition, allow boolean, tuples and maps as args
        if((lhsType == DataType.BOOLEAN) &&
                (rhsType == DataType.BOOLEAN) ) {
            // good
        }
        else if((lhsType == DataType.TUPLE) &&
                (rhsType == DataType.TUPLE) ) {
            // good
        }
        else if ( (lhsType == DataType.MAP) &&
                (rhsType == DataType.MAP) ) {
            // good
        }
        else if (lhsType == DataType.BYTEARRAY &&
                (rhsType == DataType.MAP || rhsType == DataType.TUPLE)){
            // Cast byte array to the type on lhs
            insertCast(binOp, rhsType, binOp.getLhs());
        }
        else if(rhsType == DataType.BYTEARRAY &&
                (lhsType == DataType.MAP || lhsType == DataType.TUPLE)){
            // Cast byte array to the type on lhs
            insertCast(binOp, lhsType, binOp.getRhs());
        }
        else {
            throwIncompatibleTypeError(binOp);
        }
    }
    else {
        throwIncompatibleTypeError(binOp);
    }
}
 
Example 5
Source File: Schema.java    From spork with Apache License 2.0 4 votes vote down vote up
/**
 * Recursively compare two schemas to check if the input schema 
 * can be cast to the cast schema
 * @param castFs schema of the cast operator
 * @param  inputFs schema of the cast input
 * @return true or falsew!
 */
public static boolean castable(
        Schema.FieldSchema castFs,
        Schema.FieldSchema inputFs) {
    if(castFs == null && inputFs == null) {
        return false;
    }
    
    if (castFs == null) {
        return false ;
    }
    
    if (inputFs == null) {
        return false ;
    }
    byte inputType = inputFs.type;
    byte castType = castFs.type;
    
    if (DataType.isSchemaType(castFs.type)) {
        if(inputType == DataType.BYTEARRAY) {
            // good
        } else if (inputType == castType) {
            // Don't do the comparison if both embedded schemas are
            // null.  That will cause Schema.equals to return false,
            // even though we want to view that as true.
            if (!(castFs.schema == null && inputFs.schema == null)) { 
                // compare recursively using schema
                if (!Schema.castable(castFs.schema, inputFs.schema)) {
                    return false ;
                }
            }
        } else {
            return false;
        }
    } else {
        if (inputType == castType) {
            // good
        }
        else if (inputType == DataType.BOOLEAN && (castType == DataType.CHARARRAY
                || castType == DataType.BYTEARRAY || DataType.isNumberType(castType))) {
            // good
        }
        else if (DataType.isNumberType(inputType) && (castType == DataType.CHARARRAY
                || castType == DataType.BYTEARRAY || DataType.isNumberType(castType)
                || castType == DataType.BOOLEAN || castType == DataType.DATETIME)) {
            // good
        }
        else if (inputType == DataType.DATETIME && (castType == DataType.CHARARRAY
                || castType == DataType.BYTEARRAY || DataType.isNumberType(castType))) {
            // good
        }
        else if (inputType == DataType.CHARARRAY && (castType == DataType.BYTEARRAY
                || DataType.isNumberType(castType) || castType == DataType.BOOLEAN
                || castType == DataType.DATETIME)) {
            // good
        } 
        else if (inputType == DataType.BYTEARRAY) {
            // good
        }
        else {
            return false;
        }
    }
    
    return true ;
}
 
Example 6
Source File: TestWarningFunc.java    From spork with Apache License 2.0 4 votes vote down vote up
public Double exec(Tuple input) throws IOException 
{
	if (input == null || input.size() == 0) {
           pigLogger.warn(this, "Input is empty.", PigWarning.UDF_WARNING_1); 
		return null;
       }

       Double output = null;
       boolean accumulated = false;

	try {
           for(int i = 0; i < input.size(); ++i) {
               Object o = input.get(i);
               byte inputType = DataType.findType(o);
               if(DataType.isNumberType(inputType)) {
                   if(!accumulated) {
                       output = 0.0;
                       accumulated = true;
                   }
                   switch(inputType) {
                   case DataType.INTEGER:
                       output += (Integer)o;
                       break;

                   case DataType.LONG:
                       output += (Long)o;
                       break;

                   case DataType.FLOAT:
                       output += (Float)o;
                       break;

                   case DataType.DOUBLE:
                       output += (Double)o;
                       break;
                   }

               } else {
                   pigLogger.warn(this, "Found a non-numeric type.", PigWarning.UDF_WARNING_3);
               }
           }
	} catch(Exception e){
           pigLogger.warn(this, "Problem while computing output.", PigWarning.UDF_WARNING_2); 
		return null;
	}

       if(!accumulated) {
           pigLogger.warn(this, "Did not find any numeric type in the input.", PigWarning.UDF_WARNING_4);
       }

	return output;
   }