Search in sources :

Example 26 with ClassTransformationException

use of org.apache.drill.exec.exception.ClassTransformationException in project drill by axbaretto.

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.getOptions());
    cg.getCodeGenerator().plainJavaCapable(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 (NamedExpression namedExpression : exprs) {
        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 27 with ClassTransformationException

use of org.apache.drill.exec.exception.ClassTransformationException in project drill by axbaretto.

the class FilterRecordBatch method generateSV4Filterer.

protected Filterer generateSV4Filterer() throws SchemaChangeException {
    final ErrorCollector collector = new ErrorCollectorImpl();
    final List<TransferPair> transfers = Lists.newArrayList();
    final ClassGenerator<Filterer> cg = CodeGenerator.getRoot(Filterer.TEMPLATE_DEFINITION4, context.getOptions());
    final LogicalExpression expr = ExpressionTreeMaterializer.materialize(popConfig.getExpr(), incoming, collector, context.getFunctionRegistry());
    if (collector.hasErrors()) {
        throw new SchemaChangeException(String.format("Failure while trying to materialize incoming schema.  Errors:\n %s.", collector.toErrorString()));
    }
    cg.addExpr(new ReturnValueExpression(expr), ClassGenerator.BlkCreateMode.FALSE);
    for (final VectorWrapper<?> vw : incoming) {
        for (final ValueVector vv : vw.getValueVectors()) {
            final TransferPair pair = vv.getTransferPair(oContext.getAllocator());
            container.add(pair.getTo());
            transfers.add(pair);
        }
    }
    // allocate outgoing sv4
    container.buildSchema(SelectionVectorMode.FOUR_BYTE);
    try {
        final TransferPair[] tx = transfers.toArray(new TransferPair[transfers.size()]);
        final Filterer filter = context.getImplementationClass(cg);
        filter.setup(context, incoming, this, tx);
        return filter;
    } 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) ValueVector(org.apache.drill.exec.vector.ValueVector) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException)

Example 28 with ClassTransformationException

use of org.apache.drill.exec.exception.ClassTransformationException in project drill by axbaretto.

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 {
        if (!prefetchFirstBatchFromBothSides()) {
            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().getName(), 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 29 with ClassTransformationException

use of org.apache.drill.exec.exception.ClassTransformationException in project drill by apache.

the class EnumerableRecordReader method setup.

@SuppressWarnings("unchecked")
private void setup(OperatorContext context) {
    SchemaPlus rootSchema = context.getFragmentContext().getFullRootSchema();
    DataContext root = new DrillDataContext(schemaPath != null ? SchemaUtilites.searchSchemaTree(rootSchema, SchemaUtilites.getSchemaPathAsList(schemaPath)) : rootSchema, new JavaTypeFactoryImpl(), Collections.emptyMap());
    try {
        Class<?> implementationClass = ClassBuilder.getCompiledClass(code, CLASS_NAME, context.getFragmentContext().getConfig(), context.getFragmentContext().getOptions());
        Iterable<?> iterable = (Iterable<Map<String, Object>>) implementationClass.getMethod(BuiltInMethod.BINDABLE_BIND.method.getName(), DataContext.class).invoke(implementationClass.newInstance(), root);
        if (fieldsMap.keySet().size() == 1) {
            // for the case of projecting single column, its value is returned
            records = StreamSupport.stream(iterable.spliterator(), false).map(this::wrap).iterator();
        } else {
            // for the case when all columns were projected, array is returned
            records = StreamSupport.stream(iterable.spliterator(), false).map(row -> wrap((Object[]) row)).iterator();
        }
    } catch (CompileException | IOException | ClassTransformationException | ReflectiveOperationException e) {
        logger.error("Exception happened when executing generated code", e);
        Throwable rootCause = Throwables.getRootCause(e);
        throw new DrillRuntimeException(rootCause.getMessage(), rootCause);
    }
}
Also used : ClassTransformationException(org.apache.drill.exec.exception.ClassTransformationException) SchemaPlus(org.apache.calcite.schema.SchemaPlus) IOException(java.io.IOException) DataContext(org.apache.calcite.DataContext) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) CompileException(org.codehaus.commons.compiler.CompileException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException)

Aggregations

ClassTransformationException (org.apache.drill.exec.exception.ClassTransformationException)29 IOException (java.io.IOException)27 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)21 ValueVector (org.apache.drill.exec.vector.ValueVector)9 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)8 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)8 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)8 TransferPair (org.apache.drill.exec.record.TransferPair)6 MaterializedField (org.apache.drill.exec.record.MaterializedField)5 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)5 VectorWrapper (org.apache.drill.exec.record.VectorWrapper)5 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)4 ValueVectorReadExpression (org.apache.drill.exec.expr.ValueVectorReadExpression)4 ValueVectorWriteExpression (org.apache.drill.exec.expr.ValueVectorWriteExpression)4 ExpandableHyperContainer (org.apache.drill.exec.record.ExpandableHyperContainer)4 VectorContainer (org.apache.drill.exec.record.VectorContainer)4 IntHashSet (com.carrotsearch.hppc.IntHashSet)3 Stopwatch (com.google.common.base.Stopwatch)3 FieldReference (org.apache.drill.common.expression.FieldReference)3 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)3