Source:
private PlanNode createRemoteMaterializedExchange(ExchangeNode exchange, RewriteContext<FragmentProperties> context) { checkArgument(exchange.getType() == REPARTITION, "Unexpected exchange type: %s", exchange.getType()); checkArgument(exchange.getScope() == REMOTE_MATERIALIZED, "Unexpected exchange scope: %s", exchange.getScope()); PartitioningScheme partitioningScheme = exchange.getPartitioningScheme(); checkArgument(!partitioningScheme.getHashColumn().isPresent(), "precomputed hashes are not supported in materializing exchanges"); PartitioningHandle partitioningHandle = partitioningScheme.getPartitioning().getHandle(); ConnectorId connectorId = partitioningHandle.getConnectorId() .orElseThrow(() -> new IllegalArgumentException("Unsupported partitioning handle: " + partitioningHandle)); Map<Symbol, Expression> constantExpressions = new HashMap<>(); Partitioning partitioning = partitioningScheme.getPartitioning(); List<Symbol> partitionSymbols = assignPartitioningSymbols(partitioning, constantExpressions); Map<Symbol, ColumnMetadata> temporaryTableColumns = assignColumnNames(exchange.getOutputSymbols(), constantExpressions.keySet()); List<String> partitionColumns = partitionSymbols.stream() .map(symbol -> temporaryTableColumns.get(symbol).getName()) .collect(toImmutableList()); PartitioningMetadata partitioningMetadata = new PartitioningMetadata(partitioningHandle, partitionColumns); TableHandle temporaryTable = metadata.createTemporaryTable( session, connectorId.getCatalogName(), ImmutableList.copyOf(temporaryTableColumns.values()), Optional.of(partitioningMetadata)); TableScanNode scan = createTemporaryTableScan( temporaryTable, exchange.getOutputSymbols(), temporaryTableColumns, partitioningMetadata); checkArgument( !exchange.getPartitioningScheme().isReplicateNullsAndAny(), "materialized remote exchange is not supported when replicateNullsAndAny is needed"); TableFinishNode write = createTemporaryTableWrite( temporaryTable, temporaryTableColumns, exchange.getOutputSymbols(), exchange.getInputs(), exchange.getSources(), constantExpressions, partitioningMetadata); FragmentProperties writeProperties = new FragmentProperties(new PartitioningScheme( Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), write.getOutputSymbols())); writeProperties.setCoordinatorOnlyDistribution(); List<SubPlan> children = ImmutableList.of(buildSubPlan(write, writeProperties, context)); context.get().addChildren(children); return visitTableScan(scan, context); }

Target:
private PlanNode createRemoteMaterializedExchange(ExchangeNode exchange, RewriteContext<FragmentProperties> context) { checkArgument(exchange.getType() == REPARTITION, "Unexpected exchange type: %s", exchange.getType()); checkArgument(exchange.getScope() == REMOTE_MATERIALIZED, "Unexpected exchange scope: %s", exchange.getScope()); PartitioningScheme partitioningScheme = exchange.getPartitioningScheme(); checkArgument(!partitioningScheme.getHashColumn().isPresent(), "precomputed hashes are not supported in materializing exchanges"); PartitioningHandle partitioningHandle = partitioningScheme.getPartitioning().getHandle(); ConnectorId connectorId = partitioningHandle.getConnectorId() .orElseThrow(() -> new IllegalArgumentException("Unsupported partitioning handle: " + partitioningHandle)); Partitioning partitioning = partitioningScheme.getPartitioning(); PartitioningSymbolAssignments partitioningSymbolAssignments = assignPartitioningSymbols(partitioning); Map<Symbol, ColumnMetadata> symbolToColumnMap = assignTemporaryTableColumnNames(exchange.getOutputSymbols(), partitioningSymbolAssignments.getConstants().keySet()); List<Symbol> partitioningSymbols = partitioningSymbolAssignments.getSymbols(); List<String> partitionColumns = partitioningSymbols.stream() .map(symbol -> symbolToColumnMap.get(symbol).getName()) .collect(toImmutableList()); PartitioningMetadata partitioningMetadata = new PartitioningMetadata(partitioningHandle, partitionColumns); TableHandle temporaryTableHandle = metadata.createTemporaryTable( session, connectorId.getCatalogName(), ImmutableList.copyOf(symbolToColumnMap.values()), Optional.of(partitioningMetadata)); TableScanNode scan = createTemporaryTableScan( temporaryTableHandle, exchange.getOutputSymbols(), symbolToColumnMap, partitioningMetadata); checkArgument( !exchange.getPartitioningScheme().isReplicateNullsAndAny(), "materialized remote exchange is not supported when replicateNullsAndAny is needed"); TableFinishNode write = createTemporaryTableWrite( temporaryTableHandle, symbolToColumnMap, exchange.getOutputSymbols(), exchange.getInputs(), exchange.getSources(), partitioningSymbolAssignments.getConstants(), partitioningMetadata); FragmentProperties writeProperties = new FragmentProperties(new PartitioningScheme( Partitioning.create(SINGLE_DISTRIBUTION, ImmutableList.of()), write.getOutputSymbols())); writeProperties.setCoordinatorOnlyDistribution(); List<SubPlan> children = ImmutableList.of(buildSubPlan(write, writeProperties, context)); context.get().addChildren(children); return visitTableScan(scan, context); }