Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nonstrict join between tow dataframes #87

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions src/main/java/joinery/DataFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,31 @@ public final DataFrame<V> join(final DataFrame<V> other) {
return join(other, JoinType.LEFT, null);
}

/**
* Return a new data frame created by performing a left outer join
* of this data frame with the argument and using the row indices
* as the join key.
*
* <pre> {@code
* > DataFrame<Object> left = new DataFrame<>("a", "b");
* > left.append("one", Arrays.asList(1, 2));
* > left.append("two", Arrays.asList(3, 4));
* > left.append("three", Arrays.asList(5, 6));
* > DataFrame<Object> right = new DataFrame<>("c", "d");
* > right.append("one", Arrays.asList(10, 20));
* > right.append("two", Arrays.asList(30, 40));
* > right.append("four", Arrays.asList(50, 60));
* > left.join(right)
* > .index();
* [one, two, three] }</pre>
*
* @param other the other data frame
* @return the result of the join operation as a new data frame
*/
public final DataFrame<V> nonStrictJoin(final DataFrame<V> other) {
return nonStrictJoin(other, JoinType.LEFT, null);
}

/**
* Return a new data frame created by performing a join of this
* data frame with the argument using the specified join type and
Expand All @@ -678,6 +703,19 @@ public final DataFrame<V> join(final DataFrame<V> other, final JoinType join) {
return join(other, join, null);
}

/**
* Return a new data frame created by performing a join of this
* data frame with the argument using the specified join type and
* using the row indices as the join key.
*
* @param other the other data frame
* @param join the join type
* @return the result of the join operation as a new data frame
*/
public final DataFrame<V> nonStrictJoin(final DataFrame<V> other, final JoinType join) {
return nonStrictJoin(other, join, null);
}

/**
* Return a new data frame created by performing a left outer join of this
* data frame with the argument using the specified key function.
Expand All @@ -690,6 +728,18 @@ public final DataFrame<V> join(final DataFrame<V> other, final KeyFunction<V> on
return join(other, JoinType.LEFT, on);
}

/**
* Return a new data frame created by performing a left outer join of this
* data frame with the argument using the specified key function.
*
* @param other the other data frame
* @param on the function to generate the join keys
* @return the result of the join operation as a new data frame
*/
public final DataFrame<V> nonStrictJoin(final DataFrame<V> other, final KeyFunction<V> on) {
return nonStrictJoin(other, JoinType.LEFT, on);
}

/**
* Return a new data frame created by performing a join of this
* data frame with the argument using the specified join type and
Expand All @@ -704,6 +754,20 @@ public final DataFrame<V> join(final DataFrame<V> other, final JoinType join, fi
return Combining.join(this, other, join, on);
}

/**
* Return a new data frame created by performing a join of this
* data frame with the argument using the specified join type and
* the specified key function.
*
* @param other the other data frame
* @param join the join type
* @param on the function to generate the join keys
* @return the result of the join operation as a new data frame
*/
public final DataFrame<V> nonStrictJoin(final DataFrame<V> other, final JoinType join, final KeyFunction<V> on) {
return Combining.nonStrictJoin(this, other, join, on);
}

/**
* Return a new data frame created by performing a left outer join of
* this data frame with the argument using the column values as the join key.
Expand All @@ -716,6 +780,18 @@ public final DataFrame<V> joinOn(final DataFrame<V> other, final Integer ... col
return joinOn(other, JoinType.LEFT, cols);
}

/**
* Return a new data frame created by performing a left outer join of
* this data frame with the argument using the column values as the join key.
*
* @param other the other data frame
* @param cols the indices of the columns to use as the join key
* @return the result of the join operation as a new data frame
*/
public final DataFrame<V> nonStrictJoinOn(final DataFrame<V> other, final Integer ... cols) {
return nonStrictJoinOn(other, JoinType.LEFT, cols);
}

