Search in sources :

Example 41 with TypedFieldId

use of org.apache.drill.exec.record.TypedFieldId in project drill by axbaretto.

the class UnionAllRecordBatch method createUnionAller.

private void createUnionAller(RecordBatch inputBatch) throws ClassTransformationException, IOException, SchemaChangeException {
    transfers.clear();
    allocationVectors.clear();
    final ClassGenerator<UnionAller> cg = CodeGenerator.getRoot(UnionAller.TEMPLATE_DEFINITION, context.getOptions());
    cg.getCodeGenerator().plainJavaCapable(true);
    int index = 0;
    for (VectorWrapper<?> vw : inputBatch) {
        ValueVector vvIn = vw.getValueVector();
        ValueVector vvOut = container.getValueVector(index).getValueVector();
        final ErrorCollector collector = new ErrorCollectorImpl();
        // cast data types (Minortype or DataMode)
        if (container.getSchema().getColumn(index).hasSameTypeAndMode(vvIn.getField()) && // Per DRILL-5521, existing bug for map transfer
        vvIn.getField().getType().getMinorType() != TypeProtos.MinorType.MAP) {
            // Transfer column
            TransferPair tp = vvIn.makeTransferPair(vvOut);
            transfers.add(tp);
        } else if (vvIn.getField().getType().getMinorType() == TypeProtos.MinorType.NULL) {
            continue;
        } else {
            // Copy data in order to rename the column
            SchemaPath inputPath = SchemaPath.getSimplePath(vvIn.getField().getName());
            MaterializedField inField = vvIn.getField();
            MaterializedField outputField = vvOut.getField();
            LogicalExpression expr = ExpressionTreeMaterializer.materialize(inputPath, inputBatch, collector, context.getFunctionRegistry());
            if (collector.hasErrors()) {
                throw new SchemaChangeException(String.format("Failure while trying to materialize incoming schema.  Errors:\n %s.", collector.toErrorString()));
            }
            // cast to the one with the least restriction
            if (inField.getType().getMode() == TypeProtos.DataMode.REQUIRED && outputField.getType().getMode() != TypeProtos.DataMode.REQUIRED) {
                expr = ExpressionTreeMaterializer.convertToNullableType(expr, inField.getType().getMinorType(), context.getFunctionRegistry(), collector);
                if (collector.hasErrors()) {
                    throw new SchemaChangeException(String.format("Failure while trying to materialize incoming schema.  Errors:\n %s.", collector.toErrorString()));
                }
            }
            // Insert a cast before the Union operation
            if (inField.getType().getMinorType() != outputField.getType().getMinorType()) {
                expr = ExpressionTreeMaterializer.addCastExpression(expr, outputField.getType(), context.getFunctionRegistry(), collector);
                if (collector.hasErrors()) {
                    throw new SchemaChangeException(String.format("Failure while trying to materialize incoming schema.  Errors:\n %s.", collector.toErrorString()));
                }
            }
            TypedFieldId fid = container.getValueVectorId(SchemaPath.getSimplePath(outputField.getName()));
            boolean useSetSafe = !(vvOut instanceof FixedWidthVector);
            ValueVectorWriteExpression write = new ValueVectorWriteExpression(fid, expr, useSetSafe);
            cg.addExpr(write);
            allocationVectors.add(vvOut);
        }
        ++index;
    }
    unionall = context.getImplementationClass(cg.getCodeGenerator());
    unionall.setup(context, inputBatch, this, transfers);
}
Also used : TransferPair(org.apache.drill.exec.record.TransferPair) FixedWidthVector(org.apache.drill.exec.vector.FixedWidthVector) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) MaterializedField(org.apache.drill.exec.record.MaterializedField) ValueVector(org.apache.drill.exec.vector.ValueVector) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaChangeException(org.apache.drill.exec.exception.SchemaChangeException) SchemaPath(org.apache.drill.common.expression.SchemaPath) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) ValueVectorWriteExpression(org.apache.drill.exec.expr.ValueVectorWriteExpression)

Example 42 with TypedFieldId

use of org.apache.drill.exec.record.TypedFieldId in project drill by axbaretto.

the class TestEvaluationVisitor method x.

