Search in sources :

Example 1 with ValueVectorReadExpression

use of org.apache.drill.exec.expr.ValueVectorReadExpression 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 2 with ValueVectorReadExpression

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

the class ChainedHashTable method setupOutputRecordKeys.

private void setupOutputRecordKeys(ClassGenerator<HashTable> cg, TypedFieldId[] htKeyFieldIds, TypedFieldId[] outKeyFieldIds) {
    cg.setMappingSet(OutputRecordKeysMapping);
    if (outKeyFieldIds != null) {
        for (int i = 0; i < outKeyFieldIds.length; i++) {
            ValueVectorReadExpression vvrExpr = new ValueVectorReadExpression(htKeyFieldIds[i]);
            boolean useSetSafe = !Types.isFixedWidthType(vvrExpr.getMajorType()) || Types.isRepeated(vvrExpr.getMajorType());
            ValueVectorWriteExpression vvwExpr = new ValueVectorWriteExpression(outKeyFieldIds[i], vvrExpr, useSetSafe);
            cg.addExpr(vvwExpr, ClassGenerator.BlkCreateMode.TRUE);
        }
    }
}
Also used : ValueVectorReadExpression(org.apache.drill.exec.expr.ValueVectorReadExpression) ValueVectorWriteExpression(org.apache.drill.exec.expr.ValueVectorWriteExpression)

Example 3 with ValueVectorReadExpression

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

the class TestEvaluationVisitor method x.

@Test
public void x() throws Exception {
    DrillConfig c = DrillConfig.create();
    FunctionImplementationRegistry reg = new FunctionImplementationRegistry(c);
    EvaluationVisitor v = new EvaluationVisitor(reg);
    CodeGenerator<?> g = CodeGenerator.get(Projector.TEMPLATE_DEFINITION, reg, null);
    SchemaPath path = (SchemaPath) getExpr("a.b[4][2].c[6]");
    TypedFieldId id = //
    TypedFieldId.newBuilder().addId(//
    1).addId(//
    3).remainder(//
    path.getRootSegment()).intermediateType(Types.optional(MinorType.MAP)).finalType(//
    Types.repeated(MinorType.MAP)).hyper().withIndex().build();
    ValueVectorReadExpression e = new ValueVectorReadExpression(id);
    TypedFieldId outId = //
    TypedFieldId.newBuilder().addId(//
    1).finalType(//
    Types.repeated(MinorType.MAP)).intermediateType(//
    Types.repeated(MinorType.MAP)).build();
    ValueVectorWriteExpression e2 = new ValueVectorWriteExpression(outId, e, true);
    v.addExpr(e2, g.getRoot());
    logger.debug(g.generateAndGet());
}
Also used : ValueVectorReadExpression(org.apache.drill.exec.expr.ValueVectorReadExpression) EvaluationVisitor(org.apache.drill.exec.expr.EvaluationVisitor) DrillConfig(org.apache.drill.common.config.DrillConfig) SchemaPath(org.apache.drill.common.expression.SchemaPath) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) ValueVectorWriteExpression(org.apache.drill.exec.expr.ValueVectorWriteExpression) FunctionImplementationRegistry(org.apache.drill.exec.expr.fn.FunctionImplementationRegistry) Test(org.junit.Test)

Example 4 with ValueVectorReadExpression

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

the class FlattenRecordBatch method setupNewSchema.

