/** * For backwards compatibility we strip the alias from delete/update * @param command * @param group */ private void removeAlias(ProcedureContainer command, GroupSymbol group) { AliasGenerator ag = new AliasGenerator(true); ag.setCorrelationGroups(Arrays.asList(group.getDefinition())); command.acceptVisitor(ag); final GroupSymbol clone = group.clone(); DeepPostOrderNavigator.doVisit(command, new LanguageVisitor() { public void visit(GroupSymbol obj) { if (obj.equals(clone) && obj.getMetadataID() == group.getMetadataID()) { obj.setName(obj.getDefinition()); obj.setDefinition(null); } } }); }
public AliasGenerator(boolean aliasGroups, boolean stripColumnAliases) { super(new NamingVisitor(aliasGroups)); this.visitor = (NamingVisitor)this.getVisitor(); this.stripColumnAliases = stripColumnAliases; }
for (int i = 0; i < selectSymbols.size(); i++) { Expression symbol = selectSymbols.get(i); visitNode(symbol); boolean needsAlias = visitor.namingContext.aliasColumns; newAlias = ((ElementSymbol)newSymbol).getShortName(); } else { needsAlias &= needsAlias(newAlias, (ElementSymbol)newSymbol);
/** * visit the query in definition order */ public void visit(Query obj) { visitNodes(obj.getWith()); if (obj.getOrderBy() != null || obj.getLimit() != null) { visitor.namingContext.aliasColumns = !stripColumnAliases; } visitNode(obj.getFrom()); if (this.aliasMapping != null) { HashSet<String> newSymbols = new HashSet<String>(); for (Map.Entry<String, String> entry : this.visitor.namingContext.groupNames.entrySet()) { if (!newSymbols.add(entry.getValue())) { throw new TeiidRuntimeException(new QueryPlannerException(QueryPlugin.Event.TEIID31126, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31126, entry.getValue()))); } } } visitNode(obj.getCriteria()); visitNode(obj.getGroupBy()); visitNode(obj.getHaving()); visitNode(obj.getSelect()); visitNode(obj.getOrderBy()); }
boolean aliasColumns = modelID != null && (CapabilitiesUtil.supports(Capability.QUERY_SELECT_EXPRESSION, modelID, metadata, capFinder) || CapabilitiesUtil.supports(Capability.QUERY_FROM_INLINE_VIEWS, modelID, metadata, capFinder)); AliasGenerator visitor = new AliasGenerator(aliasGroups, !aliasColumns); SourceHint sh = command.getSourceHint(); if (sh != null && aliasGroups) { visitor.setAliasMapping(context.getAliasMapping()); visitor.setCorrelationGroups(correleatedGroups);
/** * visit the branches other than the first with individual naming contexts * Aliases are being added in all cases, even though they may only be needed in the order by case. * Adding the same alias to all branches ensures cross db support (db2 in particular) */ public void visit(SetQuery obj) { visitor.createChildNamingContext(true); visitNode(obj.getRightQuery()); visitor.removeChildNamingContext(); visitor.namingContext.aliasColumns = true; visitNode(obj.getLeftQuery()); visitNode(obj.getOrderBy()); }
@Test public void testKeepAliases() throws Exception { String sql = "select g.intkey as a, g.stringkey as b from BQT1.SmallA g, BQT1.SmallB ORDER BY a, b"; //$NON-NLS-1$ String expected = "SELECT g.IntKey AS c_0, g.StringKey AS c_1 FROM BQT1.SmallA AS g, BQT1.SmallB AS g_1 ORDER BY c_0, c_1"; //$NON-NLS-1$ AliasGenerator av = new AliasGenerator(true, false); Map<String, String> aliasMap = new HashMap<String, String>(); aliasMap.put("g", "g"); av.setAliasMapping(aliasMap); helpTest(sql, expected, RealMetadataFactory.exampleBQTCached(), av); }
private Command helpTest(String sql, String expected, boolean aliasGroups, boolean stripColumnAliases, QueryMetadataInterface metadata) throws TeiidComponentException, TeiidProcessingException { AliasGenerator visitor = new AliasGenerator(aliasGroups, stripColumnAliases); return helpTest(sql, expected, metadata, visitor); }
@Override public void visit(UnaryFromClause obj) { GroupSymbol symbol = obj.getGroup(); if (visitor.aliasGroups) { recontextGroup(symbol, false); } else { visitor.namingContext.groupNames.put(symbol.getName(), symbol.getNonCorrelationName()); } super.visit(obj); }
boolean aliasColumns = modelID != null && (CapabilitiesUtil.supports(Capability.QUERY_SELECT_EXPRESSION, modelID, metadata, capFinder) || CapabilitiesUtil.supports(Capability.QUERY_FROM_INLINE_VIEWS, modelID, metadata, capFinder)); AliasGenerator visitor = new AliasGenerator(aliasGroups, !aliasColumns); SourceHint sh = command.getSourceHint(); if (sh != null && aliasGroups) { visitor.setAliasMapping(context.getAliasMapping()); visitor.setCorrelationGroups(correleatedGroups);
/** * visit the branches other than the first with individual naming contexts * Aliases are being added in all cases, even though they may only be needed in the order by case. * Adding the same alias to all branches ensures cross db support (db2 in particular) */ public void visit(SetQuery obj) { visitor.createChildNamingContext(true); visitNode(obj.getRightQuery()); visitor.removeChildNamingContext(); visitor.namingContext.aliasColumns = true; visitNode(obj.getLeftQuery()); visitNode(obj.getOrderBy()); }
@Test(expected=TeiidRuntimeException.class) public void testKeepAliases1() throws Exception { String sql = "select g_1.intkey as a, g_1.stringkey as b from BQT1.SmallA g_1, BQT1.SmallB ORDER BY a, b"; //$NON-NLS-1$ String expected = "SELECT g.IntKey AS c_0, g.StringKey AS c_1 FROM BQT1.SmallA AS g ORDER BY c_0, c_1"; //$NON-NLS-1$ AliasGenerator av = new AliasGenerator(true, false); Map<String, String> aliasMap = new HashMap<String, String>(); aliasMap.put("g_1", "g_1"); av.setAliasMapping(aliasMap); helpTest(sql, expected, RealMetadataFactory.exampleBQTCached(), av); }
@Test public void testNestedCommonTables() throws Exception { String sql = "WITH CTE0 (e1, e2) AS /*+ no_inline */ (SELECT e1, e2 FROM pm1.g2) SELECT g_0.e1, g_0.e2 FROM CTE0 AS g_0 WHERE g_0.e1 = (WITH CTE1 (e1) AS /*+ no_inline */ (SELECT g_0.e1 FROM pm1.g1 AS g_1) SELECT g_2.e1 FROM CTE1 AS g_2)"; String expected = "WITH CTE0 (e1, e2) AS /*+ no_inline */ (SELECT g_0.e1, g_0.e2 FROM pm1.g2 AS g_0) SELECT g_1.e1, g_1.e2 FROM CTE0 AS g_1 WHERE g_1.e1 = (WITH CTE1 (e1) AS /*+ no_inline */ (SELECT g_1.e1 FROM pm1.g1 AS g_2) SELECT g_3.e1 AS c_0 FROM CTE1 AS g_3 LIMIT 2)"; //$NON-NLS-1$ AliasGenerator av = new AliasGenerator(true, false); helpTest(sql, expected, RealMetadataFactory.example1Cached(), av); }
/** * visit the query in definition order */ public void visit(Query obj) { visitNodes(obj.getWith()); if (obj.getOrderBy() != null || obj.getLimit() != null) { visitor.namingContext.aliasColumns = !stripColumnAliases; } visitNode(obj.getFrom()); if (this.aliasMapping != null) { HashSet<String> newSymbols = new HashSet<String>(); for (Map.Entry<String, String> entry : this.visitor.namingContext.groupNames.entrySet()) { if (!newSymbols.add(entry.getValue())) { throw new TeiidRuntimeException(new QueryPlannerException(QueryPlugin.Event.TEIID31126, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31126, entry.getValue()))); } } } visitNode(obj.getCriteria()); visitNode(obj.getGroupBy()); visitNode(obj.getHaving()); visitNode(obj.getSelect()); visitNode(obj.getOrderBy()); }
@Override public void visit(UnaryFromClause obj) { GroupSymbol symbol = obj.getGroup(); if (visitor.aliasGroups) { recontextGroup(symbol, false); } else { visitor.namingContext.groupNames.put(symbol.getName(), symbol.getNonCorrelationName()); } super.visit(obj); }
boolean aliasColumns = modelID != null && (CapabilitiesUtil.supports(Capability.QUERY_SELECT_EXPRESSION, modelID, metadata, capFinder) || CapabilitiesUtil.supports(Capability.QUERY_FROM_INLINE_VIEWS, modelID, metadata, capFinder)); AliasGenerator visitor = new AliasGenerator(aliasGroups, !aliasColumns); SourceHint sh = command.getSourceHint(); if (sh != null && aliasGroups) { visitor.setAliasMapping(context.getAliasMapping()); visitor.setCorrelationGroups(correleatedGroups);
/** * visit the branches other than the first with individual naming contexts * Aliases are being added in all cases, even though they may only be needed in the order by case. * Adding the same alias to all branches ensures cross db support (db2 in particular) */ public void visit(SetQuery obj) { visitor.createChildNamingContext(true); visitNode(obj.getRightQuery()); visitor.removeChildNamingContext(); visitor.namingContext.aliasColumns = true; visitNode(obj.getLeftQuery()); visitNode(obj.getOrderBy()); }
for (int i = 0; i < selectSymbols.size(); i++) { Expression symbol = selectSymbols.get(i); visitNode(symbol); boolean needsAlias = visitor.namingContext.aliasColumns; newAlias = ((ElementSymbol)newSymbol).getShortName(); } else { needsAlias &= needsAlias(newAlias, (ElementSymbol)newSymbol);
/** * For backwards compatibility we strip the alias from delete/update * @param command * @param group */ private void removeAlias(ProcedureContainer command, GroupSymbol group) { AliasGenerator ag = new AliasGenerator(true); ag.setCorrelationGroups(Arrays.asList(group.getDefinition())); command.acceptVisitor(ag); final GroupSymbol clone = group.clone(); DeepPostOrderNavigator.doVisit(command, new LanguageVisitor() { public void visit(GroupSymbol obj) { if (obj.equals(clone) && obj.getMetadataID() == group.getMetadataID()) { obj.setName(obj.getDefinition()); obj.setDefinition(null); } } }); }
public static void checkAtomicQueries(String[] expectedAtomic, ProcessorPlan plan, QueryMetadataInterface md, CapabilitiesFinder capFinder) { Set<String> actualQueries = getAtomicQueries(plan); HashSet<String> expectedQueries = new HashSet<String>(); // Compare atomic queries for (int i = 0; i < expectedAtomic.length; i++) { final String sql = expectedAtomic[i]; Command command; try { command = helpGetCommand(sql, md); Collection<GroupSymbol> groups = GroupCollectorVisitor.getGroupsIgnoreInlineViews(command, false); final GroupSymbol symbol = groups.iterator().next(); Object modelId = md.getModelID(symbol.getMetadataID()); boolean supportsGroupAliases = CapabilitiesUtil.supportsGroupAliases(modelId, md, capFinder); boolean supportsProjection = CapabilitiesUtil.supports(Capability.QUERY_SELECT_EXPRESSION, modelId, md, capFinder); command.acceptVisitor(new AliasGenerator(supportsGroupAliases, !supportsProjection)); expectedQueries.add(command.toString()); } catch (Exception err) { throw new RuntimeException(err); } } assertEquals("Did not get expected atomic queries: ", expectedQueries, actualQueries); //$NON-NLS-1$ }