use of java.util.OptionalInt in project presto by prestodb.
the class TestShardMetadataRecordCursor method testSimple.
@Test
public void testSimple() throws Exception {
ShardManager shardManager = createShardManager(dbi);
// Add shards to the table
long tableId = 1;
OptionalInt bucketNumber = OptionalInt.empty();
UUID uuid1 = UUID.randomUUID();
UUID uuid2 = UUID.randomUUID();
UUID uuid3 = UUID.randomUUID();
ShardInfo shardInfo1 = new ShardInfo(uuid1, bucketNumber, ImmutableSet.of("node1"), ImmutableList.of(), 1, 10, 100);
ShardInfo shardInfo2 = new ShardInfo(uuid2, bucketNumber, ImmutableSet.of("node2"), ImmutableList.of(), 2, 20, 200);
ShardInfo shardInfo3 = new ShardInfo(uuid3, bucketNumber, ImmutableSet.of("node3"), ImmutableList.of(), 3, 30, 300);
List<ShardInfo> shards = ImmutableList.of(shardInfo1, shardInfo2, shardInfo3);
long transactionId = shardManager.beginTransaction();
shardManager.commitShards(transactionId, tableId, ImmutableList.of(new ColumnInfo(1, BIGINT), new ColumnInfo(2, DATE)), shards, Optional.empty(), 0);
Slice schema = utf8Slice(DEFAULT_TEST_ORDERS.getSchemaName());
Slice table = utf8Slice(DEFAULT_TEST_ORDERS.getTableName());
DateTime date1 = DateTime.parse("2015-01-01T00:00");
DateTime date2 = DateTime.parse("2015-01-02T00:00");
TupleDomain<Integer> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.<Integer, Domain>builder().put(0, Domain.singleValue(createVarcharType(10), schema)).put(1, Domain.create(ValueSet.ofRanges(lessThanOrEqual(createVarcharType(10), table)), true)).put(6, Domain.create(ValueSet.ofRanges(lessThanOrEqual(BIGINT, date1.getMillis()), greaterThan(BIGINT, date2.getMillis())), true)).put(7, Domain.create(ValueSet.ofRanges(lessThanOrEqual(BIGINT, date1.getMillis()), greaterThan(BIGINT, date2.getMillis())), true)).build());
List<MaterializedRow> actual;
try (RecordCursor cursor = new ShardMetadataSystemTable(dbi).cursor(null, SESSION, tupleDomain)) {
actual = getMaterializedResults(cursor, SHARD_METADATA.getColumns());
}
assertEquals(actual.size(), 3);
List<MaterializedRow> expected = ImmutableList.of(new MaterializedRow(DEFAULT_PRECISION, schema, table, utf8Slice(uuid1.toString()), null, 100L, 10L, 1L, null, null), new MaterializedRow(DEFAULT_PRECISION, schema, table, utf8Slice(uuid2.toString()), null, 200L, 20L, 2L, null, null), new MaterializedRow(DEFAULT_PRECISION, schema, table, utf8Slice(uuid3.toString()), null, 300L, 30L, 3L, null, null));
assertEquals(actual, expected);
}
use of java.util.OptionalInt in project presto by prestodb.
the class TestShardDao method testNodeShards.
@Test
public void testNodeShards() throws Exception {
assertEquals(dao.getAllNodesInUse(), ImmutableSet.of());
String nodeName1 = UUID.randomUUID().toString();
int nodeId1 = dao.insertNode(nodeName1);
String nodeName2 = UUID.randomUUID().toString();
int nodeId2 = dao.insertNode(nodeName2);
assertEquals(dao.getAllNodesInUse(), ImmutableSet.of(nodeName1, nodeName2));
UUID shardUuid1 = UUID.randomUUID();
UUID shardUuid2 = UUID.randomUUID();
UUID shardUuid3 = UUID.randomUUID();
UUID shardUuid4 = UUID.randomUUID();
UUID shardUuid5 = UUID.randomUUID();
MetadataDao metadataDao = dbi.onDemand(MetadataDao.class);
int bucketCount = 20;
long distributionId = metadataDao.insertDistribution("test", "bigint", bucketCount);
for (int i = 0; i < bucketCount; i++) {
Integer nodeId = ((i % 2) == 0) ? nodeId1 : nodeId2;
dao.insertBuckets(distributionId, ImmutableList.of(i), ImmutableList.of(nodeId));
}
long plainTableId = metadataDao.insertTable("test", "plain", false, false, null, 0);
long bucketedTableId = metadataDao.insertTable("test", "bucketed", false, false, distributionId, 0);
long shardId1 = dao.insertShard(shardUuid1, plainTableId, null, 1, 11, 111);
long shardId2 = dao.insertShard(shardUuid2, plainTableId, null, 2, 22, 222);
long shardId3 = dao.insertShard(shardUuid3, bucketedTableId, 8, 3, 33, 333);
long shardId4 = dao.insertShard(shardUuid4, bucketedTableId, 9, 4, 44, 444);
long shardId5 = dao.insertShard(shardUuid5, bucketedTableId, 7, 5, 55, 555);
OptionalInt noBucket = OptionalInt.empty();
OptionalLong noRange = OptionalLong.empty();
ShardMetadata shard1 = new ShardMetadata(plainTableId, shardId1, shardUuid1, noBucket, 1, 11, 111, noRange, noRange);
ShardMetadata shard2 = new ShardMetadata(plainTableId, shardId2, shardUuid2, noBucket, 2, 22, 222, noRange, noRange);
ShardMetadata shard3 = new ShardMetadata(bucketedTableId, shardId3, shardUuid3, OptionalInt.of(8), 3, 33, 333, noRange, noRange);
ShardMetadata shard4 = new ShardMetadata(bucketedTableId, shardId4, shardUuid4, OptionalInt.of(9), 4, 44, 444, noRange, noRange);
ShardMetadata shard5 = new ShardMetadata(bucketedTableId, shardId5, shardUuid5, OptionalInt.of(7), 5, 55, 555, noRange, noRange);
assertEquals(dao.getShards(plainTableId), ImmutableList.of(shardUuid1, shardUuid2));
assertEquals(dao.getShards(bucketedTableId), ImmutableList.of(shardUuid3, shardUuid4, shardUuid5));
assertEquals(dao.getNodeShards(nodeName1, null), ImmutableSet.of(shard3));
assertEquals(dao.getNodeShards(nodeName2, null), ImmutableSet.of(shard4, shard5));
assertEquals(dao.getNodeSizes(), ImmutableSet.of(new NodeSize(nodeName1, 33), new NodeSize(nodeName2, 44 + 55)));
dao.insertShardNode(shardId1, nodeId1);
dao.insertShardNode(shardId2, nodeId1);
dao.insertShardNode(shardId1, nodeId2);
assertEquals(dao.getNodeShards(nodeName1, null), ImmutableSet.of(shard1, shard2, shard3));
assertEquals(dao.getNodeShards(nodeName2, null), ImmutableSet.of(shard1, shard4, shard5));
assertEquals(dao.getNodeSizes(), ImmutableSet.of(new NodeSize(nodeName1, 11 + 22 + 33), new NodeSize(nodeName2, 11 + 44 + 55)));
dao.dropShardNodes(plainTableId);
assertEquals(dao.getNodeShards(nodeName1, null), ImmutableSet.of(shard3));
assertEquals(dao.getNodeShards(nodeName2, null), ImmutableSet.of(shard4, shard5));
assertEquals(dao.getNodeSizes(), ImmutableSet.of(new NodeSize(nodeName1, 33), new NodeSize(nodeName2, 44 + 55)));
dao.dropShards(plainTableId);
dao.dropShards(bucketedTableId);
assertEquals(dao.getShards(plainTableId), ImmutableList.of());
assertEquals(dao.getShards(bucketedTableId), ImmutableList.of());
assertEquals(dao.getNodeSizes(), ImmutableSet.of());
}
use of java.util.OptionalInt in project presto by prestodb.
the class TestDatabaseShardManager method testGetNodeBytes.
@Test
public void testGetNodeBytes() {
long tableId = createTable("test");
OptionalInt bucketNumber = OptionalInt.empty();
UUID shard1 = UUID.randomUUID();
UUID shard2 = UUID.randomUUID();
List<ShardInfo> shardNodes = ImmutableList.of(new ShardInfo(shard1, bucketNumber, ImmutableSet.of("node1"), ImmutableList.of(), 3, 33, 333), new ShardInfo(shard2, bucketNumber, ImmutableSet.of("node1"), ImmutableList.of(), 5, 55, 555));
List<ColumnInfo> columns = ImmutableList.of(new ColumnInfo(1, BIGINT));
shardManager.createTable(tableId, columns, false, OptionalLong.empty());
long transactionId = shardManager.beginTransaction();
shardManager.commitShards(transactionId, tableId, columns, shardNodes, Optional.empty(), 0);
assertEquals(getShardNodes(tableId, TupleDomain.all()), ImmutableSet.of(new ShardNodes(shard1, ImmutableSet.of("node1")), new ShardNodes(shard2, ImmutableSet.of("node1"))));
assertEquals(shardManager.getNodeBytes(), ImmutableMap.of("node1", 88L));
shardManager.assignShard(tableId, shard1, "node2", false);
assertEquals(getShardNodes(tableId, TupleDomain.all()), ImmutableSet.of(new ShardNodes(shard1, ImmutableSet.of("node1", "node2")), new ShardNodes(shard2, ImmutableSet.of("node1"))));
assertEquals(shardManager.getNodeBytes(), ImmutableMap.of("node1", 88L, "node2", 33L));
}
use of java.util.OptionalInt in project presto by prestodb.
the class StringFunctions method fromUtf8.
@Description("decodes the UTF-8 encoded string")
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.VARCHAR)
public static Slice fromUtf8(@SqlType(StandardTypes.VARBINARY) Slice slice, @SqlType("varchar(x)") Slice replacementCharacter) {
int count = countCodePoints(replacementCharacter);
if (count > 1) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Replacement character string must empty or a single character");
}
OptionalInt replacementCodePoint;
if (count == 1) {
try {
replacementCodePoint = OptionalInt.of(getCodePointAt(replacementCharacter, 0));
} catch (InvalidUtf8Exception e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid replacement character");
}
} else {
replacementCodePoint = OptionalInt.empty();
}
return SliceUtf8.fixInvalidUtf8(slice, replacementCodePoint);
}
use of java.util.OptionalInt in project presto by prestodb.
the class LocalExecutionPlanner method plan.
public LocalExecutionPlan plan(Session session, PlanNode plan, Map<Symbol, Type> types, PartitioningScheme partitioningScheme, OutputBuffer outputBuffer) {
List<Symbol> outputLayout = partitioningScheme.getOutputLayout();
if (partitioningScheme.getPartitioning().getHandle().equals(FIXED_BROADCAST_DISTRIBUTION) || partitioningScheme.getPartitioning().getHandle().equals(FIXED_ARBITRARY_DISTRIBUTION) || partitioningScheme.getPartitioning().getHandle().equals(SINGLE_DISTRIBUTION) || partitioningScheme.getPartitioning().getHandle().equals(COORDINATOR_DISTRIBUTION)) {
return plan(session, plan, outputLayout, types, new TaskOutputFactory(outputBuffer));
}
// We can convert the symbols directly into channels, because the root must be a sink and therefore the layout is fixed
List<Integer> partitionChannels;
List<Optional<NullableValue>> partitionConstants;
List<Type> partitionChannelTypes;
if (partitioningScheme.getHashColumn().isPresent()) {
partitionChannels = ImmutableList.of(outputLayout.indexOf(partitioningScheme.getHashColumn().get()));
partitionConstants = ImmutableList.of(Optional.empty());
partitionChannelTypes = ImmutableList.of(BIGINT);
} else {
partitionChannels = partitioningScheme.getPartitioning().getArguments().stream().map(ArgumentBinding::getColumn).map(outputLayout::indexOf).collect(toImmutableList());
partitionConstants = partitioningScheme.getPartitioning().getArguments().stream().map(argument -> {
if (argument.isConstant()) {
return Optional.of(argument.getConstant());
}
return Optional.<NullableValue>empty();
}).collect(toImmutableList());
partitionChannelTypes = partitioningScheme.getPartitioning().getArguments().stream().map(argument -> {
if (argument.isConstant()) {
return argument.getConstant().getType();
}
return types.get(argument.getColumn());
}).collect(toImmutableList());
}
PartitionFunction partitionFunction = nodePartitioningManager.getPartitionFunction(session, partitioningScheme, partitionChannelTypes);
OptionalInt nullChannel = OptionalInt.empty();
Set<Symbol> partitioningColumns = partitioningScheme.getPartitioning().getColumns();
// partitioningColumns expected to have one column in the normal case, and zero columns when partitioning on a constant
checkArgument(!partitioningScheme.isReplicateNulls() || partitioningColumns.size() <= 1);
if (partitioningScheme.isReplicateNulls() && partitioningColumns.size() == 1) {
nullChannel = OptionalInt.of(outputLayout.indexOf(getOnlyElement(partitioningColumns)));
}
return plan(session, plan, outputLayout, types, new PartitionedOutputFactory(partitionFunction, partitionChannels, partitionConstants, nullChannel, outputBuffer, maxPagePartitioningBufferSize));
}
Aggregations