Search in sources :

Example 1 with ValueType

use of org.apache.asterix.external.classad.Value.ValueType in project asterixdb by apache.

the class Operation method privateDoOperation.

public static int privateDoOperation(int op, Value val1, Value val2, Value val3, boolean valid1, boolean valid2, boolean valid3, Value result, EvalState es, ClassAdObjectPool objectPool) throws HyracksDataException {
    ValueType vt1;
    ValueType vt2;
    ValueType vt3;
    // get the types of the values
    vt1 = val1.getType();
    vt2 = val2.getType();
    vt3 = val3.getType();
    // take care of the easy cases
    if (op == OpKind_NO_OP || op == OpKind_PARENTHESES_OP) {
        result.setValue(val1);
        return SigValues.SIG_CHLD1.ordinal();
    } else if (op == OpKind_UNARY_PLUS_OP) {
        if (vt1 == ValueType.BOOLEAN_VALUE || vt1 == ValueType.STRING_VALUE || val1.isListValue() || vt1 == ValueType.CLASSAD_VALUE || vt1 == ValueType.ABSOLUTE_TIME_VALUE) {
            result.setErrorValue();
        } else {
            // applies for ERROR, UNDEFINED and Numbers
            result.setValue(val1);
        }
        return SigValues.SIG_CHLD1.ordinal();
    }
    // test for cases when evaluation is strict
    if (isStrictOperator(op)) {
        // check for error values
        if (vt1 == ValueType.ERROR_VALUE) {
            result.setErrorValue();
            return SigValues.SIG_CHLD1.ordinal();
        }
        if (valid2 && vt2 == ValueType.ERROR_VALUE) {
            result.setErrorValue();
            return SigValues.SIG_CHLD2.ordinal();
        }
        if (valid3 && vt3 == ValueType.ERROR_VALUE) {
            result.setErrorValue();
            return SigValues.SIG_CHLD3.ordinal();
        }
        // tree exists, because these values would be undefined" anyway then.
        if (valid1 && vt1 == ValueType.UNDEFINED_VALUE) {
            result.setUndefinedValue();
            return SigValues.SIG_CHLD1.ordinal();
        }
        if (valid2 && vt2 == ValueType.UNDEFINED_VALUE) {
            result.setUndefinedValue();
            return SigValues.SIG_CHLD2.ordinal();
        }
        if (valid3 && vt3 == ValueType.UNDEFINED_VALUE) {
            result.setUndefinedValue();
            return SigValues.SIG_CHLD3.ordinal();
        }
    }
    // comparison operations (binary, one unary)
    if (op >= OpKind_COMPARISON_START && op <= OpKind_COMPARISON_END) {
        return (doComparison(op, val1, val2, result, objectPool));
    }
    // arithmetic operations (binary)
    if (op >= OpKind_ARITHMETIC_START && op <= OpKind_ARITHMETIC_END) {
        return (doArithmetic(op, val1, val2, result, objectPool));
    }
    // logical operators (binary, one unary)
    if (op >= OpKind_LOGIC_START && op <= OpKind_LOGIC_END) {
        return (doLogical(op, val1, val2, result, objectPool));
    }
    // bitwise operators (binary, one unary)
    if (op >= OpKind_BITWISE_START && op <= OpKind_BITWISE_END) {
        return (doBitwise(op, val1, val2, result, objectPool));
    }
    // misc.
    if (op == OpKind_TERNARY_OP) {
        // ternary (if-operator)
        MutableBoolean b = objectPool.boolPool.get();
        // if the selector is UNDEFINED, the result is undefined
        if (vt1 == ValueType.UNDEFINED_VALUE) {
            result.setUndefinedValue();
            return SigValues.SIG_CHLD1.ordinal();
        }
        if (!val1.isBooleanValueEquiv(b)) {
            result.setErrorValue();
            return SigValues.SIG_CHLD1.ordinal();
        } else if (b.booleanValue()) {
            result.setValue(val2);
            return (SigValues.SIG_CHLD2.ordinal());
        } else {
            result.setValue(val3);
            return (SigValues.SIG_CHLD3.ordinal());
        }
    } else if (op == OpKind_SUBSCRIPT_OP) {
        if (vt1 == ValueType.CLASSAD_VALUE && vt2 == ValueType.STRING_VALUE) {
            ClassAd classad = objectPool.classAdPool.get();
            AMutableCharArrayString index = objectPool.strPool.get();
            val1.isClassAdValue(classad);
            val2.isStringValue(index);
            if (classad.lookup(index.toString()) == null) {
                result.setErrorValue();
                return SigValues.SIG_CHLD2.ordinal();
            }
            if (!classad.evaluateAttr(index.toString(), result)) {
                result.setErrorValue();
                return SigValues.SIG_CHLD2.ordinal();
            }
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        } else if (val1.isListValue() && vt2 == ValueType.INTEGER_VALUE) {
            AMutableInt64 index = objectPool.int64Pool.get();
            ExprList elist = objectPool.exprListPool.get();
            val1.isListValue(elist);
            val2.isIntegerValue(index);
            // check bounds
            if (index.getLongValue() < 0 || index.getLongValue() >= elist.getExprList().size()) {
                result.setErrorValue();
                return SigValues.SIG_CHLD2.ordinal();
            }
            // get value
            elist.getValue(result, elist.get((int) index.getLongValue()), es);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        // should not reach here
        throw new HyracksDataException("Should not get here");
    }
    return -1;
}
Also used : ValueType(org.apache.asterix.external.classad.Value.ValueType) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 2 with ValueType

use of org.apache.asterix.external.classad.Value.ValueType in project asterixdb by apache.

the class Operation method doTimeArithmetic.

public static int doTimeArithmetic(int op, Value v1, Value v2, Value result, ClassAdObjectPool objectPool) {
    ClassAdTime asecs1 = objectPool.classAdTimePool.get();
    ClassAdTime asecs2 = objectPool.classAdTimePool.get();
    ValueType vt1 = v1.getType();
    ValueType vt2 = v2.getType();
    // addition
    if (op == OpKind_ADDITION_OP) {
        if (vt1 == ValueType.ABSOLUTE_TIME_VALUE && vt2 == ValueType.RELATIVE_TIME_VALUE) {
            v1.isAbsoluteTimeValue(asecs1);
            v2.isRelativeTimeValue(asecs2);
            asecs1.setValue(asecs1.getTimeInMillis() + asecs2.getTimeInMillis());
            result.setAbsoluteTimeValue(asecs1);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.RELATIVE_TIME_VALUE && vt2 == ValueType.ABSOLUTE_TIME_VALUE) {
            v1.isRelativeTimeValue(asecs1);
            v2.isAbsoluteTimeValue(asecs2);
            asecs2.setValue(asecs1.getTimeInMillis() + asecs2.getTimeInMillis());
            result.setAbsoluteTimeValue(asecs2);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.RELATIVE_TIME_VALUE && vt2 == ValueType.RELATIVE_TIME_VALUE) {
            v1.isRelativeTimeValue(asecs1);
            v2.isRelativeTimeValue(asecs2);
            result.setRelativeTimeValue(asecs1.plus(asecs2.getRelativeTime(), false));
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
    }
    if (op == OpKind_SUBTRACTION_OP) {
        if (vt1 == ValueType.ABSOLUTE_TIME_VALUE && vt2 == ValueType.ABSOLUTE_TIME_VALUE) {
            v1.isAbsoluteTimeValue(asecs1);
            v2.isAbsoluteTimeValue(asecs2);
            result.setRelativeTimeValue(asecs1.subtract(asecs2, false));
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.ABSOLUTE_TIME_VALUE && vt2 == ValueType.RELATIVE_TIME_VALUE) {
            v1.isAbsoluteTimeValue(asecs1);
            v2.isRelativeTimeValue(asecs2);
            asecs1.setValue(asecs1.getTimeInMillis() - asecs2.getRelativeTime());
            result.setAbsoluteTimeValue(asecs1);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.RELATIVE_TIME_VALUE && vt2 == ValueType.RELATIVE_TIME_VALUE) {
            v1.isRelativeTimeValue(asecs1);
            v2.isRelativeTimeValue(asecs2);
            result.setRelativeTimeValue(asecs1.subtract(asecs2));
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
    }
    if (op == OpKind_MULTIPLICATION_OP || op == OpKind_DIVISION_OP) {
        if (vt1 == ValueType.RELATIVE_TIME_VALUE && vt2 == ValueType.INTEGER_VALUE) {
            AMutableInt64 num = objectPool.int64Pool.get();
            ClassAdTime msecs = objectPool.classAdTimePool.get();
            v1.isRelativeTimeValue(asecs1);
            v2.isIntegerValue(num);
            if (op == OpKind_MULTIPLICATION_OP) {
                msecs.setValue(asecs1.multiply(num.getLongValue(), false));
            } else {
                msecs.setValue(asecs1.divide(num.getLongValue(), false));
            }
            result.setRelativeTimeValue(msecs);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.RELATIVE_TIME_VALUE && vt2 == ValueType.REAL_VALUE) {
            AMutableDouble num = objectPool.doublePool.get();
            AMutableDouble msecs = objectPool.doublePool.get();
            v1.isRelativeTimeValue(asecs1);
            v2.isRealValue(num);
            if (op == OpKind_MULTIPLICATION_OP) {
                msecs.setValue(asecs1.getRelativeTime() * num.getDoubleValue());
            } else {
                msecs.setValue(asecs1.getRelativeTime() * num.getDoubleValue());
            }
            ClassAdTime time = objectPool.classAdTimePool.get();
            time.setRelativeTime(1000L * ((long) msecs.getDoubleValue()));
            result.setRelativeTimeValue(time);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt1 == ValueType.INTEGER_VALUE && vt2 == ValueType.RELATIVE_TIME_VALUE && op == OpKind_MULTIPLICATION_OP) {
            AMutableInt64 num = objectPool.int64Pool.get();
            v1.isIntegerValue(num);
            v2.isRelativeTimeValue(asecs1);
            ClassAdTime time = objectPool.classAdTimePool.get();
            time.setRelativeTime(num.getLongValue() * asecs1.getRelativeTime());
            result.setRelativeTimeValue(time);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        if (vt2 == ValueType.RELATIVE_TIME_VALUE && vt1 == ValueType.REAL_VALUE && op == OpKind_MULTIPLICATION_OP) {
            AMutableDouble num = objectPool.doublePool.get();
            v1.isRelativeTimeValue(asecs1);
            v2.isRealValue(num);
            ClassAdTime time = objectPool.classAdTimePool.get();
            time.setRelativeTime((long) (asecs1.getRelativeTime() * num.getDoubleValue()));
            result.setRelativeTimeValue(time);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
    }
    // no other operations are supported on times
    result.setErrorValue();
    return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
}
Also used : ValueType(org.apache.asterix.external.classad.Value.ValueType) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Example 3 with ValueType

use of org.apache.asterix.external.classad.Value.ValueType in project asterixdb by apache.

the class Operation method doComparison.

public static int doComparison(int op, Value v1, Value v2, Value result, ClassAdObjectPool objectPool) throws HyracksDataException {
    ValueType vt1;
    ValueType vt2;
    ValueType coerceResult;
    if (op == OpKind_META_EQUAL_OP || op == OpKind_META_NOT_EQUAL_OP) {
        // do not do type promotions for the meta operators
        vt1 = v1.getType();
        vt2 = v2.getType();
        coerceResult = vt1;
    } else {
        // do numerical type promotions --- other types/values are unchanged
        coerceResult = coerceToNumber(v1, v2, objectPool);
        vt1 = v1.getType();
        vt2 = v2.getType();
    }
    // perform comparison for =?= ; true iff same types and same values
    if (op == OpKind_META_EQUAL_OP) {
        if (vt1 != vt2) {
            result.setBooleanValue(false);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        // undefined or error
        if (vt1 == ValueType.UNDEFINED_VALUE || vt1 == ValueType.ERROR_VALUE) {
            result.setBooleanValue(true);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
    }
    // perform comparison for =!= ; negation of =?=
    if (op == OpKind_META_NOT_EQUAL_OP) {
        if (vt1 != vt2) {
            result.setBooleanValue(true);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
        // undefined or error
        if (vt1 == ValueType.UNDEFINED_VALUE || vt1 == ValueType.ERROR_VALUE) {
            result.setBooleanValue(false);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        }
    }
    switch(coerceResult) {
        // at least one of v1, v2 is a string
        case STRING_VALUE:
            // check if both are strings
            if (vt1 != ValueType.STRING_VALUE || vt2 != ValueType.STRING_VALUE) {
                // comparison between strings and non-exceptional non-string
                // values is error
                result.setErrorValue();
                return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
            }
            compareStrings(op, v1, v2, result, objectPool);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        case INTEGER_VALUE:
            compareIntegers(op, v1, v2, result, objectPool);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        case REAL_VALUE:
            compareReals(op, v1, v2, result, objectPool);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        case BOOLEAN_VALUE:
            // check if both are bools
            if (!v1.isBooleanValue() || !v2.isBooleanValue()) {
                result.setErrorValue();
                return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
            }
            compareBools(op, v1, v2, result, objectPool);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        case LIST_VALUE:
        case SLIST_VALUE:
        case CLASSAD_VALUE:
            result.setErrorValue();
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        case ABSOLUTE_TIME_VALUE:
            if (!v1.isAbsoluteTimeValue() || !v2.isAbsoluteTimeValue()) {
                result.setErrorValue();
                return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
            }
            compareAbsoluteTimes(op, v1, v2, result, objectPool);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        case RELATIVE_TIME_VALUE:
            if (!v1.isRelativeTimeValue() || !v2.isRelativeTimeValue()) {
                result.setErrorValue();
                return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
            }
            compareRelativeTimes(op, v1, v2, result, objectPool);
            return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
        default:
            // should not get here
            throw new HyracksDataException("Should not get here");
    }
}
Also used : ValueType(org.apache.asterix.external.classad.Value.ValueType) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 4 with ValueType

use of org.apache.asterix.external.classad.Value.ValueType in project asterixdb by apache.

the class Operation method doLogical.

public static int doLogical(int op, Value v1, Value v2, Value result, ClassAdObjectPool objectPool) throws HyracksDataException {
    MutableBoolean b1 = objectPool.boolPool.get();
    MutableBoolean b2 = objectPool.boolPool.get();
    // first coerece inputs to boolean if they are considered equivalent
    if (!v1.isBooleanValue(b1) && v1.isBooleanValueEquiv(b1)) {
        v1.setBooleanValue(b1.booleanValue());
    }
    if (!v2.isBooleanValue(b2) && v2.isBooleanValueEquiv(b2)) {
        v2.setBooleanValue(b2);
    }
    ValueType vt1 = v1.getType();
    ValueType vt2 = v2.getType();
    if (vt1 != ValueType.UNDEFINED_VALUE && vt1 != ValueType.ERROR_VALUE && vt1 != ValueType.BOOLEAN_VALUE) {
        result.setErrorValue();
        return SigValues.SIG_CHLD1.ordinal();
    }
    if (vt2 != ValueType.UNDEFINED_VALUE && vt2 != ValueType.ERROR_VALUE && vt2 != ValueType.BOOLEAN_VALUE) {
        result.setErrorValue();
        return SigValues.SIG_CHLD2.ordinal();
    }
    // handle unary operator
    if (op == OpKind_LOGICAL_NOT_OP) {
        if (vt1 == ValueType.BOOLEAN_VALUE) {
            result.setBooleanValue(!b1.booleanValue());
        } else {
            result.setValue(v1);
        }
        return SigValues.SIG_CHLD1.ordinal();
    }
    if (op == OpKind_LOGICAL_OR_OP) {
        if (vt1 == ValueType.BOOLEAN_VALUE && b1.booleanValue()) {
            result.setBooleanValue(true);
            return SigValues.SIG_CHLD1.ordinal();
        } else if (vt1 == ValueType.ERROR_VALUE) {
            result.setErrorValue();
            return SigValues.SIG_CHLD1.ordinal();
        } else if (vt1 == ValueType.BOOLEAN_VALUE && !b1.booleanValue()) {
            result.setValue(v2);
        } else if (vt2 != ValueType.BOOLEAN_VALUE) {
            result.setValue(v2);
        } else if (b2.booleanValue()) {
            result.setBooleanValue(true);
        } else {
            result.setUndefinedValue();
        }
        return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
    } else if (op == OpKind_LOGICAL_AND_OP) {
        if (vt1 == ValueType.BOOLEAN_VALUE && !b1.booleanValue()) {
            result.setBooleanValue(false);
            return SigValues.SIG_CHLD1.ordinal();
        } else if (vt1 == ValueType.ERROR_VALUE) {
            result.setErrorValue();
            return SigValues.SIG_CHLD1.ordinal();
        } else if (vt1 == ValueType.BOOLEAN_VALUE && b1.booleanValue()) {
            result.setValue(v2);
        } else if (vt2 != ValueType.BOOLEAN_VALUE) {
            result.setValue(v2);
        } else if (!b2.booleanValue()) {
            result.setBooleanValue(false);
        } else {
            result.setUndefinedValue();
        }
        return (SigValues.SIG_CHLD1.ordinal() | SigValues.SIG_CHLD2.ordinal());
    }
    throw new HyracksDataException("Shouldn't reach here");
}
Also used : ValueType(org.apache.asterix.external.classad.Value.ValueType) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Aggregations

ValueType (org.apache.asterix.external.classad.Value.ValueType)4 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)3 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)2 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)2 AMutableDouble (org.apache.asterix.om.base.AMutableDouble)1