Add optional 4th parameter to cross() function

The cross function now accepts a 4th parameter defining a regular
expression separator for splitting multi-value field values when joining
projects.

See https://github.com/OpenRefine/OpenRefine/issues/1204#issuecomment-326320954
This commit is contained in:
Ralf Claussnitzer 2017-10-26 13:58:06 +02:00
parent 9aa168633f
commit 0b107ec5e9
3 changed files with 56 additions and 31 deletions

View File

@ -69,7 +69,7 @@ public class InterProjectModel {
this.toProjectColumnName = toProjectColumnName; this.toProjectColumnName = toProjectColumnName;
} }
public HasFieldsListImpl getRows(final Object rowKey) { public HasFieldsListImpl getRows(final Object rowKey, String separatorRegexp) {
Project toProject = ProjectManager.singleton.getProject(toProjectID); Project toProject = ProjectManager.singleton.getProject(toProjectID);
if (toProject == null) { if (toProject == null) {
return null; return null;
@ -79,8 +79,8 @@ public class InterProjectModel {
if (ExpressionUtils.isNonBlankData(rowKey)) { if (ExpressionUtils.isNonBlankData(rowKey)) {
Object[] rowKeys; Object[] rowKeys;
if (rowKey instanceof String) { if (separatorRegexp != null && !separatorRegexp.isEmpty() && rowKey instanceof String) {
rowKeys = ((String) rowKey).split(","); rowKeys = ((String) rowKey).split(separatorRegexp);
} else { } else {
rowKeys = new Object[]{rowKey}; rowKeys = new Object[]{rowKey};
} }
@ -111,9 +111,10 @@ public class InterProjectModel {
* @param fromColumn * @param fromColumn
* @param toProject * @param toProject
* @param toColumn * @param toColumn
* @param separatorRegexp
* @return * @return
*/ */
public ProjectJoin getJoin(String fromProject, String fromColumn, String toProject, String toColumn) { public ProjectJoin getJoin(String fromProject, String fromColumn, String toProject, String toColumn, String separatorRegexp) {
String key = fromProject + ";" + fromColumn + ";" + toProject + ";" + toColumn; String key = fromProject + ";" + fromColumn + ";" + toProject + ";" + toColumn;
if (!_joins.containsKey(key)) { if (!_joins.containsKey(key)) {
ProjectJoin join = new ProjectJoin( ProjectJoin join = new ProjectJoin(
@ -123,7 +124,7 @@ public class InterProjectModel {
toColumn toColumn
); );
computeJoin(join); computeJoin(join, separatorRegexp);
synchronized (_joins) { synchronized (_joins) {
_joins.put(key, join); _joins.put(key, join);
@ -158,7 +159,7 @@ public class InterProjectModel {
} }
} }
protected void computeJoin(ProjectJoin join) { protected void computeJoin(ProjectJoin join, String separatorRegexp) {
if (join.fromProjectID < 0 || join.toProjectID < 0) { if (join.fromProjectID < 0 || join.toProjectID < 0) {
return; return;
} }
@ -179,8 +180,8 @@ public class InterProjectModel {
Object fromRowKey = fromRow.getCellValue(fromColumn.getCellIndex()); Object fromRowKey = fromRow.getCellValue(fromColumn.getCellIndex());
if (ExpressionUtils.isNonBlankData(fromRowKey)) { if (ExpressionUtils.isNonBlankData(fromRowKey)) {
Object[] fromRowKeys; Object[] fromRowKeys;
if (fromRowKey instanceof String) { if (separatorRegexp != null && !separatorRegexp.isEmpty() && fromRowKey instanceof String) {
fromRowKeys = ((String) fromRowKey).split(","); fromRowKeys = ((String) fromRowKey).split(separatorRegexp);
} else { } else {
fromRowKeys = new Object[]{fromRowKey}; fromRowKeys = new Object[]{fromRowKey};
} }

View File

@ -48,13 +48,18 @@ import com.google.refine.model.Project;
public class Cross implements Function { public class Cross implements Function {
public static final String EVAL_ERROR_MESSAGE =
" expects a string or cell, a project name to join with, and a column name in that project. " +
"Optional accepts a regular expression separator for source values.";
@Override @Override
public Object call(Properties bindings, Object[] args) { public Object call(Properties bindings, Object[] args) {
if (args.length == 3) { if (args.length >= 3) {
// 1st argument can take either value or cell(for backward compatibility) // 1st argument can take either value or cell(for backward compatibility)
Object v = args[0]; Object v = args[0];
Object toProjectName = args[1]; Object toProjectName = args[1];
Object toColumnName = args[2]; Object toColumnName = args[2];
String separatorRegexp = (args.length > 3) ? String.valueOf(args[3]) : null;
if (v != null && if (v != null &&
( v instanceof String || v instanceof WrappedCell ) && ( v instanceof String || v instanceof WrappedCell ) &&
@ -65,15 +70,16 @@ public class Cross implements Function {
ProjectManager.singleton.getProjectMetadata(((Project) bindings.get("project")).id).getName(), ProjectManager.singleton.getProjectMetadata(((Project) bindings.get("project")).id).getName(),
(String) bindings.get("columnName"), (String) bindings.get("columnName"),
(String) toProjectName, (String) toProjectName,
(String) toColumnName (String) toColumnName,
); separatorRegexp
);
String srcValue = v instanceof String ? (String)v : (String)((WrappedCell) v).cell.value; String srcValue = v instanceof String ? (String)v : (String)((WrappedCell) v).cell.value;
return join.getRows(srcValue); return join.getRows(srcValue, separatorRegexp);
} }
} }
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string or cell, a project name to join with, and a column name in that project"); return new EvalError(ControlFunctionRegistry.getFunctionName(this) + EVAL_ERROR_MESSAGE);
} }
@Override @Override
@ -82,7 +88,7 @@ public class Cross implements Function {
writer.object(); writer.object();
writer.key("description"); writer.value("join with another project by column"); writer.key("description"); writer.value("join with another project by column");
writer.key("params"); writer.value("cell c or string value, string projectName, string columnName"); writer.key("params"); writer.value("cell c or string value, string projectName, string columnName(, string separatorRegexp)");
writer.key("returns"); writer.value("array"); writer.key("returns"); writer.value("array");
writer.endObject(); writer.endObject();
} }

View File

@ -9,6 +9,7 @@ import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import com.google.refine.expr.functions.Cross;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
@ -163,15 +164,32 @@ public class CrossFunctionTests extends RefineTest {
* rest of cells shows "Error: cross expects a string or cell, a project name to join with, and a column name in that project" * rest of cells shows "Error: cross expects a string or cell, a project name to join with, and a column name in that project"
*/ */
@Test @Test
public void crossFunctionNonLiteralValue() throws Exception { public void crossFunctionIntegerValue() throws Exception {
Assert.assertEquals(((EvalError) invoke("cross", 1, "My Address Book", "friend")).message, String message = ((EvalError) invoke("cross", 1, "My Address Book", "friend")).message;
"cross expects a string or cell, a project name to join with, and a column name in that project"); Assert.assertTrue(message.contains(Cross.EVAL_ERROR_MESSAGE),
String.format("Message should contain `%s` but is `%s`", Cross.EVAL_ERROR_MESSAGE, message));
}
Assert.assertEquals(((EvalError) invoke("cross", null, "My Address Book", "friend")).message, /**
"cross expects a string or cell, a project name to join with, and a column name in that project"); * rest of cells shows "Error: cross expects a string or cell, a project name to join with, and a column name in
* that project"
*/
@Test
public void crossFunctionNull() throws Exception {
String message = ((EvalError) invoke("cross", null, "My Address Book", "friend")).message;
Assert.assertTrue(message.contains(Cross.EVAL_ERROR_MESSAGE),
String.format("Message should contain `%s` but is `%s`", Cross.EVAL_ERROR_MESSAGE, message));
}
Assert.assertEquals(((EvalError) invoke("cross", Calendar.getInstance(), "My Address Book", "friend")).message, /**
"cross expects a string or cell, a project name to join with, and a column name in that project"); * rest of cells shows "Error: cross expects a string or cell, a project name to join with, and a column name in
* that project"
*/
@Test
public void crossFunctionCalendarInstance() throws Exception {
String message = ((EvalError) invoke("cross", Calendar.getInstance(), "My Address Book", "friend")).message;
Assert.assertTrue(message.contains(Cross.EVAL_ERROR_MESSAGE),
String.format("Message should contain `%s` but is `%s`", Cross.EVAL_ERROR_MESSAGE, message));
} }
/** /**