Search in sources :

Example 11 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project boostkit-bigdata by kunpengcompute.

the class DataIoAdapter method reverseExpressionTree.

private RowExpression reverseExpressionTree(Expression filterExpression) {
    RowExpression resRowExpression = null;
    if (filterExpression == null) {
        return resRowExpression;
    }
    List<RowExpression> tempRowExpression = new ArrayList<>();
    if (filterExpression instanceof Or) {
        RowExpression a1 = getExpression(((Or) filterExpression).left());
        RowExpression a2 = getExpression(((Or) filterExpression).right());
        tempRowExpression.add(a1);
        tempRowExpression.add(a2);
        resRowExpression = new SpecialForm(SpecialForm.Form.valueOf("OR"), BOOLEAN, tempRowExpression);
    } else if (filterExpression instanceof And) {
        RowExpression a1 = getExpression(((And) filterExpression).left());
        RowExpression a2 = getExpression(((And) filterExpression).right());
        tempRowExpression.add(a2);
        tempRowExpression.add(a1);
        resRowExpression = new SpecialForm(SpecialForm.Form.valueOf("AND"), BOOLEAN, tempRowExpression);
    } else {
        resRowExpression = getExpression(filterExpression);
    }
    return resRowExpression;
}
Also used : Or(org.apache.spark.sql.catalyst.expressions.Or) And(org.apache.spark.sql.catalyst.expressions.And) ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Example 12 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project boostkit-bigdata by kunpengcompute.

the class OmniRowExpressionUtil method getWhenExpr.

/**
 * Generates a RowExpression for when-then expression special case
 *
 * @param staticExpr when-then expression
 * @param translatedExpr RowExpression from staticFilter conversion
 * @return new Optional<RowExpression> checked for possible like expression
 * nested within
 * note: refer to
 * SqlToRowExpressionTranslator.visitSearchedCaseExpression to see how
 * when-then expressions
 * are being translated. This method involves some assumptions so some
 * comments have been provided
 */
private static Optional<RowExpression> getWhenExpr(SearchedCaseExpression staticExpr, SpecialForm translatedExpr) {
    // generates a list of the expr involved in the nested when-then expression and
    // sends it to helper
    List<Expression> exprList = new ArrayList<Expression>();
    for (WhenClause clause : staticExpr.getWhenClauses()) {
        exprList.add(clause.getOperand());
        exprList.add(clause.getResult());
    }
    boolean defaultValueExist = (((SearchedCaseExpression) staticExpr).getDefaultValue()).isPresent();
    exprList.add(defaultValueExist ? (((SearchedCaseExpression) staticExpr).getDefaultValue()).get() : null);
    List<RowExpression> newArguments = new ArrayList<RowExpression>();
    LinkedList<Type> typeList = new LinkedList<Type>();
    SpecialForm originalExpr = translatedExpr;
    int i = 0;
    // expressions
    while (true) {
        typeList.addFirst(translatedExpr.getType());
        // layer.
        if (i >= exprList.size() - 3) {
            for (int j = 0; j < translatedExpr.getArguments().size(); i++, j++) {
                newArguments.add((generateOmniExpr(exprList.get(i), translatedExpr.getArguments().get(j))).get());
            }
            break;
        } else {
            for (int j = 0; j < translatedExpr.getArguments().size() - 1; i++, j++) {
                newArguments.add((generateOmniExpr(exprList.get(i), translatedExpr.getArguments().get(j))).get());
            }
            translatedExpr = (SpecialForm) translatedExpr.getArguments().get(2);
        }
    }
    // Recreating the SpecialFrom RowExpression in the same recursive format has it
    // was done in
    // SqlToRowExpressionTranslator.visitSearchedCaseExpression
    Collections.reverse(newArguments);
    RowExpression expression = new SpecialForm(originalExpr.getForm(), typeList.removeFirst(), newArguments.get(2), newArguments.get(1), newArguments.get(0));
    for (int j = 3; j < newArguments.size(); j += 2) {
        expression = new SpecialForm(originalExpr.getForm(), typeList.removeFirst(), newArguments.get(j + 1), newArguments.get(j), expression);
    }
    return Optional.of(expression);
}
Also used : ArrayList(java.util.ArrayList) RowExpression(io.prestosql.spi.relation.RowExpression) LinkedList(java.util.LinkedList) WhenClause(io.prestosql.sql.tree.WhenClause) UnknownType(io.prestosql.spi.type.UnknownType) CharType(io.prestosql.spi.type.CharType) DecimalType(io.prestosql.spi.type.DecimalType) OperatorUtils.toDataType(nova.hetu.olk.tool.OperatorUtils.toDataType) Type(io.prestosql.spi.type.Type) VarcharType(io.prestosql.spi.type.VarcharType) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) CallExpression(io.prestosql.spi.relation.CallExpression) SearchedCaseExpression(io.prestosql.sql.tree.SearchedCaseExpression) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression) InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Example 13 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class PushAggregationThroughOuterJoin method coalesceWithNullAggregation.

