Source:
private PlanNode createRemoteMaterializedExchange(ExchangeNode exchange, RewriteContext<FragmentProperties> context) { checkArgument(exchange.getType() == REPARTITION, "Unexpected exchange type: %s", exchange.getType()); 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> partitioningSymbols = assignPartitioningSymbols(partitioning, constantExpressions); Map<Symbol, ColumnMetadata> temporaryTableColumns = assignColumnNames(exchange.getOutputSymbols(), constantExpressions.keySet()); List<String> partitioningColumns = partitioningSymbols.stream() .map(symbol -> temporaryTableColumns.get(symbol).getName()) .collect(toImmutableList()); PartitioningMetadata partitioningMetadata = new PartitioningMetadata(partitioningHandle, partitioningColumns); 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()); 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> partitioningSymbols = assignPartitioningSymbols(partitioning, constantExpressions); Map<Symbol, ColumnMetadata> temporaryTableColumns = assignColumnNames(exchange.getOutputSymbols(), constantExpressions.keySet()); List<String> partitioningColumns = partitioningSymbols.stream() .map(symbol -> temporaryTableColumns.get(symbol).getName()) .collect(toImmutableList()); PartitioningMetadata partitioningMetadata = new PartitioningMetadata(partitioningHandle, partitioningColumns); 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(); SubPlan childFragmentPlan = buildSubPlan(write, writeProperties, context); context.get().addChildren(ImmutableList.of(childFragmentPlan)); context.get().addDependency(childFragmentPlan.getFragment().getId()); return visitTableScan(scan, context); }