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