use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.
the class LiteralInterpreter method toExpression.
public static Expression toExpression(Object object, Type type) {
requireNonNull(type, "type is null");
if (object instanceof Expression) {
return (Expression) object;
}
if (object == null) {
if (type.equals(UNKNOWN)) {
return new NullLiteral();
}
return new Cast(new NullLiteral(), type.getTypeSignature().toString(), false, true);
}
if (type.equals(INTEGER)) {
return new LongLiteral(object.toString());
}
if (type.equals(BIGINT)) {
LongLiteral expression = new LongLiteral(object.toString());
if (expression.getValue() >= Integer.MIN_VALUE && expression.getValue() <= Integer.MAX_VALUE) {
return new GenericLiteral("BIGINT", object.toString());
}
return new LongLiteral(object.toString());
}
checkArgument(Primitives.wrap(type.getJavaType()).isInstance(object), "object.getClass (%s) and type.getJavaType (%s) do not agree", object.getClass(), type.getJavaType());
if (type.equals(DOUBLE)) {
Double value = (Double) object;
// When changing this, don't forget about similar code for REAL below
if (value.isNaN()) {
return new FunctionCall(QualifiedName.of("nan"), ImmutableList.of());
} else if (value.equals(Double.NEGATIVE_INFINITY)) {
return ArithmeticUnaryExpression.negative(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()));
} else if (value.equals(Double.POSITIVE_INFINITY)) {
return new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of());
} else {
return new DoubleLiteral(object.toString());
}
}
if (type.equals(REAL)) {
Float value = intBitsToFloat(((Long) object).intValue());
// WARNING for ORC predicate code as above (for double)
if (value.isNaN()) {
return new Cast(new FunctionCall(QualifiedName.of("nan"), ImmutableList.of()), StandardTypes.REAL);
} else if (value.equals(Float.NEGATIVE_INFINITY)) {
return ArithmeticUnaryExpression.negative(new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL));
} else if (value.equals(Float.POSITIVE_INFINITY)) {
return new Cast(new FunctionCall(QualifiedName.of("infinity"), ImmutableList.of()), StandardTypes.REAL);
} else {
return new GenericLiteral("REAL", value.toString());
}
}
if (type instanceof VarcharType) {
if (object instanceof String) {
object = Slices.utf8Slice((String) object);
}
if (object instanceof Slice) {
Slice value = (Slice) object;
int length = SliceUtf8.countCodePoints(value);
if (length == ((VarcharType) type).getLength()) {
return new StringLiteral(value.toStringUtf8());
}
return new Cast(new StringLiteral(value.toStringUtf8()), type.getDisplayName(), false, true);
}
throw new IllegalArgumentException("object must be instance of Slice or String when type is VARCHAR");
}
if (type.equals(BOOLEAN)) {
return new BooleanLiteral(object.toString());
}
if (object instanceof Block) {
SliceOutput output = new DynamicSliceOutput(((Block) object).getSizeInBytes());
BlockSerdeUtil.writeBlock(output, (Block) object);
object = output.slice();
// This if condition will evaluate to true: object instanceof Slice && !type.equals(VARCHAR)
}
if (object instanceof Slice) {
// HACK: we need to serialize VARBINARY in a format that can be embedded in an expression to be
// able to encode it in the plan that gets sent to workers.
// We do this by transforming the in-memory varbinary into a call to from_base64(<base64-encoded value>)
FunctionCall fromBase64 = new FunctionCall(QualifiedName.of("from_base64"), ImmutableList.of(new StringLiteral(VarbinaryFunctions.toBase64((Slice) object).toStringUtf8())));
Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
return new FunctionCall(QualifiedName.of(signature.getName()), ImmutableList.of(fromBase64));
}
Signature signature = FunctionRegistry.getMagicLiteralFunctionSignature(type);
Expression rawLiteral = toExpression(object, FunctionRegistry.typeForMagicLiteral(type));
return new FunctionCall(QualifiedName.of(signature.getName()), ImmutableList.of(rawLiteral));
}
use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.
the class LiteralInterpreter method toExpressions.
public static List<Expression> toExpressions(List<?> objects, List<? extends Type> types) {
requireNonNull(objects, "objects is null");
requireNonNull(types, "types is null");
checkArgument(objects.size() == types.size(), "objects and types do not have the same size");
ImmutableList.Builder<Expression> expressions = ImmutableList.builder();
for (int i = 0; i < objects.size(); i++) {
Object object = objects.get(i);
Type type = types.get(i);
expressions.add(toExpression(object, type));
}
return expressions.build();
}
use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.
the class LogicalPlanner method planStatement.
public PlanNode planStatement(Analysis analysis, Statement statement) {
if (statement instanceof CreateTableAsSelect && analysis.isCreateTableAsSelectNoOp()) {
checkState(analysis.getCreateTableDestination().isPresent(), "Table destination is missing");
List<Expression> emptyRow = ImmutableList.of();
PlanNode source = new ValuesNode(idAllocator.getNextId(), ImmutableList.of(), ImmutableList.of(emptyRow));
return new OutputNode(idAllocator.getNextId(), source, ImmutableList.of(), ImmutableList.of());
}
return createOutputPlan(planStatementWithoutOutput(analysis, statement), analysis);
}
use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.
the class LogicalPlanner method buildLambdaDeclarationToSymbolMap.
private static IdentityLinkedHashMap<LambdaArgumentDeclaration, Symbol> buildLambdaDeclarationToSymbolMap(Analysis analysis, SymbolAllocator symbolAllocator) {
IdentityLinkedHashMap<LambdaArgumentDeclaration, Symbol> resultMap = new IdentityLinkedHashMap<>();
for (Map.Entry<Expression, Type> entry : analysis.getTypes().entrySet()) {
if (!(entry.getKey() instanceof LambdaArgumentDeclaration)) {
continue;
}
LambdaArgumentDeclaration lambdaArgumentDeclaration = (LambdaArgumentDeclaration) entry.getKey();
if (resultMap.containsKey(lambdaArgumentDeclaration)) {
continue;
}
resultMap.put(lambdaArgumentDeclaration, symbolAllocator.newSymbol(lambdaArgumentDeclaration, entry.getValue()));
}
return resultMap;
}
use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.
the class TranslationMap method put.
public void put(Expression expression, Symbol symbol) {
if (expression instanceof FieldReference) {
int fieldIndex = ((FieldReference) expression).getFieldIndex();
fieldSymbols[fieldIndex] = symbol;
expressionToSymbols.put(rewriteBase.getSymbol(fieldIndex).toSymbolReference(), symbol);
return;
}
Expression translated = translateNamesToSymbols(expression);
expressionToSymbols.put(translated, symbol);
// also update the field mappings if this expression is a field reference
rewriteBase.getScope().tryResolveField(expression).filter(ResolvedField::isLocal).ifPresent(field -> fieldSymbols[rewriteBase.getDescriptor().indexOf(field.getField())] = symbol);
}
Aggregations