@Test
public void x() throws Exception {
    DrillConfig c = DrillConfig.create();
    FunctionImplementationRegistry reg = new FunctionImplementationRegistry(c);
    EvaluationVisitor v = new EvaluationVisitor();
    CodeGenerator<?> g = CodeGenerator.get(Projector.TEMPLATE_DEFINITION, 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 43 with TypedFieldId

use of org.apache.drill.exec.record.TypedFieldId in project drill by axbaretto.

the class ExpressionTest method testSpecial.

@Test
public void testSpecial() throws Exception {
    final RecordBatch batch = mock(RecordBatch.class);
    final VectorWrapper wrapper = mock(VectorWrapper.class);
    final TypeProtos.MajorType type = Types.optional(MinorType.INT);
    final TypedFieldId tfid = new TypedFieldId(type, false, 0);
    when(wrapper.getValueVector()).thenReturn(new IntVector(MaterializedField.create("result", type), RootAllocatorFactory.newRoot(c)));
    when(batch.getValueVectorId(new SchemaPath("alpha", ExpressionPosition.UNKNOWN))).thenReturn(tfid);
    when(batch.getValueAccessorById(IntVector.class, tfid.getFieldIds())).thenReturn(wrapper);
    System.out.println(getExpressionCode("1 + 1", batch));
}
Also used : IntVector(org.apache.drill.exec.vector.IntVector) SchemaPath(org.apache.drill.common.expression.SchemaPath) RecordBatch(org.apache.drill.exec.record.RecordBatch) VectorWrapper(org.apache.drill.exec.record.VectorWrapper) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) TypeProtos(org.apache.drill.common.types.TypeProtos) Test(org.junit.Test) ExecTest(org.apache.drill.exec.ExecTest)

Example 44 with TypedFieldId

use of org.apache.drill.exec.record.TypedFieldId in project drill by axbaretto.

the class ExpressionTest method testSchemaExpression.

@Test
public void testSchemaExpression() throws Exception {
    final RecordBatch batch = mock(RecordBatch.class);
    when(batch.getValueVectorId(new SchemaPath("alpha", ExpressionPosition.UNKNOWN))).thenReturn(new TypedFieldId(Types.optional(MinorType.BIGINT), false, 0));
    System.out.println(getExpressionCode("1 + alpha", batch));
}
Also used : SchemaPath(org.apache.drill.common.expression.SchemaPath) RecordBatch(org.apache.drill.exec.record.RecordBatch) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) Test(org.junit.Test) ExecTest(org.apache.drill.exec.ExecTest)

Example 45 with TypedFieldId

use of org.apache.drill.exec.record.TypedFieldId in project drill by axbaretto.

the class ExpressionTest method getExpressionCode.

private String getExpressionCode(String expression, RecordBatch batch) throws Exception {
    final LogicalExpression expr = parseExpr(expression);
    final ErrorCollector error = new ErrorCollectorImpl();
    final LogicalExpression materializedExpr = ExpressionTreeMaterializer.materialize(expr, batch, error, registry);
    if (error.getErrorCount() != 0) {
        logger.error("Failure while materializing expression [{}].  Errors: {}", expression, error);
        assertEquals(0, error.getErrorCount());
    }
    FunctionImplementationRegistry funcReg = new FunctionImplementationRegistry(DrillConfig.create());
    final ClassGenerator<Projector> cg = CodeGenerator.get(Projector.TEMPLATE_DEFINITION, null).getRoot();
    cg.addExpr(new ValueVectorWriteExpression(new TypedFieldId(materializedExpr.getMajorType(), -1), materializedExpr));
    return cg.getCodeGenerator().generateAndGet();
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) Projector(org.apache.drill.exec.physical.impl.project.Projector) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) FunctionImplementationRegistry(org.apache.drill.exec.expr.fn.FunctionImplementationRegistry)

Aggregations

TypedFieldId (org.apache.drill.exec.record.TypedFieldId)63 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)30 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)22 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)22 MaterializedField (org.apache.drill.exec.record.MaterializedField)22 ValueVector (org.apache.drill.exec.vector.ValueVector)22 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)21 SchemaPath (org.apache.drill.common.expression.SchemaPath)18 ValueVectorWriteExpression (org.apache.drill.exec.expr.ValueVectorWriteExpression)17 ValueVectorReadExpression (org.apache.drill.exec.expr.ValueVectorReadExpression)12 TransferPair (org.apache.drill.exec.record.TransferPair)12 Test (org.junit.Test)11 JVar (com.sun.codemodel.JVar)10 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)9 VectorWrapper (org.apache.drill.exec.record.VectorWrapper)9 FieldReference (org.apache.drill.common.expression.FieldReference)7 TypeProtos (org.apache.drill.common.types.TypeProtos)7 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)7 JExpression (com.sun.codemodel.JExpression)6 IOException (java.io.IOException)6