use of io.crate.expression.symbol.FetchMarker in project crate by crate.
the class FetchRewrite method createFetchSources.
public Map<RelationName, FetchSource> createFetchSources() {
HashMap<RelationName, FetchSource> fetchSources = new HashMap<>();
List<Symbol> outputs = plan.outputs();
for (int i = 0; i < outputs.size(); i++) {
Symbol output = outputs.get(i);
if (output instanceof FetchMarker) {
FetchMarker fetchMarker = (FetchMarker) output;
RelationName tableName = fetchMarker.fetchId().ident().tableIdent();
FetchSource fetchSource = fetchSources.get(tableName);
if (fetchSource == null) {
fetchSource = new FetchSource();
fetchSources.put(tableName, fetchSource);
}
fetchSource.addFetchIdColumn(new InputColumn(i, fetchMarker.valueType()));
for (Reference fetchRef : fetchMarker.fetchRefs()) {
fetchSource.addRefToFetch(fetchRef);
}
}
}
return fetchSources;
}
use of io.crate.expression.symbol.FetchMarker 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);
}
use of io.crate.expression.symbol.FetchMarker in project crate by crate.
the class Eval method rewriteToFetch.
@Nullable
@Override
public FetchRewrite rewriteToFetch(TableStats tableStats, Collection<Symbol> usedColumns) {
FetchRewrite fetchRewrite = source.rewriteToFetch(tableStats, usedColumns);
if (fetchRewrite == null) {
return null;
}
LogicalPlan newSource = fetchRewrite.newPlan();
Function<Symbol, Symbol> mapToFetchStubs = fetchRewrite.mapToFetchStubs();
LinkedHashMap<Symbol, Symbol> newReplacedOutputs = new LinkedHashMap<>();
ArrayList<Symbol> newOutputs = new ArrayList<>();
for (Symbol sourceOutput : newSource.outputs()) {
if (sourceOutput instanceof FetchMarker) {
newOutputs.add(sourceOutput);
}
}
for (Symbol output : outputs) {
newReplacedOutputs.put(output, mapToFetchStubs.apply(output));
SymbolVisitors.intersection(output, newSource.outputs(), newOutputs::add);
}
return new FetchRewrite(newReplacedOutputs, Eval.create(newSource, newOutputs));
}
use of io.crate.expression.symbol.FetchMarker in project crate by crate.
the class InputColumns method visitFetchStub.
@Override
public Symbol visitFetchStub(FetchStub fetchStub, SourceSymbols sourceSymbols) {
FetchMarker fetchMarker = fetchStub.fetchMarker();
InputColumn fetchId = sourceSymbols.inputs.get(fetchMarker);
if (fetchId == null) {
throw new IllegalArgumentException("Could not find fetchMarker " + fetchMarker + " in sources: " + sourceSymbols);
}
return new FetchReference(fetchId, fetchStub.ref());
}
Aggregations