Search in sources :

Example 41 with HoldingContainer

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

the class ExternalSortBatch method generateComparisons.

private void generateComparisons(ClassGenerator<?> g, VectorAccessible batch) throws SchemaChangeException {
    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 new SchemaChangeException("Failure while materializing expression. " + collector.toErrorString());
        }
        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) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) 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 42 with HoldingContainer

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

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)

Example 43 with HoldingContainer

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

the class DrillSimpleFuncHolder method renderEnd.

@Override
public HoldingContainer renderEnd(ClassGenerator<?> classGenerator, HoldingContainer[] inputVariables, JVar[] workspaceJVars, FieldReference fieldReference) {
    // for the argument is not, then raise exception.
    for (int i = 0; i < inputVariables.length; i++) {
        if (getParameters()[i].isConstant() && !inputVariables[i].isConstant()) {
            throw new DrillRuntimeException(String.format("The argument '%s' of Function '%s' has to be constant!", getParameters()[i].getName(), this.getRegisteredNames()[0]));
        }
    }
    generateBody(classGenerator, BlockType.SETUP, setupBody(), inputVariables, workspaceJVars, true);
    HoldingContainer c = generateEvalBody(classGenerator, inputVariables, evalBody(), workspaceJVars, fieldReference);
    generateBody(classGenerator, BlockType.RESET, resetBody(), null, workspaceJVars, false);
    generateBody(classGenerator, BlockType.CLEANUP, cleanupBody(), null, workspaceJVars, false);
    return c;
}
Also used : HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException)

Example 44 with HoldingContainer

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

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 45 with HoldingContainer

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

the class DrillAggFuncHolder method addProtectedBlockHA.

/*
   * This is customized version of "addProtectedBlock" for hash aggregation. It take one additional parameter "wsIndexVariable".
   */
private void addProtectedBlockHA(ClassGenerator<?> g, JBlock sub, String body, HoldingContainer[] inputVariables, JVar[] workspaceJVars, JExpression wsIndexVariable) {
    if (inputVariables != null) {
        for (int i = 0; i < inputVariables.length; i++) {
            ValueReference parameter = getParameters()[i];
            HoldingContainer inputVariable = inputVariables[i];
            sub.decl(inputVariable.getHolder().type(), parameter.getName(), inputVariable.getHolder());
        }
    }
    JVar[] internalVars = new JVar[workspaceJVars.length];
    for (int i = 0; i < workspaceJVars.length; i++) {
        if (getWorkspaceVars()[i].isInject()) {
            internalVars[i] = sub.decl(g.getModel()._ref(getWorkspaceVars()[i].getType()), getWorkspaceVars()[i].getName(), workspaceJVars[i]);
            continue;
        }
        // sub.assign(workspaceJVars[i], JExpr._new(g.getHolderType(workspaceVars[i].majorType)));
        // Access workspaceVar through workspace vector.
        JInvocation getValueAccessor = g.getWorkspaceVectors().get(getWorkspaceVars()[i]).invoke("getAccessor").invoke("get");
        if (Types.usesHolderForGet(getWorkspaceVars()[i].getMajorType())) {
            sub.add(getValueAccessor.arg(wsIndexVariable).arg(workspaceJVars[i]));
        } else {
            sub.assign(workspaceJVars[i].ref("value"), getValueAccessor.arg(wsIndexVariable));
        }
        internalVars[i] = sub.decl(g.getHolderType(getWorkspaceVars()[i].getMajorType()), getWorkspaceVars()[i].getName(), workspaceJVars[i]);
    }
    Preconditions.checkNotNull(body);
    sub.directStatement(body);
    // reassign workspace variables back.
    for (int i = 0; i < workspaceJVars.length; i++) {
        sub.assign(workspaceJVars[i], internalVars[i]);
        // Injected buffers are not stored as vectors skip storing them in vectors
        if (getWorkspaceVars()[i].isInject()) {
            continue;
        }
        // Change workspaceVar through workspace vector.
        JInvocation setMeth;
        MajorType type = getWorkspaceVars()[i].getMajorType();
        if (Types.usesHolderForGet(type)) {
            setMeth = g.getWorkspaceVectors().get(getWorkspaceVars()[i]).invoke("getMutator").invoke("setSafe").arg(wsIndexVariable).arg(workspaceJVars[i]);
        } else {
            if (!Types.isFixedWidthType(type) || Types.isRepeated(type)) {
                setMeth = g.getWorkspaceVectors().get(getWorkspaceVars()[i]).invoke("getMutator").invoke("setSafe").arg(wsIndexVariable).arg(workspaceJVars[i].ref("value"));
            } else {
                setMeth = g.getWorkspaceVectors().get(getWorkspaceVars()[i]).invoke("getMutator").invoke("set").arg(wsIndexVariable).arg(workspaceJVars[i].ref("value"));
            }
        }
        sub.add(setMeth);
        JClass drillRunTimeException = g.getModel().ref(DrillRuntimeException.class);
    }
}
Also used : HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) JClass(com.sun.codemodel.JClass) JInvocation(com.sun.codemodel.JInvocation) 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