Skip to content

Commit

Permalink
TEIID-5283 fixing recursive with planning
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Mar 14, 2018
1 parent bd5018b commit a808ad1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
Expand Up @@ -338,14 +338,15 @@ private void planWith(PlanNode plan, Command command) throws QueryPlannerExcepti
//the strategy here is to replace the actual projections with null. this keeps
//the definition of the with clause consistent
if (!toRemove.isEmpty()) {
GroupSymbol gs = new GroupSymbol("x"); //$NON-NLS-1$
gs = RulePlaceAccess.recontextSymbol(gs, context.getGroups());
Query query = QueryRewriter.createInlineViewQuery(gs, subCommand, metadata, ResolverUtil.resolveElementsInGroup(with.getGroupSymbol(), metadata));
for (int i : toRemove) {
query.getSelect().getSymbols().set(i, new ExpressionSymbol(elements.get(i).getName(), new Constant(null, elements.get(i).getType())));
}
subCommand = query;
with.setCommand(subCommand);
if (with.isRecursive()) {
SetQuery setQuery = (SetQuery) subCommand;
setQuery.setLeftQuery(removeUnusedProjection(with, setQuery.getLeftQuery(), elements, toRemove));
setQuery.setRightQuery(removeUnusedProjection(with, setQuery.getRightQuery(), elements, toRemove));
} else {
subCommand = removeUnusedProjection(with, subCommand, elements,
toRemove);
with.setCommand(subCommand);
}
}
if (with.isRecursive()) {
SetQuery setQuery = (SetQuery) subCommand;
Expand Down Expand Up @@ -411,6 +412,35 @@ private void planWith(PlanNode plan, Command command) throws QueryPlannerExcepti
}
}

/**
* Remove unused projects by replacing with null
* @param with
* @param subCommand
* @param elements
* @param toRemove
* @return
* @throws QueryMetadataException
* @throws QueryResolverException
* @throws TeiidComponentException
*/
private Query removeUnusedProjection(WithQueryCommand with,
QueryCommand subCommand, List<TempMetadataID> elements,
List<Integer> toRemove) throws QueryMetadataException,
QueryResolverException, TeiidComponentException {
Query query = null;
if (!(subCommand instanceof Query) || subCommand.getOrderBy() != null) {
GroupSymbol gs = new GroupSymbol("x"); //$NON-NLS-1$
gs = RulePlaceAccess.recontextSymbol(gs, context.getGroups());
query = QueryRewriter.createInlineViewQuery(gs, subCommand, metadata, ResolverUtil.resolveElementsInGroup(with.getGroupSymbol(), metadata));
} else {
query = (Query)subCommand;
}
for (int i : toRemove) {
query.getSelect().getSymbols().set(i, new ExpressionSymbol(elements.get(i).getName(), new Constant(null, elements.get(i).getType())));
}
return query;
}

private boolean isPushdownValid(WithQueryCommand with, SetQuery setQuery,
Object modelID, QueryCommand withCommand, RelationalPlan subPlan1)
throws QueryMetadataException, TeiidComponentException {
Expand Down
Expand Up @@ -1239,6 +1239,36 @@ public TupleSource registerRequest(CommandContext context,
reads = bm.getReadAttempts() - reads;
assertTrue(reads < 500000);
}

@Test public void testRecursiveWithProjectionMinimization() throws Exception {
String sql = "WITH table3 (column5,column6,column7) AS (\n" +
"SELECT column1, column2, column3 FROM table1 where column1 = 'joe'\n" +
"UNION ALL \n" +
"SELECT column1, column2, column3 FROM table1 inner join table3 on table3.column6=table1.column1 \n" +
") \n" +
"SELECT column3, column4 FROM table2 WHERE column3 IN (SELECT column5 FROM table3)";

String ddl = "CREATE FOREIGN TABLE table1 (\n" +
"column1 string(4000),\n" +
"column2 string(4000),\n" +
"column3 string(4000)\n" +
") OPTIONS(NAMEINSOURCE 'table1', UPDATABLE 'TRUE');\n" +
"\n" +
"CREATE FOREIGN TABLE table2 (\n" +
"column3 string(4000),\n" +
"column4 string(4000)\n" +
") OPTIONS(NAMEINSOURCE 'table2', UPDATABLE 'TRUE');";
TransformationMetadata metadata = RealMetadataFactory.fromDDL(ddl, "x", "y");
CommandContext cc = createCommandContext();
cc.setSession(new SessionMetadata());
ProcessorPlan plan = helpGetPlan(helpParse(sql), metadata, new DefaultCapabilitiesFinder(TestOptimizer.getTypicalCapabilities()), cc);

HardcodedDataManager dataManager = new HardcodedDataManager();
dataManager.addData("SELECT g_0.column1, g_0.column2 FROM y.table1 AS g_0 WHERE g_0.column1 = 'joe'", Arrays.asList("joe", "bob"));
dataManager.addData("SELECT g_0.column1, g_0.column2 FROM y.table1 AS g_0 WHERE g_0.column1 = 'bob'");
dataManager.addData("SELECT g_0.column3 AS c_0, g_0.column4 AS c_1 FROM y.table2 AS g_0 WHERE g_0.column3 = 'joe' ORDER BY c_0", Arrays.asList("joe", "employee"));
helpProcess(plan, cc, dataManager, new List[] {Arrays.asList("joe", "employee")});
}

}

0 comments on commit a808ad1

Please sign in to comment.