Search in sources :

Example 31 with HoldingContainer

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

the class FunctionGenerationHelper method getFunctionExpression.

private static LogicalExpression getFunctionExpression(String name, HoldingContainer... args) {
    List<MajorType> argTypes = new ArrayList<MajorType>(args.length);
    List<LogicalExpression> argExpressions = new ArrayList<LogicalExpression>(args.length);
    for (HoldingContainer c : args) {
        argTypes.add(c.getMajorType());
        argExpressions.add(new HoldingContainerExpression(c));
    }
    return new FunctionCall(name, argExpressions, ExpressionPosition.UNKNOWN);
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) ArrayList(java.util.ArrayList) FunctionCall(org.apache.drill.common.expression.FunctionCall) HoldingContainerExpression(org.apache.drill.exec.expr.HoldingContainerExpression)

Example 32 with HoldingContainer

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

the class DrillAggFuncHolder method renderEnd.

@Override
public HoldingContainer renderEnd(ClassGenerator<?> classGenerator, HoldingContainer[] inputVariables, JVar[] workspaceJVars, FunctionHolderExpression holderExpr) {
    HoldingContainer out = null;
    JVar internalOutput = null;
    if (getReturnType().getMinorType() != TypeProtos.MinorType.LATE) {
        out = classGenerator.declare(getReturnType(), false);
    }
    JBlock sub = new JBlock();
    if (getReturnType().getMinorType() != TypeProtos.MinorType.LATE) {
        internalOutput = sub.decl(JMod.FINAL, classGenerator.getHolderType(getReturnType()), getReturnValue().getName(), JExpr._new(classGenerator.getHolderType(getReturnType())));
    }
    classGenerator.getEvalBlock().add(sub);
    addProtectedBlock(classGenerator, sub, output(), null, workspaceJVars, false);
    if (getReturnType().getMinorType() != TypeProtos.MinorType.LATE) {
        sub.assign(out.getHolder(), internalOutput);
    }
    // hash aggregate uses workspace vectors. Initialization is done in "setup" and does not require "reset" block.
    if (!classGenerator.getMappingSet().isHashAggMapping()) {
        generateBody(classGenerator, BlockType.RESET, reset(), null, workspaceJVars, false);
    }
    generateBody(classGenerator, BlockType.CLEANUP, cleanup(), null, workspaceJVars, false);
    return out;
}
Also used : HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) JBlock(com.sun.codemodel.JBlock) JVar(com.sun.codemodel.JVar)

Example 33 with HoldingContainer

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

the class TopNBatch method createNewPriorityQueue.

public static PriorityQueue createNewPriorityQueue(MappingSet mainMapping, MappingSet leftMapping, MappingSet rightMapping, List<Ordering> orderings, VectorAccessible batch, boolean unionTypeEnabled, boolean codegenDump, int limit, BufferAllocator allocator, SelectionVectorMode mode, FragmentContext context) {
    OptionSet optionSet = context.getOptions();
    FunctionLookupContext functionLookupContext = context.getFunctionRegistry();
    CodeGenerator<PriorityQueue> cg = CodeGenerator.get(PriorityQueue.TEMPLATE_DEFINITION, optionSet);
    cg.plainJavaCapable(true);
    cg.saveCodeForDebugging(codegenDump);
    // Uncomment out this line to debug the generated code.
    // cg.saveCodeForDebugging(true);
    ClassGenerator<PriorityQueue> 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, functionLookupContext, unionTypeEnabled);
        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, functionLookupContext);
        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));
    PriorityQueue q = context.getImplementationClass(cg);
    try {
        q.init(limit, allocator, mode == BatchSchema.SelectionVectorMode.TWO_BYTE);
    } catch (SchemaChangeException e) {
        throw TopNBatch.schemaChangeException(e, "Top N", logger);
    }
    return q;
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) FunctionLookupContext(org.apache.drill.exec.expr.fn.FunctionLookupContext) 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) OptionSet(org.apache.drill.exec.server.options.OptionSet)

Example 34 with HoldingContainer

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

the class ChainedHashTable method setupIsKeyMatchInternal.

