Search in sources :

Example 1 with CoalesceExpression

use of io.prestosql.sql.tree.CoalesceExpression in project hetu-core by openlookeng.

the class ImpalaAstBuilder method visitFunctionCall.

@Override
public Node visitFunctionCall(ImpalaSqlParser.FunctionCallContext context) {
    Optional<Expression> filter = visitIfPresent(context.filter(), Expression.class);
    Optional<Window> window = visitIfPresent(context.over(), Window.class);
    Optional<OrderBy> orderBy = Optional.empty();
    if (context.ORDER() != null) {
        orderBy = Optional.of(new OrderBy(visit(context.sortItem(), SortItem.class)));
    }
    QualifiedName name = getQualifiedName(context.qualifiedName());
    boolean distinct = isDistinct(context.setQuantifier());
    if (name.toString().equalsIgnoreCase("if")) {
        check(context.expression().size() == 2 || context.expression().size() == 3, "Invalid number of arguments for 'if' function", context);
        check(!window.isPresent(), "OVER clause not valid for 'if' function", context);
        check(!distinct, "DISTINCT not valid for 'if' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'if' function", context);
        Expression elseExpression = null;
        if (context.expression().size() == 3) {
            elseExpression = (Expression) visit(context.expression(2));
        }
        return new IfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)), elseExpression);
    }
    if (name.toString().equalsIgnoreCase("nullif")) {
        check(context.expression().size() == 2, "Invalid number of arguments for 'nullif' function", context);
        check(!window.isPresent(), "OVER clause not valid for 'nullif' function", context);
        check(!distinct, "DISTINCT not valid for 'nullif' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'nullif' function", context);
        return new NullIfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)));
    }
    if (name.toString().equalsIgnoreCase("coalesce")) {
        check(context.expression().size() >= 2, "The 'coalesce' function must have at least two arguments", context);
        check(!window.isPresent(), "OVER clause not valid for 'coalesce' function", context);
        check(!distinct, "DISTINCT not valid for 'coalesce' function", context);
        check(!filter.isPresent(), "FILTER not valid for 'coalesce' function", context);
        return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
    }
    return new FunctionCall(Optional.of(getLocation(context)), getQualifiedName(context.qualifiedName()), window, filter, orderBy, distinct, false, visit(context.expression(), Expression.class));
}
Also used : Window(io.prestosql.sql.tree.Window) OrderBy(io.prestosql.sql.tree.OrderBy) IfExpression(io.prestosql.sql.tree.IfExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) QualifiedName(io.prestosql.sql.tree.QualifiedName) SortItem(io.prestosql.sql.tree.SortItem) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LambdaExpression(io.prestosql.sql.tree.LambdaExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) FunctionCall(io.prestosql.sql.tree.FunctionCall) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression)

Example 2 with CoalesceExpression

use of io.prestosql.sql.tree.CoalesceExpression in project hetu-core by openlookeng.

the class TestSqlToRowExpressionTranslator method testPossibleExponentialOptimizationTime.

@Test(timeOut = 10_000)
public void testPossibleExponentialOptimizationTime() {
    Expression expression = new LongLiteral("1");
    ImmutableMap.Builder<NodeRef<Expression>, Type> types = ImmutableMap.builder();
    types.put(NodeRef.of(expression), BIGINT);
    for (int i = 0; i < 100; i++) {
        expression = new CoalesceExpression(expression, new LongLiteral("2"));
        types.put(NodeRef.of(expression), BIGINT);
    }
    translateAndOptimize(expression, types.build());
}
Also used : NodeRef(io.prestosql.sql.tree.NodeRef) Type(io.prestosql.spi.type.Type) DecimalType.createDecimalType(io.prestosql.spi.type.DecimalType.createDecimalType) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) LongLiteral(io.prestosql.sql.tree.LongLiteral) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.testng.annotations.Test)

Example 3 with CoalesceExpression

use of io.prestosql.sql.tree.CoalesceExpression in project hetu-core by openlookeng.

