use of com.facebook.presto.spi.type.Type in project presto by prestodb.
the class TestStateCompiler method testComplexSerialization.
@Test
public void testComplexSerialization() {
Type arrayType = new ArrayType(BIGINT);
Type mapType = new MapType(BIGINT, VARCHAR);
Map<String, Type> fieldMap = ImmutableMap.of("Block", arrayType, "AnotherBlock", mapType);
AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
AccumulatorStateSerializer<TestComplexState> serializer = StateCompiler.generateStateSerializer(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
TestComplexState singleState = factory.createSingleState();
TestComplexState deserializedState = factory.createSingleState();
singleState.setBoolean(true);
singleState.setLong(1);
singleState.setDouble(2.0);
singleState.setByte((byte) 3);
singleState.setSlice(utf8Slice("test"));
singleState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
singleState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
singleState.setBlock(array);
BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
BIGINT.writeLong(mapBlockBuilder, 123L);
VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
Block map = mapBlockBuilder.build();
singleState.setAnotherBlock(map);
BlockBuilder builder = new RowType(ImmutableList.of(BOOLEAN, TINYINT, DOUBLE, BIGINT, mapType, VARBINARY, arrayType, VARBINARY, VARBINARY), Optional.empty()).createBlockBuilder(new BlockBuilderStatus(), 1);
serializer.serialize(singleState, builder);
Block block = builder.build();
serializer.deserialize(block, 0, deserializedState);
assertEquals(deserializedState.getBoolean(), singleState.getBoolean());
assertEquals(deserializedState.getLong(), singleState.getLong());
assertEquals(deserializedState.getDouble(), singleState.getDouble());
assertEquals(deserializedState.getByte(), singleState.getByte());
assertEquals(deserializedState.getSlice(), singleState.getSlice());
assertEquals(deserializedState.getAnotherSlice(), singleState.getAnotherSlice());
assertEquals(deserializedState.getYetAnotherSlice(), singleState.getYetAnotherSlice());
assertEquals(deserializedState.getBlock().getLong(0, 0), singleState.getBlock().getLong(0, 0));
assertEquals(deserializedState.getAnotherBlock().getLong(0, 0), singleState.getAnotherBlock().getLong(0, 0));
assertEquals(deserializedState.getAnotherBlock().getSlice(1, 0, 9), singleState.getAnotherBlock().getSlice(1, 0, 9));
}
use of com.facebook.presto.spi.type.Type in project presto by prestodb.
the class TestStateCompiler method testComplexStateEstimatedSize.
@Test
public void testComplexStateEstimatedSize() {
Map<String, Type> fieldMap = ImmutableMap.of("Block", new ArrayType(BIGINT), "AnotherBlock", new MapType(BIGINT, VARCHAR));
AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
TestComplexState groupedState = factory.createGroupedState();
assertEquals(groupedState.getEstimatedSize(), 76064);
for (int i = 0; i < 1000; i++) {
((GroupedAccumulatorState) groupedState).setGroupId(i);
groupedState.setBoolean(true);
groupedState.setLong(1);
groupedState.setDouble(2.0);
groupedState.setByte((byte) 3);
groupedState.setSlice(utf8Slice("test"));
groupedState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
groupedState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
groupedState.setBlock(array);
BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
BIGINT.writeLong(mapBlockBuilder, 123L);
VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
Block map = mapBlockBuilder.build();
groupedState.setAnotherBlock(map);
assertEquals(groupedState.getEstimatedSize(), 76064 + 1274 * (i + 1));
}
for (int i = 0; i < 1000; i++) {
((GroupedAccumulatorState) groupedState).setGroupId(i);
groupedState.setBoolean(true);
groupedState.setLong(1);
groupedState.setDouble(2.0);
groupedState.setByte((byte) 3);
groupedState.setSlice(utf8Slice("test"));
groupedState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
groupedState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
groupedState.setBlock(array);
BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
BIGINT.writeLong(mapBlockBuilder, 123L);
VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
Block map = mapBlockBuilder.build();
groupedState.setAnotherBlock(map);
assertEquals(groupedState.getEstimatedSize(), 76064 + 1274 * 1000);
}
}
use of com.facebook.presto.spi.type.Type in project presto by prestodb.
the class TestMinMaxByAggregation method testAllRegistered.
@Test
public void testAllRegistered() {
Set<Type> orderableTypes = getTypes().stream().filter(Type::isOrderable).collect(toImmutableSet());
for (Type keyType : orderableTypes) {
for (Type valueType : getTypes()) {
if (StateCompiler.getSupportedFieldTypes().contains(valueType.getJavaType())) {
assertNotNull(METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("min_by", AGGREGATE, valueType.getTypeSignature(), valueType.getTypeSignature(), keyType.getTypeSignature())));
assertNotNull(METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("max_by", AGGREGATE, valueType.getTypeSignature(), valueType.getTypeSignature(), keyType.getTypeSignature())));
}
}
}
}
use of com.facebook.presto.spi.type.Type in project presto by prestodb.
the class AbstractTestAccumuloRowSerializer method testArray.
@Test
public void testArray() throws Exception {
AccumuloRowSerializer serializer = serializerClass.getConstructor().newInstance();
Type type = new ArrayType(VARCHAR);
List<Object> expected = ImmutableList.of("a", "b", "c");
byte[] data = serializer.encode(type, AccumuloRowSerializer.getBlockFromArray(VARCHAR, expected));
List<Object> actual = serializer.decode(type, data);
assertEquals(actual, expected);
deserializeData(serializer, data);
actual = AccumuloRowSerializer.getArrayFromBlock(VARCHAR, serializer.getArray(COLUMN_NAME, type));
assertEquals(actual, expected);
}
use of com.facebook.presto.spi.type.Type in project presto by prestodb.
the class Indexer method index.
/**
* Index the given mutation, adding mutations to the index and metrics table
* <p>
* Like typical use of a BatchWriter, this method does not flush mutations to the underlying index table.
* For higher throughput the modifications to the metrics table are tracked in memory and added to the metrics table when the indexer is flushed or closed.
*
* @param mutation Mutation to index
*/
public void index(Mutation mutation) {
// Increment the cardinality for the number of rows in the table
metrics.get(METRICS_TABLE_ROW_COUNT).incrementAndGet();
// Set the first and last row values of the table based on existing row IDs
if (firstRow == null || byteArrayComparator.compare(mutation.getRow(), firstRow) < 0) {
firstRow = mutation.getRow();
}
if (lastRow == null || byteArrayComparator.compare(mutation.getRow(), lastRow) > 0) {
lastRow = mutation.getRow();
}
// For each column update in this mutation
for (ColumnUpdate columnUpdate : mutation.getUpdates()) {
// Get the column qualifiers we want to index for this column family (if any)
ByteBuffer family = wrap(columnUpdate.getColumnFamily());
Collection<ByteBuffer> indexQualifiers = indexColumns.get(family);
// If we have column qualifiers we want to index for this column family
if (indexQualifiers != null) {
// Check if we want to index this particular qualifier
ByteBuffer qualifier = wrap(columnUpdate.getColumnQualifier());
if (indexQualifiers.contains(qualifier)) {
// If so, create a mutation using the following mapping:
// Row ID = column value
// Column Family = columnqualifier_columnfamily
// Column Qualifier = row ID
// Value = empty
ByteBuffer indexFamily = getIndexColumnFamily(columnUpdate.getColumnFamily(), columnUpdate.getColumnQualifier());
Type type = indexColumnTypes.get(family).get(qualifier);
ColumnVisibility visibility = new ColumnVisibility(columnUpdate.getColumnVisibility());
// If this is an array type, then index each individual element in the array
if (Types.isArrayType(type)) {
Type elementType = Types.getElementType(type);
List<?> elements = serializer.decode(type, columnUpdate.getValue());
for (Object element : elements) {
addIndexMutation(wrap(serializer.encode(elementType, element)), indexFamily, visibility, mutation.getRow());
}
} else {
addIndexMutation(wrap(columnUpdate.getValue()), indexFamily, visibility, mutation.getRow());
}
}
}
}
}
Aggregations