private final static Pattern EQ_PATTERN = Pattern.compile("#(\\d+)=#(\\d+)");
public Relation join(Relation that, String pred, boolean conditionIsEq) {
ColumnIndexesImpl attributesOrder = new ColumnIndexesImpl(); // the columns that comprise the result of the join
ColumnIndexesImpl attributesOrderInExpanded = new ColumnIndexesImpl();
attributesOrderInExpanded.addAll(this.expandedColumns());
attributesOrderInExpanded.addAll(that.expandedColumns());
Term t1 = formula;
Term t2 = that.formula();
Term joined;
if (! conditionIsEq) {
joined = Utils.genJoin(t1, t2, pred);
attributesOrder.addAll(this.columns());
attributesOrder.addAll(that.columns());
} else {
// TODO pass index1, index2 from caller instead of reading it from the string
Matcher eqMatcher = EQ_PATTERN.matcher(pred);
checkState(eqMatcher.matches());
int index1 = Integer.parseInt(eqMatcher.group(1));
int index2 = Integer.parseInt(eqMatcher.group(2));
if (index1 > index2) {
int index;
index = index2;
index2 = index1;
index1 = index;
}
index2 = index2 - this.columns().size();
joined = Utils.eqJoin(t1, t2, index1, index2);
attributesOrder.add(this.columns().get(index1-1));
for (int i = 0; i < this.columns().size(); ++i) {
if (i + 1 != index1) {
attributesOrder.add(this.columns().get(i));
}
}
for (int i = 0; i < that.columns().size(); ++i) {
if (i + 1 != index2) {
attributesOrder.add(that.columns().get(i));
}
}
SimpleColumn replacedColumn = that.columns.get(index2-1);
SimpleColumn replacedBy = this.columns.get(index1-1);
attributesOrderInExpanded.replace(replacedColumn, replacedBy);
}
return new Relation(namesRelations, null, joined, attributesOrder, attributesOrderInExpanded);
}