Search in sources :

Example 86 with Expression

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));
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) VarcharType(com.facebook.presto.spi.type.VarcharType) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) Expression(com.facebook.presto.sql.tree.Expression) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) Block(com.facebook.presto.spi.block.Block) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) NullLiteral(com.facebook.presto.sql.tree.NullLiteral)

Example 87 with Expression

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();
}
Also used : Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) Expression(com.facebook.presto.sql.tree.Expression) ImmutableList(com.google.common.collect.ImmutableList)

Example 88 with Expression

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);
}
Also used : ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) OutputNode(com.facebook.presto.sql.planner.plan.OutputNode) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect)

Example 89 with Expression

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;
}
Also used : IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) Type(com.facebook.presto.spi.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) LambdaArgumentDeclaration(com.facebook.presto.sql.tree.LambdaArgumentDeclaration) Expression(com.facebook.presto.sql.tree.Expression) Map(java.util.Map) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap)

Example 90 with Expression

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);
}
Also used : FieldReference(com.facebook.presto.sql.tree.FieldReference) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) Expression(com.facebook.presto.sql.tree.Expression)

Aggregations

Expression (com.facebook.presto.sql.tree.Expression)137 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)74 Test (org.testng.annotations.Test)46 NotExpression (com.facebook.presto.sql.tree.NotExpression)42 InListExpression (com.facebook.presto.sql.tree.InListExpression)40 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)33 Type (com.facebook.presto.spi.type.Type)26 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)26 LiteralInterpreter.toExpression (com.facebook.presto.sql.planner.LiteralInterpreter.toExpression)25 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)22 ImmutableList (com.google.common.collect.ImmutableList)22 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)19 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)18 Cast (com.facebook.presto.sql.tree.Cast)17 ArrayList (java.util.ArrayList)17 ExtractionResult (com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult)16 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)16 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)16 SubscriptExpression (com.facebook.presto.sql.tree.SubscriptExpression)16 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)15