private void collectSymbolsFromOtherAggregates(Collection<AggregateSymbol> allAggregates, Collection<AggregateSymbol> aggregates, PlanNode current, Set<Expression> stagedGroupingSymbols) { Set<AggregateSymbol> otherAggs = new HashSet<AggregateSymbol>(allAggregates); if (aggregates != null) { otherAggs.removeAll(aggregates); } PlanNode source = FrameUtil.findJoinSourceNode(current); for (AggregateSymbol aggregateSymbol : otherAggs) { for (ElementSymbol symbol : ElementCollectorVisitor.getElements(aggregateSymbol, true)) { if (source.getGroups().contains(symbol.getGroupSymbol())) { stagedGroupingSymbols.add(symbol); } } } }
/** * Find the SOURCE, SET_OP, JOIN, or NULL node that originates the given groups (typically from a criteria node). * In the case of join nodes the best fit will be found rather than just the first * join node that contains all the groups. * * Returns null if the originating node cannot be found. * * @param root * @param groups * @return */ static PlanNode findOriginatingNode(PlanNode root, Set<GroupSymbol> groups) { return findOriginatingNode(root, groups, false); }
static void correctSymbolMap(Map symbolMap, PlanNode endNode) { // Top of a frame - fix symbol mappings on endNode SymbolMap parentSymbolMap = (SymbolMap) endNode.getProperty(NodeConstants.Info.SYMBOL_MAP); if(parentSymbolMap == null) { return; } for (Map.Entry<ElementSymbol, Expression> entry : parentSymbolMap.asUpdatableMap().entrySet()) { entry.setValue(convertExpression(entry.getValue(), symbolMap)); } }
correctSymbolMap(symbolMap, node); for (Map.Entry<ElementSymbol, Expression> ref : refs.asUpdatableMap().entrySet()) { Expression expr = ref.getValue(); Expression convertedExpr = convertExpression(expr, symbolMap); ref.setValue(convertedExpr); correlatedExpression.add(convertedExpr); crit = convertCriteria(crit, symbolMap, metadata, rewrite); node.setProperty(NodeConstants.Info.SELECT_CRITERIA, crit); if (rewrite) { for (LanguageObject expr : select.getSymbols()) { rewriteSingleElementSymbol(metadata, (Expression) expr); if(joinCrits != null && !joinCrits.isEmpty()) { Criteria crit = new CompoundCriteria(joinCrits); crit = convertCriteria(crit, symbolMap, metadata, rewrite); if (crit instanceof CompoundCriteria && ((CompoundCriteria)crit).getOperator() == CompoundCriteria.AND) { node.setProperty(NodeConstants.Info.JOIN_CRITERIA, ((CompoundCriteria)crit).getCriteria()); convertAccessPatterns(symbolMap, node); if (rewrite) { for (OrderByItem item : orderBy.getOrderByItems()) { rewriteSingleElementSymbol(metadata, item.getSymbol()); convertAccessPatterns(symbolMap, node);
if (!FrameUtil.canConvertAccessPatterns(frame)) { return root; if (FrameUtil.isProcedure(projectNode)) { return root; PlanNode left = FrameUtil.findJoinSourceNode(parentJoin.getFirstChild()); boolean isLeft = left == frame; JoinType jt = (JoinType) parentJoin.getProperty(Info.JOIN_TYPE); return root; //cannot remove if the no source side is an outer side, or if it can change the meaning of the plan PlanNode other = isLeft? FrameUtil.findJoinSourceNode(parentJoin.getLastChild()):left; if (other != null) { groups = FrameUtil.findJoinSourceNode(projectNode).getGroups(); } else if (references != null) { FrameUtil.convertFrame(frame, virtualGroup, groups, symbolMap.asMap(), metadata);
if (FrameUtil.getNonQueryCommand(sourceNode.getParent()) != null) { return; Map<ElementSymbol, Expression> replacementSymbols = FrameUtil.buildSymbolMap(group, newGroup, metadata); FrameUtil.convertFrame(sourceNode, group, new HashSet<GroupSymbol>(Arrays.asList(newGroup)), replacementSymbols, metadata);
private void addView(QueryMetadataInterface metadata, CommandContext context, GroupSymbol group, List<ElementSymbol> cols, PlanNode viewRoot) throws TeiidComponentException, QueryMetadataException, QueryPlannerException { GroupSymbol securityVeiw = new GroupSymbol("sec"); //$NON-NLS-1$ Set<String> groups = context.getGroups(); securityVeiw = RulePlaceAccess.recontextSymbol(securityVeiw, groups); List<ElementSymbol> newCols = RulePushAggregates.defineNewGroup(securityVeiw, cols, metadata); PlanNode newSourceNode = RuleDecomposeJoin.createSource(securityVeiw, viewRoot, newCols); Map<ElementSymbol, Expression> upperMapping = SymbolMap.createSymbolMap(cols, newCols).asMap(); FrameUtil.convertFrame(newSourceNode.getParent(), group, Collections.singleton(securityVeiw), upperMapping, metadata); }
while (node != endNode) { if (node.getType() == NodeConstants.Types.JOIN) { node.getGroups().removeAll(FrameUtil.findJoinSourceNode(stageGroup.getFirstChild()).getGroups()); node.getGroups().add(newGroup); FrameUtil.convertNode(node, null, null, reverseMapping, metadata, false); if (node.getType() == NodeConstants.Types.JOIN) {
Map<ElementSymbol, Expression> nullSymbolMap = FrameUtil.buildSymbolMap(group, null, metadata); FrameUtil.convertFrame(frameStart, group, null, nullSymbolMap, metadata);
FrameUtil.correctSymbolMap(aggMap, groupNode); } else { RuleDecomposeJoin.createSource(oldGroup, projectNode, correctedMap); } else { FrameUtil.convertFrame(projectNode, oldGroup, null, correctedMap.asMap(), metadata);
Boolean pushAcrossFrame(PlanNode sourceNode, PlanNode critNode, QueryMetadataInterface metadata, CapabilitiesFinder capFinder, CommandContext context) throws QueryPlannerException, QueryMetadataException, TeiidComponentException { //ensure that the criteria can be pushed further if (sourceNode.getChildCount() == 1 && FrameUtil.isOrderedOrStrictLimit(sourceNode.getFirstChild())) { return false; } //check to see if this is a move across a union if (sourceNode.getChildCount() > 0) { PlanNode child = sourceNode.getFirstChild(); child = FrameUtil.findOriginatingNode(child, child.getGroups()); if (child != null && child.getType() == NodeConstants.Types.SET_OP) { //only allow criteria without subqueires - node cloning doesn't allow for the proper creation of //multiple nodes with the same subqueries if (child == sourceNode.getFirstChild() && critNode.getSubqueryContainers().isEmpty()) { return pushAcrossSetOp(critNode, child, metadata, capFinder, context); } //this could be an access node in the middle of the source and set op, //it is an odd case that is not supported for now return false; } } // See if we can move it towards the SOURCE node return moveNodeAcrossFrame(critNode, sourceNode, metadata); }
convertNode(current, oldGroup, newGroups, symbolMap, metadata, rewrite); return; correctSymbolMap(symbolMap, endNode);
tgtCrit = FrameUtil.convertCriteria(tgtCrit, tgtMap, metadata, true); } catch (QueryPlannerException err) { LogManager.logDetail(LogConstants.CTX_QUERY_PLANNER, err, "Could not remap target criteria in RuleCopyCriteria"); //$NON-NLS-1$
FrameUtil.convertNode(node, null, null, mapping, metadata, false); node = node.getParent();
private void processWith(final QueryCommand command, List<WithQueryCommand> withList) throws QueryMetadataException, TeiidComponentException { for (int i = 0; i < withList.size(); i++) { WithQueryCommand with = withList.get(i); //check for a duplicate with clause, which can occur in a self-join scenario WithQueryCommand existing = this.withPlanningState.withList.get(with.getCommand()); if (existing != null) { final GroupSymbol old = with.getGroupSymbol(); replaceSymbol(command, old, existing.getGroupSymbol()); continue; } final GroupSymbol old = with.getGroupSymbol(); if (!context.getGroups().add(old.getName()) || old.getName().matches("(g|v)_\\d*")) { final GroupSymbol gs = RulePlaceAccess.recontextSymbol(old, context.getGroups()); LinkedHashMap<ElementSymbol, Expression> replacementSymbols = FrameUtil.buildSymbolMap(old, gs, metadata); gs.setDefinition(null); //update the with clause with the new group name / columns with.setGroupSymbol(gs); with.setColumns(new ArrayList(replacementSymbols.values())); //we use equality checks here because there may be a similarly named at lower scopes replaceSymbol(command, old, gs); } this.context.getAliasMapping().put(with.getGroupSymbol().getName(), old.getName()); this.withPlanningState.withList.put(with.getCommand(), with); } }
FrameUtil.correctSymbolMap(((SymbolMap)frame.getProperty(NodeConstants.Info.SYMBOL_MAP)).asMap(), parentSource);
correctSymbolMap(symbolMap, node); for (Map.Entry<ElementSymbol, Expression> ref : refs.asUpdatableMap().entrySet()) { Expression expr = ref.getValue(); Expression convertedExpr = convertExpression(expr, symbolMap); ref.setValue(convertedExpr); correlatedExpression.add(convertedExpr); crit = convertCriteria(crit, symbolMap, metadata, rewrite); node.setProperty(NodeConstants.Info.SELECT_CRITERIA, crit); if (rewrite) { for (LanguageObject expr : select.getSymbols()) { rewriteSingleElementSymbol(metadata, (Expression) expr); if(joinCrits != null && !joinCrits.isEmpty()) { Criteria crit = new CompoundCriteria(joinCrits); crit = convertCriteria(crit, symbolMap, metadata, rewrite); if (crit instanceof CompoundCriteria && ((CompoundCriteria)crit).getOperator() == CompoundCriteria.AND) { node.setProperty(NodeConstants.Info.JOIN_CRITERIA, ((CompoundCriteria)crit).getCriteria()); convertAccessPatterns(symbolMap, node); if (rewrite) { for (OrderByItem item : orderBy.getOrderByItems()) { rewriteSingleElementSymbol(metadata, item.getSymbol()); convertAccessPatterns(symbolMap, node);
if (!FrameUtil.canConvertAccessPatterns(frame)) { return root; if (FrameUtil.isProcedure(projectNode)) { return root; if (FrameUtil.findJoinSourceNode(joinToTest.getFirstChild()).getGroups().contains(virtualGroup)) { groups = FrameUtil.findJoinSourceNode(projectNode).getGroups(); } else if (references != null) { FrameUtil.convertFrame(frame, virtualGroup, groups, symbolMap.asMap(), metadata);
if (FrameUtil.getNonQueryCommand(sourceNode.getParent()) != null) { return; Map<ElementSymbol, Expression> replacementSymbols = FrameUtil.buildSymbolMap(group, newGroup, metadata); FrameUtil.convertFrame(sourceNode, group, new HashSet<GroupSymbol>(Arrays.asList(newGroup)), replacementSymbols, metadata);
private void addView(QueryMetadataInterface metadata, CommandContext context, GroupSymbol group, List<ElementSymbol> cols, PlanNode viewRoot) throws TeiidComponentException, QueryMetadataException, QueryPlannerException { GroupSymbol securityVeiw = new GroupSymbol("sec"); //$NON-NLS-1$ Set<String> groups = context.getGroups(); securityVeiw = RulePlaceAccess.recontextSymbol(securityVeiw, groups); List<ElementSymbol> newCols = RulePushAggregates.defineNewGroup(securityVeiw, cols, metadata); PlanNode newSourceNode = RuleDecomposeJoin.createSource(securityVeiw, viewRoot, newCols); Map<ElementSymbol, Expression> upperMapping = SymbolMap.createSymbolMap(cols, newCols).asMap(); FrameUtil.convertFrame(newSourceNode.getParent(), group, Collections.singleton(securityVeiw), upperMapping, metadata); }