Search in sources :

Example 1 with AbstractContainerVector

use of org.apache.drill.exec.vector.complex.AbstractContainerVector in project drill by apache.

the class TopNBatch method buildSchema.

@Override
public void buildSchema() throws SchemaChangeException {
    VectorContainer c = new VectorContainer(oContext);
    IterOutcome outcome = next(incoming);
    switch(outcome) {
        case OK:
        case OK_NEW_SCHEMA:
            for (VectorWrapper<?> w : incoming) {
                @SuppressWarnings("resource") ValueVector v = c.addOrGet(w.getField());
                if (v instanceof AbstractContainerVector) {
                    w.getValueVector().makeTransferPair(v);
                    v.clear();
                }
            }
            c = VectorContainer.canonicalize(c);
            for (VectorWrapper<?> w : c) {
                @SuppressWarnings("resource") ValueVector v = container.addOrGet(w.getField());
                if (v instanceof AbstractContainerVector) {
                    w.getValueVector().makeTransferPair(v);
                    v.clear();
                }
                v.allocateNew();
            }
            container.buildSchema(SelectionVectorMode.NONE);
            container.setRecordCount(0);
            return;
        case STOP:
            state = BatchState.STOP;
            return;
        case OUT_OF_MEMORY:
            state = BatchState.OUT_OF_MEMORY;
            return;
        case NONE:
            state = BatchState.DONE;
        default:
            return;
    }
}
Also used : ValueVector(org.apache.drill.exec.vector.ValueVector) AbstractContainerVector(org.apache.drill.exec.vector.complex.AbstractContainerVector) VectorContainer(org.apache.drill.exec.record.VectorContainer)

Example 2 with AbstractContainerVector

use of org.apache.drill.exec.vector.complex.AbstractContainerVector in project drill by apache.

the class HashJoinBatch method setupHashJoinProbe.

public HashJoinProbe setupHashJoinProbe() throws ClassTransformationException, IOException {
    final CodeGenerator<HashJoinProbe> cg = CodeGenerator.get(HashJoinProbe.TEMPLATE_DEFINITION, context.getFunctionRegistry(), context.getOptions());
    cg.plainJavaCapable(true);
    // Uncomment out this line to debug the generated code.
    //    cg.saveCodeForDebugging(true);
    final ClassGenerator<HashJoinProbe> g = cg.getRoot();
    // Generate the code to project build side records
    g.setMappingSet(projectBuildMapping);
    int fieldId = 0;
    final JExpression buildIndex = JExpr.direct("buildIndex");
    final JExpression outIndex = JExpr.direct("outIndex");
    g.rotateBlock();
    if (rightSchema != null) {
        for (final MaterializedField field : rightSchema) {
            final MajorType inputType = field.getType();
            final MajorType outputType;
            // not nullable so we must exclude them from the check below (see DRILL-2197).
            if ((joinType == JoinRelType.LEFT || joinType == JoinRelType.FULL) && inputType.getMode() == DataMode.REQUIRED && inputType.getMinorType() != TypeProtos.MinorType.MAP) {
                outputType = Types.overrideMode(inputType, DataMode.OPTIONAL);
            } else {
                outputType = inputType;
            }
            // make sure to project field with children for children to show up in the schema
            final MaterializedField projected = field.withType(outputType);
            // Add the vector to our output container
            container.addOrGet(projected);
            final JVar inVV = g.declareVectorValueSetupAndMember("buildBatch", new TypedFieldId(field.getType(), true, fieldId));
            final JVar outVV = g.declareVectorValueSetupAndMember("outgoing", new TypedFieldId(outputType, false, fieldId));
            g.getEvalBlock().add(outVV.invoke("copyFromSafe").arg(buildIndex.band(JExpr.lit((int) Character.MAX_VALUE))).arg(outIndex).arg(inVV.component(buildIndex.shrz(JExpr.lit(16)))));
            g.rotateBlock();
            fieldId++;
        }
    }
    // Generate the code to project probe side records
    g.setMappingSet(projectProbeMapping);
    int outputFieldId = fieldId;
    fieldId = 0;
    final JExpression probeIndex = JExpr.direct("probeIndex");
    if (leftUpstream == IterOutcome.OK || leftUpstream == IterOutcome.OK_NEW_SCHEMA) {
        for (final VectorWrapper<?> vv : left) {
            final MajorType inputType = vv.getField().getType();
            final MajorType outputType;
            // not nullable so we must exclude them from the check below (see DRILL-2771, DRILL-2197).
            if ((joinType == JoinRelType.RIGHT || joinType == JoinRelType.FULL) && inputType.getMode() == DataMode.REQUIRED && inputType.getMinorType() != TypeProtos.MinorType.MAP) {
                outputType = Types.overrideMode(inputType, DataMode.OPTIONAL);
            } else {
                outputType = inputType;
            }
            final ValueVector v = container.addOrGet(MaterializedField.create(vv.getField().getPath(), outputType));
            if (v instanceof AbstractContainerVector) {
                vv.getValueVector().makeTransferPair(v);
                v.clear();
            }
            final JVar inVV = g.declareVectorValueSetupAndMember("probeBatch", new TypedFieldId(inputType, false, fieldId));
            final JVar outVV = g.declareVectorValueSetupAndMember("outgoing", new TypedFieldId(outputType, false, outputFieldId));
            g.getEvalBlock().add(outVV.invoke("copyFromSafe").arg(probeIndex).arg(outIndex).arg(inVV));
            g.rotateBlock();
            fieldId++;
            outputFieldId++;
        }
    }
    final HashJoinProbe hj = context.getImplementationClass(cg);
    return hj;
}
Also used : ValueVector(org.apache.drill.exec.vector.ValueVector) AbstractContainerVector(org.apache.drill.exec.vector.complex.AbstractContainerVector) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) MaterializedField(org.apache.drill.exec.record.MaterializedField) JExpression(com.sun.codemodel.JExpression) JVar(com.sun.codemodel.JVar)

