Search in sources :

Example 6 with ClassGenerator

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

the class WindowFrameRecordBatch method setupIsFunction.

/**
 * setup comparison functions isSamePartition and isPeer
 */
private void setupIsFunction(final ClassGenerator<WindowFramer> cg, final Iterable<LogicalExpression> exprs, final MappingSet leftMapping, final MappingSet rightMapping) {
    cg.setMappingSet(leftMapping);
    for (LogicalExpression expr : exprs) {
        if (expr == null) {
            continue;
        }
        cg.setMappingSet(leftMapping);
        ClassGenerator.HoldingContainer first = cg.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        cg.setMappingSet(rightMapping);
        ClassGenerator.HoldingContainer second = cg.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        final LogicalExpression fh = FunctionGenerationHelper.getOrderingComparatorNullsHigh(first, second, context.getFunctionRegistry());
        final ClassGenerator.HoldingContainer out = cg.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
        cg.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)))._then()._return(JExpr.FALSE);
    }
    cg.getEvalBlock()._return(JExpr.TRUE);
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ClassGenerator(org.apache.drill.exec.expr.ClassGenerator)

Example 7 with ClassGenerator

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

the class OrderedPartitionRecordBatch method setupNewSchema.

/**
 * Sets up projection that will transfer all of the columns in batch, and also populate the partition column based on
 * which partition a record falls into in the partition table
 *
 * @param batch
 * @throws SchemaChangeException
 */
protected void setupNewSchema(VectorAccessible batch) throws SchemaChangeException {
    container.clear();
    final ErrorCollector collector = new ErrorCollectorImpl();
    final List<TransferPair> transfers = Lists.newArrayList();
    final ClassGenerator<OrderedPartitionProjector> cg = CodeGenerator.getRoot(OrderedPartitionProjector.TEMPLATE_DEFINITION, context.getOptions());
    for (VectorWrapper<?> vw : batch) {
        TransferPair tp = vw.getValueVector().getTransferPair(oContext.getAllocator());
        transfers.add(tp);
        container.add(tp.getTo());
    }
    cg.setMappingSet(mainMapping);
    int count = 0;
    for (Ordering od : popConfig.getOrderings()) {
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), batch, collector, context.getFunctionRegistry());
        if (collector.hasErrors()) {
            throw new SchemaChangeException("Failure while materializing expression. " + collector.toErrorString());
        }
        cg.setMappingSet(incomingMapping);
        ClassGenerator.HoldingContainer left = cg.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        cg.setMappingSet(partitionMapping);
        ClassGenerator.HoldingContainer right = cg.addExpr(new ValueVectorReadExpression(new TypedFieldId(expr.getMajorType(), count++)), ClassGenerator.BlkCreateMode.FALSE);
        cg.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());
        ClassGenerator.HoldingContainer out = cg.addExpr(fh, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc = cg.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());
        }
    }
    cg.getEvalBlock()._return(JExpr.lit(0));
    container.add(this.partitionKeyVector);
    container.buildSchema(batch.getSchema().getSelectionVectorMode());
    try {
        this.projector = context.getImplementationClass(cg);
        projector.setup(context, batch, this, transfers, partitionVectors, partitions, popConfig.getRef());
    } catch (ClassTransformationException | IOException e) {
        throw new SchemaChangeException("Failure while attempting to load generated class", e);
    }
}
Also used : TransferPair(org.apache.drill.exec.record.TransferPair) ClassTransformationException(org.apache.drill.exec.exception.ClassTransformationException) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) IOException(java.io.IOException) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) ValueVectorReadExpression(org.apache.drill.exec.expr.ValueVectorReadExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) ClassGenerator(org.apache.drill.exec.expr.ClassGenerator) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) Ordering(org.apache.drill.common.logical.data.Order.Ordering) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) JConditional(com.sun.codemodel.JConditional)

Example 8 with ClassGenerator

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

the class MergeJoinBatch method generateDoCompare.

private void generateDoCompare(ClassGenerator<JoinWorker> cg, JVar incomingRecordBatch, LogicalExpression[] leftExpression, JVar incomingLeftRecordBatch, LogicalExpression[] rightExpression, JVar incomingRightRecordBatch, ErrorCollector collector) throws ClassTransformationException {
    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)

Example 9 with ClassGenerator

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

the class PartitionSenderRootExec method createClassInstances.