/**
* Return a new data frame created by performing a join of this
* data frame with the argument using the specified join type and
Expand All @@ -730,6 +806,20 @@ public final DataFrame<V> joinOn(final DataFrame<V> other, final JoinType join,
return Combining.joinOn(this, other, join, cols);
}

/**
* Return a new data frame created by performing a join of this
* data frame with the argument using the specified join type and
* the column values as the join key.
*
* @param other the other data frame
* @param join the join type
* @param cols the indices of the columns to use as the join key
* @return the result of the join operation as a new data frame
*/
public final DataFrame<V> nonStrictJoinOn(final DataFrame<V> other, final JoinType join, final Integer ... cols) {
return Combining.nonStrictJoinOn(this, other, join, cols);
}

/**
* Return a new data frame created by performing a left outer join of
* this data frame with the argument using the column values as the join key.
Expand All @@ -742,6 +832,18 @@ public final DataFrame<V> joinOn(final DataFrame<V> other, final Object ... cols
return joinOn(other, JoinType.LEFT, cols);
}

/**
* Return a new data frame created by performing a left outer join of
* this data frame with the argument using the column values as the join key.
*
* @param other the other data frame
* @param cols the names of the columns to use as the join key
* @return the result of the join operation as a new data frame
*/
public final DataFrame<V> nonStrictJoinOn(final DataFrame<V> other, final Object ... cols) {
return nonStrictJoinOn(other, JoinType.LEFT, cols);
}

/**
* Return a new data frame created by performing a join of this
* data frame with the argument using the specified join type and
Expand All @@ -756,6 +858,20 @@ public final DataFrame<V> joinOn(final DataFrame<V> other, final JoinType join,
return joinOn(other, join, columns.indices(cols));
}

/**
* Return a new data frame created by performing a join of this
* data frame with the argument using the specified join type and
* the column values as the join key.
*
* @param other the other data frame
* @param join the join type
* @param cols the names of the columns to use as the join key
* @return the result of the join operation as a new data frame
*/
public final DataFrame<V> nonStrictJoinOn(final DataFrame<V> other, final JoinType join, final Object ... cols) {
return nonStrictJoinOn(other, join, columns.indices(cols));
}

