Search in sources :

Example 31 with MajorType

use of org.apache.drill.common.types.TypeProtos.MajorType in project drill by apache.

the class DrillColumnMetaDataListTest method setUp.

@Before
public void setUp() throws Exception {
    emptyList = new DrillColumnMetaDataList();
    // Create mock columns
    final MaterializedField exampleIntField = mock(MaterializedField.class);
    MajorType exampleIntType = MajorType.newBuilder().setMinorType(MinorType.INT).build();
    when(exampleIntField.getPath()).thenReturn("/path/to/testInt");
    when(exampleIntField.getType()).thenReturn(exampleIntType);
    when(exampleIntField.getDataMode()).thenReturn(DataMode.OPTIONAL);
    final MaterializedField exampleStringField = mock(MaterializedField.class);
    MajorType exampleStringType = MajorType.newBuilder().setMinorType(MinorType.VARCHAR).build();
    when(exampleStringField.getPath()).thenReturn("/path/to/testString");
    when(exampleStringField.getType()).thenReturn(exampleStringType);
    when(exampleStringField.getDataMode()).thenReturn(DataMode.REQUIRED);
    oneElementList = new DrillColumnMetaDataList();
    BatchSchema oneElementSchema = mock(BatchSchema.class);
    when(oneElementSchema.getFieldCount()).thenReturn(1);
    doAnswer(new Answer<MaterializedField>() {

        @Override
        public MaterializedField answer(InvocationOnMock invocationOnMock) throws Throwable {
            Integer index = (Integer) invocationOnMock.getArguments()[0];
            if (index == 0) {
                return exampleIntField;
            }
            return null;
        }
    }).when(oneElementSchema).getColumn(Mockito.anyInt());
    List<Class<?>> oneClassList = new ArrayList<>();
    oneClassList.add(Integer.class);
    oneElementList.updateColumnMetaData("testCatalog", "testSchema", "testTable", oneElementSchema, oneClassList);
    twoElementList = new DrillColumnMetaDataList();
    BatchSchema twoElementSchema = mock(BatchSchema.class);
    when(twoElementSchema.getFieldCount()).thenReturn(2);
    doAnswer(new Answer<MaterializedField>() {

        @Override
        public MaterializedField answer(InvocationOnMock invocationOnMock) throws Throwable {
            Integer index = (Integer) invocationOnMock.getArguments()[0];
            if (index == 0) {
                return exampleIntField;
            } else if (index == 1) {
                return exampleStringField;
            }
            return null;
        }
    }).when(twoElementSchema).getColumn(Mockito.anyInt());
    List<Class<?>> twoClassList = new ArrayList<>();
    twoClassList.add(Integer.class);
    twoClassList.add(String.class);
    twoElementList.updateColumnMetaData("testCatalog", "testSchema", "testTable", twoElementSchema, twoClassList);
}
Also used : BatchSchema(org.apache.drill.exec.record.BatchSchema) DrillColumnMetaDataList(org.apache.drill.jdbc.impl.DrillColumnMetaDataList) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) ArrayList(java.util.ArrayList) MaterializedField(org.apache.drill.exec.record.MaterializedField) Before(org.junit.Before)

Example 32 with MajorType

use of org.apache.drill.common.types.TypeProtos.MajorType in project drill by apache.

the class ExpressionTreeMaterializerTest method testMaterializingLateboundTreeValidated.

@Test
public void testMaterializingLateboundTreeValidated(@Injectable final RecordBatch batch) throws SchemaChangeException {
    ErrorCollector ec = new ErrorCollector() {

        int errorCount = 0;

        @Override
        public void addGeneralError(ExpressionPosition expr, String s) {
            errorCount++;
        }

        @Override
        public void addUnexpectedArgumentType(ExpressionPosition expr, String name, MajorType actual, MajorType[] expected, int argumentIndex) {
            errorCount++;
        }

        @Override
        public void addUnexpectedArgumentCount(ExpressionPosition expr, int actual, Range<Integer> expected) {
            errorCount++;
        }

        @Override
        public void addUnexpectedArgumentCount(ExpressionPosition expr, int actual, int expected) {
            errorCount++;
        }

        @Override
        public void addNonNumericType(ExpressionPosition expr, MajorType actual) {
            errorCount++;
        }

        @Override
        public void addUnexpectedType(ExpressionPosition expr, int index, MajorType actual) {
            errorCount++;
        }

        @Override
        public void addExpectedConstantValue(ExpressionPosition expr, int actual, String s) {
            errorCount++;
        }

        @Override
        public boolean hasErrors() {
            return errorCount > 0;
        }

        @Override
        public String toErrorString() {
            return String.format("Found %s errors.", errorCount);
        }

        @Override
        public int getErrorCount() {
            return errorCount;
        }
    };
    new NonStrictExpectations() {

        {
            batch.getValueVectorId(new SchemaPath("test", ExpressionPosition.UNKNOWN));
            result = new TypedFieldId(Types.required(MinorType.BIGINT), -5);
        }
    };
    new MockUp<RemoteFunctionRegistry>() {

        @Mock
        long getRegistryVersion() {
            return 0L;
        }
    };
    LogicalExpression functionCallExpr = new FunctionCall("testFunc", ImmutableList.of((LogicalExpression) new FieldReference("test", ExpressionPosition.UNKNOWN)), ExpressionPosition.UNKNOWN);
    LogicalExpression newExpr = ExpressionTreeMaterializer.materialize(functionCallExpr, batch, ec, registry);
    assertTrue(newExpr instanceof TypedNullConstant);
    assertEquals(1, ec.getErrorCount());
    System.out.println(ec.toErrorString());
}
Also used : FieldReference(org.apache.drill.common.expression.FieldReference) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) MockUp(mockit.MockUp) TypedNullConstant(org.apache.drill.common.expression.TypedNullConstant) Range(com.google.common.collect.Range) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) SchemaPath(org.apache.drill.common.expression.SchemaPath) FunctionCall(org.apache.drill.common.expression.FunctionCall) ExpressionPosition(org.apache.drill.common.expression.ExpressionPosition) NonStrictExpectations(mockit.NonStrictExpectations) ExecTest(org.apache.drill.exec.ExecTest) Test(org.junit.Test)

