Search in sources :

Example 1 with FieldResolver

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);
}
Also used : IdentityHashMap(java.util.IdentityHashMap) RelationName(io.crate.metadata.RelationName) Collection(java.util.Collection) Set(java.util.Set) FetchMarker(io.crate.expression.symbol.FetchMarker) Function(java.util.function.Function) FieldResolver(io.crate.analyze.relations.FieldResolver) Lists2(io.crate.common.collections.Lists2) SymbolVisitors(io.crate.expression.symbol.SymbolVisitors) ExecutionPlan(io.crate.planner.ExecutionPlan) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) TableStats(io.crate.statistics.TableStats) Row(io.crate.data.Row) Symbol(io.crate.expression.symbol.Symbol) PlannerContext(io.crate.planner.PlannerContext) Objects.requireNonNull(java.util.Objects.requireNonNull) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Nullable(javax.annotation.Nullable) ProjectionBuilder(io.crate.execution.dsl.projection.builder.ProjectionBuilder) Symbol(io.crate.expression.symbol.Symbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) FetchMarker(io.crate.expression.symbol.FetchMarker) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Nullable(javax.annotation.Nullable)

Aggregations

OrderBy (io.crate.analyze.OrderBy)1 FieldResolver (io.crate.analyze.relations.FieldResolver)1 Lists2 (io.crate.common.collections.Lists2)1 Row (io.crate.data.Row)1 ProjectionBuilder (io.crate.execution.dsl.projection.builder.ProjectionBuilder)1 FetchMarker (io.crate.expression.symbol.FetchMarker)1 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)1 Symbol (io.crate.expression.symbol.Symbol)1 SymbolVisitors (io.crate.expression.symbol.SymbolVisitors)1 RelationName (io.crate.metadata.RelationName)1 ExecutionPlan (io.crate.planner.ExecutionPlan)1 PlannerContext (io.crate.planner.PlannerContext)1 TableStats (io.crate.statistics.TableStats)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 IdentityHashMap (java.util.IdentityHashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Set (java.util.Set)1