/**
* Return a new data frame created by performing a left outer join of this
* data frame with the argument using the common, non-numeric columns
Expand Down
106 changes: 105 additions & 1 deletion src/main/java/joinery/impl/Combining.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import joinery.DataFrame.KeyFunction;

public class Combining {

public static <V> DataFrame<V> join(final DataFrame<V> left, final DataFrame<V> right, final JoinType how, final KeyFunction<V> on) {
final Iterator<Object> leftIt = left.index().iterator();
final Iterator<Object> rightIt = right.index().iterator();
Expand Down Expand Up @@ -89,7 +90,7 @@ public static <V> DataFrame<V> join(final DataFrame<V> left, final DataFrame<V>
final List<V> row = how != JoinType.RIGHT ? leftMap.get(entry.getKey()) : rightMap.get(entry.getKey());
if (row == null) {
final List<V> tmp = new ArrayList<>(Collections.<V>nCopies(
how != JoinType.RIGHT ? left.columns().size() : right.columns().size(), null));
how != JoinType.RIGHT ? left.columns().size() : right.columns().size(), null));
tmp.addAll(entry.getValue());
df.append(entry.getKey(), tmp);
}
Expand All @@ -99,6 +100,109 @@ public static <V> DataFrame<V> join(final DataFrame<V> left, final DataFrame<V>
return df;
}

public static <V> DataFrame<V> nonStrictJoin(final DataFrame<V> left, final DataFrame<V> right, final JoinType how, final KeyFunction<V> on) {
final Iterator<Object> leftIt = left.index().iterator();
final Iterator<Object> rightIt = right.index().iterator();
final Map<Object, List<List<V>>> leftMap = new LinkedHashMap<>();
final Map<Object, List<List<V>>> rightMap = new LinkedHashMap<>();
for (final List<V> row : left) {
final Object name = leftIt.next();
final Object key = on == null ? name : on.apply(row);
List<List<V>> orDefault = leftMap.getOrDefault(key, new ArrayList<>());
leftMap.put(key, orDefault);
orDefault.add(row);
}

for (final List<V> row : right) {
final Object name = rightIt.next();
final Object key = on == null ? name : on.apply(row);
List<List<V>> orDefault = rightMap.getOrDefault(key, new ArrayList<>());
rightMap.put(key, orDefault);
orDefault.add(row);
}

final List<Object> columns = new ArrayList<>(how != JoinType.RIGHT ? left.columns() : right.columns());
for (Object column : how != JoinType.RIGHT ? right.columns() : left.columns()) {
final int index = columns.indexOf(column);
if (index >= 0) {
if (column instanceof List) {
@SuppressWarnings("unchecked")
final List<Object> l1 = List.class.cast(columns.get(index));
l1.add(how != JoinType.RIGHT ? "left" : "right");
@SuppressWarnings("unchecked")
final List<Object> l2= List.class.cast(column);
l2.add(how != JoinType.RIGHT ? "right" : "left");
} else {
columns.set(index, String.format("%s_%s", columns.get(index), how != JoinType.RIGHT ? "left" : "right"));
column = String.format("%s_%s", column, how != JoinType.RIGHT ? "right" : "left");
}
}
columns.add(column);
}

final DataFrame<V> df = new DataFrame<>(columns);
int counter = 0;
for (final Map.Entry<Object, List<List<V>>> entry : how != JoinType.RIGHT ? leftMap.entrySet() : rightMap.entrySet()) {
List<List<V>> values = entry.getValue();
for(final List<V> tmp : values){
final List<List<V>> rows = how != JoinType.RIGHT ? rightMap.get(entry.getKey()) : leftMap.get(entry.getKey());
if ((rows != null && rows.size() > 0) || how != JoinType.INNER) {
if(rows == null) {
System.out.println("rows is null for " + entry.getKey());
List<V> ttmp = new ArrayList<>();
ttmp.addAll(tmp);
ttmp.addAll(Collections.<V>nCopies(right.columns().size(), null));
df.append(counter++, ttmp);
continue;
} else {
for (List<V> row : rows) {
List<V> ttmp = new ArrayList<>();
ttmp.addAll(tmp);
ttmp.addAll(row != null ? row : Collections.<V>nCopies(right.columns().size(), null));
df.append(counter++, ttmp); // nonstrict join, key can be not unique
}
}
}
}
}

if (how == JoinType.OUTER) {
for (final Map.Entry<Object, List<List<V>>> entry : how != JoinType.RIGHT ? rightMap.entrySet() : leftMap.entrySet()) {
List<List<V>> values = entry.getValue();
for (final List<V> tmp : values) {
final List<List<V>> rows = how != JoinType.RIGHT ? leftMap.get(entry.getKey()) : rightMap.get(entry.getKey());
if ((rows != null && rows.size() > 0) || how != JoinType.INNER) {
if(rows == null) {
System.out.println("rows is null for " + entry.getKey());
continue;
}
for (List<V> row : rows) {
List<V> ttmp = new ArrayList<>();
ttmp.addAll(tmp);
ttmp.addAll(row != null ? row : Collections.<V>nCopies(right.columns().size(), null));
df.append(counter++, ttmp);
}
}
}
}
}

return df;
}

public static <V> DataFrame<V> nonStrictJoinOn(final DataFrame<V> left, final DataFrame<V> right, final JoinType how, final Integer ... cols) {
return nonStrictJoin(left, right, how, new KeyFunction<V>() {
@Override
public Object apply(final List<V> value) {
final List<V> key = new ArrayList<>(cols.length);
for (final int col : cols) {
key.add(value.get(col));
}
return Collections.unmodifiableList(key);
}
});
}

public static <V> DataFrame<V> joinOn(final DataFrame<V> left, final DataFrame<V> right, final JoinType how, final Integer ... cols) {
return join(left, right, how, new KeyFunction<V>() {
@Override
Expand Down