use of io.crate.analyze.relations.FieldResolver in project crate by crate.
the class Rename method rewriteToFetch.
@Nullable
@Override
public FetchRewrite rewriteToFetch(TableStats tableStats, Collection<Symbol> usedColumns) {
IdentityHashMap<Symbol, Symbol> parentToChildMap = new IdentityHashMap<>(outputs.size());
IdentityHashMap<Symbol, Symbol> childToParentMap = new IdentityHashMap<>(outputs.size());
for (int i = 0; i < outputs.size(); i++) {
parentToChildMap.put(outputs.get(i), source.outputs().get(i));
childToParentMap.put(source.outputs().get(i), outputs.get(i));
}
ArrayList<Symbol> mappedUsedColumns = new ArrayList<>();
for (Symbol usedColumn : usedColumns) {
SymbolVisitors.intersection(usedColumn, outputs, s -> {
Symbol childSymbol = parentToChildMap.get(s);
assert childSymbol != null : "There must be a mapping available for symbol " + s;
mappedUsedColumns.add(childSymbol);
});
}
FetchRewrite fetchRewrite = source.rewriteToFetch(tableStats, mappedUsedColumns);
if (fetchRewrite == null) {
return null;
}
LogicalPlan newSource = fetchRewrite.newPlan();
ArrayList<Symbol> newOutputs = new ArrayList<>();
for (Symbol output : newSource.outputs()) {
if (output instanceof FetchMarker) {
FetchMarker marker = (FetchMarker) output;
FetchMarker newMarker = new FetchMarker(name, marker.fetchRefs(), marker.fetchId());
newOutputs.add(newMarker);
childToParentMap.put(marker, newMarker);
} else {
Symbol mappedOutput = requireNonNull(childToParentMap.get(output), () -> "Mapping must exist for output from source. `" + output + "` is missing in " + childToParentMap);
newOutputs.add(mappedOutput);
}
}
LinkedHashMap<Symbol, Symbol> replacedOutputs = new LinkedHashMap<>();
Function<Symbol, Symbol> convertChildrenToScopedSymbols = s -> MapBackedSymbolReplacer.convert(s, childToParentMap);
for (var entry : fetchRewrite.replacedOutputs().entrySet()) {
Symbol key = entry.getKey();
Symbol value = entry.getValue();
Symbol parentSymbolForKey = requireNonNull(childToParentMap.get(key), () -> "Mapping must exist for output from source. `" + key + "` is missing in " + childToParentMap);
replacedOutputs.put(parentSymbolForKey, convertChildrenToScopedSymbols.apply(value));
}
Rename newRename = new Rename(newOutputs, name, fieldResolver, newSource);
return new FetchRewrite(replacedOutputs, newRename);
}
Aggregations