Throw JoinException if attempt to join with non-existent column

This commit is contained in:
Owen Stephens 2019-03-23 14:15:00 +00:00
parent ae5f72a8df
commit 3e01c15c37
3 changed files with 85 additions and 17 deletions

View File

@ -46,6 +46,7 @@ import com.google.refine.expr.WrappedRow;
import com.google.refine.model.Column; import com.google.refine.model.Column;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.model.Row; import com.google.refine.model.Row;
import com.google.refine.util.JoinException;
public class InterProjectModel { public class InterProjectModel {
static public class ProjectJoin { static public class ProjectJoin {
@ -97,7 +98,7 @@ public class InterProjectModel {
* @param toColumn * @param toColumn
* @return * @return
*/ */
public ProjectJoin getJoin(Long fromProject, String fromColumn, Long toProject, String toColumn) { public ProjectJoin getJoin(Long fromProject, String fromColumn, Long toProject, String toColumn) throws JoinException {
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(
@ -142,21 +143,28 @@ public class InterProjectModel {
} }
} }
protected void computeJoin(ProjectJoin join) { protected void computeJoin(ProjectJoin join) throws JoinException {
if (join.fromProjectID < 0 || join.toProjectID < 0) { if (join.fromProjectID < 0 || join.toProjectID < 0) {
return; return;
} }
Project fromProject = ProjectManager.singleton.getProject(join.fromProjectID); Project fromProject = ProjectManager.singleton.getProject(join.fromProjectID);
ProjectMetadata fromProjectMD = ProjectManager.singleton.getProjectMetadata(join.fromProjectID);
Project toProject = ProjectManager.singleton.getProject(join.toProjectID); Project toProject = ProjectManager.singleton.getProject(join.toProjectID);
ProjectMetadata toProjectMD = ProjectManager.singleton.getProjectMetadata(join.toProjectID);
// split this test to check each one and throw an appropriate error
if (fromProject == null || toProject == null) { if (fromProject == null || toProject == null) {
return; return;
} }
Column fromColumn = fromProject.columnModel.getColumnByName(join.fromProjectColumnName); Column fromColumn = fromProject.columnModel.getColumnByName(join.fromProjectColumnName);
Column toColumn = toProject.columnModel.getColumnByName(join.toProjectColumnName); Column toColumn = toProject.columnModel.getColumnByName(join.toProjectColumnName);
if (fromColumn == null || toColumn == null) { if (fromColumn == null) {
return; throw new JoinException("Unable to find column " + join.fromProjectColumnName + " in project " + fromProjectMD.getName());
}
if (toColumn == null) {
throw new JoinException("Unable to find column " + join.toProjectColumnName + " in project " + toProjectMD.getName());
} }
for (Row fromRow : fromProject.rows) { for (Row fromRow : fromProject.rows) {

View File

@ -33,7 +33,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.expr.functions; package com.google.refine.expr.functions;
import com.google.refine.util.GetProjectIDException;
import java.util.Properties; import java.util.Properties;
import com.google.refine.InterProjectModel.ProjectJoin; import com.google.refine.InterProjectModel.ProjectJoin;
@ -43,6 +42,8 @@ import com.google.refine.expr.WrappedCell;
import com.google.refine.grel.ControlFunctionRegistry; import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function; import com.google.refine.grel.Function;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.util.GetProjectIDException;
import com.google.refine.util.JoinException;
public class Cross implements Function { public class Cross implements Function {
@ -53,7 +54,8 @@ public class Cross implements Function {
Object v = args[0]; Object v = args[0];
Object toProjectName = args[1]; Object toProjectName = args[1];
Object toColumnName = args[2]; Object toColumnName = args[2];
Long toProjectID; Long toProjectID;
ProjectJoin join;
if (v != null && if (v != null &&
( v instanceof String || v instanceof WrappedCell ) && ( v instanceof String || v instanceof WrappedCell ) &&
@ -64,17 +66,22 @@ public class Cross implements Function {
} catch (GetProjectIDException e){ } catch (GetProjectIDException e){
return new EvalError(e.getMessage()); return new EvalError(e.getMessage());
} }
ProjectJoin join = ProjectManager.singleton.getInterProjectModel().getJoin( // add a try/catch here - error should bubble up from getInterProjectModel.computeJoin once that's modified
// getJoin(Long fromProject, String fromColumn, Long toProject, String toColumn) { try {
// source project name join = ProjectManager.singleton.getInterProjectModel().getJoin(
(Long) ((Project) bindings.get("project")).id, // getJoin(Long fromProject, String fromColumn, Long toProject, String toColumn) {
// source column name // source project name
(String) bindings.get("columnName"), (Long) ((Project) bindings.get("project")).id,
// target project name // source column name
toProjectID, (String) bindings.get("columnName"),
// target column name // target project name
(String) toColumnName toProjectID,
); // target column name
(String) toColumnName
);
} catch (JoinException e) {
return new EvalError(e.getMessage());
}
if(v instanceof String) { if(v instanceof String) {
return join.getRows(v); return join.getRows(v);
} else { } else {

View File

@ -0,0 +1,53 @@
/*
Copyright 2019, Owen Stephens
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* The name of the contributor may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.refine.util;
/**
* Thrown when there is an error on a project Join.
*/
public class JoinException extends Exception {
private static final long serialVersionUID = 1553348086L;
/**
* Default JoinException format exception.
*/
public JoinException() { super(); }
/**
* JoinException exception.
*
* @param message error message
*/
public JoinException(String message) { super(message); }
}