Skip to content

Commit

Permalink
TEIID-2870 expanding the allow-join property
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Mar 6, 2014
1 parent 79a1b77 commit 544884d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
Expand Up @@ -766,8 +766,8 @@ static Object canRaiseOverJoin(List<PlanNode> children,
if (rightGroup == null) {
return null;
}
if (!matchesForeignKey(metadata, leftIds, rightIds, left.getGroups().iterator().next(), true)
&& !matchesForeignKey(metadata, rightIds, leftIds, rightGroup, true)) {
if (!matchesForeignKey(metadata, leftIds, rightIds, left.getGroups().iterator().next(), true, !type.isOuter() || type == JoinType.JOIN_LEFT_OUTER)
&& !matchesForeignKey(metadata, rightIds, leftIds, rightGroup, true, !type.isOuter())) {
return null;
}
}
Expand Down Expand Up @@ -888,13 +888,18 @@ static boolean isSupportedJoinCriteria(SupportedJoinCriteria sjc, Criteria crit,
}

public static boolean matchesForeignKey(QueryMetadataInterface metadata,
Collection<Object> leftIds, Collection<Object> rightIds, GroupSymbol leftGroup, boolean exact)
Collection<Object> leftIds, Collection<Object> rightIds, GroupSymbol leftGroup, boolean exact, boolean inner)
throws TeiidComponentException, QueryMetadataException {
Collection fks = metadata.getForeignKeysInGroup(leftGroup.getMetadataID());
for (Object fk : fks) {
String allow = metadata.getExtensionProperty(fk, ForeignKey.ALLOW_JOIN, false);
if (allow != null && !Boolean.valueOf(allow)) {
continue;
if (exact) {
String allow = metadata.getExtensionProperty(fk, ForeignKey.ALLOW_JOIN, false);
if (allow != null) {
boolean allowed = true;
if (!Boolean.valueOf(allow) && (!allow.equalsIgnoreCase("INNER") || !inner)) { //$NON-NLS-1$
continue;
}
}
}
List fkColumns = metadata.getElementIDsInKey(fk);
if ((exact && leftIds.size() != fkColumns.size()) || !leftIds.containsAll(fkColumns)) {
Expand Down
Expand Up @@ -1057,7 +1057,7 @@ static private HashSet<GroupSymbol> findKeyPreserved(Set<GroupSymbol> keyPreserv
if (!entry.getKey().get(left?0:1).equals(gs) || !otherGroups.contains(entry.getKey().get(left?1:0))) {
continue;
}
if (RuleRaiseAccess.matchesForeignKey(metadata, entry.getValue().get(left?0:1), entry.getValue().get(left?1:0), gs, false)) {
if (RuleRaiseAccess.matchesForeignKey(metadata, entry.getValue().get(left?0:1), entry.getValue().get(left?1:0), gs, false, true)) {
keyPreservingGroups.add(gs);
result.add(entry.getKey().get(left?1:0));
}
Expand Down
Expand Up @@ -131,6 +131,33 @@ public class TestJoinPushdownRestrictions {
new String[] {"SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM pm4.g1 AS g_0 ORDER BY c_0, c_1", "SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM pm4.g2 AS g_0 ORDER BY c_0, c_1"}, capFinder, TestOptimizer.ComparisonMode.EXACT_COMMAND_STRING); //$NON-NLS-1$
}

@Test public void testAllowJoinInner() throws Exception {
String sql = "select a.e1, b.e1 from pm4.g1 a, pm4.g2 b where a.e1 = b.e1 and a.e2 = b.e2"; //$NON-NLS-1$

FakeCapabilitiesFinder capFinder = new FakeCapabilitiesFinder();
BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
caps.setCapabilitySupport(Capability.QUERY_FROM_JOIN_INNER, true);
caps.setSourceProperty(Capability.JOIN_CRITERIA_ALLOWED, SupportedJoinCriteria.KEY);
capFinder.addCapabilities("pm4", caps); //$NON-NLS-1$

TransformationMetadata example4 = RealMetadataFactory.example4();
example4.getMetadataStore().getSchema("pm4").getTable("g2").getForeignKeys().get(0).setProperty(ForeignKey.ALLOW_JOIN, "INNER");

//should not inhibit
TestOptimizer.helpPlan(sql, example4,
new String[] {"SELECT g_0.e1, g_1.e1 FROM pm4.g1 AS g_0, pm4.g2 AS g_1 WHERE (g_0.e1 = g_1.e1) AND (g_0.e2 = g_1.e2)"}, capFinder, TestOptimizer.ComparisonMode.EXACT_COMMAND_STRING); //$NON-NLS-1$

//should inhibit
sql = "select a.e1, b.e1 from pm4.g1 a left outer join pm4.g2 b on a.e1 = b.e1 and a.e2 = b.e2"; //$NON-NLS-1$
TestOptimizer.helpPlan(sql, example4,
new String[] {"SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM pm4.g1 AS g_0 ORDER BY c_0, c_1", "SELECT g_0.e1 AS c_0, g_0.e2 AS c_1 FROM pm4.g2 AS g_0 ORDER BY c_0, c_1"}, capFinder, TestOptimizer.ComparisonMode.EXACT_COMMAND_STRING); //$NON-NLS-1$

//should not inhibit
sql = "select a.e1, b.e1 from pm4.g1 a right outer join pm4.g2 b on a.e1 = b.e1 and a.e2 = b.e2"; //$NON-NLS-1$
TestOptimizer.helpPlan(sql, example4,
new String[] {"SELECT g_1.e1, g_0.e1 FROM pm4.g2 AS g_0 LEFT OUTER JOIN pm4.g1 AS g_1 ON g_1.e1 = g_0.e1 AND g_1.e2 = g_0.e2"}, capFinder, TestOptimizer.ComparisonMode.EXACT_COMMAND_STRING); //$NON-NLS-1$
}

@Test public void testCrossJoinWithRestriction() throws Exception {
String sql = "select pm1.g1.e2, pm1.g2.e2 from pm1.g1, pm1.g2"; //$NON-NLS-1$

Expand Down

0 comments on commit 544884d

Please sign in to comment.