the class TransformExistsApplyToLateralNode method rewriteToNonDefaultAggregation.

private Optional<PlanNode> rewriteToNonDefaultAggregation(ApplyNode applyNode, Context context) {
    checkState(applyNode.getSubquery().getOutputSymbols().isEmpty(), "Expected subquery output symbols to be pruned");
    Symbol exists = getOnlyElement(applyNode.getSubqueryAssignments().getSymbols());
    Symbol subqueryTrue = context.getSymbolAllocator().newSymbol("subqueryTrue", BOOLEAN);
    Assignments.Builder assignments = Assignments.builder();
    assignments.putAll(AssignmentUtils.identityAsSymbolReferences(applyNode.getInput().getOutputSymbols()));
    assignments.put(exists, castToRowExpression(new CoalesceExpression(ImmutableList.of(toSymbolReference(subqueryTrue), BooleanLiteral.FALSE_LITERAL))));
    PlanNode subquery = new ProjectNode(context.getIdAllocator().getNextId(), new LimitNode(context.getIdAllocator().getNextId(), applyNode.getSubquery(), 1L, false), Assignments.of(subqueryTrue, castToRowExpression(TRUE_LITERAL)));
    PlanNodeDecorrelator decorrelator = new PlanNodeDecorrelator(context.getIdAllocator(), context.getLookup());
    if (!decorrelator.decorrelateFilters(subquery, applyNode.getCorrelation()).isPresent()) {
        return Optional.empty();
    }
    return Optional.of(new ProjectNode(context.getIdAllocator().getNextId(), new LateralJoinNode(applyNode.getId(), applyNode.getInput(), subquery, applyNode.getCorrelation(), LEFT, TRUE_LITERAL, applyNode.getOriginSubquery()), assignments.build()));
}
Also used : PlanNodeDecorrelator(io.prestosql.sql.planner.optimizations.PlanNodeDecorrelator) PlanNode(io.prestosql.spi.plan.PlanNode) LateralJoinNode(io.prestosql.sql.planner.plan.LateralJoinNode) LimitNode(io.prestosql.spi.plan.LimitNode) Symbol(io.prestosql.spi.plan.Symbol) Assignments(io.prestosql.spi.plan.Assignments) ProjectNode(io.prestosql.spi.plan.ProjectNode) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression)

Example 4 with CoalesceExpression

use of io.prestosql.sql.tree.CoalesceExpression in project hetu-core by openlookeng.

the class RelationPlanner method planJoinUsing.