Example 3 with AbstractContainerVector

use of org.apache.drill.exec.vector.complex.AbstractContainerVector in project drill by apache.

the class MergeJoinBatch method allocateBatch.

private void allocateBatch(boolean newSchema) {
    boolean leftAllowed = status.getLeftStatus() != IterOutcome.NONE;
    boolean rightAllowed = status.getRightStatus() != IterOutcome.NONE;
    if (newSchema) {
        container.clear();
        // add fields from both batches
        if (leftAllowed) {
            for (VectorWrapper<?> w : leftIterator) {
                MajorType inputType = w.getField().getType();
                MajorType outputType;
                if (joinType == JoinRelType.RIGHT && inputType.getMode() == DataMode.REQUIRED) {
                    outputType = Types.overrideMode(inputType, DataMode.OPTIONAL);
                } else {
                    outputType = inputType;
                }
                MaterializedField newField = MaterializedField.create(w.getField().getPath(), outputType);
                ValueVector v = container.addOrGet(newField);
                if (v instanceof AbstractContainerVector) {
                    w.getValueVector().makeTransferPair(v);
                    v.clear();
                }
            }
        }
        if (rightAllowed) {
            for (VectorWrapper<?> w : rightIterator) {
                MajorType inputType = w.getField().getType();
                MajorType outputType;
                if (joinType == JoinRelType.LEFT && inputType.getMode() == DataMode.REQUIRED) {
                    outputType = Types.overrideMode(inputType, DataMode.OPTIONAL);
                } else {
                    outputType = inputType;
                }
                MaterializedField newField = MaterializedField.create(w.getField().getPath(), outputType);
                ValueVector v = container.addOrGet(newField);
                if (v instanceof AbstractContainerVector) {
                    w.getValueVector().makeTransferPair(v);
                    v.clear();
                }
            }
        }
    } else {
        container.zeroVectors();
    }
    for (VectorWrapper w : container) {
        AllocationHelper.allocateNew(w.getValueVector(), Character.MAX_VALUE);
    }
    container.buildSchema(BatchSchema.SelectionVectorMode.NONE);
    logger.debug("Built joined schema: {}", container.getSchema());
}
Also used : ValueVector(org.apache.drill.exec.vector.ValueVector) AbstractContainerVector(org.apache.drill.exec.vector.complex.AbstractContainerVector) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) VectorWrapper(org.apache.drill.exec.record.VectorWrapper) MaterializedField(org.apache.drill.exec.record.MaterializedField)

Example 4 with AbstractContainerVector

use of org.apache.drill.exec.vector.complex.AbstractContainerVector in project drill by apache.

the class NestedLoopJoinBatch method buildSchema.

/**
   * Builds the output container's schema. Goes over the left and the right
   * batch and adds the corresponding vectors to the output container.
   * @throws SchemaChangeException if batch schema was changed during execution
   */
