Search in sources :

Example 11 with LongLiteral

use of com.facebook.presto.sql.tree.LongLiteral in project presto by prestodb.

the class TreePrinter method print.

public void print(Node root) {
    AstVisitor<Void, Integer> printer = new DefaultTraversalVisitor<Void, Integer>() {

        @Override
        protected Void visitNode(Node node, Integer indentLevel) {
            throw new UnsupportedOperationException("not yet implemented: " + node);
        }

        @Override
        protected Void visitQuery(Query node, Integer indentLevel) {
            print(indentLevel, "Query ");
            indentLevel++;
            print(indentLevel, "QueryBody");
            process(node.getQueryBody(), indentLevel);
            if (node.getOrderBy().isPresent()) {
                print(indentLevel, "OrderBy");
                process(node.getOrderBy().get(), indentLevel + 1);
            }
            if (node.getLimit().isPresent()) {
                print(indentLevel, "Limit: " + node.getLimit().get());
            }
            return null;
        }

        @Override
        protected Void visitQuerySpecification(QuerySpecification node, Integer indentLevel) {
            print(indentLevel, "QuerySpecification ");
            indentLevel++;
            process(node.getSelect(), indentLevel);
            if (node.getFrom().isPresent()) {
                print(indentLevel, "From");
                process(node.getFrom().get(), indentLevel + 1);
            }
            if (node.getWhere().isPresent()) {
                print(indentLevel, "Where");
                process(node.getWhere().get(), indentLevel + 1);
            }
            if (node.getGroupBy().isPresent()) {
                String distinct = "";
                if (node.getGroupBy().get().isDistinct()) {
                    distinct = "[DISTINCT]";
                }
                print(indentLevel, "GroupBy" + distinct);
                for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) {
                    print(indentLevel, "SimpleGroupBy");
                    if (groupingElement instanceof SimpleGroupBy) {
                        for (Expression column : ((SimpleGroupBy) groupingElement).getColumnExpressions()) {
                            process(column, indentLevel + 1);
                        }
                    } else if (groupingElement instanceof GroupingSets) {
                        print(indentLevel + 1, "GroupingSets");
                        for (Set<Expression> column : groupingElement.enumerateGroupingSets()) {
                            print(indentLevel + 2, "GroupingSet[");
                            for (Expression expression : column) {
                                process(expression, indentLevel + 3);
                            }
                            print(indentLevel + 2, "]");
                        }
                    } else if (groupingElement instanceof Cube) {
                        print(indentLevel + 1, "Cube");
                        for (QualifiedName column : ((Cube) groupingElement).getColumns()) {
                            print(indentLevel + 1, column.toString());
                        }
                    } else if (groupingElement instanceof Rollup) {
                        print(indentLevel + 1, "Rollup");
                        for (QualifiedName column : ((Rollup) groupingElement).getColumns()) {
                            print(indentLevel + 1, column.toString());
                        }
                    }
                }
            }
            if (node.getHaving().isPresent()) {
                print(indentLevel, "Having");
                process(node.getHaving().get(), indentLevel + 1);
            }
            if (node.getOrderBy().isPresent()) {
                print(indentLevel, "OrderBy");
                process(node.getOrderBy().get(), indentLevel + 1);
            }
            if (node.getLimit().isPresent()) {
                print(indentLevel, "Limit: " + node.getLimit().get());
            }
            return null;
        }

        protected Void visitOrderBy(OrderBy node, Integer indentLevel) {
            for (SortItem sortItem : node.getSortItems()) {
                process(sortItem, indentLevel);
            }
            return null;
        }

        @Override
        protected Void visitSelect(Select node, Integer indentLevel) {
            String distinct = "";
            if (node.isDistinct()) {
                distinct = "[DISTINCT]";
            }
            print(indentLevel, "Select" + distinct);
            // visit children
            super.visitSelect(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitAllColumns(AllColumns node, Integer indent) {
            if (node.getPrefix().isPresent()) {
                print(indent, node.getPrefix() + ".*");
            } else {
                print(indent, "*");
            }
            return null;
        }

        @Override
        protected Void visitSingleColumn(SingleColumn node, Integer indent) {
            if (node.getAlias().isPresent()) {
                print(indent, "Alias: " + node.getAlias().get());
            }
            // visit children
            super.visitSingleColumn(node, indent + 1);
            return null;
        }

        @Override
        protected Void visitComparisonExpression(ComparisonExpression node, Integer indentLevel) {
            print(indentLevel, node.getType().toString());
            super.visitComparisonExpression(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitArithmeticBinary(ArithmeticBinaryExpression node, Integer indentLevel) {
            print(indentLevel, node.getType().toString());
            super.visitArithmeticBinary(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitLogicalBinaryExpression(LogicalBinaryExpression node, Integer indentLevel) {
            print(indentLevel, node.getType().toString());
            super.visitLogicalBinaryExpression(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitStringLiteral(StringLiteral node, Integer indentLevel) {
            print(indentLevel, "String[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitBinaryLiteral(BinaryLiteral node, Integer indentLevel) {
            print(indentLevel, "Binary[" + node.toHexString() + "]");
            return null;
        }

        @Override
        protected Void visitBooleanLiteral(BooleanLiteral node, Integer indentLevel) {
            print(indentLevel, "Boolean[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitLongLiteral(LongLiteral node, Integer indentLevel) {
            print(indentLevel, "Long[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitLikePredicate(LikePredicate node, Integer indentLevel) {
            print(indentLevel, "LIKE");
            super.visitLikePredicate(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitIdentifier(Identifier node, Integer indentLevel) {
            QualifiedName resolved = resolvedNameReferences.get(node);
            String resolvedName = "";
            if (resolved != null) {
                resolvedName = "=>" + resolved.toString();
            }
            print(indentLevel, "Identifier[" + node.getName() + resolvedName + "]");
            return null;
        }

        @Override
        protected Void visitDereferenceExpression(DereferenceExpression node, Integer indentLevel) {
            QualifiedName resolved = resolvedNameReferences.get(node);
            String resolvedName = "";
            if (resolved != null) {
                resolvedName = "=>" + resolved.toString();
            }
            print(indentLevel, "DereferenceExpression[" + node + resolvedName + "]");
            return null;
        }

        @Override
        protected Void visitFunctionCall(FunctionCall node, Integer indentLevel) {
            String name = Joiner.on('.').join(node.getName().getParts());
            print(indentLevel, "FunctionCall[" + name + "]");
            super.visitFunctionCall(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitTable(Table node, Integer indentLevel) {
            String name = Joiner.on('.').join(node.getName().getParts());
            print(indentLevel, "Table[" + name + "]");
            return null;
        }

        @Override
        protected Void visitValues(Values node, Integer indentLevel) {
            print(indentLevel, "Values");
            super.visitValues(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitRow(Row node, Integer indentLevel) {
            print(indentLevel, "Row");
            super.visitRow(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitAliasedRelation(AliasedRelation node, Integer indentLevel) {
            print(indentLevel, "Alias[" + node.getAlias() + "]");
            super.visitAliasedRelation(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitSampledRelation(SampledRelation node, Integer indentLevel) {
            print(indentLevel, "TABLESAMPLE[" + node.getType() + " (" + node.getSamplePercentage() + ")]");
            super.visitSampledRelation(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitTableSubquery(TableSubquery node, Integer indentLevel) {
            print(indentLevel, "SubQuery");
            super.visitTableSubquery(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitInPredicate(InPredicate node, Integer indentLevel) {
            print(indentLevel, "IN");
            super.visitInPredicate(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitSubqueryExpression(SubqueryExpression node, Integer indentLevel) {
            print(indentLevel, "SubQuery");
            super.visitSubqueryExpression(node, indentLevel + 1);
            return null;
        }
    };
    printer.process(root, 0);
}
Also used : ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SimpleGroupBy(com.facebook.presto.sql.tree.SimpleGroupBy) Set(java.util.Set) Query(com.facebook.presto.sql.tree.Query) Rollup(com.facebook.presto.sql.tree.Rollup) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) Node(com.facebook.presto.sql.tree.Node) Values(com.facebook.presto.sql.tree.Values) AllColumns(com.facebook.presto.sql.tree.AllColumns) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SortItem(com.facebook.presto.sql.tree.SortItem) Identifier(com.facebook.presto.sql.tree.Identifier) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) GroupingSets(com.facebook.presto.sql.tree.GroupingSets) OrderBy(com.facebook.presto.sql.tree.OrderBy) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Table(com.facebook.presto.sql.tree.Table) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) DefaultTraversalVisitor(com.facebook.presto.sql.tree.DefaultTraversalVisitor) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) LikePredicate(com.facebook.presto.sql.tree.LikePredicate) TableSubquery(com.facebook.presto.sql.tree.TableSubquery) InPredicate(com.facebook.presto.sql.tree.InPredicate) GroupingElement(com.facebook.presto.sql.tree.GroupingElement) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) BinaryLiteral(com.facebook.presto.sql.tree.BinaryLiteral) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) Cube(com.facebook.presto.sql.tree.Cube) Select(com.facebook.presto.sql.tree.Select) Row(com.facebook.presto.sql.tree.Row) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation)

Example 12 with LongLiteral

use of com.facebook.presto.sql.tree.LongLiteral in project presto by prestodb.

the class QueryRewriter method checksumSql.

private String checksumSql(List<Column> columns, QualifiedName table) throws SQLException {
    ImmutableList.Builder<SelectItem> selectItems = ImmutableList.builder();
    for (Column column : columns) {
        Expression expression = new Identifier(column.getName());
        if (column.isApproximateType()) {
            expression = new FunctionCall(QualifiedName.of("round"), ImmutableList.of(expression, new LongLiteral(Integer.toString(doublePrecision))));
        }
        selectItems.add(new SingleColumn(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(expression))));
    }
    Select select = new Select(false, selectItems.build());
    return formatSql(new QuerySpecification(select, Optional.of(new Table(table)), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), Optional.empty());
}
Also used : QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) Identifier(com.facebook.presto.sql.tree.Identifier) Table(com.facebook.presto.sql.tree.Table) DropTable(com.facebook.presto.sql.tree.DropTable) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) Expression(com.facebook.presto.sql.tree.Expression) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) ImmutableList(com.google.common.collect.ImmutableList) SelectItem(com.facebook.presto.sql.tree.SelectItem) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect) Select(com.facebook.presto.sql.tree.Select) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) SingleColumn(com.facebook.presto.sql.tree.SingleColumn)

Example 13 with LongLiteral

use of com.facebook.presto.sql.tree.LongLiteral 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 14 with LongLiteral

use of com.facebook.presto.sql.tree.LongLiteral in project presto by prestodb.

the class RemoveEmptyDelete method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof TableFinishNode)) {
        return Optional.empty();
    }
    TableFinishNode finish = (TableFinishNode) node;
    PlanNode finishSource = lookup.resolve(finish.getSource());
    if (!(finishSource instanceof ExchangeNode)) {
        return Optional.empty();
    }
    ExchangeNode exchange = (ExchangeNode) finishSource;
    if (exchange.getSources().size() != 1) {
        return Optional.empty();
    }
    PlanNode exchangeSource = lookup.resolve(getOnlyElement(exchange.getSources()));
    if (!(exchangeSource instanceof DeleteNode)) {
        return Optional.empty();
    }
    DeleteNode delete = (DeleteNode) exchangeSource;
    PlanNode deleteSource = lookup.resolve(delete.getSource());
    if (!(deleteSource instanceof ValuesNode)) {
        return Optional.empty();
    }
    ValuesNode values = (ValuesNode) deleteSource;
    if (!values.getRows().isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(new ValuesNode(node.getId(), node.getOutputSymbols(), ImmutableList.of(ImmutableList.of(new LongLiteral("0")))));
}
Also used : DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) ExchangeNode(com.facebook.presto.sql.planner.plan.ExchangeNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode)

Example 15 with LongLiteral

use of com.facebook.presto.sql.tree.LongLiteral in project presto by prestodb.

the class TestSqlToRowExpressionTranslator method testPossibleExponentialOptimizationTime.

@Test(timeOut = 10_000)
public void testPossibleExponentialOptimizationTime() {
    Expression expression = new LongLiteral("1");
    IdentityLinkedHashMap<Expression, Type> types = new IdentityLinkedHashMap<>();
    types.put(expression, BIGINT);
    for (int i = 0; i < 100; i++) {
        expression = new CoalesceExpression(expression);
        types.put(expression, BIGINT);
    }
    SqlToRowExpressionTranslator.translate(expression, SCALAR, types, FUNCTION_REGISTRY, TYPE_MANAGER, TEST_SESSION, true);
}
Also used : IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) Type(com.facebook.presto.spi.type.Type) Expression(com.facebook.presto.sql.tree.Expression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) Test(org.testng.annotations.Test)

Aggregations

LongLiteral (com.facebook.presto.sql.tree.LongLiteral)25 Test (org.testng.annotations.Test)20 StringLiteral (com.facebook.presto.sql.tree.StringLiteral)11 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)9 Query (com.facebook.presto.sql.tree.Query)7 QueryUtil.simpleQuery (com.facebook.presto.sql.QueryUtil.simpleQuery)6 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)6 Expression (com.facebook.presto.sql.tree.Expression)6 Identifier (com.facebook.presto.sql.tree.Identifier)6 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)6 WithQuery (com.facebook.presto.sql.tree.WithQuery)6 NotExpression (com.facebook.presto.sql.tree.NotExpression)5 AllColumns (com.facebook.presto.sql.tree.AllColumns)4 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)3 Cast (com.facebook.presto.sql.tree.Cast)3 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)3 DoubleLiteral (com.facebook.presto.sql.tree.DoubleLiteral)3 DropTable (com.facebook.presto.sql.tree.DropTable)3 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)3 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)3