private List<Partitioner> createClassInstances(int actualPartitions) {
    // set up partitioning function
    final LogicalExpression expr = operator.getExpr();
    final ErrorCollector collector = new ErrorCollectorImpl();
    final ClassGenerator<Partitioner> cg;
    cg = CodeGenerator.getRoot(Partitioner.TEMPLATE_DEFINITION, context.getOptions());
    cg.getCodeGenerator().plainJavaCapable(true);
    // Uncomment out this line to debug the generated code.
    // cg.getCodeGenerator().saveCodeForDebugging(true);
    ClassGenerator<Partitioner> cgInner = cg.getInnerGenerator("OutgoingRecordBatch");
    final LogicalExpression materializedExpr = ExpressionTreeMaterializer.materialize(expr, incoming, collector, context.getFunctionRegistry());
    collector.reportErrors(logger);
    // generate code to copy from an incoming value vector to the destination partition's outgoing value vector
    JExpression bucket = JExpr.direct("bucket");
    // generate evaluate expression to determine the hash
    ClassGenerator.HoldingContainer exprHolder = cg.addExpr(materializedExpr);
    cg.getEvalBlock().decl(JType.parse(cg.getModel(), "int"), "bucket", exprHolder.getValue().mod(JExpr.lit(outGoingBatchCount)));
    cg.getEvalBlock()._return(cg.getModel().ref(Math.class).staticInvoke("abs").arg(bucket));
    CopyUtil.generateCopies(cgInner, incoming, incoming.getSchema().getSelectionVectorMode() == SelectionVectorMode.FOUR_BYTE);
    // compile and setup generated code
    List<Partitioner> subPartitioners = context.getImplementationClass(cg, actualPartitions);
    final int divisor = Math.max(1, outGoingBatchCount / actualPartitions);
    final int longTail = outGoingBatchCount % actualPartitions;
    int startIndex = 0;
    int endIndex = 0;
    boolean success = false;
    try {
        for (int i = 0; i < actualPartitions; i++) {
            startIndex = endIndex;
            endIndex = (i < actualPartitions - 1) ? startIndex + divisor : outGoingBatchCount;
            if (i < longTail) {
                endIndex++;
            }
            final OperatorStats partitionStats = new OperatorStats(stats, true);
            subPartitioners.get(i).setup(context, incoming, popConfig, partitionStats, oContext, cgInner, startIndex, endIndex);
        }
        partitioner = new PartitionerDecorator(subPartitioners, stats, context);
        for (int index = 0; index < terminations.size(); index++) {
            partitioner.getOutgoingBatches(terminations.buffer[index]).terminate();
        }
        terminations.clear();
        success = true;
    } catch (SchemaChangeException e) {
        throw AbstractRecordBatch.schemaChangeException(e, "Partition Sender", logger);
    } finally {
        if (!success) {
            for (Partitioner p : subPartitioners) {
                p.clear();
            }
        }
    }
    return subPartitioners;
}
Also used : ErrorCollector(org.apache.drill.common.expression.ErrorCollector) JExpression(com.sun.codemodel.JExpression) OperatorStats(org.apache.drill.exec.ops.OperatorStats) MinorFragmentEndpoint(org.apache.drill.exec.physical.MinorFragmentEndpoint) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ClassGenerator(org.apache.drill.exec.expr.ClassGenerator) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException)

Aggregations

LogicalExpression (org.apache.drill.common.expression.LogicalExpression)9 ClassGenerator (org.apache.drill.exec.expr.ClassGenerator)9 JConditional (com.sun.codemodel.JConditional)4 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)4 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)4 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)4 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)4 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)3 JExpression (com.sun.codemodel.JExpression)2 IOException (java.io.IOException)2 Ordering (org.apache.drill.common.logical.data.Order.Ordering)2 ClassTransformationException (org.apache.drill.exec.exception.ClassTransformationException)2 ValueVectorReadExpression (org.apache.drill.exec.expr.ValueVectorReadExpression)2 TransferPair (org.apache.drill.exec.record.TransferPair)2 JBlock (com.sun.codemodel.JBlock)1 JFieldRef (com.sun.codemodel.JFieldRef)1 ValueExpressions (org.apache.drill.common.expression.ValueExpressions)1 OperatorStats (org.apache.drill.exec.ops.OperatorStats)1 MinorFragmentEndpoint (org.apache.drill.exec.physical.MinorFragmentEndpoint)1