use of org.apache.drill.common.expression.FieldReference in project drill by apache.
the class ExpressionTreeMaterializerTest method testMaterializingLateboundTree.
@Test
public void testMaterializingLateboundTree(@Injectable final RecordBatch batch) throws SchemaChangeException {
new NonStrictExpectations() {
{
batch.getValueVectorId(SchemaPath.getSimplePath("test"));
result = new TypedFieldId(Types.required(MinorType.BIT), -4);
batch.getValueVectorId(SchemaPath.getSimplePath("test1"));
result = new TypedFieldId(Types.required(MinorType.BIGINT), -5);
}
};
ErrorCollector ec = new ErrorCollectorImpl();
LogicalExpression elseExpression = new IfExpression.Builder().setElse(new ValueExpressions.LongExpression(1L, ExpressionPosition.UNKNOWN)).setIfCondition(new IfExpression.IfCondition(new ValueExpressions.BooleanExpression("true", ExpressionPosition.UNKNOWN), new FieldReference("test1", ExpressionPosition.UNKNOWN))).build();
LogicalExpression expr = new IfExpression.Builder().setIfCondition(new IfExpression.IfCondition(new FieldReference("test", ExpressionPosition.UNKNOWN), new ValueExpressions.LongExpression(2L, ExpressionPosition.UNKNOWN))).setElse(elseExpression).build();
LogicalExpression newExpr = ExpressionTreeMaterializer.materialize(expr, batch, ec, registry);
assertTrue(newExpr instanceof IfExpression);
IfExpression newIfExpr = (IfExpression) newExpr;
//assertEquals(1, newIfExpr.conditions.size());
IfExpression.IfCondition ifCondition = newIfExpr.ifCondition;
assertTrue(newIfExpr.elseExpression instanceof IfExpression);
//assertEquals(1, newIfExpr.conditions.size());
//ifCondition = newIfExpr.conditions.get(0);
assertEquals(bigIntType, ifCondition.expression.getMajorType());
assertEquals(true, ((ValueExpressions.BooleanExpression) ((IfExpression) (newIfExpr.elseExpression)).ifCondition.condition).value);
if (ec.hasErrors()) {
System.out.println(ec.toErrorString());
}
assertFalse(ec.hasErrors());
}
use of org.apache.drill.common.expression.FieldReference in project drill by apache.
the class ExpressionTreeMaterializerTest method testMaterializingLateboundField.
@Test
public void testMaterializingLateboundField(@Injectable final RecordBatch batch) throws SchemaChangeException {
final SchemaBuilder builder = BatchSchema.newBuilder();
builder.addField(getField(2, "test", bigIntType));
final BatchSchema schema = builder.build();
new NonStrictExpectations() {
{
batch.getValueVectorId(new SchemaPath("test", ExpressionPosition.UNKNOWN));
result = new TypedFieldId(Types.required(MinorType.BIGINT), -5);
}
};
ErrorCollector ec = new ErrorCollectorImpl();
LogicalExpression expr = ExpressionTreeMaterializer.materialize(new FieldReference("test", ExpressionPosition.UNKNOWN), batch, ec, registry);
assertEquals(bigIntType, expr.getMajorType());
assertFalse(ec.hasErrors());
}
use of org.apache.drill.common.expression.FieldReference in project drill by apache.
the class DrillAggregateRel method toDrill.
public static LogicalExpression toDrill(AggregateCall call, List<String> fn, DrillImplementor implementor) {
List<LogicalExpression> args = Lists.newArrayList();
for (Integer i : call.getArgList()) {
args.add(new FieldReference(fn.get(i)));
}
// for count(1).
if (args.isEmpty()) {
args.add(new ValueExpressions.LongExpression(1l));
}
LogicalExpression expr = FunctionCallFactory.createExpression(call.getAggregation().getName().toLowerCase(), ExpressionPosition.UNKNOWN, args);
return expr;
}
use of org.apache.drill.common.expression.FieldReference in project drill by apache.
the class PhysicalOpUnitTestBase method parseExprs.
protected List<NamedExpression> parseExprs(String... expressionsAndOutputNames) {
Preconditions.checkArgument(expressionsAndOutputNames.length % 2 == 0, "List of expressions and output field names" + " is not complete, each expression must explicitly give and output name,");
List<NamedExpression> ret = new ArrayList<>();
for (int i = 0; i < expressionsAndOutputNames.length; i += 2) {
ret.add(new NamedExpression(parseExpr(expressionsAndOutputNames[i]), new FieldReference(new SchemaPath(new PathSegment.NameSegment(expressionsAndOutputNames[i + 1])))));
}
return ret;
}
use of org.apache.drill.common.expression.FieldReference in project drill by axbaretto.
the class ProjectRecordBatch method getExpressionList.
private List<NamedExpression> getExpressionList() {
if (popConfig.getExprs() != null) {
return popConfig.getExprs();
}
final List<NamedExpression> exprs = Lists.newArrayList();
for (final MaterializedField field : incoming.getSchema()) {
String fieldName = field.getName();
if (Types.isComplex(field.getType()) || Types.isRepeated(field.getType())) {
final LogicalExpression convertToJson = FunctionCallFactory.createConvert(ConvertExpression.CONVERT_TO, "JSON", SchemaPath.getSimplePath(fieldName), ExpressionPosition.UNKNOWN);
final String castFuncName = CastFunctions.getCastFunc(MinorType.VARCHAR);
final List<LogicalExpression> castArgs = Lists.newArrayList();
// input_expr
castArgs.add(convertToJson);
// implicitly casting to varchar, since we don't know actual source length, cast to undefined length, which will preserve source length
castArgs.add(new ValueExpressions.LongExpression(Types.MAX_VARCHAR_LENGTH, null));
final FunctionCall castCall = new FunctionCall(castFuncName, castArgs, ExpressionPosition.UNKNOWN);
exprs.add(new NamedExpression(castCall, new FieldReference(fieldName)));
} else {
exprs.add(new NamedExpression(SchemaPath.getSimplePath(fieldName), new FieldReference(fieldName)));
}
}
return exprs;
}
Aggregations