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);
}
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());
}
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));
}
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));
}
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();
}
Aggregations