Search in sources :

Example 6 with AliasSymbol

use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.

the class MetaDataProcessor method createProjectedSymbolMetadata.

private Map<Integer, Object>[] createProjectedSymbolMetadata(Command originalCommand) throws TeiidComponentException {
    Map<Integer, Object>[] columnMetadata;
    // Allow command to use temporary metadata
    TempMetadataStore tempMetadata = originalCommand.getTemporaryMetadata();
    if (tempMetadata != null && tempMetadata.getData().size() > 0) {
        TempMetadataAdapter tempFacade = new TempMetadataAdapter(this.metadata, tempMetadata);
        this.metadata = tempFacade;
    }
    List<Expression> projectedSymbols = originalCommand.getProjectedSymbols();
    columnMetadata = new Map[projectedSymbols.size()];
    Iterator<Expression> symbolIter = projectedSymbols.iterator();
    for (int i = 0; symbolIter.hasNext(); i++) {
        Expression symbol = symbolIter.next();
        String shortColumnName = Symbol.getShortName(Symbol.getOutputName(symbol));
        if (symbol instanceof AliasSymbol) {
            symbol = ((AliasSymbol) symbol).getSymbol();
        }
        try {
            columnMetadata[i] = createColumnMetadata(shortColumnName, symbol);
        } catch (QueryMetadataException e) {
            throw new TeiidComponentException(QueryPlugin.Event.TEIID30559, e);
        }
    }
    return columnMetadata;
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Expression(org.teiid.query.sql.symbol.Expression) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) HashMap(java.util.HashMap) Map(java.util.Map) SymbolMap(org.teiid.query.sql.util.SymbolMap) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore)

Example 7 with AliasSymbol

use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.

the class LanguageBridgeFactory method translate.

/* Query */
Select translate(Query query) {
    With with = translate(query.getWith());
    List<Expression> symbols = query.getSelect().getSymbols();
    List<DerivedColumn> translatedSymbols = new ArrayList<DerivedColumn>(symbols.size());
    for (Iterator<Expression> i = symbols.iterator(); i.hasNext(); ) {
        Expression symbol = i.next();
        String alias = null;
        if (symbol instanceof AliasSymbol) {
            alias = ((AliasSymbol) symbol).getOutputName();
            symbol = ((AliasSymbol) symbol).getSymbol();
        }
        org.teiid.language.Expression iExp = translate(symbol);
        DerivedColumn selectSymbol = new DerivedColumn(alias, iExp);
        translatedSymbols.add(selectSymbol);
    }
    List<TableReference> items = null;
    if (query.getFrom() != null) {
        List<FromClause> clauses = query.getFrom().getClauses();
        items = new ArrayList<TableReference>(clauses.size());
        for (Iterator<FromClause> i = clauses.iterator(); i.hasNext(); ) {
            items.add(translate(i.next()));
        }
    }
    Select q = new Select(translatedSymbols, query.getSelect().isDistinct(), items, translate(query.getCriteria()), translate(query.getGroupBy()), translate(query.getHaving()), translate(query.getOrderBy(), false));
    q.setLimit(translate(query.getLimit()));
    q.setWith(with);
    return q;
}
Also used : org.teiid.language(org.teiid.language) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Expression(org.teiid.query.sql.symbol.Expression) Select(org.teiid.language.Select) DerivedColumn(org.teiid.language.DerivedColumn)

Example 8 with AliasSymbol

use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.

the class SetQuery method getTypedProjectedSymbols.

public static List<Expression> getTypedProjectedSymbols(List<? extends Expression> acutal, List<Class<?>> projectedTypes, QueryMetadataInterface metadata) {
    List<Expression> newProject = new ArrayList<Expression>();
    for (int i = 0; i < acutal.size(); i++) {
        Expression originalSymbol = acutal.get(i);
        Expression symbol = originalSymbol;
        Class<?> type = projectedTypes.get(i);
        if (symbol.getType() != type) {
            symbol = SymbolMap.getExpression(originalSymbol);
            try {
                symbol = ResolverUtil.convertExpression(symbol, DataTypeManager.getDataTypeName(type), metadata);
            } catch (QueryResolverException err) {
                throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30447, err);
            }
            if (originalSymbol instanceof Symbol) {
                symbol = new AliasSymbol(Symbol.getShortName(originalSymbol), symbol);
            }
        }
        newProject.add(symbol);
    }
    return newProject;
}
Also used : AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Expression(org.teiid.query.sql.symbol.Expression) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Symbol(org.teiid.query.sql.symbol.Symbol) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 9 with AliasSymbol

use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.

