Search in sources :

Example 1 with Symbol

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

the class LanguageBridgeFactory method translate.

public org.teiid.language.OrderBy translate(OrderBy orderBy, boolean set) {
    if (orderBy == null) {
        return null;
    }
    List<OrderByItem> items = orderBy.getOrderByItems();
    List<SortSpecification> translatedItems = new ArrayList<SortSpecification>();
    for (int i = 0; i < items.size(); i++) {
        Expression symbol = items.get(i).getSymbol();
        Ordering direction = items.get(i).isAscending() ? Ordering.ASC : Ordering.DESC;
        SortSpecification orderByItem = null;
        if (!set && (items.get(i).isUnrelated() || symbol instanceof ElementSymbol)) {
            orderByItem = new SortSpecification(direction, translate(symbol));
        } else {
            orderByItem = new SortSpecification(direction, new ColumnReference(null, Symbol.getShortName(((Symbol) symbol).getOutputName()), null, symbol.getType()));
        }
        orderByItem.setNullOrdering(items.get(i).getNullOrdering());
        translatedItems.add(orderByItem);
    }
    return new org.teiid.language.OrderBy(translatedItems);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) OrderBy(org.teiid.query.sql.lang.OrderBy) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) Symbol(org.teiid.query.sql.symbol.Symbol) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Expression(org.teiid.query.sql.symbol.Expression) Ordering(org.teiid.language.SortSpecification.Ordering)

Example 2 with Symbol

use of org.teiid.query.sql.symbol.Symbol 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 3 with Symbol

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

the class AliasGenerator method visit.

public void visit(OrderBy obj) {
    // add/correct aliases if necessary
    for (int i = 0; i < obj.getVariableCount(); i++) {
        OrderByItem item = obj.getOrderByItems().get(i);
        Expression element = item.getSymbol();
        visitNode(element);
        Expression expr = SymbolMap.getExpression(element);
        if (item.isUnrelated()) {
            item.setSymbol(expr);
            continue;
        }
        String name = null;
        if (visitor.namingContext.currentSymbols != null) {
            name = visitor.namingContext.currentSymbols.get(element);
        }
        if (name == null) {
            // this is a bit messy, because we have cloned to do the aliasing, there
            // is a chance that a subquery is throwing off the above get
            int pos = item.getExpressionPosition();
            if (pos < visitor.namingContext.currentSymbols.size()) {
                ArrayList<Map.Entry<Expression, String>> list = new ArrayList<Map.Entry<Expression, String>>(visitor.namingContext.currentSymbols.entrySet());
                name = list.get(pos).getValue();
                expr = SymbolMap.getExpression(list.get(pos).getKey());
            } else {
                name = Symbol.getShortName(element);
            }
        }
        boolean needsAlias = visitor.namingContext.aliasColumns;
        if (name == null) {
            continue;
        }
        if (expr instanceof ElementSymbol) {
            needsAlias &= needsAlias(name, (ElementSymbol) expr);
        }
        if (needsAlias) {
            element = new AliasSymbol(Symbol.getShortName(element), expr);
        } else {
            element = expr;
            if (expr instanceof ElementSymbol && visitor.namingContext.aliasColumns) {
                ((ElementSymbol) expr).setDisplayMode(DisplayMode.SHORT_OUTPUT_NAME);
            }
        }
        item.setSymbol(element);
        if (element instanceof Symbol) {
            ((Symbol) element).setShortName(name);
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Entry(java.util.Map.Entry) 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) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) SymbolMap(org.teiid.query.sql.util.SymbolMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 4 with Symbol

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

the class TestResolver method helpResolveSubquery.

/**
 * Helps resolve command, then check that the actual resolved Elements variables are the same as
 * the expected variable names.  The variableNames param will be empty unless the subquery
 * is a correlated subquery.
 * @param sql Command to parse and resolve
 * @param variableNames expected element symbol variable names, in order
 * @return parsed and resolved Query
 */
private Command helpResolveSubquery(String sql, String[] variableNames) {
    Query query = (Query) helpResolve(sql);
    Collection<ElementSymbol> variables = getVariables(query);
    assertTrue(// $NON-NLS-1$ //$NON-NLS-2$
    "Expected variables size " + variableNames.length + " but was " + variables.size(), variables.size() == variableNames.length);
    Iterator<ElementSymbol> variablesIter = variables.iterator();
    for (int i = 0; variablesIter.hasNext(); i++) {
        ElementSymbol variable = variablesIter.next();
        assertTrue(// $NON-NLS-1$ //$NON-NLS-2$
        "Expected variable name " + variableNames[i] + " but was " + variable.getName(), variable.getName().equalsIgnoreCase(variableNames[i]));
    }
    if (variableNames.length == 0) {
        // There should be no TempMetadataIDs
        Collection<Symbol> symbols = CheckNoTempMetadataIDsVisitor.checkSymbols(query);
        // $NON-NLS-1$
        assertTrue("Expected no symbols with temp metadataIDs, but got " + symbols, symbols.isEmpty());
    }
    return query;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) XMLQuery(org.teiid.query.sql.symbol.XMLQuery) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Symbol(org.teiid.query.sql.symbol.Symbol) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol)

Example 5 with Symbol

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

the class TestStaticSymbolMappingVisitor method helpTest.

private void helpTest(LanguageObject obj, Map symbolMap) {
    // Get old elements and groups
    List oldSymbols = (List) ElementCollectorVisitor.getElements(obj, false);
    GroupCollectorVisitor.getGroups(obj, oldSymbols);
    // Run symbol mapper
    StaticSymbolMappingVisitor visitor = new StaticSymbolMappingVisitor(symbolMap);
    DeepPreOrderNavigator.doVisit(obj, visitor);
    // Get new elements and groups
    List newSymbols = (List) ElementCollectorVisitor.getElements(obj, false);
    GroupCollectorVisitor.getGroups(obj, newSymbols);
    // Check number of elements and groups
    // $NON-NLS-1$
    assertEquals("Different number of symbols after mapping: ", oldSymbols.size(), newSymbols.size());
    // Compare mapped elements
    Iterator oldIter = oldSymbols.iterator();
    Iterator newIter = newSymbols.iterator();
    while (oldIter.hasNext()) {
        Symbol oldSymbol = (Symbol) oldIter.next();
        Symbol newSymbol = (Symbol) newIter.next();
        Symbol expectedSymbol = (Symbol) symbolMap.get(oldSymbol);
        // $NON-NLS-1$
        assertEquals("Did not get correct mapped symbol: ", expectedSymbol, newSymbol);
    }
}
Also used : Symbol(org.teiid.query.sql.symbol.Symbol) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) MultipleElementSymbol(org.teiid.query.sql.symbol.MultipleElementSymbol) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Symbol (org.teiid.query.sql.symbol.Symbol)5 AliasSymbol (org.teiid.query.sql.symbol.AliasSymbol)4 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)4 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)4 ArrayList (java.util.ArrayList)3 Expression (org.teiid.query.sql.symbol.Expression)3 ExpressionSymbol (org.teiid.query.sql.symbol.ExpressionSymbol)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)1 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)1 Ordering (org.teiid.language.SortSpecification.Ordering)1 OrderBy (org.teiid.query.sql.lang.OrderBy)1 AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)1 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)1 SearchedCaseExpression (org.teiid.query.sql.symbol.SearchedCaseExpression)1