@Override
protected void buildSchema() throws SchemaChangeException {
    try {
        leftUpstream = next(LEFT_INPUT, left);
        rightUpstream = next(RIGHT_INPUT, right);
        if (leftUpstream == IterOutcome.STOP || rightUpstream == IterOutcome.STOP) {
            state = BatchState.STOP;
            return;
        }
        if (leftUpstream == IterOutcome.OUT_OF_MEMORY || rightUpstream == IterOutcome.OUT_OF_MEMORY) {
            state = BatchState.OUT_OF_MEMORY;
            return;
        }
        if (leftUpstream != IterOutcome.NONE) {
            leftSchema = left.getSchema();
            for (final VectorWrapper<?> vw : left) {
                container.addOrGet(vw.getField());
            }
        }
        if (rightUpstream != IterOutcome.NONE) {
            // make right input schema optional if we have LEFT join
            for (final VectorWrapper<?> vectorWrapper : right) {
                TypeProtos.MajorType inputType = vectorWrapper.getField().getType();
                TypeProtos.MajorType outputType;
                if (popConfig.getJoinType() == JoinRelType.LEFT && inputType.getMode() == TypeProtos.DataMode.REQUIRED) {
                    outputType = Types.overrideMode(inputType, TypeProtos.DataMode.OPTIONAL);
                } else {
                    outputType = inputType;
                }
                MaterializedField newField = MaterializedField.create(vectorWrapper.getField().getPath(), outputType);
                ValueVector valueVector = container.addOrGet(newField);
                if (valueVector instanceof AbstractContainerVector) {
                    vectorWrapper.getValueVector().makeTransferPair(valueVector);
                    valueVector.clear();
                }
            }
            rightSchema = right.getSchema();
            addBatchToHyperContainer(right);
        }
        allocateVectors();
        nljWorker = setupWorker();
        // if left batch is empty, fetch next
        if (leftUpstream != IterOutcome.NONE && left.getRecordCount() == 0) {
            leftUpstream = next(LEFT_INPUT, left);
        }
        container.setRecordCount(0);
        container.buildSchema(BatchSchema.SelectionVectorMode.NONE);
    } catch (ClassTransformationException | IOException e) {
        throw new SchemaChangeException(e);
    }
}
Also used : ValueVector(org.apache.drill.exec.vector.ValueVector) AbstractContainerVector(org.apache.drill.exec.vector.complex.AbstractContainerVector) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) ClassTransformationException(org.apache.drill.exec.exception.ClassTransformationException) MaterializedField(org.apache.drill.exec.record.MaterializedField) IOException(java.io.IOException) TypeProtos(org.apache.drill.common.types.TypeProtos)

Example 5 with AbstractContainerVector

use of org.apache.drill.exec.vector.complex.AbstractContainerVector in project drill by apache.

the class ExternalSortBatch method buildSchema.

@Override
public void buildSchema() throws SchemaChangeException {
    IterOutcome outcome = next(incoming);
    switch(outcome) {
        case OK:
        case OK_NEW_SCHEMA:
            for (VectorWrapper<?> w : incoming) {
                @SuppressWarnings("resource") ValueVector v = container.addOrGet(w.getField());
                if (v instanceof AbstractContainerVector) {
                    // Can we remove this hack?
                    w.getValueVector().makeTransferPair(v);
                    v.clear();
                }
                // Can we remove this? - SVR fails with NPE (TODO)
                v.allocateNew();
            }
            container.buildSchema(SelectionVectorMode.NONE);
            container.setRecordCount(0);
            break;
        case STOP:
            state = BatchState.STOP;
            break;
        case OUT_OF_MEMORY:
            state = BatchState.OUT_OF_MEMORY;
            break;
        case NONE:
            state = BatchState.DONE;
            break;
        default:
            break;
    }
}
Also used : ValueVector(org.apache.drill.exec.vector.ValueVector) AbstractContainerVector(org.apache.drill.exec.vector.complex.AbstractContainerVector)

Aggregations

ValueVector (org.apache.drill.exec.vector.ValueVector)6 AbstractContainerVector (org.apache.drill.exec.vector.complex.AbstractContainerVector)6 MaterializedField (org.apache.drill.exec.record.MaterializedField)3 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)2 JExpression (com.sun.codemodel.JExpression)1 JVar (com.sun.codemodel.JVar)1 IOException (java.io.IOException)1 TypeProtos (org.apache.drill.common.types.TypeProtos)1 ClassTransformationException (org.apache.drill.exec.exception.ClassTransformationException)1 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)1 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)1 VectorContainer (org.apache.drill.exec.record.VectorContainer)1 VectorWrapper (org.apache.drill.exec.record.VectorWrapper)1