Search in sources :

Example 6 with ScopedSymbol

use of io.crate.expression.symbol.ScopedSymbol in project crate by crate.

the class Rename method pruneOutputsExcept.

@Override
public LogicalPlan pruneOutputsExcept(TableStats tableStats, Collection<Symbol> outputsToKeep) {
    /* In `SELECT * FROM (SELECT t1.*, t2.* FROM tbl AS t1, tbl AS t2) AS tjoin`
         * The `ScopedSymbol`s are ambiguous; To map them correctly this uses a IdentityHashMap
         */
    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> mappedToKeep = new ArrayList<>();
    for (Symbol outputToKeep : outputsToKeep) {
        SymbolVisitors.intersection(outputToKeep, outputs, s -> {
            Symbol childSymbol = parentToChildMap.get(s);
            assert childSymbol != null : "There must be a mapping available for symbol " + s;
            mappedToKeep.add(childSymbol);
        });
    }
    LogicalPlan newSource = source.pruneOutputsExcept(tableStats, mappedToKeep);
    if (newSource == source) {
        return this;
    }
    ArrayList<Symbol> newOutputs = new ArrayList<>(newSource.outputs().size());
    for (Symbol sourceOutput : newSource.outputs()) {
        newOutputs.add(childToParentMap.get(sourceOutput));
    }
    return new Rename(newOutputs, name, fieldResolver, newSource);
}
Also used : Symbol(io.crate.expression.symbol.Symbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList)

Example 7 with ScopedSymbol

use of io.crate.expression.symbol.ScopedSymbol in project crate by crate.

the class AliasedAnalyzedRelation method getField.

@Override
public Symbol getField(ColumnIdent column, Operation operation, boolean errorOnUnknownObjectKey) throws AmbiguousColumnException, ColumnUnknownException, UnsupportedOperationException {
    if (operation != Operation.READ) {
        throw new UnsupportedOperationException(operation + " is not supported on " + alias);
    }
    ColumnIdent childColumnName = aliasToColumnMapping.get(column);
    if (childColumnName == null) {
        if (column.isTopLevel()) {
            return null;
        }
        childColumnName = aliasToColumnMapping.get(column.getRoot());
        if (childColumnName == null) {
            // The column ident maybe a quoted subscript which points to an alias of a sub relation.
            // Aliases are always strings but due to the support for quoted subscript expressions,
            // the select column ident may already be expanded to a subscript.
            var maybeQuotedSubscriptColumnAlias = new ColumnIdent(column.sqlFqn());
            childColumnName = aliasToColumnMapping.get(maybeQuotedSubscriptColumnAlias);
            if (childColumnName == null) {
                return null;
            }
            column = maybeQuotedSubscriptColumnAlias;
        } else {
            childColumnName = new ColumnIdent(childColumnName.name(), column.path());
        }
    }
    Symbol field = relation.getField(childColumnName, operation, errorOnUnknownObjectKey);
    if (field == null || field instanceof VoidReference) {
        return field;
    }
    ScopedSymbol scopedSymbol = new ScopedSymbol(alias, column, field.valueType());
    // If the scopedSymbol exists already, return that instance.
    // Otherwise (e.g. lazy-loaded subscript expression) it must be stored to comply with
    // IdentityHashMap constraints.
    int i = scopedSymbols.indexOf(scopedSymbol);
    if (i >= 0) {
        return scopedSymbols.get(i);
    }
    scopedSymbols.add(scopedSymbol);
    return scopedSymbol;
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) VoidReference(io.crate.expression.symbol.VoidReference) Symbol(io.crate.expression.symbol.Symbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol)

Example 8 with ScopedSymbol

use of io.crate.expression.symbol.ScopedSymbol in project crate by crate.

the class MoveOrderBeneathNestedLoop method apply.

