Search in sources :

Example 1 with Literal

use of org.apache.asterix.lang.common.base.Literal in project asterixdb by apache.

the class LangRecordParseUtil method exprToStringLiteral.

public static Literal exprToStringLiteral(Expression expr) throws HyracksDataException {
    if (expr.getKind() != Expression.Kind.LITERAL_EXPRESSION) {
        throw new HyracksDataException(ErrorCode.ASTERIX, ErrorCode.PARSE_ERROR, "Expected expression can only be of type %1$s", Expression.Kind.LITERAL_EXPRESSION);
    }
    LiteralExpr keyLiteralExpr = (LiteralExpr) expr;
    Literal keyLiteral = keyLiteralExpr.getValue();
    if (keyLiteral.getLiteralType() != Literal.Type.STRING) {
        throw new HyracksDataException(ErrorCode.ASTERIX, ErrorCode.PARSE_ERROR, "Expected Literal can only be of type %1$s", Literal.Type.STRING);
    }
    return keyLiteral;
}
Also used : Literal(org.apache.asterix.lang.common.base.Literal) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 2 with Literal

use of org.apache.asterix.lang.common.base.Literal in project asterixdb by apache.

the class FormatPrintVisitor method visit.

@Override
public Void visit(LiteralExpr l, Integer step) {
    Literal lc = l.getValue();
    if (lc.getLiteralType().equals(Literal.Type.TRUE) || lc.getLiteralType().equals(Literal.Type.FALSE) || lc.getLiteralType().equals(Literal.Type.NULL) || lc.getLiteralType().equals(Literal.Type.MISSING)) {
        out.print(lc.getLiteralType().toString().toLowerCase());
    } else if (lc.getLiteralType().equals(Literal.Type.STRING)) {
        out.print(revertStringToLiteral(lc.getStringValue()));
    } else {
        if (lc.getLiteralType().equals(Literal.Type.FLOAT)) {
            out.printf("%ff", lc.getValue());
        } else if (lc.getLiteralType().equals(Literal.Type.DOUBLE)) {
            DecimalFormat df = new DecimalFormat("#.#");
            df.setMinimumFractionDigits(1);
            df.setMaximumFractionDigits(16);
            out.print(df.format(lc.getValue()));
        } else {
            out.print(lc.getStringValue());
        }
    }
    return null;
}
Also used : Literal(org.apache.asterix.lang.common.base.Literal) DecimalFormat(java.text.DecimalFormat)

Example 3 with Literal

use of org.apache.asterix.lang.common.base.Literal in project asterixdb by apache.

the class RangeMapBuilder method parseLiteralToBytes.

@SuppressWarnings("unchecked")
private static void parseLiteralToBytes(Expression item, DataOutput out) throws CompilationException {
    AMutableDouble aDouble = new AMutableDouble(0);
    AMutableFloat aFloat = new AMutableFloat(0);
    AMutableInt64 aInt64 = new AMutableInt64(0);
    AMutableInt32 aInt32 = new AMutableInt32(0);
    AMutableString aString = new AMutableString("");
    @SuppressWarnings("rawtypes") ISerializerDeserializer serde;
    Literal l = ((LiteralExpr) item).getValue();
    try {
        switch(l.getLiteralType()) {
            case DOUBLE:
                DoubleLiteral dl = (DoubleLiteral) l;
                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
                aDouble.setValue(dl.getValue());
                serde.serialize(aDouble, out);
                break;
            case FLOAT:
                FloatLiteral fl = (FloatLiteral) l;
                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AFLOAT);
                aFloat.setValue(fl.getValue());
                serde.serialize(aFloat, out);
                break;
            case INTEGER:
                IntegerLiteral il = (IntegerLiteral) l;
                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT32);
                aInt32.setValue(il.getValue());
                serde.serialize(aInt32, out);
                break;
            case LONG:
                LongIntegerLiteral lil = (LongIntegerLiteral) l;
                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);
                aInt64.setValue(lil.getValue());
                serde.serialize(aInt64, out);
                break;
            case STRING:
                StringLiteral sl = (StringLiteral) l;
                serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING);
                aString.setValue(sl.getValue());
                serde.serialize(aString, out);
                break;
            default:
                throw new NotImplementedException("The range map builder has not been implemented for " + item.getKind() + " type of expressions.");
        }
    } catch (HyracksDataException e) {
        throw new CompilationException(e.getMessage());
    }
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) FloatLiteral(org.apache.asterix.lang.common.literal.FloatLiteral) NotImplementedException(org.apache.hyracks.algebricks.common.exceptions.NotImplementedException) AMutableString(org.apache.asterix.om.base.AMutableString) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) LongIntegerLiteral(org.apache.asterix.lang.common.literal.LongIntegerLiteral) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) Literal(org.apache.asterix.lang.common.base.Literal) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) IntegerLiteral(org.apache.asterix.lang.common.literal.IntegerLiteral) FloatLiteral(org.apache.asterix.lang.common.literal.FloatLiteral) DoubleLiteral(org.apache.asterix.lang.common.literal.DoubleLiteral) LongIntegerLiteral(org.apache.asterix.lang.common.literal.LongIntegerLiteral) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) DoubleLiteral(org.apache.asterix.lang.common.literal.DoubleLiteral) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) AMutableInt32(org.apache.asterix.om.base.AMutableInt32) AMutableFloat(org.apache.asterix.om.base.AMutableFloat) IntegerLiteral(org.apache.asterix.lang.common.literal.IntegerLiteral) LongIntegerLiteral(org.apache.asterix.lang.common.literal.LongIntegerLiteral)

Aggregations

Literal (org.apache.asterix.lang.common.base.Literal)3 LiteralExpr (org.apache.asterix.lang.common.expression.LiteralExpr)2 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)2 DecimalFormat (java.text.DecimalFormat)1 CompilationException (org.apache.asterix.common.exceptions.CompilationException)1 DoubleLiteral (org.apache.asterix.lang.common.literal.DoubleLiteral)1 FloatLiteral (org.apache.asterix.lang.common.literal.FloatLiteral)1 IntegerLiteral (org.apache.asterix.lang.common.literal.IntegerLiteral)1 LongIntegerLiteral (org.apache.asterix.lang.common.literal.LongIntegerLiteral)1 StringLiteral (org.apache.asterix.lang.common.literal.StringLiteral)1 AMutableDouble (org.apache.asterix.om.base.AMutableDouble)1 AMutableFloat (org.apache.asterix.om.base.AMutableFloat)1 AMutableInt32 (org.apache.asterix.om.base.AMutableInt32)1 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)1 AMutableString (org.apache.asterix.om.base.AMutableString)1 NotImplementedException (org.apache.hyracks.algebricks.common.exceptions.NotImplementedException)1 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)1