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;
}
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;
}
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());
}
}
Aggregations