Author: shawkins
Date: 2010-02-24 21:07:39 -0500 (Wed, 24 Feb 2010)
New Revision: 1874
Modified:
trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverUtil.java
trunk/engine/src/main/java/com/metamatrix/query/rewriter/QueryRewriter.java
trunk/engine/src/test/java/com/metamatrix/query/rewriter/TestOrderByRewrite.java
Log:
TEIID-989 aliased items should not be referenced in an order by expression
Modified: trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverUtil.java
===================================================================
---
trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverUtil.java 2010-02-24
22:49:00 UTC (rev 1873)
+++
trunk/engine/src/main/java/com/metamatrix/query/resolver/util/ResolverUtil.java 2010-02-25
02:07:39 UTC (rev 1874)
@@ -348,8 +348,8 @@
for (int i = 0; i < orderBy.getVariableCount(); i++) {
SingleElementSymbol sortKey = orderBy.getVariable(i);
if (sortKey instanceof ElementSymbol) {
- int index = resolveSortKey(orderBy, fromClauseGroups, knownElements, metadata,
- isSimpleQuery, knownShortNames, (ElementSymbol)sortKey);
+ int index = resolveSortKey(fromClauseGroups, knownElements, metadata,
isSimpleQuery,
+ knownShortNames, (ElementSymbol)sortKey);
if (index == -1) {
index = expressions.indexOf(SymbolMap.getExpression(sortKey));
}
@@ -374,8 +374,7 @@
throw new
QueryResolverException(QueryPlugin.Util.getString("ResolverUtil.setquery_order_expression",
sortKey)); //$NON-NLS-1$
}
for (ElementSymbol symbol : ElementCollectorVisitor.getElements(sortKey, false))
{
- resolveSortKey(orderBy, fromClauseGroups, knownElements, metadata,
- isSimpleQuery, knownShortNames, symbol);
+ resolveSortKey(fromClauseGroups, null, metadata, isSimpleQuery, null,
symbol);
}
ResolverVisitor.resolveLanguageObject(sortKey, metadata);
@@ -384,10 +383,9 @@
}
}
- private static int resolveSortKey(OrderBy orderBy, List fromClauseGroups,
- List knownElements, QueryMetadataInterface metadata,
- boolean isSimpleQuery, String[] knownShortNames,
- ElementSymbol symbol) throws MetaMatrixComponentException,
+ private static int resolveSortKey(List fromClauseGroups, List knownElements,
+ QueryMetadataInterface metadata, boolean isSimpleQuery,
+ String[] knownShortNames, ElementSymbol symbol) throws MetaMatrixComponentException,
QueryMetadataException, QueryResolverException {
SingleElementSymbol matchedSymbol = null;
String symbolName = symbol.getName();
@@ -399,38 +397,40 @@
throw new QueryResolverException(ErrorMessageKeys.RESOLVER_0043,
QueryPlugin.Util.getString(ErrorMessageKeys.RESOLVER_0043, symbolName));
}
- // walk the SELECT col short names, looking for a match on the current ORDER BY
'short name'
- for(int i=0; i<knownShortNames.length; i++) {
- if( shortName.equalsIgnoreCase( knownShortNames[i] )) {
- if (groupPart != null) {
- Object knownSymbol = knownElements.get(i);
- if(knownSymbol instanceof ElementSymbol) {
- ElementSymbol knownElement = (ElementSymbol) knownSymbol;
- GroupSymbol group = knownElement.getGroupSymbol();
-
- // skip this one if the two short names are not from the same group
- if (!nameMatchesGroup(groupPart.toUpperCase(),
group.getCanonicalName())) {
- continue;
- }
- }
- }
-
- // if we already have a matched symbol, matching again here means it is
duplicate/ambiguous
- if(matchedSymbol != null) {
- throw new QueryResolverException(ErrorMessageKeys.RESOLVER_0042,
QueryPlugin.Util.getString(ErrorMessageKeys.RESOLVER_0042, symbolName));
- }
- matchedSymbol = (SingleElementSymbol)knownElements.get(i);
- }
+ if (knownShortNames != null) {
+ // walk the SELECT col short names, looking for a match on the current ORDER BY
'short name'
+ for(int i=0; i<knownShortNames.length; i++) {
+ if( shortName.equalsIgnoreCase( knownShortNames[i] )) {
+ if (groupPart != null) {
+ Object knownSymbol = knownElements.get(i);
+ if(knownSymbol instanceof ElementSymbol) {
+ ElementSymbol knownElement = (ElementSymbol) knownSymbol;
+ GroupSymbol group = knownElement.getGroupSymbol();
+
+ // skip this one if the two short names are not from the same group
+ if (!nameMatchesGroup(groupPart.toUpperCase(),
group.getCanonicalName())) {
+ continue;
+ }
+ }
+ }
+
+ // if we already have a matched symbol, matching again here means it is
duplicate/ambiguous
+ if(matchedSymbol != null) {
+ throw new QueryResolverException(ErrorMessageKeys.RESOLVER_0042,
QueryPlugin.Util.getString(ErrorMessageKeys.RESOLVER_0042, symbolName));
+ }
+ matchedSymbol = (SingleElementSymbol)knownElements.get(i);
+ }
+ }
+ if (matchedSymbol != null) {
+ TempMetadataID tempMetadataID = new TempMetadataID(symbol.getName(),
matchedSymbol.getType());
+ int position = knownElements.indexOf(matchedSymbol);
+ tempMetadataID.setPosition(position);
+ symbol.setMetadataID(tempMetadataID);
+ symbol.setType(matchedSymbol.getType());
+ return position;
+ }
}
- if (matchedSymbol != null) {
- TempMetadataID tempMetadataID = new TempMetadataID(symbol.getName(),
matchedSymbol.getType());
- int position = knownElements.indexOf(matchedSymbol);
- tempMetadataID.setPosition(position);
- symbol.setMetadataID(tempMetadataID);
- symbol.setType(matchedSymbol.getType());
- return position;
- }
if (!isSimpleQuery) {
throw new
QueryResolverException(QueryPlugin.Util.getString("ResolverUtil.invalid_unrelated",
symbol.getName())); //$NON-NLS-1$
}
Modified: trunk/engine/src/main/java/com/metamatrix/query/rewriter/QueryRewriter.java
===================================================================
--- trunk/engine/src/main/java/com/metamatrix/query/rewriter/QueryRewriter.java 2010-02-24
22:49:00 UTC (rev 1873)
+++ trunk/engine/src/main/java/com/metamatrix/query/rewriter/QueryRewriter.java 2010-02-25
02:07:39 UTC (rev 1874)
@@ -65,7 +65,6 @@
import com.metamatrix.query.function.FunctionMethods;
import com.metamatrix.query.metadata.QueryMetadataInterface;
import com.metamatrix.query.metadata.TempMetadataAdapter;
-import com.metamatrix.query.metadata.TempMetadataID;
import com.metamatrix.query.metadata.TempMetadataStore;
import com.metamatrix.query.processor.ProcessorDataManager;
import com.metamatrix.query.processor.relational.DependentValueSource;
@@ -141,7 +140,6 @@
import com.metamatrix.query.sql.symbol.Reference;
import com.metamatrix.query.sql.symbol.ScalarSubquery;
import com.metamatrix.query.sql.symbol.SearchedCaseExpression;
-import com.metamatrix.query.sql.symbol.SelectSymbol;
import com.metamatrix.query.sql.symbol.SingleElementSymbol;
import com.metamatrix.query.sql.util.SymbolMap;
import com.metamatrix.query.sql.util.ValueIterator;
@@ -807,7 +805,7 @@
if (index == -1) {
unrelatedItems.add(orderBy.getOrderByItems().get(i));
hasUnrelatedExpression |= (querySymbol instanceof ExpressionSymbol);
- continue; // must be unrelated - but potentially contains references to the
select clause
+ continue; // must be unrelated
}
querySymbol = (SingleElementSymbol)projectedSymbols.get(index);
Expression expr = SymbolMap.getExpression(querySymbol);
@@ -831,12 +829,7 @@
//add unrelated to select
for (OrderByItem orderByItem : unrelatedItems) {
- Collection<ElementSymbol> elements =
ElementCollectorVisitor.getElements(orderByItem.getSymbol(), true);
- for (ElementSymbol elementSymbol : elements) {
- if (!(elementSymbol.getMetadataID() instanceof TempMetadataID) ||
((TempMetadataID)elementSymbol.getMetadataID()).getPosition() == -1) {
- select.addSymbol(elementSymbol);
- }
- }
+ select.addSymbol(orderByItem.getSymbol());
}
makeSelectUnique(select, false);
@@ -851,8 +844,8 @@
Query top = null;
try {
- Query intermediate = createInlineViewQuery(new GroupSymbol("X"),
query, metadata, select.getProjectedSymbols()); //$NON-NLS-1$
- Iterator iter = intermediate.getSelect().getProjectedSymbols().iterator();
+ top = createInlineViewQuery(new GroupSymbol("X"), query, metadata,
select.getProjectedSymbols()); //$NON-NLS-1$
+ Iterator iter = top.getSelect().getProjectedSymbols().iterator();
HashMap<Expression, SingleElementSymbol> expressionMap = new
HashMap<Expression, SingleElementSymbol>();
for (SingleElementSymbol symbol :
(List<SingleElementSymbol>)select.getProjectedSymbols()) {
SingleElementSymbol ses = (SingleElementSymbol)iter.next();
@@ -860,22 +853,6 @@
expressionMap.put(new ElementSymbol(symbol.getName()), ses);
}
ExpressionMappingVisitor.mapExpressions(orderBy, expressionMap);
- for (int i = 0; i < orderBy.getVariableCount(); i++) {
- int index = orderBy.getExpressionPosition(i);
- SingleElementSymbol ss = orderBy.getVariable(i);
- if (index == -1 && !(ss instanceof ElementSymbol)) {
- intermediate.getSelect().addSymbol((SelectSymbol)ss.clone());
- }
- }
- makeSelectUnique(intermediate.getSelect(), true);
-
- top = createInlineViewQuery(new GroupSymbol("Y"), intermediate,
metadata, intermediate.getSelect().getProjectedSymbols()); //$NON-NLS-1$
- iter = top.getSelect().getProjectedSymbols().iterator();
- expressionMap = new HashMap<Expression, SingleElementSymbol>();
- for (SingleElementSymbol symbol :
(List<SingleElementSymbol>)intermediate.getSelect().getProjectedSymbols()) {
- expressionMap.put(SymbolMap.getExpression(symbol),
(SingleElementSymbol)iter.next());
- }
- ExpressionMappingVisitor.mapExpressions(orderBy, expressionMap);
//now the order by should only contain element symbols
} catch (QueryResolverException e) {
throw new QueryValidatorException(e, e.getMessage());
Modified:
trunk/engine/src/test/java/com/metamatrix/query/rewriter/TestOrderByRewrite.java
===================================================================
---
trunk/engine/src/test/java/com/metamatrix/query/rewriter/TestOrderByRewrite.java 2010-02-24
22:49:00 UTC (rev 1873)
+++
trunk/engine/src/test/java/com/metamatrix/query/rewriter/TestOrderByRewrite.java 2010-02-25
02:07:39 UTC (rev 1874)
@@ -309,8 +309,8 @@
}
@Test public void testOrderByExpression() throws Exception {
- Query resolvedQuery = (Query) getCommand("SELECT 0 AS SOMEINT, pm1.g1.e2 as y
FROM pm1.g1 ORDER BY y + SOMEINT, e3"); //$NON-NLS-1$
- assertEquals("SELECT Y_1.SOMEINT, Y_1.Y FROM (SELECT X_1.SOMEINT, X_1.Y,
X_1.E3, (X_1.Y + X_1.SOMEINT) AS EXPR1 FROM (SELECT 0 AS SOMEINT, pm1.g1.e2 AS y, e3 FROM
pm1.g1) AS X_1) AS Y_1 ORDER BY Y_1.EXPR1, Y_1.E3", resolvedQuery.toString());
//$NON-NLS-1$
+ Query resolvedQuery = (Query) getCommand("SELECT 0 AS SOMEINT, pm1.g1.e2 as y
FROM pm1.g1 ORDER BY e2 || e1, e3"); //$NON-NLS-1$
+ assertEquals("SELECT X_1.SOMEINT, X_1.Y FROM (SELECT 0 AS SOMEINT, pm1.g1.e2
AS y, (e2 || e1) AS EXPR1, e3 FROM pm1.g1) AS X_1 ORDER BY X_1.EXPR1, X_1.E3",
resolvedQuery.toString()); //$NON-NLS-1$
}
@Test public void testRewiteOrderBy() {