@Override
protected boolean setupNewSchema() throws SchemaChangeException {
    this.allocationVectors = Lists.newArrayList();
    container.clear();
    final List<NamedExpression> exprs = getExpressionList();
    final ErrorCollector collector = new ErrorCollectorImpl();
    final List<TransferPair> transfers = Lists.newArrayList();
    final ClassGenerator<Flattener> cg = CodeGenerator.getRoot(Flattener.TEMPLATE_DEFINITION, context.getFunctionRegistry(), context.getOptions());
    cg.getCodeGenerator().plainJavaCapable(true);
    // Uncomment out this line to debug the generated code.
    //    cg.getCodeGenerator().saveCodeForDebugging(true);
    final IntHashSet transferFieldIds = new IntHashSet();
    final NamedExpression flattenExpr = new NamedExpression(popConfig.getColumn(), new FieldReference(popConfig.getColumn()));
    final ValueVectorReadExpression vectorRead = (ValueVectorReadExpression) ExpressionTreeMaterializer.materialize(flattenExpr.getExpr(), incoming, collector, context.getFunctionRegistry(), true);
    final FieldReference fieldReference = flattenExpr.getRef();
    final TransferPair transferPair = getFlattenFieldTransferPair(fieldReference);
    if (transferPair != null) {
        final ValueVector flattenVector = transferPair.getTo();
        // checks that list has only default ValueVector and replaces resulting ValueVector to INT typed ValueVector
        if (exprs.size() == 0 && flattenVector.getField().getType().equals(Types.LATE_BIND_TYPE)) {
            final MaterializedField outputField = MaterializedField.create(fieldReference.getAsNamePart().getName(), Types.OPTIONAL_INT);
            final ValueVector vector = TypeHelper.getNewVector(outputField, oContext.getAllocator());
            container.add(vector);
        } else {
            transfers.add(transferPair);
            container.add(flattenVector);
            transferFieldIds.add(vectorRead.getFieldId().getFieldIds()[0]);
        }
    }
    logger.debug("Added transfer for project expression.");
    ClassifierResult result = new ClassifierResult();
    for (int i = 0; i < exprs.size(); i++) {
        final NamedExpression namedExpression = exprs.get(i);
        result.clear();
        String outputName = getRef(namedExpression).getRootSegment().getPath();
        if (result != null && result.outputNames != null && result.outputNames.size() > 0) {
            for (int j = 0; j < result.outputNames.size(); j++) {
                if (!result.outputNames.get(j).equals(EMPTY_STRING)) {
                    outputName = result.outputNames.get(j);
                    break;
                }
            }
        }
        final LogicalExpression expr = ExpressionTreeMaterializer.materialize(namedExpression.getExpr(), incoming, collector, context.getFunctionRegistry(), true);
        if (collector.hasErrors()) {
            throw new SchemaChangeException(String.format("Failure while trying to materialize incoming schema.  Errors:\n %s.", collector.toErrorString()));
        }
        if (expr instanceof DrillFuncHolderExpr && ((DrillFuncHolderExpr) expr).getHolder().isComplexWriterFuncHolder()) {
            // Lazy initialization of the list of complex writers, if not done yet.
            if (complexWriters == null) {
                complexWriters = Lists.newArrayList();
            }
            // The reference name will be passed to ComplexWriter, used as the name of the output vector from the writer.
            ((DrillFuncHolderExpr) expr).getFieldReference(namedExpression.getRef());
            cg.addExpr(expr);
        } else {
            // need to do evaluation.
            final MaterializedField outputField;
            if (expr instanceof ValueVectorReadExpression) {
                final TypedFieldId id = ValueVectorReadExpression.class.cast(expr).getFieldId();
                @SuppressWarnings("resource") final ValueVector incomingVector = incoming.getValueAccessorById(id.getIntermediateClass(), id.getFieldIds()).getValueVector();
                // when the first batch will be empty.
                if (incomingVector != null) {
                    outputField = incomingVector.getField().clone();
                } else {
                    outputField = MaterializedField.create(outputName, expr.getMajorType());
                }
            } else {
                outputField = MaterializedField.create(outputName, expr.getMajorType());
            }
            @SuppressWarnings("resource") final ValueVector vector = TypeHelper.getNewVector(outputField, oContext.getAllocator());
            allocationVectors.add(vector);
            TypedFieldId fid = container.add(vector);
            ValueVectorWriteExpression write = new ValueVectorWriteExpression(fid, expr, true);
            cg.addExpr(write);
            logger.debug("Added eval for project expression.");
        }
    }
    cg.rotateBlock();
    cg.getEvalBlock()._return(JExpr.TRUE);
    container.buildSchema(SelectionVectorMode.NONE);
    try {
        this.flattener = context.getImplementationClass(cg.getCodeGenerator());
        flattener.setup(context, incoming, this, transfers);
    } catch (ClassTransformationException | IOException e) {
        throw new SchemaChangeException("Failure while attempting to load generated class", e);
    }
    return true;
}
Also used : TransferPair(org.apache.drill.exec.record.TransferPair) IntHashSet(com.carrotsearch.hppc.IntHashSet) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) DrillFuncHolderExpr(org.apache.drill.exec.expr.DrillFuncHolderExpr) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) ValueVectorWriteExpression(org.apache.drill.exec.expr.ValueVectorWriteExpression) FieldReference(org.apache.drill.common.expression.FieldReference) ClassTransformationException(org.apache.drill.exec.exception.ClassTransformationException) MaterializedField(org.apache.drill.exec.record.MaterializedField) IOException(java.io.IOException) ValueVectorReadExpression(org.apache.drill.exec.expr.ValueVectorReadExpression) RepeatedValueVector(org.apache.drill.exec.vector.complex.RepeatedValueVector) ValueVector(org.apache.drill.exec.vector.ValueVector) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) NamedExpression(org.apache.drill.common.logical.data.NamedExpression)

Example 5 with ValueVectorReadExpression

use of org.apache.drill.exec.expr.ValueVectorReadExpression 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
   * @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.getFunctionRegistry(), 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)

Aggregations

ValueVectorReadExpression (org.apache.drill.exec.expr.ValueVectorReadExpression)6 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)4 ValueVectorWriteExpression (org.apache.drill.exec.expr.ValueVectorWriteExpression)4 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)4 IOException (java.io.IOException)3 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)3 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)3 ClassTransformationException (org.apache.drill.exec.exception.ClassTransformationException)3 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)3 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)3 TransferPair (org.apache.drill.exec.record.TransferPair)3 IntHashSet (com.carrotsearch.hppc.IntHashSet)2 JConditional (com.sun.codemodel.JConditional)2 FieldReference (org.apache.drill.common.expression.FieldReference)2 SchemaPath (org.apache.drill.common.expression.SchemaPath)2 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)2 DrillFuncHolderExpr (org.apache.drill.exec.expr.DrillFuncHolderExpr)2 MaterializedField (org.apache.drill.exec.record.MaterializedField)2 ValueVector (org.apache.drill.exec.vector.ValueVector)2 DrillConfig (org.apache.drill.common.config.DrillConfig)1