Author: shawkins
Date: 2011-07-25 14:19:13 -0400 (Mon, 25 Jul 2011)
New Revision: 3334
Modified:
branches/7.4.x/engine/src/main/java/org/teiid/query/optimizer/relational/PlanToProcessConverter.java
branches/7.4.x/engine/src/main/java/org/teiid/query/optimizer/relational/rules/RulePlanSorts.java
branches/7.4.x/engine/src/main/java/org/teiid/query/processor/relational/GroupingNode.java
branches/7.4.x/engine/src/test/java/org/teiid/query/processor/TestProcessor.java
Log:
TEIID-1682 preserving sort options
Modified:
branches/7.4.x/engine/src/main/java/org/teiid/query/optimizer/relational/PlanToProcessConverter.java
===================================================================
---
branches/7.4.x/engine/src/main/java/org/teiid/query/optimizer/relational/PlanToProcessConverter.java 2011-07-25
17:39:32 UTC (rev 3333)
+++
branches/7.4.x/engine/src/main/java/org/teiid/query/optimizer/relational/PlanToProcessConverter.java 2011-07-25
18:19:13 UTC (rev 3334)
@@ -53,6 +53,7 @@
import org.teiid.query.processor.relational.DependentAccessNode;
import org.teiid.query.processor.relational.DependentProcedureAccessNode;
import org.teiid.query.processor.relational.DependentProcedureExecutionNode;
+import org.teiid.query.processor.relational.EnhancedSortMergeJoinStrategy;
import org.teiid.query.processor.relational.GroupingNode;
import org.teiid.query.processor.relational.InsertPlanExecutionNode;
import org.teiid.query.processor.relational.JoinNode;
@@ -61,7 +62,6 @@
import org.teiid.query.processor.relational.NestedLoopJoinStrategy;
import org.teiid.query.processor.relational.NestedTableJoinStrategy;
import org.teiid.query.processor.relational.NullNode;
-import org.teiid.query.processor.relational.EnhancedSortMergeJoinStrategy;
import org.teiid.query.processor.relational.PlanExecutionNode;
import org.teiid.query.processor.relational.ProjectIntoNode;
import org.teiid.query.processor.relational.ProjectNode;
@@ -93,6 +93,7 @@
import org.teiid.query.sql.symbol.ElementSymbol;
import org.teiid.query.sql.symbol.Expression;
import org.teiid.query.sql.symbol.GroupSymbol;
+import org.teiid.query.sql.symbol.SingleElementSymbol;
import org.teiid.query.sql.util.SymbolMap;
import org.teiid.query.sql.visitor.EvaluatableVisitor;
import org.teiid.query.sql.visitor.GroupCollectorVisitor;
@@ -377,8 +378,22 @@
break;
case NodeConstants.Types.GROUP:
GroupingNode gnode = new GroupingNode(getID());
- gnode.setGroupingElements( (List) node.getProperty(NodeConstants.Info.GROUP_COLS) );
+ List<SingleElementSymbol> gCols = (List)
node.getProperty(NodeConstants.Info.GROUP_COLS);
+ gnode.setGroupingElements( gCols );
gnode.setRemoveDuplicates(node.hasBooleanProperty(NodeConstants.Info.IS_DUP_REMOVAL));
+ orderBy = (OrderBy) node.getProperty(Info.SORT_ORDER);
+ if (orderBy == null) {
+ if (gCols != null) {
+ orderBy = new OrderBy(gCols);
+ }
+ } else {
+ for (int i = orderBy.getOrderByItems().size(); i < gCols.size(); i++) {
+ orderBy.addVariable(gCols.get(i), OrderBy.ASC);
+ }
+ }
+ if (orderBy != null) {
+ gnode.setOrderBy(orderBy.getOrderByItems());
+ }
processNode = gnode;
break;
Modified:
branches/7.4.x/engine/src/main/java/org/teiid/query/optimizer/relational/rules/RulePlanSorts.java
===================================================================
---
branches/7.4.x/engine/src/main/java/org/teiid/query/optimizer/relational/rules/RulePlanSorts.java 2011-07-25
17:39:32 UTC (rev 3333)
+++
branches/7.4.x/engine/src/main/java/org/teiid/query/optimizer/relational/rules/RulePlanSorts.java 2011-07-25
18:19:13 UTC (rev 3334)
@@ -85,7 +85,8 @@
} else {
root = checkForProjectOptimization(node, root, metadata, capFinder, record);
}
- List<SingleElementSymbol> orderColumns =
((OrderBy)node.getProperty(NodeConstants.Info.SORT_ORDER)).getSortKeys();
+ OrderBy orderBy = (OrderBy)node.getProperty(NodeConstants.Info.SORT_ORDER);
+ List<SingleElementSymbol> orderColumns = orderBy.getSortKeys();
PlanNode possibleSort = NodeEditor.findNodePreOrder(node, NodeConstants.Types.GROUP,
NodeConstants.Types.SOURCE | NodeConstants.Types.ACCESS);
if (possibleSort != null) {
List exprs = (List)possibleSort.getProperty(Info.GROUP_COLS);
@@ -102,6 +103,7 @@
NodeEditor.removeChildNode(node.getParent(), node);
node = nextNode;
}
+ possibleSort.setProperty(Info.SORT_ORDER, orderBy);
}
break;
}
Modified:
branches/7.4.x/engine/src/main/java/org/teiid/query/processor/relational/GroupingNode.java
===================================================================
---
branches/7.4.x/engine/src/main/java/org/teiid/query/processor/relational/GroupingNode.java 2011-07-25
17:39:32 UTC (rev 3333)
+++
branches/7.4.x/engine/src/main/java/org/teiid/query/processor/relational/GroupingNode.java 2011-07-25
18:19:13 UTC (rev 3334)
@@ -22,12 +22,10 @@
package org.teiid.query.processor.relational;
-import static org.teiid.query.analysis.AnalysisRecord.PROP_GROUP_COLS;
-import static org.teiid.query.analysis.AnalysisRecord.PROP_SORT_MODE;
+import static org.teiid.query.analysis.AnalysisRecord.*;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
@@ -42,6 +40,7 @@
import org.teiid.core.TeiidComponentException;
import org.teiid.core.TeiidProcessingException;
import org.teiid.language.SQLConstants.NonReserved;
+import org.teiid.query.analysis.AnalysisRecord;
import org.teiid.query.eval.Evaluator;
import org.teiid.query.function.aggregate.AggregateFunction;
import org.teiid.query.function.aggregate.ArrayAgg;
@@ -57,7 +56,6 @@
import org.teiid.query.processor.BatchCollector;
import org.teiid.query.processor.ProcessorDataManager;
import org.teiid.query.processor.relational.SortUtility.Mode;
-import org.teiid.query.sql.lang.OrderBy;
import org.teiid.query.sql.lang.OrderByItem;
import org.teiid.query.sql.symbol.AggregateSymbol;
import org.teiid.query.sql.symbol.ElementSymbol;
@@ -72,7 +70,7 @@
// Grouping columns set by the planner
private List sortElements;
- private List sortTypes;
+ private List<OrderByItem> orderBy;
private boolean removeDuplicates;
// Collection phase
@@ -129,10 +127,11 @@
*/
public void setGroupingElements(List groupingElements) {
this.sortElements = groupingElements;
- if(groupingElements != null) {
- sortTypes = Collections.nCopies(groupingElements.size(),
Boolean.valueOf(OrderBy.ASC));
- }
}
+
+ public void setOrderBy(List<OrderByItem> orderBy) {
+ this.orderBy = orderBy;
+ }
@Override
public void initialize(CommandContext context, BufferManager bufferManager,
@@ -305,8 +304,7 @@
this.groupTupleSource = getCollectionTupleSource();
this.phase = GROUP;
} else {
- this.sortUtility = new SortUtility(getCollectionTupleSource(), sortElements,
- sortTypes,
removeDuplicates?Mode.DUP_REMOVE_SORT:Mode.SORT, getBufferManager(),
+ this.sortUtility = new SortUtility(getCollectionTupleSource(), orderBy,
removeDuplicates?Mode.DUP_REMOVE_SORT:Mode.SORT, getBufferManager(),
getConnectionID(),
collectedExpressions);
this.phase = SORT;
}
@@ -421,8 +419,8 @@
GroupingNode clonedNode = new GroupingNode(super.getID());
super.copy(this, clonedNode);
clonedNode.sortElements = sortElements;
- clonedNode.sortTypes = sortTypes;
clonedNode.removeDuplicates = removeDuplicates;
+ clonedNode.orderBy = orderBy;
return clonedNode;
}
@@ -438,7 +436,9 @@
}
props.addProperty(PROP_GROUP_COLS, groupCols);
}
-
+ if (orderBy != null) {
+ props.addProperty(AnalysisRecord.PROP_SORT_COLS, orderBy.toString());
+ }
props.addProperty(PROP_SORT_MODE, String.valueOf(this.removeDuplicates));
return props;
Modified:
branches/7.4.x/engine/src/test/java/org/teiid/query/processor/TestProcessor.java
===================================================================
---
branches/7.4.x/engine/src/test/java/org/teiid/query/processor/TestProcessor.java 2011-07-25
17:39:32 UTC (rev 3333)
+++
branches/7.4.x/engine/src/test/java/org/teiid/query/processor/TestProcessor.java 2011-07-25
18:19:13 UTC (rev 3334)
@@ -7559,5 +7559,19 @@
helpProcess(plan, dataManager, new List[] {Arrays.asList("a")});
}
+ @Test public void testSortGroupCombination() throws Exception {
+ String sql = "select e2, max(e1) from pm1.g1 x group by e2 order by e2
desc"; //$NON-NLS-1$
+
+ FakeDataManager dataManager = new FakeDataManager();
+ sampleData1(dataManager);
+ ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached(),
TestOptimizer.getGenericFinder());
+
+ helpProcess(plan, dataManager, new List[] {
+ Arrays.asList(3, "a"),
+ Arrays.asList(2, "b"),
+ Arrays.asList(1, "c"),
+ Arrays.asList(0, "a")});
+ }
+
private static final boolean DEBUG = false;
}