Search in sources :

Example 6 with ClassAd

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

the class ClassAdParser method parsePrimaryExpression.

// PrimaryExpression .= Identifier
// | FunctionCall
// | '.' Identifier
// | '(' Expression ')'
// | Literal
// FunctionCall .= Identifier ArgumentList
// ( Constant may be
// boolean,undefined,error,string,integer,real,classad,list )
// ( ArgumentList non-terminal includes parentheses )
private boolean parsePrimaryExpression(ExprTreeHolder tree) throws IOException {
    ExprTreeHolder treeL;
    TokenValue tv = objectPool.tokenValuePool.get();
    TokenType tt;
    tree.setInnerTree(null);
    switch((tt = lexer.peekToken(tv))) {
        // identifiers
        case LEX_IDENTIFIER:
            isExpr = true;
            lexer.consumeToken();
            // check for funcion call
            if ((tt = lexer.peekToken()) == TokenType.LEX_OPEN_PAREN) {
                ExprList argList = objectPool.exprListPool.get();
                if (!parseArgumentList(argList)) {
                    tree.setInnerTree(null);
                    return false;
                }
                ;
                // string literal
                if (shouldEvaluateAtParseTime(tv.getStrValue().toString(), argList)) {
                    tree.setInnerTree(evaluateFunction(tv.getStrValue().toString(), argList));
                } else {
                    tree.setInnerTree(FunctionCall.createFunctionCall(tv.getStrValue().toString(), argList, objectPool));
                }
            } else {
                // I don't think this is ever hit
                tree.setInnerTree(AttributeReference.createAttributeReference(null, tv.getStrValue(), false, objectPool));
            }
            return (tree.getInnerTree() != null);
        case LEX_SELECTION:
            isExpr = true;
            lexer.consumeToken();
            if ((tt = lexer.consumeToken(tv)) == TokenType.LEX_IDENTIFIER) {
                // the boolean final arg signifies that reference is absolute
                tree.setInnerTree(AttributeReference.createAttributeReference(null, tv.getStrValue(), true, objectPool));
                return (tree.size() != 0);
            }
            // not an identifier following the '.'
            throw new HyracksDataException("need identifier in selection expression (got" + Lexer.strLexToken(tt) + ")");
        // parenthesized expression
        case LEX_OPEN_PAREN:
            {
                isExpr = true;
                lexer.consumeToken();
                treeL = objectPool.mutableExprPool.get();
                parseExpression(treeL);
                if (treeL.getInnerTree() == null) {
                    tree.resetExprTree(null);
                    return false;
                }
                if ((tt = lexer.consumeToken()) != TokenType.LEX_CLOSE_PAREN) {
                    throw new HyracksDataException("exptected LEX_CLOSE_PAREN, but got " + Lexer.strLexToken(tt));
                // tree.resetExprTree(null);
                // return false;
                }
                // assume make operation will return a new tree
                tree.setInnerTree(Operation.createOperation(Operation.OpKind_PARENTHESES_OP, treeL, objectPool));
                return (tree.size() != 0);
            }
        // constants
        case LEX_OPEN_BOX:
            {
                isExpr = true;
                ClassAd newAd = objectPool.classAdPool.get();
                if (!parseClassAd(newAd)) {
                    tree.resetExprTree(null);
                    return false;
                }
                tree.setInnerTree(newAd);
            }
            return true;
        case LEX_OPEN_BRACE:
            {
                isExpr = true;
                ExprList newList = objectPool.exprListPool.get();
                if (!parseExprList(newList)) {
                    tree.setInnerTree(null);
                    return false;
                }
                tree.setInnerTree(newList);
            }
            return true;
        case LEX_UNDEFINED_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setUndefinedValue();
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_ERROR_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setErrorValue();
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_BOOLEAN_VALUE:
            {
                Value val = objectPool.valuePool.get();
                MutableBoolean b = new MutableBoolean();
                tv.getBoolValue(b);
                lexer.consumeToken();
                val.setBooleanValue(b);
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_INTEGER_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setIntegerValue(tv.getIntValue());
                tree.setInnerTree(Literal.createLiteral(val, tv.getFactor(), objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_REAL_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setRealValue(tv.getRealValue());
                tree.setInnerTree(Literal.createLiteral(val, tv.getFactor(), objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_STRING_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setStringValue(tv.getStrValue());
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_ABSOLUTE_TIME_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setAbsoluteTimeValue(tv.getTimeValue());
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        case LEX_RELATIVE_TIME_VALUE:
            {
                Value val = objectPool.valuePool.get();
                lexer.consumeToken();
                val.setRelativeTimeValue(tv.getTimeValue().getRelativeTime());
                tree.setInnerTree(Literal.createLiteral(val, objectPool));
                return (tree.getInnerTree() != null);
            }
        default:
            tree.setInnerTree(null);
            return false;
    }
}
Also used : TokenType(org.apache.asterix.external.classad.Lexer.TokenType) ClassAd(org.apache.asterix.external.classad.ClassAd) ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) ExprList(org.apache.asterix.external.classad.ExprList) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) TokenValue(org.apache.asterix.external.classad.TokenValue) Value(org.apache.asterix.external.classad.Value) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) TokenValue(org.apache.asterix.external.classad.TokenValue)

Example 7 with ClassAd

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

the class ClassAdParser method writeFieldValueToBuffer.

private void writeFieldValueToBuffer(IAType fieldType, DataOutput out, String name, ExprTree tree, ClassAd pAd) throws IOException, AsterixException {
    Value val;
    switch(tree.getKind()) {
        case ATTRREF_NODE:
        case CLASSAD_NODE:
        case EXPR_ENVELOPE:
        case EXPR_LIST_NODE:
        case FN_CALL_NODE:
        case OP_NODE:
            val = objectPool.valuePool.get();
            if (pAd.evaluateAttr(name, val)) {
            } else {
                // just write the expr
                val = ((Literal) pAd.getAttrList().get(name + "Expr")).getValue();
            }
            break;
        case LITERAL_NODE:
            val = ((Literal) tree.getTree()).getValue();
            break;
        default:
            throw new HyracksDataException("Unknown Expression type detected: " + tree.getKind());
    }
    if (fieldType != null) {
        if (NonTaggedFormatUtil.isOptional(fieldType)) {
            fieldType = ((AUnionType) fieldType).getActualType();
        }
    }
    switch(val.getValueType()) {
        case ABSOLUTE_TIME_VALUE:
            if (checkType(ATypeTag.DATETIME, fieldType)) {
                parseDateTime(val, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case BOOLEAN_VALUE:
            if (checkType(ATypeTag.BOOLEAN, fieldType)) {
                booleanSerde.serialize(val.getBoolVal() ? ABoolean.TRUE : ABoolean.FALSE, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case CLASSAD_VALUE:
            if (checkType(ATypeTag.OBJECT, fieldType)) {
                IAType objectType = getComplexType(fieldType, ATypeTag.OBJECT);
                ClassAd classad = val.getClassadVal();
                parseRecord((ARecordType) objectType, classad, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case ERROR_VALUE:
        case STRING_VALUE:
        case UNDEFINED_VALUE:
            if (checkType(ATypeTag.STRING, fieldType)) {
                parseString(val, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case INTEGER_VALUE:
            if (checkType(ATypeTag.BIGINT, fieldType)) {
                if (fieldType == null || fieldType.getTypeTag() == ATypeTag.BIGINT) {
                    aInt64.setValue(val.getLongVal());
                    int64Serde.serialize(aInt64, out);
                } else if (fieldType.getTypeTag() == ATypeTag.INTEGER) {
                    aInt32.setValue((int) val.getLongVal());
                    int32Serde.serialize(aInt32, out);
                } else if (fieldType.getTypeTag() == ATypeTag.DOUBLE) {
                    aDouble.setValue(val.getLongVal());
                    doubleSerde.serialize(aDouble, out);
                } else if (fieldType.getTypeTag() == ATypeTag.SMALLINT) {
                    aInt16.setValue((short) val.getLongVal());
                    int16Serde.serialize(aInt16, out);
                } else if (fieldType.getTypeTag() == ATypeTag.TINYINT) {
                    aInt8.setValue((byte) val.getLongVal());
                    int8Serde.serialize(aInt8, out);
                } else if (fieldType.getTypeTag() == ATypeTag.FLOAT) {
                    aFloat.setValue(val.getLongVal());
                    floatSerde.serialize(aFloat, out);
                }
            } else if (checkType(ATypeTag.DATETIME, fieldType)) {
                // Classad uses Linux Timestamps (s instead of ms)
                aDateTime.setValue(val.getLongVal() * 1000);
                datetimeSerde.serialize(aDateTime, out);
            } else if (checkType(ATypeTag.DURATION, fieldType)) {
                // Classad uses Linux Timestamps (s instead of ms)
                aDuration.setValue(0, val.getLongVal() * 1000);
                durationSerde.serialize(aDuration, out);
            } else if (checkType(ATypeTag.INTEGER, fieldType)) {
                aInt32.setValue((int) val.getLongVal());
                int32Serde.serialize(aInt32, out);
            } else if (checkType(ATypeTag.DOUBLE, fieldType)) {
                aDouble.setValue(val.getLongVal());
                doubleSerde.serialize(aDouble, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case LIST_VALUE:
        case SLIST_VALUE:
            IAType objectType;
            if (checkType(ATypeTag.MULTISET, fieldType)) {
                objectType = getComplexType(fieldType, ATypeTag.MULTISET);
                parseUnorderedList((AUnorderedListType) objectType, val, out);
            } else if (checkType(ATypeTag.ARRAY, fieldType)) {
                objectType = getComplexType(fieldType, ATypeTag.ARRAY);
                parseOrderedList((AOrderedListType) objectType, val, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case REAL_VALUE:
            if (checkType(ATypeTag.DOUBLE, fieldType)) {
                if (fieldType == null || fieldType.getTypeTag() == ATypeTag.DOUBLE) {
                    aDouble.setValue(val.getDoubleVal());
                    doubleSerde.serialize(aDouble, out);
                } else if (fieldType.getTypeTag() == ATypeTag.INTEGER) {
                    aInt32.setValue((int) val.getDoubleVal());
                    int32Serde.serialize(aInt32, out);
                } else if (fieldType.getTypeTag() == ATypeTag.BIGINT) {
                    aInt64.setValue((long) val.getDoubleVal());
                    int64Serde.serialize(aInt64, out);
                } else if (fieldType.getTypeTag() == ATypeTag.SMALLINT) {
                    aInt16.setValue((short) val.getDoubleVal());
                    int16Serde.serialize(aInt16, out);
                } else if (fieldType.getTypeTag() == ATypeTag.TINYINT) {
                    aInt8.setValue((byte) val.getDoubleVal());
                    int8Serde.serialize(aInt8, out);
                } else if (fieldType.getTypeTag() == ATypeTag.FLOAT) {
                    aFloat.setValue((float) val.getDoubleVal());
                    floatSerde.serialize(aFloat, out);
                }
            } else if (checkType(ATypeTag.INTEGER, fieldType)) {
                aInt32.setValue((int) val.getDoubleVal());
                int32Serde.serialize(aInt32, out);
            } else if (checkType(ATypeTag.BIGINT, fieldType)) {
                aInt64.setValue((long) val.getDoubleVal());
                int64Serde.serialize(aInt64, out);
            } else if (checkType(ATypeTag.DATETIME, fieldType)) {
                // Classad uses Linux Timestamps (s instead of ms)
                aDateTime.setValue(val.getLongVal() * 1000);
                datetimeSerde.serialize(aDateTime, out);
            } else if (checkType(ATypeTag.DURATION, fieldType)) {
                // Classad uses Linux Timestamps (s instead of ms)
                aDuration.setValue(0, (long) (val.getDoubleVal() * 1000.0));
                durationSerde.serialize(aDuration, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        case RELATIVE_TIME_VALUE:
            if (checkType(ATypeTag.DURATION, fieldType)) {
                parseDuration(val, out);
            } else {
                throw new HyracksDataException(mismatchErrorMessage + fieldType.getTypeTag());
            }
            break;
        default:
            throw new HyracksDataException("unknown data type " + val.getValueType());
    }
}
Also used : ClassAd(org.apache.asterix.external.classad.ClassAd) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) TokenValue(org.apache.asterix.external.classad.TokenValue) Value(org.apache.asterix.external.classad.Value) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IAType(org.apache.asterix.om.types.IAType)

Example 8 with ClassAd

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

the class ClassAdParser method parseClassAd.

public ClassAd parseClassAd(String buffer, AMutableInt32 offset) throws IOException {
    currentSource = new StringLexerSource(buffer);
    ClassAd ad = parseClassAd((StringLexerSource) currentSource);
    offset.setValue(((StringLexerSource) currentSource).getCurrentLocation());
    return ad;
}
Also used : StringLexerSource(org.apache.asterix.external.classad.StringLexerSource) ClassAd(org.apache.asterix.external.classad.ClassAd)

Example 9 with ClassAd

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

the class FunctionalTester method evaluate_expr.

/*********************************************************************
     * Function: evaluate_expr
     * Purpose:
     *
     * @throws HyracksDataException
     *********************************************************************/
public static boolean evaluate_expr(ExprTree tree, Value value, Parameters parameters, ClassAdObjectPool objectPool) throws HyracksDataException {
    ClassAd classad = new ClassAd(objectPool);
    boolean success = false;
    ;
    classad.insert("internal___", tree);
    success = classad.evaluateAttr("internal___", value);
    classad.remove("internal___");
    return success;
}
Also used : ClassAd(org.apache.asterix.external.classad.ClassAd)

Example 10 with ClassAd

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

the class ClassAdParser method parseClassAd.

public ClassAd parseClassAd(LexerSource lexer_source, boolean full) throws IOException {
    System.out.println("Don't use this call. instead, pass a mutable classad instance");
    ClassAd ad = objectPool.classAdPool.get();
    if (lexer.initialize(lexer_source)) {
        if (!parseClassAd(ad, full)) {
            return null;
        } else if (lexer_source.readPreviousCharacter() != '\0') {
            // The lexer swallows one extra character, so if we have
            // two classads back to back we need to make sure to unread
            // one of the characters.
            lexer_source.unreadCharacter();
        }
    }
    return ad;
}
Also used : ClassAd(org.apache.asterix.external.classad.ClassAd)

Aggregations

ClassAd (org.apache.asterix.external.classad.ClassAd)11 Value (org.apache.asterix.external.classad.Value)6 ClassAdParser (org.apache.asterix.external.library.ClassAdParser)4 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)4 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)4 ArrayList (java.util.ArrayList)3 AMutableCharArrayString (org.apache.asterix.external.classad.AMutableCharArrayString)3 ExprList (org.apache.asterix.external.classad.ExprList)3 ExprTree (org.apache.asterix.external.classad.ExprTree)3 ClassAdObjectPool (org.apache.asterix.external.classad.object.pool.ClassAdObjectPool)3 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 CaseInsensitiveString (org.apache.asterix.external.classad.CaseInsensitiveString)2 CharArrayLexerSource (org.apache.asterix.external.classad.CharArrayLexerSource)2 TokenValue (org.apache.asterix.external.classad.TokenValue)2 SemiStructuredRecordReader (org.apache.asterix.external.input.record.reader.stream.SemiStructuredRecordReader)2 LocalFSInputStream (org.apache.asterix.external.input.stream.LocalFSInputStream)2 FileSystemWatcher (org.apache.asterix.external.util.FileSystemWatcher)2 AMutableDouble (org.apache.asterix.om.base.AMutableDouble)2 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)2