Search in sources :

Example 1 with BasicQueryMetadata

use of org.teiid.query.metadata.BasicQueryMetadata in project teiid by teiid.

the class RelationalPlanner method buildGroupingNode.

/**
 * Build a grouping node that introduces a anon group (without a inline view source node)
 */
public static SymbolMap buildGroupingNode(Collection<AggregateSymbol> aggs, List<? extends Expression> groupingCols, PlanNode groupNode, CommandContext cc, IDGenerator idGenerator) throws QueryMetadataException, TeiidComponentException {
    SymbolMap map = new SymbolMap();
    aggs = LanguageObject.Util.deepClone(aggs, AggregateSymbol.class);
    groupingCols = LanguageObject.Util.deepClone(groupingCols, Expression.class);
    // $NON-NLS-1$
    GroupSymbol group = new GroupSymbol("anon_grp" + idGenerator.nextInt());
    if (!cc.getGroups().add(group.getName())) {
        group = RulePlaceAccess.recontextSymbol(group, cc.getGroups());
    }
    TempMetadataStore tms = new TempMetadataStore();
    int i = 0;
    List<AliasSymbol> symbols = new LinkedList<AliasSymbol>();
    List<Expression> targets = new LinkedList<Expression>();
    if (groupingCols != null) {
        groupNode.setProperty(NodeConstants.Info.GROUP_COLS, groupingCols);
        groupNode.addGroups(GroupsUsedByElementsVisitor.getGroups(groupingCols));
        for (Expression ex : groupingCols) {
            // $NON-NLS-1$ //$NON-NLS-2$
            AliasSymbol as = new AliasSymbol("gcol" + i++, new ExpressionSymbol("expr", ex));
            targets.add(ex);
            symbols.add(as);
        }
    }
    i = 0;
    for (AggregateSymbol ex : aggs) {
        // $NON-NLS-1$ //$NON-NLS-2$
        AliasSymbol as = new AliasSymbol("agg" + i++, new ExpressionSymbol("expr", ex));
        targets.add(ex);
        symbols.add(as);
    }
    group.setMetadataID(tms.addTempGroup(group.getName(), symbols, true, false));
    Iterator<Expression> targetIter = targets.iterator();
    for (ElementSymbol es : ResolverUtil.resolveElementsInGroup(group, new TempMetadataAdapter(new BasicQueryMetadata(), tms))) {
        Expression target = targetIter.next();
        es.setAggregate(target instanceof AggregateSymbol);
        map.addMapping(es, target);
    }
    groupNode.setProperty(NodeConstants.Info.SYMBOL_MAP, map);
    groupNode.addGroup(group);
    return map;
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) SymbolMap(org.teiid.query.sql.util.SymbolMap) BasicQueryMetadata(org.teiid.query.metadata.BasicQueryMetadata) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore)

Aggregations

BasicQueryMetadata (org.teiid.query.metadata.BasicQueryMetadata)1 TempMetadataAdapter (org.teiid.query.metadata.TempMetadataAdapter)1 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)1 SymbolMap (org.teiid.query.sql.util.SymbolMap)1