Search in sources :

Example 1 with ClassGenerator

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

the class PartitionSenderRootExec method createClassInstances.

private List<Partitioner> createClassInstances(int actualPartitions) throws SchemaChangeException {
    // 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());
    if (collector.hasErrors()) {
        throw new SchemaChangeException(String.format("Failure while trying to materialize incoming schema.  Errors:\n %s.", collector.toErrorString()));
    }
    // 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);
    try {
        // compile and setup generated code
        List<Partitioner> subPartitioners = context.getImplementationClass(cg, actualPartitions);
        return subPartitioners;
    } catch (ClassTransformationException | IOException e) {
        throw new SchemaChangeException("Failure while attempting to load generated class", e);
    }
}
Also used : ClassTransformationException(org.apache.drill.exec.exception.ClassTransformationException) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) JExpression(com.sun.codemodel.JExpression) IOException(java.io.IOException) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) ClassGenerator(org.apache.drill.exec.expr.ClassGenerator)

Example 2 with ClassGenerator

use of org.apache.drill.exec.expr.ClassGenerator 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)

Example 3 with ClassGenerator

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

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
 */
protected void setupNewSchema(VectorAccessible batch) {
    container.clear();
    ErrorCollector collector = new ErrorCollectorImpl();
    List<TransferPair> transfers = Lists.newArrayList();
    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()) {
        LogicalExpression expr = ExpressionTreeMaterializer.materialize(od.getExpr(), batch, collector, context.getFunctionRegistry());
        collector.reportErrors(logger);
        cg.setMappingSet(incomingMapping);
        ClassGenerator.HoldingContainer left = cg.addExpr(expr, ClassGenerator.BlkCreateMode.FALSE);
        cg.setMappingSet(partitionMapping);
        TypedFieldId fieldId = new TypedFieldId.Builder().finalType(expr.getMajorType()).addId(count++).build();
        ClassGenerator.HoldingContainer right = cg.addExpr(new ValueVectorReadExpression(fieldId), 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());
    projector = context.getImplementationClass(cg);
    try {
        projector.setup(context, batch, this, transfers, partitionVectors, partitions, popConfig.getRef());
    } catch (SchemaChangeException e) {
        throw UserException.schemaChangeError(e).addContext("Unexpected schema change in the Ordered Partitioner").build(logger);
    }
}
Also used : TransferPair(org.apache.drill.exec.record.TransferPair) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) ValueVectorReadExpression(org.apache.drill.exec.expr.ValueVectorReadExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) ClassGenerator(org.apache.drill.exec.expr.ClassGenerator) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) 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 4 with ClassGenerator

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

the class WindowFrameRecordBatch method setupIsFunction.

/**
 * setup comparison functions isSamePartition and isPeer
 */
private void setupIsFunction(ClassGenerator<WindowFramer> cg, Iterable<LogicalExpression> exprs, MappingSet leftMapping, 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);
        LogicalExpression fh = FunctionGenerationHelper.getOrderingComparatorNullsHigh(first, second, context.getFunctionRegistry());
        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 5 with ClassGenerator

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

the class ValueVectorHashHelper method setupBuild64Hash.

private void setupBuild64Hash(ClassGenerator<Hash64> cg, MappingSet incomingMapping, VectorAccessible batch, LogicalExpression[] keyExprs, TypedFieldId[] toHashKeyFieldIds) throws SchemaChangeException {
    cg.setMappingSet(incomingMapping);
    if (keyExprs == null || keyExprs.length == 0) {
        cg.getEvalBlock()._return(JExpr.lit(0));
    }
    String seedValue = "seedValue";
    String fieldId = "fieldId";
    LogicalExpression seed = ValueExpressions.getParameterExpression(seedValue, Types.required(TypeProtos.MinorType.INT));
    LogicalExpression fieldIdParamExpr = ValueExpressions.getParameterExpression(fieldId, Types.required(TypeProtos.MinorType.INT));
    ClassGenerator.HoldingContainer fieldIdParamHolder = cg.addExpr(fieldIdParamExpr);
    int i = 0;
    for (LogicalExpression expr : keyExprs) {
        TypedFieldId targetTypeFieldId = toHashKeyFieldIds[i];
        ValueExpressions.IntExpression targetBuildFieldIdExp = new ValueExpressions.IntExpression(targetTypeFieldId.getFieldIds()[0], ExpressionPosition.UNKNOWN);
        JFieldRef targetBuildSideFieldId = cg.addExpr(targetBuildFieldIdExp, ClassGenerator.BlkCreateMode.TRUE_IF_BOUND).getValue();
        JBlock ifBlock = cg.getEvalBlock()._if(fieldIdParamHolder.getValue().eq(targetBuildSideFieldId))._then();
        cg.nestEvalBlock(ifBlock);
        LogicalExpression hashExpression = HashPrelUtil.getHash64Expression(expr, seed, true);
        LogicalExpression materializedExpr = ExpressionTreeMaterializer.materializeAndCheckErrors(hashExpression, batch, context.getFunctionRegistry());
        ClassGenerator.HoldingContainer hash = cg.addExpr(materializedExpr, ClassGenerator.BlkCreateMode.TRUE_IF_BOUND);
        ifBlock._return(hash.getValue());
        cg.unNestEvalBlock();
        i++;
    }
    cg.getEvalBlock()._return(JExpr.lit(0));
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) JFieldRef(com.sun.codemodel.JFieldRef) ClassGenerator(org.apache.drill.exec.expr.ClassGenerator) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) ValueExpressions(org.apache.drill.common.expression.ValueExpressions) JBlock(com.sun.codemodel.JBlock)

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