use of com.facebook.presto.spi.ConnectorId in project presto by prestodb.
the class SqlTask method getMetadataUpdateRequests.
private MetadataUpdates getMetadataUpdateRequests(TaskHolder taskHolder) {
ConnectorId connectorId = null;
ImmutableList.Builder<ConnectorMetadataUpdateHandle> connectorMetadataUpdatesBuilder = ImmutableList.builder();
if (taskHolder.getTaskExecution() != null) {
TaskMetadataContext taskMetadataContext = taskHolder.getTaskExecution().getTaskContext().getTaskMetadataContext();
if (!taskMetadataContext.getMetadataUpdaters().isEmpty()) {
connectorId = taskMetadataContext.getConnectorId();
for (ConnectorMetadataUpdater metadataUpdater : taskMetadataContext.getMetadataUpdaters()) {
connectorMetadataUpdatesBuilder.addAll(metadataUpdater.getPendingMetadataUpdateRequests());
}
}
}
return new MetadataUpdates(connectorId, connectorMetadataUpdatesBuilder.build());
}
use of com.facebook.presto.spi.ConnectorId in project presto by prestodb.
the class ApplyConnectorOptimization method buildConnectorPlanNodeContext.
private static ConnectorPlanNodeContext buildConnectorPlanNodeContext(PlanNode node, PlanNode parent, ImmutableMap.Builder<PlanNode, ConnectorPlanNodeContext> contextBuilder) {
Set<ConnectorId> connectorIds;
Set<Class<? extends PlanNode>> planNodeTypes;
if (node.getSources().isEmpty()) {
if (node instanceof TableScanNode) {
connectorIds = ImmutableSet.of(((TableScanNode) node).getTable().getConnectorId());
planNodeTypes = ImmutableSet.of(TableScanNode.class);
} else {
connectorIds = ImmutableSet.of(EMPTY_CONNECTOR_ID);
planNodeTypes = ImmutableSet.of(node.getClass());
}
} else {
connectorIds = new HashSet<>();
planNodeTypes = new HashSet<>();
for (PlanNode child : node.getSources()) {
ConnectorPlanNodeContext childContext = buildConnectorPlanNodeContext(child, node, contextBuilder);
connectorIds.addAll(childContext.getReachableConnectors());
planNodeTypes.addAll(childContext.getReachablePlanNodeTypes());
}
planNodeTypes.add(node.getClass());
}
ConnectorPlanNodeContext connectorPlanNodeContext = new ConnectorPlanNodeContext(parent, connectorIds, planNodeTypes);
contextBuilder.put(node, connectorPlanNodeContext);
return connectorPlanNodeContext;
}
use of com.facebook.presto.spi.ConnectorId in project presto by prestodb.
the class ApplyConnectorOptimization method optimize.
@Override
public PlanNode optimize(PlanNode plan, Session session, TypeProvider types, PlanVariableAllocator variableAllocator, PlanNodeIdAllocator idAllocator, WarningCollector warningCollector) {
requireNonNull(plan, "plan is null");
requireNonNull(session, "session is null");
requireNonNull(types, "types is null");
requireNonNull(variableAllocator, "variableAllocator is null");
requireNonNull(idAllocator, "idAllocator is null");
Map<ConnectorId, Set<ConnectorPlanOptimizer>> connectorOptimizers = connectorOptimizersSupplier.get();
if (connectorOptimizers.isEmpty()) {
return plan;
}
// retrieve all the connectors
ImmutableSet.Builder<ConnectorId> connectorIds = ImmutableSet.builder();
getAllConnectorIds(plan, connectorIds);
// In order to preserve the fixpoint, we will "pretend" the newly added C2 table scan is part of C1's job to maintain.
for (ConnectorId connectorId : connectorIds.build()) {
Set<ConnectorPlanOptimizer> optimizers = connectorOptimizers.get(connectorId);
if (optimizers == null) {
continue;
}
ImmutableMap.Builder<PlanNode, ConnectorPlanNodeContext> contextMapBuilder = ImmutableMap.builder();
buildConnectorPlanNodeContext(plan, null, contextMapBuilder);
Map<PlanNode, ConnectorPlanNodeContext> contextMap = contextMapBuilder.build();
// keep track of changed nodes; the keys are original nodes and the values are the new nodes
Map<PlanNode, PlanNode> updates = new HashMap<>();
// process connector optimizers
for (PlanNode node : contextMap.keySet()) {
// For a subtree with root `node` to be a max closure, the following conditions must hold:
// * The subtree with root `node` is a closure.
// * `node` has no parent, or the subtree with root as `node`'s parent is not a closure.
ConnectorPlanNodeContext context = contextMap.get(node);
if (!context.isClosure(connectorId) || !context.getParent().isPresent() || contextMap.get(context.getParent().get()).isClosure(connectorId)) {
continue;
}
PlanNode newNode = node;
// the returned node is still a max closure (only if there is no new connector added, which does happen but ignored here)
for (ConnectorPlanOptimizer optimizer : optimizers) {
newNode = optimizer.optimize(newNode, session.toConnectorSession(connectorId), variableAllocator, idAllocator);
}
if (node != newNode) {
// the optimizer has allocated a new PlanNode
checkState(containsAll(ImmutableSet.copyOf(newNode.getOutputVariables()), node.getOutputVariables()), "the connector optimizer from %s returns a node that does not cover all output before optimization", connectorId);
updates.put(node, newNode);
}
}
// up to this point, we have a set of updated nodes; need to recursively update their parents
// alter the plan with a bottom-up approach (but does not have to be strict bottom-up to guarantee the correctness of the algorithm)
// use "original nodes" to keep track of the plan structure and "updates" to keep track of the new nodes
Queue<PlanNode> originalNodes = new LinkedList<>(updates.keySet());
while (!originalNodes.isEmpty()) {
PlanNode originalNode = originalNodes.poll();
if (!contextMap.get(originalNode).getParent().isPresent()) {
// originalNode must be the root; update the plan
plan = updates.get(originalNode);
continue;
}
PlanNode originalParent = contextMap.get(originalNode).getParent().get();
// need to create a new parent given the child has changed; the new parent needs to point to the new child.
// if a node has been updated, it will occur in `updates`; otherwise, just use the original node
ImmutableList.Builder<PlanNode> newChildren = ImmutableList.builder();
originalParent.getSources().forEach(child -> newChildren.add(updates.getOrDefault(child, child)));
PlanNode newParent = originalParent.replaceChildren(newChildren.build());
// mark the new parent as updated
updates.put(originalParent, newParent);
// enqueue the parent node in order to recursively update its ancestors
originalNodes.add(originalParent);
}
}
return plan;
}
use of com.facebook.presto.spi.ConnectorId in project presto by prestodb.
the class TestCostCalculator method tableScan.
private TableScanNode tableScan(String id, List<VariableReferenceExpression> variables) {
ImmutableMap.Builder<VariableReferenceExpression, ColumnHandle> assignments = ImmutableMap.builder();
for (VariableReferenceExpression variable : variables) {
assignments.put(variable, new TpchColumnHandle("orderkey", BIGINT));
}
TpchTableHandle tableHandle = new TpchTableHandle("orders", 1.0);
return new TableScanNode(Optional.empty(), new PlanNodeId(id), new TableHandle(new ConnectorId("tpch"), tableHandle, TpchTransactionHandle.INSTANCE, Optional.of(new TpchTableLayoutHandle(tableHandle, TupleDomain.all()))), variables, assignments.build(), TupleDomain.all(), TupleDomain.all());
}
use of com.facebook.presto.spi.ConnectorId in project presto by prestodb.
the class TestSystemSplit method testSerialization.
@Test
public void testSerialization() {
ConnectorId connectorId = new ConnectorId("testid");
SystemTableHandle tableHandle = new SystemTableHandle(connectorId, "xyz", "foo");
SystemSplit expected = new SystemSplit(connectorId, tableHandle, HostAddress.fromParts("127.0.0.1", 0), TupleDomain.all());
JsonCodec<SystemSplit> codec = jsonCodec(SystemSplit.class);
SystemSplit actual = codec.fromJson(codec.toJson(expected));
assertEquals(actual.getConnectorId(), expected.getConnectorId());
assertEquals(actual.getTableHandle(), expected.getTableHandle());
assertEquals(actual.getAddresses(), expected.getAddresses());
assertEquals(actual.getConstraint(), expected.getConstraint());
}
Aggregations