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