private RelationPlan planJoinUsing(Join node, RelationPlan left, RelationPlan right) {
    /* Given: l JOIN r USING (k1, ..., kn)

           produces:

            - project
                    coalesce(l.k1, r.k1)
                    ...,
                    coalesce(l.kn, r.kn)
                    l.v1,
                    ...,
                    l.vn,
                    r.v1,
                    ...,
                    r.vn
              - join (l.k1 = r.k1 and ... l.kn = r.kn)
                    - project
                        cast(l.k1 as commonType(l.k1, r.k1))
                        ...
                    - project
                        cast(rl.k1 as commonType(l.k1, r.k1))

            If casts are redundant (due to column type and common type being equal),
            they will be removed by optimization passes.
        */
    List<Identifier> joinColumns = ((JoinUsing) node.getCriteria().get()).getColumns();
    Analysis.JoinUsingAnalysis joinAnalysis = analysis.getJoinUsing(node);
    ImmutableList.Builder<JoinNode.EquiJoinClause> clauses = ImmutableList.builder();
    Map<Identifier, Symbol> leftJoinColumns = new HashMap<>();
    Map<Identifier, Symbol> rightJoinColumns = new HashMap<>();
    Assignments.Builder leftCoercions = Assignments.builder();
    Assignments.Builder rightCoercions = Assignments.builder();
    leftCoercions.putAll(AssignmentUtils.identityAsSymbolReferences(left.getRoot().getOutputSymbols()));
    rightCoercions.putAll(AssignmentUtils.identityAsSymbolReferences(right.getRoot().getOutputSymbols()));
    for (int i = 0; i < joinColumns.size(); i++) {
        Identifier identifier = joinColumns.get(i);
        Type type = analysis.getType(identifier);
        // compute the coercion for the field on the left to the common supertype of left & right
        Symbol leftOutput = planSymbolAllocator.newSymbol(identifier, type);
        int leftField = joinAnalysis.getLeftJoinFields().get(i);
        leftCoercions.put(leftOutput, castToRowExpression(new Cast(toSymbolReference(left.getSymbol(leftField)), type.getTypeSignature().toString(), false, typeCoercion.isTypeOnlyCoercion(left.getDescriptor().getFieldByIndex(leftField).getType(), type))));
        leftJoinColumns.put(identifier, leftOutput);
        // compute the coercion for the field on the right to the common supertype of left & right
        Symbol rightOutput = planSymbolAllocator.newSymbol(identifier, type);
        int rightField = joinAnalysis.getRightJoinFields().get(i);
        rightCoercions.put(rightOutput, castToRowExpression(new Cast(toSymbolReference(right.getSymbol(rightField)), type.getTypeSignature().toString(), false, typeCoercion.isTypeOnlyCoercion(right.getDescriptor().getFieldByIndex(rightField).getType(), type))));
        rightJoinColumns.put(identifier, rightOutput);
        clauses.add(new JoinNode.EquiJoinClause(leftOutput, rightOutput));
    }
    ProjectNode leftCoercion = new ProjectNode(idAllocator.getNextId(), left.getRoot(), leftCoercions.build());
    ProjectNode rightCoercion = new ProjectNode(idAllocator.getNextId(), right.getRoot(), rightCoercions.build());
    JoinNode join = new JoinNode(idAllocator.getNextId(), JoinNodeUtils.typeConvert(node.getType()), leftCoercion, rightCoercion, clauses.build(), ImmutableList.<Symbol>builder().addAll(leftCoercion.getOutputSymbols()).addAll(rightCoercion.getOutputSymbols()).build(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of());
    // Add a projection to produce the outputs of the columns in the USING clause,
    // which are defined as coalesce(l.k, r.k)
    Assignments.Builder assignments = Assignments.builder();
    ImmutableList.Builder<Symbol> outputs = ImmutableList.builder();
    for (Identifier column : joinColumns) {
        Symbol output = planSymbolAllocator.newSymbol(column, analysis.getType(column));
        outputs.add(output);
        assignments.put(output, castToRowExpression(new CoalesceExpression(toSymbolReference(leftJoinColumns.get(column)), toSymbolReference(rightJoinColumns.get(column)))));
    }
    for (int field : joinAnalysis.getOtherLeftFields()) {
        Symbol symbol = left.getFieldMappings().get(field);
        outputs.add(symbol);
        assignments.put(symbol, castToRowExpression(toSymbolReference(symbol)));
    }
    for (int field : joinAnalysis.getOtherRightFields()) {
        Symbol symbol = right.getFieldMappings().get(field);
        outputs.add(symbol);
        assignments.put(symbol, castToRowExpression(toSymbolReference(symbol)));
    }
    return new RelationPlan(new ProjectNode(idAllocator.getNextId(), join, assignments.build()), analysis.getScope(node), outputs.build());
}
Also used : Cast(io.prestosql.sql.tree.Cast) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Symbol(io.prestosql.spi.plan.Symbol) LateralJoinNode(io.prestosql.sql.planner.plan.LateralJoinNode) JoinNode(io.prestosql.spi.plan.JoinNode) Assignments(io.prestosql.spi.plan.Assignments) JoinUsing(io.prestosql.sql.tree.JoinUsing) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) RelationType(io.prestosql.sql.analyzer.RelationType) Identifier(io.prestosql.sql.tree.Identifier) Analysis(io.prestosql.sql.analyzer.Analysis) ProjectNode(io.prestosql.spi.plan.ProjectNode) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression)

Example 5 with CoalesceExpression

use of io.prestosql.sql.tree.CoalesceExpression in project hetu-core by openlookeng.

