Search in sources :

Example 21 with HoldingContainer

use of org.apache.drill.exec.expr.ClassGenerator.HoldingContainer in project drill by axbaretto.

the class OperatorCodeGenerator method generateComparisons.

protected void generateComparisons(ClassGenerator<?> g, VectorAccessible batch) {
    g.setMappingSet(MAIN_MAPPING);
    for (Ordering od : popConfig.getOrderings()) {
        // first, we rewrite the evaluation stack for each side of the comparison.
        ErrorCollector collector = new ErrorCollectorImpl();
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), batch, collector, context.getFunctionRegistry());
        if (collector.hasErrors()) {
            throw UserException.unsupportedError().message("Failure while materializing expression. " + collector.toErrorString()).build(logger);
        }
        g.setMappingSet(LEFT_MAPPING);
        HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(RIGHT_MAPPING);
        HoldingContainer right = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(MAIN_MAPPING);
        // next we wrap the two comparison sides and add the expression block for the comparison.
        LogicalExpression fh = FunctionGenerationHelper.getOrderingComparator(od.nullsSortHigh(), left, right, context.getFunctionRegistry());
        HoldingContainer out = g.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
        if (od.getDirection() == Direction.ASCENDING) {
            jc._then()._return(out.getValue());
        } else {
            jc._then()._return(out.getValue().minus());
        }
        g.rotateBlock();
    }
    g.rotateBlock();
    g.getEvalBlock()._return(JExpr.lit(0));
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) Ordering(org.apache.drill.common.logical.data.Order.Ordering) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) JConditional(com.sun.codemodel.JConditional)

Example 22 with HoldingContainer

use of org.apache.drill.exec.expr.ClassGenerator.HoldingContainer in project drill by apache.

the class SortBatch method createNewSorter.

public static Sorter createNewSorter(FragmentContext context, List<Ordering> orderings, VectorAccessible batch, MappingSet mainMapping, MappingSet leftMapping, MappingSet rightMapping) {
    CodeGenerator<Sorter> cg = CodeGenerator.get(Sorter.TEMPLATE_DEFINITION, context.getOptions());
    // This operator may be deprecated. No tests exercise it.
    // There is no way, at present, to verify if the generated code
    // works with Plain-old Java.
    // cg.plainOldJavaCapable(true);
    // Uncomment out this line to debug the generated code.
    // cg.saveCodeForDebugging(true);
    ClassGenerator<Sorter> g = cg.getRoot();
    g.setMappingSet(mainMapping);
    for (Ordering od : orderings) {
        // first, we rewrite the evaluation stack for each side of the comparison.
        ErrorCollector collector = new ErrorCollectorImpl();
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), batch, collector, context.getFunctionRegistry());
        collector.reportErrors(logger);
        g.setMappingSet(leftMapping);
        HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(rightMapping);
        HoldingContainer right = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(mainMapping);
        // next we wrap the two comparison sides and add the expression block for the comparison.
        LogicalExpression fh = FunctionGenerationHelper.getOrderingComparator(od.nullsSortHigh(), left, right, context.getFunctionRegistry());
        HoldingContainer out = g.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
        if (od.getDirection() == Direction.ASCENDING) {
            jc._then()._return(out.getValue());
        } else {
            jc._then()._return(out.getValue().minus());
        }
    }
    g.getEvalBlock()._return(JExpr.lit(0));
    return context.getImplementationClass(cg);
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) Ordering(org.apache.drill.common.logical.data.Order.Ordering) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) JConditional(com.sun.codemodel.JConditional)

Example 23 with HoldingContainer

use of org.apache.drill.exec.expr.ClassGenerator.HoldingContainer in project drill by apache.

the class EvaluationVisitor method getPrevious.

/**
 * Get a HoldingContainer for the expression if it had been already evaluated
 */
private HoldingContainer getPrevious(LogicalExpression expression, MappingSet mappingSet) {
    ExpressionHolder holder = new ExpressionHolder(expression, mappingSet);
    HoldingContainer previous = null;
    for (Map<ExpressionHolder, HoldingContainer> m : mapStack) {
        previous = m.get(holder);
        if (previous != null) {
            break;
        }
    }
    if (previous == null) {
        previous = previousExpressions.get(holder);
    }
    if (previous != null) {
        logger.debug("Found previously evaluated expression: {}", ExpressionStringBuilder.toString(expression));
    }
    return previous;
}
Also used : HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)

Example 24 with HoldingContainer

use of org.apache.drill.exec.expr.ClassGenerator.HoldingContainer in project drill by apache.

the class MergeSortWrapper method createNewMSorter.