the class RuleAssignOutputElements method collectRequiredInputSymbols.

/**
 * Collect all required input symbols for a given node.  Input symbols
 * are any symbols that are required in the processing of this node,
 * for instance to create a new element symbol or sort on it, etc.
 * @param node Node to collect for
 * @param metadata
 * @param capFinder
 * @throws TeiidComponentException
 * @throws QueryMetadataException
 */
private List<Expression> collectRequiredInputSymbols(PlanNode node, QueryMetadataInterface metadata, CapabilitiesFinder capFinder) throws QueryMetadataException, TeiidComponentException {
    Set<Expression> requiredSymbols = new LinkedHashSet<Expression>();
    Set<Expression> createdSymbols = new HashSet<Expression>();
    List<Expression> outputCols = (List<Expression>) node.getProperty(NodeConstants.Info.OUTPUT_COLS);
    switch(node.getType()) {
        case NodeConstants.Types.PROJECT:
            {
                List<Expression> projectCols = (List<Expression>) node.getProperty(NodeConstants.Info.PROJECT_COLS);
                PlanNode accessParent = NodeEditor.findParent(node, NodeConstants.Types.ACCESS);
                PlanNode accessNode = null;
                if (accessParent == null) {
                    // find the direct access node
                    accessNode = NodeEditor.findNodePreOrder(node, NodeConstants.Types.ACCESS, NodeConstants.Types.SOURCE | NodeConstants.Types.JOIN | NodeConstants.Types.SET_OP | NodeConstants.Types.GROUP);
                }
                for (Expression ss : projectCols) {
                    if (ss instanceof AliasSymbol) {
                        createdSymbols.add(ss);
                        ss = ((AliasSymbol) ss).getSymbol();
                    }
                    if (ss instanceof WindowFunction || ss instanceof ExpressionSymbol) {
                        createdSymbols.add(ss);
                    }
                    if (!pushProjection(node, metadata, capFinder, requiredSymbols, accessParent, accessNode, ss)) {
                        ElementCollectorVisitor.getElements(ss, requiredSymbols);
                    }
                }
                break;
            }
        case NodeConstants.Types.SELECT:
            Criteria selectCriteria = (Criteria) node.getProperty(NodeConstants.Info.SELECT_CRITERIA);
            ElementCollectorVisitor.getElements(selectCriteria, requiredSymbols);
            break;
        case NodeConstants.Types.JOIN:
            List<Criteria> crits = (List) node.getProperty(NodeConstants.Info.JOIN_CRITERIA);
            if (crits != null) {
                for (Criteria joinCriteria : crits) {
                    ElementCollectorVisitor.getElements(joinCriteria, requiredSymbols);
                }
            }
            break;
        case NodeConstants.Types.GROUP:
            List<Expression> groupCols = (List<Expression>) node.getProperty(NodeConstants.Info.GROUP_COLS);
            PlanNode accessParent = NodeEditor.findParent(node, NodeConstants.Types.ACCESS);
            PlanNode accessNode = null;
            if (accessParent == null) {
                // find the direct access node
                accessNode = NodeEditor.findNodePreOrder(node.getFirstChild(), NodeConstants.Types.ACCESS, NodeConstants.Types.SOURCE | NodeConstants.Types.JOIN | NodeConstants.Types.SET_OP | NodeConstants.Types.GROUP);
            }
            if (groupCols != null) {
                for (Expression expression : groupCols) {
                    if (!pushProjection(node, metadata, capFinder, requiredSymbols, accessParent, accessNode, expression)) {
                        ElementCollectorVisitor.getElements(expression, requiredSymbols);
                    }
                }
            }
            SymbolMap symbolMap = (SymbolMap) node.getProperty(NodeConstants.Info.SYMBOL_MAP);
            Set<ElementSymbol> usedAggregates = new HashSet<ElementSymbol>();
            // Take credit for creating any aggregates that are needed above
            for (Expression outputSymbol : outputCols) {
                if (!(outputSymbol instanceof ElementSymbol)) {
                    continue;
                }
                createdSymbols.add(outputSymbol);
                Expression ex = symbolMap.getMappedExpression((ElementSymbol) outputSymbol);
                if (ex instanceof AggregateSymbol) {
                    AggregateSymbol agg = (AggregateSymbol) ex;
                    Expression[] aggExprs = agg.getArgs();
                    for (Expression expression : aggExprs) {
                        if (!pushProjection(node, metadata, capFinder, requiredSymbols, accessParent, accessNode, expression)) {
                            ElementCollectorVisitor.getElements(expression, requiredSymbols);
                        }
                    }
                    OrderBy orderBy = agg.getOrderBy();
                    if (orderBy != null) {
                        ElementCollectorVisitor.getElements(orderBy, requiredSymbols);
                    }
                    Expression condition = agg.getCondition();
                    if (condition != null) {
                        ElementCollectorVisitor.getElements(condition, requiredSymbols);
                    }
                    usedAggregates.add((ElementSymbol) outputSymbol);
                }
            }
            // update the aggs in the symbolmap
            for (Map.Entry<ElementSymbol, Expression> entry : new ArrayList<Map.Entry<ElementSymbol, Expression>>(symbolMap.asMap().entrySet())) {
                if (entry.getValue() instanceof AggregateSymbol && !usedAggregates.contains(entry.getKey())) {
                    symbolMap.asUpdatableMap().remove(entry.getKey());
                }
            }
            if (requiredSymbols.isEmpty() && usedAggregates.isEmpty()) {
                node.setProperty(Info.IS_OPTIONAL, true);
            }
            break;
    }
    // Gather elements from correlated subquery references;
    for (SymbolMap refs : node.getAllReferences()) {
        for (Expression expr : refs.asMap().values()) {
            ElementCollectorVisitor.getElements(expr, requiredSymbols);
        }
    }
    // Add any columns to required that are in this node's output but were not created here
    for (Expression currentOutputSymbol : outputCols) {
        if (!createdSymbols.contains(currentOutputSymbol) && (finalRun || node.getType() != NodeConstants.Types.PROJECT || currentOutputSymbol instanceof ElementSymbol)) {
            requiredSymbols.add(currentOutputSymbol);
        }
    }
    // TODO: this should depend upon whether the expressions are deterministic
    if (node.getType() == NodeConstants.Types.PROJECT) {
        Set<Expression> expressions = new HashSet<Expression>();
        for (Iterator<Expression> iterator = requiredSymbols.iterator(); iterator.hasNext(); ) {
            Expression ses = iterator.next();
            if (!expressions.add(SymbolMap.getExpression(ses))) {
                iterator.remove();
            }
        }
    }
    return new ArrayList<Expression>(requiredSymbols);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) OrderBy(org.teiid.query.sql.lang.OrderBy) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) ArrayList(java.util.ArrayList) SymbolMap(org.teiid.query.sql.util.SymbolMap) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) Criteria(org.teiid.query.sql.lang.Criteria) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Expression(org.teiid.query.sql.symbol.Expression) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) SymbolMap(org.teiid.query.sql.util.SymbolMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 10 with AliasSymbol

use of org.teiid.query.sql.symbol.AliasSymbol in project teiid by teiid.

the class AliasGenerator method visit.

public void visit(Select obj) {
    List<Expression> selectSymbols = obj.getSymbols();
    LinkedHashMap<Expression, String> symbols = new LinkedHashMap<Expression, String>(selectSymbols.size());
    for (int i = 0; i < selectSymbols.size(); i++) {
        Expression symbol = selectSymbols.get(i);
        visitNode(symbol);
        boolean needsAlias = visitor.namingContext.aliasColumns;
        // $NON-NLS-1$
        String newAlias = "c_" + i;
        Expression newSymbol = SymbolMap.getExpression(symbol);
        if (newSymbol instanceof ElementSymbol) {
            if (!needsAlias) {
                newAlias = ((ElementSymbol) newSymbol).getShortName();
            } else {
                needsAlias &= needsAlias(newAlias, (ElementSymbol) newSymbol);
            }
        }
        symbols.put(symbol, newAlias);
        if (visitor.namingContext.aliasColumns && needsAlias) {
            newSymbol = new AliasSymbol(Symbol.getShortName(symbol), newSymbol);
            ((AliasSymbol) newSymbol).setShortName(newAlias);
        }
        selectSymbols.set(i, newSymbol);
    }
    visitor.namingContext.currentSymbols = symbols;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Expression(org.teiid.query.sql.symbol.Expression) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

AliasSymbol (org.teiid.query.sql.symbol.AliasSymbol)19 Expression (org.teiid.query.sql.symbol.Expression)12 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)9 ArrayList (java.util.ArrayList)7 ExpressionSymbol (org.teiid.query.sql.symbol.ExpressionSymbol)5 List (java.util.List)3 Map (java.util.Map)3 OrderBy (org.teiid.query.sql.lang.OrderBy)3 AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)3 Constant (org.teiid.query.sql.symbol.Constant)3 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)3 SymbolMap (org.teiid.query.sql.util.SymbolMap)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Test (org.junit.Test)2 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)2 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)2 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)2 Reference (org.teiid.query.sql.symbol.Reference)2