the class HiveAstBuilder method visitFunctionCall.

@Override
public Node visitFunctionCall(HiveSqlParser.FunctionCallContext context) {
    Optional<Expression> filter = Optional.empty();
    Optional<OrderBy> orderBy = Optional.empty();
    Optional<Window> window = visitIfPresent(context.over(), Window.class);
    boolean distinct = isDistinct(context.setQuantifier());
    QualifiedName name = getQualifiedName(context.qualifiedName());
    if (name.toString().equalsIgnoreCase("if")) {
        check(context.expression().size() == 3, "Illegal arguments for 'if' function", context);
        check(!window.isPresent(), "OVER not valid for 'if' function", context);
        check(!distinct, "DISTINCT not valid for 'if' function", context);
        Expression elseExpression = (Expression) visit(context.expression(2));
        return new IfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)), elseExpression);
    }
    if (name.toString().equalsIgnoreCase("nullif")) {
        check(context.expression().size() == 2, "Illegal arguments for 'nullif' function", context);
        check(!window.isPresent(), "OVER not valid for 'nullif' function", context);
        check(!distinct, "DISTINCT not valid for 'nullif' function", context);
        return new NullIfExpression(getLocation(context), (Expression) visit(context.expression(0)), (Expression) visit(context.expression(1)));
    }
    if (name.toString().equalsIgnoreCase("coalesce")) {
        check(context.expression().size() > 0, "The 'coalesce' function must have at least one argument", context);
        check(!window.isPresent(), "OVER not valid for 'coalesce' function", context);
        check(!distinct, "DISTINCT not valid for 'coalesce' function", context);
        return new CoalesceExpression(getLocation(context), visit(context.expression(), Expression.class));
    }
    return new FunctionCall(Optional.of(getLocation(context)), getQualifiedName(context.qualifiedName()), window, filter, orderBy, distinct, false, visit(context.expression(), Expression.class));
}
Also used : OrderBy(io.prestosql.sql.tree.OrderBy) Window(io.prestosql.sql.tree.Window) IfExpression(io.prestosql.sql.tree.IfExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) ArithmeticUnaryExpression(io.prestosql.sql.tree.ArithmeticUnaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) NotExpression(io.prestosql.sql.tree.NotExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) SimpleCaseExpression(io.prestosql.sql.tree.SimpleCaseExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) IfExpression(io.prestosql.sql.tree.IfExpression) InListExpression(io.prestosql.sql.tree.InListExpression) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) SubscriptExpression(io.prestosql.sql.tree.SubscriptExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) QualifiedName(io.prestosql.sql.tree.QualifiedName) NullIfExpression(io.prestosql.sql.tree.NullIfExpression) FunctionCall(io.prestosql.sql.tree.FunctionCall) CoalesceExpression(io.prestosql.sql.tree.CoalesceExpression)

Aggregations

CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)8 Expression (io.prestosql.sql.tree.Expression)5 Symbol (io.prestosql.spi.plan.Symbol)3 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)3 ArithmeticUnaryExpression (io.prestosql.sql.tree.ArithmeticUnaryExpression)3 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)3 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)3 FunctionCall (io.prestosql.sql.tree.FunctionCall)3 IfExpression (io.prestosql.sql.tree.IfExpression)3 InListExpression (io.prestosql.sql.tree.InListExpression)3 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)3 LongLiteral (io.prestosql.sql.tree.LongLiteral)3 NotExpression (io.prestosql.sql.tree.NotExpression)3 NullIfExpression (io.prestosql.sql.tree.NullIfExpression)3 OrderBy (io.prestosql.sql.tree.OrderBy)3 QualifiedName (io.prestosql.sql.tree.QualifiedName)3 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)3 SearchedCaseExpression (io.prestosql.sql.tree.SearchedCaseExpression)3 SimpleCaseExpression (io.prestosql.sql.tree.SimpleCaseExpression)3 SubqueryExpression (io.prestosql.sql.tree.SubqueryExpression)3