@@ -97,9 +97,9 @@ public class RulePlanUnions implements OptimizerRule { boolean all = unionNode.hasBooleanProperty(NodeConstants.Info.USE_ALL); Operation op = (Operation)unionNode.getProperty(NodeConstants.Info.SET_OPERATION); - - collectUnionSources(metadata, capabilitiesFinder, unionNode, sourceNodes, all, op); - + + List childrenToKeep = collectUnionSources(metadata, capabilitiesFinder, unionNode, sourceNodes, all, op); + if (sourceNodes.size() == 1) { continue; } @@ -132,6 +132,7 @@ public class RulePlanUnions implements OptimizerRule { unionNode.removeAllChildren(); unionNode.addChildren(tempRoot.removeAllChildren()); + unionNode.addChildren(childrenToKeep); } } @@ -159,12 +160,13 @@ public class RulePlanUnions implements OptimizerRule { /** * TODO: union and intersect are associative */ - private void collectUnionSources(QueryMetadataInterface metadata, - CapabilitiesFinder capabilitiesFinder, - PlanNode unionNode, - Map> sourceNodes, - boolean all, Operation setOp) throws QueryMetadataException, + private List collectUnionSources(QueryMetadataInterface metadata, + CapabilitiesFinder capabilitiesFinder, + PlanNode unionNode, + Map> sourceNodes, + boolean all, Operation setOp) throws QueryMetadataException, TeiidComponentException { + List childrenToKeep = new ArrayList<>(); for (PlanNode child : unionNode.getChildren()) { if (child.getType() == NodeConstants.Types.SET_OP) { if (!all && Operation.UNION == child.getProperty(NodeConstants.Info.SET_OPERATION)) { @@ -183,6 +185,7 @@ public class RulePlanUnions implements OptimizerRule { } } else { //recursively optimize optimizeUnions(child, metadata, capabilitiesFinder); + childrenToKeep.add(child); } } else { //this must be a source, see if it has a consistent access node List accessNodes = NodeEditor.findAllNodes(child, NodeConstants.Types.ACCESS); @@ -194,9 +197,11 @@ public class RulePlanUnions implements OptimizerRule { if (id == null) { //recursively optimize below this point optimizeUnions(child, metadata, capabilitiesFinder); + childrenToKeep.add(child); } } } + return childrenToKeep; } private Object getModelId(QueryMetadataInterface metadata,