private MSorter createNewMSorter(List<Ordering> orderings, MappingSet mainMapping, MappingSet leftMapping, MappingSet rightMapping) {
    CodeGenerator<MSorter> cg = CodeGenerator.get(MSorter.TEMPLATE_DEFINITION, context.getFragmentContext().getOptions());
    cg.plainJavaCapable(true);
    // Uncomment out this line to debug the generated code.
    // cg.saveCodeForDebugging(true);
    ClassGenerator<MSorter> g = cg.getRoot();
    g.setMappingSet(mainMapping);
    for (Ordering od : orderings) {
        // first, we rewrite the evaluation stack for each side of the comparison.
        ErrorCollector collector = new ErrorCollectorImpl();
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), destContainer, collector, context.getFragmentContext().getFunctionRegistry());
        if (collector.hasErrors()) {
            throw UserException.unsupportedError().message("Failure while materializing expression. " + collector.toErrorString()).build(logger);
        }
        g.setMappingSet(leftMapping);
        HoldingContainer left = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(rightMapping);
        HoldingContainer right = g.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        g.setMappingSet(mainMapping);
        // next we wrap the two comparison sides and add the expression block for the comparison.
        LogicalExpression fh = FunctionGenerationHelper.getOrderingComparator(od.nullsSortHigh(), left, right, context.getFragmentContext().getFunctionRegistry());
        HoldingContainer out = g.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc = g.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
        if (od.getDirection() == Direction.ASCENDING) {
            jc._then()._return(out.getValue());
        } else {
            jc._then()._return(out.getValue().minus());
        }
        g.rotateBlock();
    }
    g.rotateBlock();
    g.getEvalBlock()._return(JExpr.lit(0));
    return getInstance(cg, logger);
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) Ordering(org.apache.drill.common.logical.data.Order.Ordering) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) JConditional(com.sun.codemodel.JConditional)

Example 25 with HoldingContainer

use of org.apache.drill.exec.expr.ClassGenerator.HoldingContainer in project drill by apache.

the class MergeJoinBatch method generateDoCompare.

private void generateDoCompare(ClassGenerator<JoinWorker> cg, JVar incomingRecordBatch, LogicalExpression[] leftExpression, JVar incomingLeftRecordBatch, LogicalExpression[] rightExpression, JVar incomingRightRecordBatch, ErrorCollector collector) {
    cg.setMappingSet(compareMapping);
    if (status.getRightStatus() != IterOutcome.NONE) {
        assert leftExpression.length == rightExpression.length;
        for (int i = 0; i < leftExpression.length; i++) {
            // generate compare()
            // //////////////////////
            cg.setMappingSet(compareMapping);
            cg.getSetupBlock().assign(JExpr._this().ref(incomingRecordBatch), JExpr._this().ref(incomingLeftRecordBatch));
            ClassGenerator.HoldingContainer compareLeftExprHolder = cg.addExpr(leftExpression[i], ClassGenerator.BlkCreateMode.FALSE);
            cg.setMappingSet(compareRightMapping);
            cg.getSetupBlock().assign(JExpr._this().ref(incomingRecordBatch), JExpr._this().ref(incomingRightRecordBatch));
            ClassGenerator.HoldingContainer compareRightExprHolder = cg.addExpr(rightExpression[i], ClassGenerator.BlkCreateMode.FALSE);
            LogicalExpression fh = FunctionGenerationHelper.getOrderingComparatorNullsHigh(compareLeftExprHolder, compareRightExprHolder, context.getFunctionRegistry());
            HoldingContainer out = cg.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
            // Null compares to Null should returns null (unknown). In such case, we return 1 to indicate they are not equal.
            if (compareLeftExprHolder.isOptional() && compareRightExprHolder.isOptional() && comparators.get(i) == Comparator.EQUALS) {
                JConditional jc = cg.getEvalBlock()._if(compareLeftExprHolder.getIsSet().eq(JExpr.lit(0)).cand(compareRightExprHolder.getIsSet().eq(JExpr.lit(0))));
                jc._then()._return(JExpr.lit(1));
                jc._elseif(out.getValue().ne(JExpr.lit(0)))._then()._return(out.getValue());
            } else {
                cg.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)))._then()._return(out.getValue());
            }
        }
    }
    // Pass the equality check for all the join conditions. Finally, return 0.
    cg.getEvalBlock()._return(JExpr.lit(0));
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ClassGenerator(org.apache.drill.exec.expr.ClassGenerator) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) JConditional(com.sun.codemodel.JConditional)

Aggregations

HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)58 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)40 JConditional (com.sun.codemodel.JConditional)29 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)23 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)23 Ordering (org.apache.drill.common.logical.data.Order.Ordering)21 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)14 JVar (com.sun.codemodel.JVar)12 JBlock (com.sun.codemodel.JBlock)9 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)9 HoldingContainerExpression (org.apache.drill.exec.expr.HoldingContainerExpression)7 ValueVectorWriteExpression (org.apache.drill.exec.expr.ValueVectorWriteExpression)6 FunctionCall (org.apache.drill.common.expression.FunctionCall)5 ValueVectorReadExpression (org.apache.drill.exec.expr.ValueVectorReadExpression)5 JExpression (com.sun.codemodel.JExpression)4 MaterializedField (org.apache.drill.exec.record.MaterializedField)4 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)4 ValueVector (org.apache.drill.exec.vector.ValueVector)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3