Search in sources :

Example 1 with FetchMarker

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;
}
Also used : FetchSource(io.crate.planner.node.fetch.FetchSource) HashMap(java.util.HashMap) Symbol(io.crate.expression.symbol.Symbol) FetchMarker(io.crate.expression.symbol.FetchMarker) InputColumn(io.crate.expression.symbol.InputColumn) Reference(io.crate.metadata.Reference) RelationName(io.crate.metadata.RelationName)

Example 2 with FetchMarker

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);
}
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)

Example 3 with FetchMarker

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));
}
Also used : Symbol(io.crate.expression.symbol.Symbol) FetchMarker(io.crate.expression.symbol.FetchMarker) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Nullable(javax.annotation.Nullable)

Example 4 with FetchMarker

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());
}
Also used : FetchMarker(io.crate.expression.symbol.FetchMarker) InputColumn(io.crate.expression.symbol.InputColumn) FetchReference(io.crate.expression.symbol.FetchReference)

Aggregations

FetchMarker (io.crate.expression.symbol.FetchMarker)4 Symbol (io.crate.expression.symbol.Symbol)3 InputColumn (io.crate.expression.symbol.InputColumn)2 RelationName (io.crate.metadata.RelationName)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Nullable (javax.annotation.Nullable)2 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 FetchReference (io.crate.expression.symbol.FetchReference)1 ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)1 SymbolVisitors (io.crate.expression.symbol.SymbolVisitors)1 Reference (io.crate.metadata.Reference)1 ExecutionPlan (io.crate.planner.ExecutionPlan)1 PlannerContext (io.crate.planner.PlannerContext)1 FetchSource (io.crate.planner.node.fetch.FetchSource)1 TableStats (io.crate.statistics.TableStats)1