@Override
public LogicalPlan apply(Order order, Captures captures, TableStats tableStats, TransactionContext txnCtx, NodeContext nodeCtx) {
    NestedLoopJoin nestedLoop = captures.get(nlCapture);
    Set<RelationName> relationsInOrderBy = Collections.newSetFromMap(new IdentityHashMap<>());
    Consumer<ScopedSymbol> gatherRelationsFromField = f -> relationsInOrderBy.add(f.relation());
    Consumer<Reference> gatherRelationsFromRef = r -> relationsInOrderBy.add(r.ident().tableIdent());
    OrderBy orderBy = order.orderBy();
    for (Symbol orderExpr : orderBy.orderBySymbols()) {
        FieldsVisitor.visitFields(orderExpr, gatherRelationsFromField);
        RefVisitor.visitRefs(orderExpr, gatherRelationsFromRef);
    }
    if (relationsInOrderBy.size() == 1) {
        RelationName relationInOrderBy = relationsInOrderBy.iterator().next();
        if (relationInOrderBy == nestedLoop.topMostLeftRelation().relationName()) {
            LogicalPlan lhs = nestedLoop.sources().get(0);
            LogicalPlan newLhs = order.replaceSources(List.of(lhs));
            return new NestedLoopJoin(newLhs, nestedLoop.sources().get(1), nestedLoop.joinType(), nestedLoop.joinCondition(), nestedLoop.isFiltered(), nestedLoop.topMostLeftRelation(), true, nestedLoop.isRewriteFilterOnOuterJoinToInnerJoinDone());
        }
    }
    return null;
}
Also used : TransactionContext(io.crate.metadata.TransactionContext) NodeContext(io.crate.metadata.NodeContext) LogicalPlan(io.crate.planner.operators.LogicalPlan) Captures(io.crate.planner.optimizer.matcher.Captures) IdentityHashMap(java.util.IdentityHashMap) RelationName(io.crate.metadata.RelationName) Reference(io.crate.metadata.Reference) Pattern(io.crate.planner.optimizer.matcher.Pattern) Set(java.util.Set) NestedLoopJoin(io.crate.planner.operators.NestedLoopJoin) Rule(io.crate.planner.optimizer.Rule) Consumer(java.util.function.Consumer) Order(io.crate.planner.operators.Order) RefVisitor(io.crate.expression.symbol.RefVisitor) List(java.util.List) OrderBy(io.crate.analyze.OrderBy) TableStats(io.crate.statistics.TableStats) Symbol(io.crate.expression.symbol.Symbol) Pattern.typeOf(io.crate.planner.optimizer.matcher.Pattern.typeOf) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) Patterns.source(io.crate.planner.optimizer.matcher.Patterns.source) Collections(java.util.Collections) FieldsVisitor(io.crate.expression.symbol.FieldsVisitor) Capture(io.crate.planner.optimizer.matcher.Capture) OrderBy(io.crate.analyze.OrderBy) Reference(io.crate.metadata.Reference) Symbol(io.crate.expression.symbol.Symbol) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol) NestedLoopJoin(io.crate.planner.operators.NestedLoopJoin) RelationName(io.crate.metadata.RelationName) LogicalPlan(io.crate.planner.operators.LogicalPlan) ScopedSymbol(io.crate.expression.symbol.ScopedSymbol)

Aggregations

ScopedSymbol (io.crate.expression.symbol.ScopedSymbol)8 Symbol (io.crate.expression.symbol.Symbol)6 Reference (io.crate.metadata.Reference)3 RelationName (io.crate.metadata.RelationName)3 VoidReference (io.crate.expression.symbol.VoidReference)2 ColumnIdent (io.crate.metadata.ColumnIdent)2 TableStats (io.crate.statistics.TableStats)2 IdentityHashMap (java.util.IdentityHashMap)2 Test (org.junit.Test)2 SessionContext (io.crate.action.sql.SessionContext)1 OrderBy (io.crate.analyze.OrderBy)1 ValuesResolver (io.crate.analyze.ValuesResolver)1 AliasedAnalyzedRelation (io.crate.analyze.relations.AliasedAnalyzedRelation)1 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)1 FullQualifiedNameFieldProvider (io.crate.analyze.relations.FullQualifiedNameFieldProvider)1 TableRelation (io.crate.analyze.relations.TableRelation)1 ArraySliceFunction (io.crate.expression.scalar.ArraySliceFunction)1 ImplicitCastFunction (io.crate.expression.scalar.cast.ImplicitCastFunction)1 CoalesceFunction (io.crate.expression.scalar.conditional.CoalesceFunction)1 FieldsVisitor (io.crate.expression.symbol.FieldsVisitor)1