// When the aggregation is done after the join, there will be a null value that gets aggregated over
// where rows did not exist in the inner table.  For some aggregate functions, such as count, the result
// of an aggregation over a single null row is one or zero rather than null. In order to ensure correct results,
// we add a coalesce function with the output of the new outer join and the agggregation performed over a single
// null row.
private Optional<PlanNode> coalesceWithNullAggregation(AggregationNode aggregationNode, PlanNode outerJoin, PlanSymbolAllocator planSymbolAllocator, PlanNodeIdAllocator idAllocator, Lookup lookup) {
    // Create an aggregation node over a row of nulls.
    Optional<MappedAggregationInfo> aggregationOverNullInfoResultNode = createAggregationOverNull(aggregationNode, planSymbolAllocator, idAllocator, lookup);
    if (!aggregationOverNullInfoResultNode.isPresent()) {
        return Optional.empty();
    }
    MappedAggregationInfo aggregationOverNullInfo = aggregationOverNullInfoResultNode.get();
    AggregationNode aggregationOverNull = aggregationOverNullInfo.getAggregation();
    Map<Symbol, Symbol> sourceAggregationToOverNullMapping = aggregationOverNullInfo.getSymbolMapping();
    // Do a cross join with the aggregation over null
    JoinNode crossJoin = new JoinNode(idAllocator.getNextId(), JoinNode.Type.INNER, outerJoin, aggregationOverNull, ImmutableList.of(), ImmutableList.<Symbol>builder().addAll(outerJoin.getOutputSymbols()).addAll(aggregationOverNull.getOutputSymbols()).build(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableMap.of());
    // Add coalesce expressions for all aggregation functions
    Assignments.Builder assignmentsBuilder = Assignments.builder();
    for (Symbol symbol : outerJoin.getOutputSymbols()) {
        if (aggregationNode.getAggregations().containsKey(symbol)) {
            assignmentsBuilder.put(symbol, new SpecialForm(COALESCE, planSymbolAllocator.getTypes().get(symbol), ImmutableList.of(toVariableReference(symbol, planSymbolAllocator.getTypes()), toVariableReference(sourceAggregationToOverNullMapping.get(symbol), planSymbolAllocator.getTypes()))));
        } else {
            assignmentsBuilder.put(symbol, toVariableReference(symbol, planSymbolAllocator.getTypes()));
        }
    }
    return Optional.of(new ProjectNode(idAllocator.getNextId(), crossJoin, assignmentsBuilder.build()));
}
Also used : Symbol(io.prestosql.spi.plan.Symbol) JoinNode(io.prestosql.spi.plan.JoinNode) Assignments(io.prestosql.spi.plan.Assignments) AggregationNode(io.prestosql.spi.plan.AggregationNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Example 14 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class SplitFiltering method isSupportedExpression.

private static boolean isSupportedExpression(RowExpression predicate) {
    if (predicate instanceof SpecialForm) {
        SpecialForm specialForm = (SpecialForm) predicate;
        switch(specialForm.getForm()) {
            case BETWEEN:
            case IN:
                return true;
            case AND:
            case OR:
                return isSupportedExpression(specialForm.getArguments().get(0)) && isSupportedExpression(specialForm.getArguments().get(1));
            default:
                return false;
        }
    }
    if (predicate instanceof CallExpression) {
        CallExpression call = (CallExpression) predicate;
        FunctionHandle builtInFunctionHandle = call.getFunctionHandle();
        if (builtInFunctionHandle instanceof BuiltInFunctionHandle) {
            Signature signature = ((BuiltInFunctionHandle) builtInFunctionHandle).getSignature();
            if (signature.getName().getObjectName().equals("not")) {
                return true;
            }
            try {
                OperatorType operatorType = Signature.unmangleOperator(signature.getName().getObjectName());
                if (operatorType.isComparisonOperator() && operatorType != IS_DISTINCT_FROM) {
                    return true;
                }
                return false;
            } catch (IllegalArgumentException e) {
                return false;
            }
        }
    }
    return false;
}
Also used : Signature(io.prestosql.spi.function.Signature) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) FunctionHandle(io.prestosql.spi.function.FunctionHandle) SpecialForm(io.prestosql.spi.relation.SpecialForm) OperatorType(io.prestosql.spi.function.OperatorType)

Example 15 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class SplitFiltering method getAllColumns.

public static void getAllColumns(RowExpression expression, Set<String> columns, Map<Symbol, ColumnHandle> assignments) {
    if (expression instanceof SpecialForm) {
        SpecialForm specialForm = (SpecialForm) expression;
        RowExpression left;
        switch(specialForm.getForm()) {
            case BETWEEN:
            case IN:
                left = extractExpression(specialForm.getArguments().get(0));
                break;
            case AND:
            case OR:
                getAllColumns(specialForm.getArguments().get(0), columns, assignments);
                getAllColumns(specialForm.getArguments().get(1), columns, assignments);
                return;
            default:
                return;
        }
        if (!(left instanceof VariableReferenceExpression)) {
            LOG.warn("Invalid Left of expression %s, should be an VariableReferenceExpression", left.toString());
            return;
        }
        String columnName = ((VariableReferenceExpression) left).getName();
        Symbol columnSymbol = new Symbol(columnName);
        if (assignments.containsKey(columnSymbol)) {
            columnName = assignments.get(columnSymbol).getColumnName();
        }
        columns.add(columnName);
        return;
    }
    if (expression instanceof CallExpression) {
        CallExpression call = (CallExpression) expression;
        try {
            FunctionHandle builtInFunctionHandle = call.getFunctionHandle();
            Signature signature;
            if (builtInFunctionHandle instanceof BuiltInFunctionHandle) {
                signature = ((BuiltInFunctionHandle) builtInFunctionHandle).getSignature();
            } else {
                return;
            }
            OperatorType operatorType = Signature.unmangleOperator(signature.getName().getObjectName());
            if (!operatorType.isComparisonOperator()) {
                return;
            }
            RowExpression left = extractExpression(call.getArguments().get(0));
            if (!(left instanceof VariableReferenceExpression)) {
                LOG.warn("Invalid Left of expression %s, should be an VariableReferenceExpression", left.toString());
                return;
            }
            String columnName = ((VariableReferenceExpression) left).getName();
            Symbol columnSymbol = new Symbol(columnName);
            if (assignments.containsKey(columnSymbol)) {
                columnName = assignments.get(columnSymbol).getColumnName();
            }
            columns.add(columnName);
            return;
        } catch (IllegalArgumentException e) {
            return;
        }
    }
    return;
}
Also used : VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) SerializationUtils.deserializeStripeSymbol(io.prestosql.spi.heuristicindex.SerializationUtils.deserializeStripeSymbol) Symbol(io.prestosql.spi.plan.Symbol) Signature(io.prestosql.spi.function.Signature) RowExpression(io.prestosql.spi.relation.RowExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) CallExpression(io.prestosql.spi.relation.CallExpression) BuiltInFunctionHandle(io.prestosql.spi.function.BuiltInFunctionHandle) FunctionHandle(io.prestosql.spi.function.FunctionHandle) SpecialForm(io.prestosql.spi.relation.SpecialForm) OperatorType(io.prestosql.spi.function.OperatorType)

Aggregations

SpecialForm (io.prestosql.spi.relation.SpecialForm)27 RowExpression (io.prestosql.spi.relation.RowExpression)20 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)14 CallExpression (io.prestosql.spi.relation.CallExpression)12 ConstantExpression (io.prestosql.spi.relation.ConstantExpression)12 ArrayList (java.util.ArrayList)8 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)6 InputReferenceExpression (io.prestosql.spi.relation.InputReferenceExpression)6 Test (org.testng.annotations.Test)6 Signature (io.prestosql.spi.function.Signature)5 LambdaDefinitionExpression (io.prestosql.spi.relation.LambdaDefinitionExpression)5 OperatorType (io.prestosql.spi.function.OperatorType)4 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)3 Scope (io.airlift.bytecode.Scope)3 Variable (io.airlift.bytecode.Variable)3 FunctionHandle (io.prestosql.spi.function.FunctionHandle)3 IndexMetadata (io.prestosql.spi.heuristicindex.IndexMetadata)3 Type (io.prestosql.spi.type.Type)3 IfStatement (io.airlift.bytecode.control.IfStatement)2 LabelNode (io.airlift.bytecode.instruction.LabelNode)2