Throw JoinException if attempt to join with non-existent column
This commit is contained in:
parent
ae5f72a8df
commit
3e01c15c37
@ -46,6 +46,7 @@ import com.google.refine.expr.WrappedRow;
|
||||
import com.google.refine.model.Column;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.model.Row;
|
||||
import com.google.refine.util.JoinException;
|
||||
|
||||
public class InterProjectModel {
|
||||
static public class ProjectJoin {
|
||||
@ -97,7 +98,7 @@ public class InterProjectModel {
|
||||
* @param toColumn
|
||||
* @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;
|
||||
if (!_joins.containsKey(key)) {
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
Project fromProject = ProjectManager.singleton.getProject(join.fromProjectID);
|
||||
ProjectMetadata fromProjectMD = ProjectManager.singleton.getProjectMetadata(join.fromProjectID);
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
Column fromColumn = fromProject.columnModel.getColumnByName(join.fromProjectColumnName);
|
||||
Column toColumn = toProject.columnModel.getColumnByName(join.toProjectColumnName);
|
||||
if (fromColumn == null || toColumn == null) {
|
||||
return;
|
||||
if (fromColumn == null) {
|
||||
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) {
|
||||
|
@ -33,7 +33,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.expr.functions;
|
||||
|
||||
import com.google.refine.util.GetProjectIDException;
|
||||
import java.util.Properties;
|
||||
|
||||
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.Function;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.util.GetProjectIDException;
|
||||
import com.google.refine.util.JoinException;
|
||||
|
||||
public class Cross implements Function {
|
||||
|
||||
@ -53,7 +54,8 @@ public class Cross implements Function {
|
||||
Object v = args[0];
|
||||
Object toProjectName = args[1];
|
||||
Object toColumnName = args[2];
|
||||
Long toProjectID;
|
||||
Long toProjectID;
|
||||
ProjectJoin join;
|
||||
|
||||
if (v != null &&
|
||||
( v instanceof String || v instanceof WrappedCell ) &&
|
||||
@ -64,17 +66,22 @@ public class Cross implements Function {
|
||||
} catch (GetProjectIDException e){
|
||||
return new EvalError(e.getMessage());
|
||||
}
|
||||
ProjectJoin join = ProjectManager.singleton.getInterProjectModel().getJoin(
|
||||
// getJoin(Long fromProject, String fromColumn, Long toProject, String toColumn) {
|
||||
// source project name
|
||||
(Long) ((Project) bindings.get("project")).id,
|
||||
// source column name
|
||||
(String) bindings.get("columnName"),
|
||||
// target project name
|
||||
toProjectID,
|
||||
// target column name
|
||||
(String) toColumnName
|
||||
);
|
||||
// add a try/catch here - error should bubble up from getInterProjectModel.computeJoin once that's modified
|
||||
try {
|
||||
join = ProjectManager.singleton.getInterProjectModel().getJoin(
|
||||
// getJoin(Long fromProject, String fromColumn, Long toProject, String toColumn) {
|
||||
// source project name
|
||||
(Long) ((Project) bindings.get("project")).id,
|
||||
// source column name
|
||||
(String) bindings.get("columnName"),
|
||||
// target project name
|
||||
toProjectID,
|
||||
// target column name
|
||||
(String) toColumnName
|
||||
);
|
||||
} catch (JoinException e) {
|
||||
return new EvalError(e.getMessage());
|
||||
}
|
||||
if(v instanceof String) {
|
||||
return join.getRows(v);
|
||||
} else {
|
||||
|
53
main/src/com/google/refine/util/JoinException.java
Normal file
53
main/src/com/google/refine/util/JoinException.java
Normal 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); }
|
||||
}
|
Loading…
Reference in New Issue
Block a user