Search in sources :

Example 1 with FieldReference

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());
}
Also used : IfExpression(org.apache.drill.common.expression.IfExpression) FieldReference(org.apache.drill.common.expression.FieldReference) ValueExpressions(org.apache.drill.common.expression.ValueExpressions) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) NonStrictExpectations(mockit.NonStrictExpectations) ExecTest(org.apache.drill.exec.ExecTest) Test(org.junit.Test)

Example 2 with FieldReference

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());
}
Also used : ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) FieldReference(org.apache.drill.common.expression.FieldReference) SchemaPath(org.apache.drill.common.expression.SchemaPath) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) NonStrictExpectations(mockit.NonStrictExpectations) ExecTest(org.apache.drill.exec.ExecTest) Test(org.junit.Test)

Example 3 with FieldReference

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;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) FieldReference(org.apache.drill.common.expression.FieldReference) ValueExpressions(org.apache.drill.common.expression.ValueExpressions)

Example 4 with FieldReference

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;
}
Also used : FieldReference(org.apache.drill.common.expression.FieldReference) SchemaPath(org.apache.drill.common.expression.SchemaPath) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) ArrayList(java.util.ArrayList)

Example 5 with FieldReference

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;
}
Also used : LogicalExpression(org.apache.drill.common.expression.LogicalExpression) FieldReference(org.apache.drill.common.expression.FieldReference) NamedExpression(org.apache.drill.common.logical.data.NamedExpression) ValueExpressions(org.apache.drill.common.expression.ValueExpressions) MaterializedField(org.apache.drill.exec.record.MaterializedField) FunctionCall(org.apache.drill.common.expression.FunctionCall)

Aggregations

FieldReference (org.apache.drill.common.expression.FieldReference)84 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)38 Test (org.junit.Test)22 NamedExpression (org.apache.drill.common.logical.data.NamedExpression)19 FunctionCall (org.apache.drill.common.expression.FunctionCall)16 SchemaPath (org.apache.drill.common.expression.SchemaPath)14 Ordering (org.apache.drill.common.logical.data.Order.Ordering)14 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)13 ArrayList (java.util.ArrayList)12 ValueExpressions (org.apache.drill.common.expression.ValueExpressions)12 MaterializedField (org.apache.drill.exec.record.MaterializedField)12 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)10 RelFieldCollation (org.apache.calcite.rel.RelFieldCollation)9 Order (org.apache.drill.common.logical.data.Order)9 ExecTest (org.apache.drill.exec.ExecTest)9 AggregateCall (org.apache.calcite.rel.core.AggregateCall)8 OperatorTest (org.apache.drill.categories.OperatorTest)8 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)8 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)7 VectorTest (org.apache.drill.categories.VectorTest)6