Example 33 with MajorType

use of org.apache.drill.common.types.TypeProtos.MajorType in project drill by apache.

the class ExpressionStringBuilder method visitCastExpression.

@Override
public Void visitCastExpression(CastExpression e, StringBuilder sb) throws RuntimeException {
    MajorType mt = e.getMajorType();
    sb.append("cast( (");
    e.getInput().accept(this, sb);
    sb.append(" ) as ");
    sb.append(mt.getMinorType().name());
    switch(mt.getMinorType()) {
        case FLOAT4:
        case FLOAT8:
        case BIT:
        case INT:
        case TINYINT:
        case SMALLINT:
        case BIGINT:
        case UINT1:
        case UINT2:
        case UINT4:
        case UINT8:
        case DATE:
        case TIMESTAMP:
        case TIMESTAMPTZ:
        case TIME:
        case INTERVAL:
        case INTERVALDAY:
        case INTERVALYEAR:
            // do nothing else.
            break;
        case VAR16CHAR:
        case VARBINARY:
        case VARCHAR:
        case FIXED16CHAR:
        case FIXEDBINARY:
        case FIXEDCHAR:
            // add size in parens
            sb.append("(");
            sb.append(mt.getPrecision());
            sb.append(")");
            break;
        case DECIMAL9:
        case DECIMAL18:
        case DECIMAL28DENSE:
        case DECIMAL28SPARSE:
        case DECIMAL38DENSE:
        case DECIMAL38SPARSE:
            // add scale and precision
            sb.append("(");
            sb.append(mt.getPrecision());
            sb.append(", ");
            sb.append(mt.getScale());
            sb.append(")");
            break;
        default:
            throw new UnsupportedOperationException(String.format("Unable to convert cast expression %s into string.", e));
    }
    sb.append(" )");
    return null;
}
Also used : MajorType(org.apache.drill.common.types.TypeProtos.MajorType)

Example 34 with MajorType

use of org.apache.drill.common.types.TypeProtos.MajorType in project drill by apache.

the class ExpressionValidator method visitIfExpression.

@Override
public Void visitIfExpression(IfExpression ifExpr, ErrorCollector errors) throws RuntimeException {
    // confirm that all conditions are required boolean values.
    IfCondition cond = ifExpr.ifCondition;
    MajorType majorType = cond.condition.getMajorType();
    if (majorType.getMinorType() != MinorType.BIT) {
        errors.addGeneralError(cond.condition.getPosition(), String.format("Failure composing If Expression.  All conditions must return a boolean type.  Condition was of Type %s.", majorType.getMinorType()));
    }
    // confirm that all outcomes are the same type.
    final MajorType mt = ifExpr.elseExpression.getMajorType();
    cond = ifExpr.ifCondition;
    MajorType innerT = cond.expression.getMajorType();
    if (//
    (innerT.getMode() == DataMode.REPEATED && mt.getMode() != DataMode.REPEATED) || ((innerT.getMinorType() != mt.getMinorType()) && (innerT.getMode() != DataMode.OPTIONAL && mt.getMode() != DataMode.OPTIONAL && (innerT.getMinorType() != MinorType.NULL && mt.getMinorType() != MinorType.NULL)))) {
        errors.addGeneralError(cond.condition.getPosition(), String.format("Failure composing If Expression.  All expressions must return the same MinorType as the else expression.  The if condition returned type type %s but the else expression was of type %s", innerT, mt));
    }
    return null;
}
Also used : MajorType(org.apache.drill.common.types.TypeProtos.MajorType) IfCondition(org.apache.drill.common.expression.IfExpression.IfCondition)

Aggregations

MajorType (org.apache.drill.common.types.TypeProtos.MajorType)34 MaterializedField (org.apache.drill.exec.record.MaterializedField)13 MinorType (org.apache.drill.common.types.TypeProtos.MinorType)8 ValueVector (org.apache.drill.exec.vector.ValueVector)8 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)7 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)5 JVar (com.sun.codemodel.JVar)4 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)4 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)4 ArrayList (java.util.ArrayList)3 ExecutionSetupException (org.apache.drill.common.exceptions.ExecutionSetupException)3 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)3 FunctionCall (org.apache.drill.common.expression.FunctionCall)3 SchemaPath (org.apache.drill.common.expression.SchemaPath)3 ImmutableList (com.google.common.collect.ImmutableList)2 JClass (com.sun.codemodel.JClass)2 JExpression (com.sun.codemodel.JExpression)2 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)2 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)2 IfCondition (org.apache.drill.common.expression.IfExpression.IfCondition)2