use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by axbaretto.
the class JsonWriter method writeValue.
private void writeValue(FieldReader reader) throws JsonGenerationException, IOException {
final DataMode m = reader.getType().getMode();
final MinorType mt = reader.getType().getMinorType();
switch(m) {
case OPTIONAL:
case REQUIRED:
switch(mt) {
case FLOAT4:
gen.writeFloat(reader);
break;
case FLOAT8:
gen.writeDouble(reader);
break;
case INT:
gen.writeInt(reader);
break;
case SMALLINT:
gen.writeSmallInt(reader);
break;
case TINYINT:
gen.writeTinyInt(reader);
break;
case BIGINT:
gen.writeBigInt(reader);
break;
case BIT:
gen.writeBoolean(reader);
break;
case DATE:
gen.writeDate(reader);
break;
case TIME:
gen.writeTime(reader);
break;
case TIMESTAMP:
gen.writeTimestamp(reader);
break;
case INTERVALYEAR:
case INTERVALDAY:
case INTERVAL:
gen.writeInterval(reader);
break;
case DECIMAL28DENSE:
case DECIMAL28SPARSE:
case DECIMAL38DENSE:
case DECIMAL38SPARSE:
case DECIMAL9:
case DECIMAL18:
gen.writeDecimal(reader);
break;
case LIST:
// this is a pseudo class, doesn't actually contain the real reader so we have to drop down.
gen.writeStartArray();
while (reader.next()) {
writeValue(reader.reader());
}
gen.writeEndArray();
break;
case MAP:
gen.writeStartObject();
if (reader.isSet()) {
for (String name : reader) {
FieldReader childReader = reader.reader(name);
if (childReader.isSet()) {
gen.writeFieldName(name);
writeValue(childReader);
}
}
}
gen.writeEndObject();
break;
case NULL:
case LATE:
gen.writeUntypedNull();
break;
case VAR16CHAR:
gen.writeVar16Char(reader);
break;
case VARBINARY:
gen.writeBinary(reader);
break;
case VARCHAR:
gen.writeVarChar(reader);
break;
}
break;
case REPEATED:
gen.writeStartArray();
switch(mt) {
case FLOAT4:
for (int i = 0; i < reader.size(); i++) {
gen.writeFloat(i, reader);
}
break;
case FLOAT8:
for (int i = 0; i < reader.size(); i++) {
gen.writeDouble(i, reader);
}
break;
case INT:
for (int i = 0; i < reader.size(); i++) {
gen.writeInt(i, reader);
}
break;
case SMALLINT:
for (int i = 0; i < reader.size(); i++) {
gen.writeSmallInt(i, reader);
}
break;
case TINYINT:
for (int i = 0; i < reader.size(); i++) {
gen.writeTinyInt(i, reader);
}
break;
case BIGINT:
for (int i = 0; i < reader.size(); i++) {
gen.writeBigInt(i, reader);
}
break;
case BIT:
for (int i = 0; i < reader.size(); i++) {
gen.writeBoolean(i, reader);
}
break;
case DATE:
for (int i = 0; i < reader.size(); i++) {
gen.writeDate(i, reader);
}
break;
case TIME:
for (int i = 0; i < reader.size(); i++) {
gen.writeTime(i, reader);
}
break;
case TIMESTAMP:
for (int i = 0; i < reader.size(); i++) {
gen.writeTimestamp(i, reader);
}
break;
case INTERVALYEAR:
case INTERVALDAY:
case INTERVAL:
for (int i = 0; i < reader.size(); i++) {
gen.writeInterval(i, reader);
}
break;
case DECIMAL28DENSE:
case DECIMAL28SPARSE:
case DECIMAL38DENSE:
case DECIMAL38SPARSE:
case DECIMAL9:
case DECIMAL18:
for (int i = 0; i < reader.size(); i++) {
gen.writeDecimal(i, reader);
}
break;
case LIST:
for (int i = 0; i < reader.size(); i++) {
while (reader.next()) {
writeValue(reader.reader());
}
}
break;
case MAP:
while (reader.next()) {
gen.writeStartObject();
for (String name : reader) {
FieldReader mapField = reader.reader(name);
if (mapField.isSet()) {
gen.writeFieldName(name);
writeValue(mapField);
}
}
gen.writeEndObject();
}
break;
case NULL:
break;
case VAR16CHAR:
for (int i = 0; i < reader.size(); i++) {
gen.writeVar16Char(i, reader);
}
break;
case VARBINARY:
for (int i = 0; i < reader.size(); i++) {
gen.writeBinary(i, reader);
}
break;
case VARCHAR:
for (int i = 0; i < reader.size(); i++) {
gen.writeVarChar(i, reader);
}
break;
default:
throw new IllegalStateException(String.format("Unable to handle type %s.", mt));
}
gen.writeEndArray();
break;
}
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by axbaretto.
the class TestFillEmpties method doFillEmptiesScalar.
private void doFillEmptiesScalar(MajorType majorType) {
TupleMetadata schema = new SchemaBuilder().add("a", majorType).buildSchema();
ExtendableRowSet rs = fixture.rowSet(schema);
RowSetWriter writer = rs.writer();
ScalarWriter colWriter = writer.scalar(0);
ValueType valueType = colWriter.valueType();
boolean nullable = majorType.getMode() == DataMode.OPTIONAL;
for (int i = 0; i < ROW_COUNT; i++) {
if (i % 5 == 0) {
colWriter.setObject(RowSetUtilities.testDataFromInt(valueType, majorType, i));
}
writer.save();
}
SingleRowSet result = writer.done();
RowSetReader reader = result.reader();
ScalarReader colReader = reader.scalar(0);
MinorType type = majorType.getMinorType();
boolean isVariable = (type == MinorType.VARCHAR || type == MinorType.VAR16CHAR || type == MinorType.VARBINARY);
for (int i = 0; i < ROW_COUNT; i++) {
assertTrue(reader.next());
if (i % 5 != 0) {
if (nullable) {
// Nullable types fill with nulls.
assertTrue(colReader.isNull());
continue;
}
if (isVariable) {
// Variable width types fill with a zero-length value.
assertEquals(0, colReader.getBytes().length);
continue;
}
}
// All other types fill with zero-bytes, interpreted as some form
// of zero for each type.
Object actual = colReader.getObject();
Object expected = RowSetUtilities.testDataFromInt(valueType, majorType, i % 5 == 0 ? i : 0);
RowSetUtilities.assertEqualValues(majorType.toString().replace('\n', ' ') + "[" + i + "]", valueType, expected, actual);
}
result.clear();
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by apache.
the class FragmentContextImpl method getConstantValueHolder.
@Override
public ValueHolder getConstantValueHolder(String value, MinorType type, Function<DrillBuf, ValueHolder> holderInitializer) {
if (!constantValueHolderCache.containsKey(value)) {
constantValueHolderCache.put(value, new HashMap<>());
}
Map<MinorType, ValueHolder> holdersByType = constantValueHolderCache.get(value);
ValueHolder valueHolder = holdersByType.get(type);
if (valueHolder == null) {
valueHolder = holderInitializer.apply(getManagedBuffer());
holdersByType.put(type, valueHolder);
}
return valueHolder;
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by apache.
the class QueryContext method getConstantValueHolder.
@Override
public ValueHolder getConstantValueHolder(String value, MinorType type, Function<DrillBuf, ValueHolder> holderInitializer) {
if (!constantValueHolderCache.containsKey(value)) {
constantValueHolderCache.put(value, Maps.<MinorType, ValueHolder>newHashMap());
}
Map<MinorType, ValueHolder> holdersByType = constantValueHolderCache.get(value);
ValueHolder valueHolder = holdersByType.get(type);
if (valueHolder == null) {
valueHolder = holderInitializer.apply(getManagedBuffer());
holdersByType.put(type, valueHolder);
}
return valueHolder;
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by apache.
the class FieldIdUtil method getFieldId.
public static TypedFieldId getFieldId(ValueVector vector, int id, SchemaPath expectedPath, boolean hyper) {
if (!expectedPath.getRootSegment().getPath().equalsIgnoreCase(vector.getField().getName())) {
return null;
}
PathSegment seg = expectedPath.getRootSegment();
TypedFieldId.Builder builder = TypedFieldId.newBuilder().hyper(hyper);
if (vector instanceof UnionVector) {
builder.addId(id).remainder(expectedPath.getRootSegment().getChild());
List<MinorType> minorTypes = ((UnionVector) vector).getSubTypes();
MajorType.Builder majorTypeBuilder = MajorType.newBuilder().setMinorType(MinorType.UNION);
for (MinorType type : minorTypes) {
majorTypeBuilder.addSubType(type);
}
MajorType majorType = majorTypeBuilder.build();
builder.intermediateType(majorType);
if (seg.isLastPath()) {
builder.finalType(majorType);
return builder.build();
} else {
return getFieldIdIfMatchesUnion((UnionVector) vector, builder, false, seg.getChild());
}
} else if (vector instanceof ListVector) {
builder.intermediateType(vector.getField().getType());
builder.addId(id);
return getFieldIdIfMatches(vector, builder, true, expectedPath.getRootSegment().getChild());
} else if (vector instanceof DictVector) {
MajorType vectorType = vector.getField().getType();
builder.intermediateType(vectorType);
builder.addId(id);
if (seg.isLastPath()) {
builder.finalType(vectorType);
return builder.build();
} else {
PathSegment child = seg.getChild();
builder.remainder(child);
return getFieldIdIfMatches(vector, builder, false, expectedPath.getRootSegment().getChild());
}
} else if (vector instanceof AbstractContainerVector) {
// we're looking for a multi path.
builder.intermediateType(vector.getField().getType());
builder.addId(id);
return getFieldIdIfMatches(vector, builder, true, expectedPath.getRootSegment().getChild());
} else if (vector instanceof RepeatedDictVector) {
MajorType vectorType = vector.getField().getType();
builder.intermediateType(vectorType);
builder.addId(id);
if (seg.isLastPath()) {
builder.finalType(vectorType);
return builder.build();
} else {
PathSegment child = seg.getChild();
if (!child.isArray()) {
return null;
} else {
builder.remainder(child);
builder.withIndex();
if (child.isLastPath()) {
return builder.finalType(DictVector.TYPE).build();
} else {
return getFieldIdIfMatches(vector, builder, true, expectedPath.getRootSegment().getChild());
}
}
}
} else {
builder.intermediateType(vector.getField().getType());
builder.addId(id);
builder.finalType(vector.getField().getType());
if (seg.isLastPath()) {
return builder.build();
} else {
PathSegment child = seg.getChild();
if (child.isArray() && child.isLastPath()) {
builder.remainder(child);
builder.withIndex();
builder.finalType(vector.getField().getType().toBuilder().setMode(DataMode.OPTIONAL).build());
return builder.build();
} else {
return null;
}
}
}
}
Aggregations