protected TableGroupJoin findParentJoin(TableSource childTable,
ConditionList conditions,
EquivalenceFinder<ColumnExpression> columnEquivs) {
if ((conditions == null) || conditions.isEmpty()) return null;
TableNode childNode = childTable.getTable();
Join groupJoin = childNode.getTable().getParentJoin();
if (groupJoin == null) return null;
TableNode parentNode = childNode.getTree().getNode(groupJoin.getParent());
if (parentNode == null) return null;
List<JoinColumn> joinColumns = groupJoin.getJoinColumns();
int ncols = joinColumns.size();
Map<TableSource,GroupJoinConditions> parentTables = new HashMap<>();
for (int i = 0; i < ncols; ++i) {
if (!findGroupCondition(joinColumns, i, childTable, conditions, true, parentTables, columnEquivs)) {
if (!findGroupCondition(joinColumns, i, childTable, conditions, false, parentTables, columnEquivs)) {
return null; // join column had no direct or equivalent group joins, so we know the answer
}
}
}
TableSource parentTable = null;
GroupJoinConditions groupJoinConditions = null;
for (Map.Entry<TableSource,GroupJoinConditions> entry : parentTables.entrySet()) {
boolean found = true;
for (ComparisonCondition elem : entry.getValue().getConditions()) {
if (elem == null) {
found = false;
break;
}
}
if (found) {
if (parentTable == null) {
parentTable = entry.getKey();
groupJoinConditions = entry.getValue();
}
else {
// TODO: What we need is something
// earlier to decide that the primary
// keys are equated and so share the
// references somehow.
ConditionExpression c1 = groupJoinConditions.getConditions().get(0);
ConditionExpression c2 = entry.getValue().getConditions().get(0);
if (conditions.indexOf(c1) > conditions.indexOf(c2)) {
// Make the order predictable for tests.
ConditionExpression temp = c1;
c1 = c2;
c2 = temp;
}
throw new UnsupportedSQLException("Found two possible parent joins",
c2.getSQLsource());
}
}
}
if (parentTable == null) return null;
TableGroup group = parentTable.getGroup();
if (group == null) {
group = childTable.getGroup();
if (group == null)
group = new TableGroup(groupJoin.getGroup());
}
else if (childTable.getGroup() != null) {
group.merge(childTable.getGroup());
}
if (!tableAllowedInGroup(group, childTable))