Search in sources :

Example 1 with HoldingContainer

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

the class ChainedHashTable method setupGetHash.

private void setupGetHash(ClassGenerator<HashTable> cg, MappingSet incomingMapping, VectorAccessible batch, LogicalExpression[] keyExprs, boolean isProbe) throws SchemaChangeException {
    cg.setMappingSet(incomingMapping);
    if (keyExprs == null || keyExprs.length == 0) {
        cg.getEvalBlock()._return(JExpr.lit(0));
        return;
    }
    /*
     * We use the same logic to generate run time code for the hash function both for hash join and hash
     * aggregate. For join we need to hash everything as double (both for distribution and for comparison) but
     * for aggregation we can avoid the penalty of casting to double
     */
    LogicalExpression hashExpression = HashPrelUtil.getHashExpression(Arrays.asList(keyExprs), incomingProbe != null ? true : false);
    final LogicalExpression materializedExpr = ExpressionTreeMaterializer.materializeAndCheckErrors(hashExpression, batch, context.getFunctionRegistry());
    HoldingContainer hash = cg.addExpr(materializedExpr);
    cg.getEvalBlock()._return(hash.getValue());
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)

Example 2 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) throws SchemaChangeException {
    cg.setMappingSet(incomingMapping);
    if (keyExprs == null || keyExprs.length == 0) {
        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.FALSE);
        cg.setMappingSet(htableMapping);
        ValueVectorReadExpression vvrExpr = new ValueVectorReadExpression(htKeyFieldIds[i]);
        HoldingContainer right = cg.addExpr(vvrExpr, ClassGenerator.BlkCreateMode.FALSE);
        JConditional jc;
        // codegen for nullable columns if nulls are not equal
        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(JExpr.FALSE);
        }
        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(JExpr.FALSE);
    }
    // All key expressions compared equal, so return TRUE
    cg.getEvalBlock()._return(JExpr.TRUE);
}
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)

Example 3 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, FieldReference fieldReference) {
    HoldingContainer out = classGenerator.declare(getReturnType(), false);
    JBlock sub = new JBlock();
    classGenerator.getEvalBlock().add(sub);
    JVar internalOutput = sub.decl(JMod.FINAL, classGenerator.getHolderType(getReturnType()), getReturnValue().getName(), JExpr._new(classGenerator.getHolderType(getReturnType())));
    addProtectedBlock(classGenerator, sub, output(), null, workspaceJVars, false);
    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 4 with HoldingContainer

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

the class DrillFuncHolder method addProtectedBlock.

protected void addProtectedBlock(ClassGenerator<?> g, JBlock sub, String body, HoldingContainer[] inputVariables, JVar[] workspaceJVars, boolean decConstInputOnly) {
    if (inputVariables != null) {
        for (int i = 0; i < inputVariables.length; i++) {
            if (decConstInputOnly && !inputVariables[i].isConstant()) {
                continue;
            }
            ValueReference parameter = attributes.getParameters()[i];
            HoldingContainer inputVariable = inputVariables[i];
            if (parameter.isFieldReader() && !inputVariable.isReader() && !Types.isComplex(inputVariable.getMajorType()) && inputVariable.getMinorType() != MinorType.UNION) {
                JType singularReaderClass = g.getModel()._ref(TypeHelper.getHolderReaderImpl(inputVariable.getMajorType().getMinorType(), inputVariable.getMajorType().getMode()));
                JType fieldReadClass = g.getModel()._ref(FieldReader.class);
                sub.decl(fieldReadClass, parameter.getName(), JExpr._new(singularReaderClass).arg(inputVariable.getHolder()));
            } else {
                sub.decl(inputVariable.getHolder().type(), parameter.getName(), inputVariable.getHolder());
            }
        }
    }
    JVar[] internalVars = new JVar[workspaceJVars.length];
    for (int i = 0; i < workspaceJVars.length; i++) {
        if (decConstInputOnly) {
            internalVars[i] = sub.decl(g.getModel()._ref(attributes.getWorkspaceVars()[i].getType()), attributes.getWorkspaceVars()[i].getName(), workspaceJVars[i]);
        } else {
            internalVars[i] = sub.decl(g.getModel()._ref(attributes.getWorkspaceVars()[i].getType()), attributes.getWorkspaceVars()[i].getName(), workspaceJVars[i]);
        }
    }
    Preconditions.checkNotNull(body);
    sub.directStatement(body);
    // reassign workspace variables back to global space.
    for (int i = 0; i < workspaceJVars.length; i++) {
        sub.assign(workspaceJVars[i], internalVars[i]);
    }
}
Also used : HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 5 with HoldingContainer

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

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)

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