private void setupIsKeyMatchInternal(ClassGenerator<HashTable> cg, MappingSet incomingMapping, MappingSet htableMapping, LogicalExpression[] keyExprs, List<Comparator> comparators, TypedFieldId[] htKeyFieldIds, SetupWork work) {
    boolean checkIfBothNulls = work == SetupWork.CHECK_BOTH_NULLS;
    // Regular key matching may return false in the middle (i.e., some pair of columns did not match), and true only if all matched;
    // but "both nulls" check returns the opposite logic (i.e., true when one pair of nulls is found, need check no more)
    JExpression midPointResult = checkIfBothNulls ? JExpr.TRUE : JExpr.FALSE;
    JExpression finalResult = checkIfBothNulls ? JExpr.FALSE : JExpr.TRUE;
    cg.setMappingSet(incomingMapping);
    if (keyExprs == null || keyExprs.length == 0 || checkIfBothNulls && !comparators.contains(Comparator.EQUALS)) {
        // e.g. for Hash-Aggr, or non-equi join
        cg.getEvalBlock()._return(JExpr.FALSE);
        return;
    }
    for (int i = 0; i < keyExprs.length; i++) {
        final LogicalExpression expr = keyExprs[i];
        cg.setMappingSet(incomingMapping);
        HoldingContainer left = cg.addExpr(expr, ClassGenerator.BlkCreateMode.TRUE_IF_BOUND);
        cg.setMappingSet(htableMapping);
        ValueVectorReadExpression vvrExpr = new ValueVectorReadExpression(htKeyFieldIds[i]);
        HoldingContainer right = cg.addExpr(vvrExpr, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc;
        if (work != SetupWork.DO_BUILD) {
            // codegen for the special case when both columns are null (i.e., return early with midPointResult)
            if (comparators.get(i) == Comparator.EQUALS && left.isOptional() && right.isOptional()) {
                jc = cg.getEvalBlock()._if(left.getIsSet().eq(JExpr.lit(0)).cand(right.getIsSet().eq(JExpr.lit(0))));
                jc._then()._return(midPointResult);
            }
        }
        if (!checkIfBothNulls) {
            // generate comparison code (at least one of the two columns' values is non-null)
            final LogicalExpression f = FunctionGenerationHelper.getOrderingComparatorNullsHigh(left, right, context.getFunctionRegistry());
            HoldingContainer out = cg.addExpr(f, ClassGenerator.BlkCreateMode.FALSE);
            // check if two values are not equal (comparator result != 0)
            jc = cg.getEvalBlock()._if(out.getValue().ne(JExpr.lit(0)));
            jc._then()._return(midPointResult);
        }
    }
    // All key expressions compared the same way, so return the appropriate final result
    cg.getEvalBlock()._return(finalResult);
}
Also used : ValueVectorReadExpression(org.apache.drill.exec.expr.ValueVectorReadExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) JConditional(com.sun.codemodel.JConditional) JExpression(com.sun.codemodel.JExpression)

Example 35 with HoldingContainer

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

the class DrillSimpleFuncHolder method generateEvalBody.

protected HoldingContainer generateEvalBody(ClassGenerator<?> g, HoldingContainer[] inputVariables, String body, JVar[] workspaceJVars, FieldReference ref) {
    g.getEvalBlock().directStatement(String.format("//---- start of eval portion of %s function. ----//", getRegisteredNames()[0]));
    JBlock sub = new JBlock(true, true);
    JBlock topSub = sub;
    HoldingContainer out = null;
    MajorType returnValueType = getReturnType();
    // add outside null handling if it is defined.
    if (getNullHandling() == NullHandling.NULL_IF_NULL) {
        JExpression e = null;
        for (HoldingContainer v : inputVariables) {
            if (v.isOptional()) {
                JExpression isNullExpr;
                if (v.isReader()) {
                    isNullExpr = JOp.cond(v.getHolder().invoke("isSet"), JExpr.lit(1), JExpr.lit(0));
                } else {
                    isNullExpr = v.getIsSet();
                }
                if (e == null) {
                    e = isNullExpr;
                } else {
                    e = e.mul(isNullExpr);
                }
            }
        }
        if (e != null) {
            // if at least one expression must be checked, set up the conditional.
            returnValueType = getReturnType().toBuilder().setMode(DataMode.OPTIONAL).build();
            out = g.declare(returnValueType);
            e = e.eq(JExpr.lit(0));
            JConditional jc = sub._if(e);
            jc._then().assign(out.getIsSet(), JExpr.lit(0));
            sub = jc._else();
        }
    }
    if (out == null) {
        out = g.declare(returnValueType);
    }
    // add the subblock after the out declaration.
    g.getEvalBlock().add(topSub);
    JVar internalOutput = sub.decl(JMod.FINAL, g.getHolderType(returnValueType), getReturnValue().getName(), JExpr._new(g.getHolderType(returnValueType)));
    addProtectedBlock(g, sub, body, inputVariables, workspaceJVars, false);
    if (sub != topSub) {
        // Assign null if NULL_IF_NULL mode
        sub.assign(internalOutput.ref("isSet"), JExpr.lit(1));
    }
    sub.assign(out.getHolder(), internalOutput);
    if (sub != topSub) {
        // Assign null if NULL_IF_NULL mode
        sub.assign(internalOutput.ref("isSet"), JExpr.lit(1));
    }
    g.getEvalBlock().directStatement(String.format("//---- end of eval portion of %s function. ----//", getRegisteredNames()[0]));
    return out;
}
Also used : HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) JExpression(com.sun.codemodel.JExpression) JVar(com.sun.codemodel.JVar)

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