use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by apache.
the class MetadataUtils method fromField.
/**
* Create a column metadata object that holds the given
* {@link MaterializedField}. The type of the object will be either a
* primitive, map or dict column, depending on the field's type. The logic
* here mimics the code as written, which is very messy in some places.
*
* @param field the materialized field to wrap
* @return the column metadata that wraps the field
*/
public static ColumnMetadata fromField(MaterializedField field) {
MajorType majorType = field.getType();
MinorType type = majorType.getMinorType();
switch(type) {
case DICT:
return MetadataUtils.newDict(field);
case MAP:
return MetadataUtils.newMap(field);
case UNION:
if (field.getType().getMode() != DataMode.OPTIONAL) {
throw new UnsupportedOperationException(type.name() + " type must be nullable");
}
return new VariantColumnMetadata(field);
case VARDECIMAL:
int precision = majorType.hasPrecision() ? majorType.getPrecision() : Types.maxPrecision(type);
int scale = majorType.hasScale() ? majorType.getScale() : 0;
return MetadataUtils.newDecimal(field.getName(), type, majorType.getMode(), precision, scale);
case LIST:
switch(field.getType().getMode()) {
case OPTIONAL:
return new VariantColumnMetadata(field);
case REPEATED:
return new RepeatedListColumnMetadata(field);
default:
throw new UnsupportedOperationException(String.format("Unsupported mode %s for type %s", field.getType().getMode().name(), type.name()));
}
default:
return new PrimitiveColumnMetadata(field);
}
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by apache.
the class BatchValidator method validateUnionVector.
private void validateUnionVector(String name, UnionVector vector) {
int valueCount = vector.getAccessor().getValueCount();
MapVector internalMap = vector.getTypeMap();
for (MinorType type : vector.getSubTypes()) {
if (type == MinorType.LATE) {
error(name, vector, String.format("Union vector includes illegal type LATE %s", type.name()));
continue;
}
// Warning: do not call getMember(type), doing so will create an
// empty map vector that causes validation to fail.
ValueVector child = internalMap.getChild(type.name());
if (child == null) {
// Disabling this check for now. TopNBatch, SortBatch
// and perhaps others will create vectors with a set of
// types, but won't actually populate some of the types.
//
// error(name, vector, String.format(
// "Union vector includes type %s, but the internal map has no matching member",
// type.name()));
} else {
validateVector(name + "-type-" + type.name(), valueCount, child);
}
}
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by apache.
the class TestVariantAccessors method testAddTypes.
/**
* Test a variant (AKA "union vector") at the top level, using
* just scalar values.
*/
@Test
public void testAddTypes() {
final TupleMetadata batchSchema = new SchemaBuilder().addNullable("v", MinorType.UNION).buildSchema();
final ExtendableRowSet rs = fixture.rowSet(batchSchema);
final RowSetWriter writer = rs.writer();
// Sanity check of writer structure
final ObjectWriter wo = writer.column(0);
assertEquals(ObjectType.VARIANT, wo.type());
final VariantWriter vw = wo.variant();
assertSame(vw, writer.variant(0));
assertSame(vw, writer.variant("v"));
for (final MinorType type : MinorType.values()) {
assertFalse(vw.hasType(type));
}
// Write values of different types
vw.scalar(MinorType.INT).setInt(10);
assertTrue(vw.hasType(MinorType.INT));
assertFalse(vw.hasType(MinorType.VARCHAR));
writer.save();
vw.scalar(MinorType.VARCHAR).setString("fred");
assertTrue(vw.hasType(MinorType.VARCHAR));
writer.save();
vw.setNull();
writer.save();
vw.scalar(MinorType.FLOAT8).setDouble(123.45);
assertTrue(vw.hasType(MinorType.INT));
assertTrue(vw.hasType(MinorType.FLOAT8));
writer.save();
final SingleRowSet result = writer.done();
assertEquals(4, result.rowCount());
// Read the values.
final RowSetReader reader = result.reader();
// Sanity check of structure
final ObjectReader ro = reader.column(0);
assertEquals(ObjectType.VARIANT, ro.type());
final VariantReader vr = ro.variant();
assertSame(vr, reader.variant(0));
assertSame(vr, reader.variant("v"));
for (final MinorType type : MinorType.values()) {
if (type == MinorType.INT || type == MinorType.VARCHAR || type == MinorType.FLOAT8) {
assertTrue(vr.hasType(type));
} else {
assertFalse(vr.hasType(type));
}
}
// Verify the data
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.INT);
assertSame(vr.scalar(MinorType.INT), vr.scalar());
assertNotNull(vr.member());
assertSame(vr.scalar(), vr.member().scalar());
assertEquals(10, vr.scalar().getInt());
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.VARCHAR);
assertSame(vr.scalar(MinorType.VARCHAR), vr.scalar());
assertEquals("fred", vr.scalar().getString());
assertTrue(reader.next());
assertTrue(vr.isNull());
assertNull(vr.dataType());
assertNull(vr.scalar());
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.FLOAT8);
assertSame(vr.scalar(MinorType.FLOAT8), vr.scalar());
assertEquals(123.45, vr.scalar().getDouble(), 0.001);
assertFalse(reader.next());
result.clear();
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by apache.
the class TestVariantAccessors method testScalarVariant.
/**
* Test a variant (AKA "union vector") at the top level, using
* just scalar values.
*/
@Test
public void testScalarVariant() {
final TupleMetadata schema = new SchemaBuilder().addUnion("u").addType(MinorType.INT).addType(MinorType.VARCHAR).addType(MinorType.FLOAT8).resumeSchema().buildSchema();
final ExtendableRowSet rs = fixture.rowSet(schema);
final RowSetWriter writer = rs.writer();
// Sanity check of writer structure
final ObjectWriter wo = writer.column(0);
assertEquals(ObjectType.VARIANT, wo.type());
final VariantWriter vw = wo.variant();
assertSame(vw, writer.variant(0));
assertSame(vw, writer.variant("u"));
assertTrue(vw.hasType(MinorType.INT));
assertTrue(vw.hasType(MinorType.VARCHAR));
assertTrue(vw.hasType(MinorType.FLOAT8));
// Write values of different types
vw.scalar(MinorType.INT).setInt(10);
writer.save();
vw.scalar(MinorType.VARCHAR).setString("fred");
writer.save();
// The entire variant is null
vw.setNull();
writer.save();
vw.scalar(MinorType.FLOAT8).setDouble(123.45);
writer.save();
// Strange case: just the value is null, but the variant
// is not null.
vw.scalar(MinorType.INT).setNull();
writer.save();
// Marker to avoid fill-empty issues (fill-empties tested elsewhere.)
vw.scalar(MinorType.INT).setInt(20);
writer.save();
final SingleRowSet result = writer.done();
assertEquals(6, result.rowCount());
// Read the values.
final RowSetReader reader = result.reader();
// Sanity check of structure
final ObjectReader ro = reader.column(0);
assertEquals(ObjectType.VARIANT, ro.type());
final VariantReader vr = ro.variant();
assertSame(vr, reader.variant(0));
assertSame(vr, reader.variant("u"));
for (final MinorType type : MinorType.values()) {
if (type == MinorType.INT || type == MinorType.VARCHAR || type == MinorType.FLOAT8) {
assertTrue(vr.hasType(type));
} else {
assertFalse(vr.hasType(type));
}
}
// Can get readers up front
final ScalarReader intReader = vr.scalar(MinorType.INT);
final ScalarReader strReader = vr.scalar(MinorType.VARCHAR);
final ScalarReader floatReader = vr.scalar(MinorType.FLOAT8);
// Verify the data
// Int 10
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.INT);
assertSame(intReader, vr.scalar());
assertNotNull(vr.member());
assertSame(vr.scalar(), vr.member().scalar());
assertFalse(intReader.isNull());
assertEquals(10, intReader.getInt());
assertTrue(strReader.isNull());
assertTrue(floatReader.isNull());
// String "fred"
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.VARCHAR);
assertSame(strReader, vr.scalar());
assertFalse(strReader.isNull());
assertEquals("fred", strReader.getString());
assertTrue(intReader.isNull());
assertTrue(floatReader.isNull());
// Null value
assertTrue(reader.next());
assertTrue(vr.isNull());
assertNull(vr.dataType());
assertNull(vr.scalar());
assertTrue(intReader.isNull());
assertTrue(strReader.isNull());
assertTrue(floatReader.isNull());
// Double 123.45
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.FLOAT8);
assertSame(floatReader, vr.scalar());
assertFalse(floatReader.isNull());
assertEquals(123.45, vr.scalar().getDouble(), 0.001);
assertTrue(intReader.isNull());
assertTrue(strReader.isNull());
// Strange case: null int (but union is not null)
assertTrue(reader.next());
assertFalse(vr.isNull());
assertSame(vr.dataType(), MinorType.INT);
assertTrue(intReader.isNull());
// Int 20
assertTrue(reader.next());
assertFalse(vr.isNull());
assertFalse(intReader.isNull());
assertEquals(20, intReader.getInt());
assertFalse(reader.next());
result.clear();
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by apache.
the class TestVariantAccessors method testBuildRowSetUnionArray.
@Test
public void testBuildRowSetUnionArray() {
final TupleMetadata schema = new SchemaBuilder().addList("list1").addType(MinorType.BIGINT).addMap().addNullable("a", MinorType.INT).addNullable("b", MinorType.VARCHAR).resumeUnion().addList().addType(MinorType.FLOAT8).resumeUnion().resumeSchema().buildSchema();
final ExtendableRowSet rowSet = fixture.rowSet(schema);
final VectorContainer vc = rowSet.container();
assertEquals(1, vc.getNumberOfColumns());
// List with complex internal structure
final ValueVector vector = vc.getValueVector(0).getValueVector();
assertTrue(vector instanceof ListVector);
final ListVector list = (ListVector) vector;
assertTrue(list.getDataVector() instanceof UnionVector);
final UnionVector union = (UnionVector) list.getDataVector();
// Union inside the list
final MajorType unionType = union.getField().getType();
final List<MinorType> types = unionType.getSubTypeList();
assertEquals(3, types.size());
assertTrue(types.contains(MinorType.BIGINT));
assertTrue(types.contains(MinorType.MAP));
assertTrue(types.contains(MinorType.LIST));
final MapVector typeMap = union.getTypeMap();
ValueVector member = typeMap.getChild(MinorType.BIGINT.name());
assertTrue(member instanceof NullableBigIntVector);
// Map inside the list
member = typeMap.getChild(MinorType.MAP.name());
assertTrue(member instanceof MapVector);
final MapVector childMap = (MapVector) member;
ValueVector mapMember = childMap.getChild("a");
assertNotNull(mapMember);
assertTrue(mapMember instanceof NullableIntVector);
mapMember = childMap.getChild("b");
assertNotNull(mapMember);
assertTrue(mapMember instanceof NullableVarCharVector);
// Single-type list inside the outer list
member = typeMap.getChild(MinorType.LIST.name());
assertTrue(member instanceof ListVector);
final ListVector childList = (ListVector) member;
assertTrue(childList.getDataVector() instanceof NullableFloat8Vector);
rowSet.clear();
}
Aggregations