Skip to content

Commit 0466cb4

Browse files
committed
Merge branch 'master' into slxu-kill-query-at-aborted-downloading
2 parents 36c0c39 + 5e770cc commit 0466cb4

12 files changed

+140
-46
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ dependencies {
138138
compile "net.sf.supercsv:super-csv:2.2.0"
139139

140140
// Expression compiler
141-
compile "org.codehaus.janino:janino:2.7.5"
141+
compile "org.codehaus.janino:janino:2.7.6"
142142

143143
runtime "monetdb:monetdb-jdbc:2.11"
144144
runtime "mysql:mysql-connector-java:5.1.31"

src/edu/washington/escience/myria/MyriaConstants.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
/**
66
* This class holds the constants for the Myria execution.
7-
*
8-
*
7+
*
8+
*
99
*/
1010
public final class MyriaConstants {
1111
/**
@@ -270,6 +270,12 @@ public final class MyriaConstants {
270270
*/
271271
public static final int MAXIMUM_NUM_SUBQUERIES = 100;
272272

273+
/**
274+
* Default imports for janino. Modules imported here can be used in expressions.
275+
*/
276+
public static final String[] DEFAULT_JANINO_IMPORTS =
277+
{ "com.google.common.hash.Hashing", "java.nio.charset.Charset" };
278+
273279
/** Private constructor to disallow building utility class. */
274280
private MyriaConstants() {
275281
}

src/edu/washington/escience/myria/expression/ExpressionOperator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
*
2+
*
33
*/
44
package edu.washington.escience.myria.expression;
55

@@ -26,10 +26,10 @@
2626
@Type(name = "ABS", value = AbsExpression.class), @Type(name = "CAST", value = CastExpression.class),
2727
@Type(name = "CEIL", value = CeilExpression.class), @Type(name = "COS", value = CosExpression.class),
2828
@Type(name = "FLOOR", value = FloorExpression.class), @Type(name = "LEN", value = LenExpression.class),
29-
@Type(name = "LOG", value = LogExpression.class), @Type(name = "NEG", value = NegateExpression.class),
30-
@Type(name = "NOT", value = NotExpression.class), @Type(name = "SIN", value = SinExpression.class),
31-
@Type(name = "SQRT", value = SqrtExpression.class), @Type(name = "TAN", value = TanExpression.class),
32-
@Type(name = "UPPER", value = ToUpperCaseExpression.class),
29+
@Type(name = "LOG", value = LogExpression.class), @Type(name = "MD5", value = HashMd5Expression.class),
30+
@Type(name = "NEG", value = NegateExpression.class), @Type(name = "NOT", value = NotExpression.class),
31+
@Type(name = "SIN", value = SinExpression.class), @Type(name = "SQRT", value = SqrtExpression.class),
32+
@Type(name = "TAN", value = TanExpression.class), @Type(name = "UPPER", value = ToUpperCaseExpression.class),
3333
/* Binary */
3434
@Type(name = "AND", value = AndExpression.class), @Type(name = "DIVIDE", value = DivideExpression.class),
3535
@Type(name = "EQ", value = EqualsExpression.class), @Type(name = "GREATER", value = GreaterExpression.class),
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package edu.washington.escience.myria.expression;
2+
3+
import com.google.common.base.Preconditions;
4+
import com.google.common.collect.ImmutableList;
5+
6+
import edu.washington.escience.myria.Type;
7+
import edu.washington.escience.myria.expression.evaluate.ExpressionOperatorParameter;
8+
9+
/**
10+
* Return the a hash representation as long of the operand.
11+
*/
12+
public class HashMd5Expression extends UnaryExpression {
13+
/***/
14+
private static final long serialVersionUID = 1L;
15+
16+
/**
17+
* This is not really unused, it's used automagically by Jackson deserialization.
18+
*/
19+
@SuppressWarnings("unused")
20+
private HashMd5Expression() {
21+
super();
22+
}
23+
24+
/**
25+
* Compute MD5 hash code.
26+
*
27+
* @param operand the operand.
28+
*/
29+
public HashMd5Expression(final ExpressionOperator operand) {
30+
super(operand);
31+
}
32+
33+
@Override
34+
public Type getOutputType(final ExpressionOperatorParameter parameters) {
35+
Type operandType = getOperand().getOutputType(parameters);
36+
ImmutableList<Type> validTypes = ImmutableList.of(Type.STRING_TYPE, Type.LONG_TYPE, Type.INT_TYPE);
37+
int operandIdx = validTypes.indexOf(operandType);
38+
Preconditions.checkArgument(operandIdx != -1, "%s cannot handle operand [%s] of Type %s", getClass()
39+
.getSimpleName(), getOperand(), operandType);
40+
return Type.LONG_TYPE;
41+
}
42+
43+
@Override
44+
public String getJavaString(final ExpressionOperatorParameter parameters) {
45+
Type operandType = getOperand().getOutputType(parameters);
46+
if (operandType == Type.LONG_TYPE) {
47+
return getFunctionCallUnaryString("Hashing.md5().hashLong", parameters).concat(".asLong()");
48+
} else if (operandType == Type.INT_TYPE) {
49+
return getFunctionCallUnaryString("Hashing.md5().hashInt", parameters).concat(".asLong()");
50+
} else {
51+
return new StringBuilder("Hashing.md5().hashString(").append(getOperand().getJavaString(parameters)).append(
52+
", Charset.defaultCharset()).asLong()").toString();
53+
}
54+
}
55+
}

src/edu/washington/escience/myria/expression/ToUpperCaseExpression.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package edu.washington.escience.myria.expression;
22

33
import com.google.common.base.Preconditions;
4-
import com.google.common.collect.ImmutableList;
54

65
import edu.washington.escience.myria.Type;
76
import edu.washington.escience.myria.expression.evaluate.ExpressionOperatorParameter;
@@ -23,7 +22,7 @@ private ToUpperCaseExpression() {
2322

2423
/**
2524
* Change all characters in a string to upper case.
26-
*
25+
*
2726
* @param operand the operand.
2827
*/
2928
public ToUpperCaseExpression(final ExpressionOperator operand) {
@@ -33,9 +32,7 @@ public ToUpperCaseExpression(final ExpressionOperator operand) {
3332
@Override
3433
public Type getOutputType(final ExpressionOperatorParameter parameters) {
3534
Type operandType = getOperand().getOutputType(parameters);
36-
ImmutableList<Type> validTypes = ImmutableList.of(Type.STRING_TYPE);
37-
int operandIdx = validTypes.indexOf(operandType);
38-
Preconditions.checkArgument(operandIdx != -1, "%s cannot handle operand [%s] of Type %s", getClass()
35+
Preconditions.checkArgument(operandType == Type.STRING_TYPE, "%s cannot handle operand [%s] of Type %s", getClass()
3936
.getSimpleName(), getOperand(), operandType);
4037
return Type.STRING_TYPE;
4138
}

src/edu/washington/escience/myria/expression/evaluate/BooleanEvaluator.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import java.lang.reflect.InvocationTargetException;
44

55
import org.codehaus.commons.compiler.CompilerFactoryFactory;
6-
import org.codehaus.commons.compiler.IScriptEvaluator;
6+
import org.codehaus.commons.compiler.IExpressionEvaluator;
77

88
import com.google.common.base.Preconditions;
99

1010
import edu.washington.escience.myria.DbException;
11+
import edu.washington.escience.myria.MyriaConstants;
1112
import edu.washington.escience.myria.Type;
1213
import edu.washington.escience.myria.expression.Expression;
1314
import edu.washington.escience.myria.storage.TupleBatch;
@@ -23,7 +24,7 @@ public class BooleanEvaluator extends Evaluator {
2324

2425
/**
2526
* Default constructor.
26-
*
27+
*
2728
* @param expression the expression for the evaluator
2829
* @param parameters parameters that are passed to the expression
2930
*/
@@ -34,13 +35,15 @@ public BooleanEvaluator(final Expression expression, final ExpressionOperatorPar
3435

3536
/**
3637
* Compiles the {@link #javaExpression}.
37-
*
38+
*
3839
* @throws DbException compilation failed
3940
*/
4041
@Override
4142
public void compile() throws DbException {
4243
try {
43-
IScriptEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
44+
IExpressionEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
45+
46+
se.setDefaultImports(MyriaConstants.DEFAULT_JANINO_IMPORTS);
4447

4548
evaluator =
4649
(BooleanEvalInterface) se.createFastEvaluator(getJavaExpression(), BooleanEvalInterface.class, new String[] {
@@ -52,7 +55,7 @@ public void compile() throws DbException {
5255

5356
/**
5457
* Evaluates the {@link #getJavaExpression()} using the {@link #evaluator}.
55-
*
58+
*
5659
* @param tb a tuple batch
5760
* @param rowId the row that should be used for input data
5861
* @return the result from the evaluation

src/edu/washington/escience/myria/expression/evaluate/ConstEvalInterface.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/edu/washington/escience/myria/expression/evaluate/ConstantEvaluator.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.google.common.base.Preconditions;
99

1010
import edu.washington.escience.myria.DbException;
11+
import edu.washington.escience.myria.MyriaConstants;
1112
import edu.washington.escience.myria.Type;
1213
import edu.washington.escience.myria.column.Column;
1314
import edu.washington.escience.myria.column.ConstantValueColumn;
@@ -33,7 +34,7 @@ public final class ConstantEvaluator extends GenericEvaluator {
3334

3435
/**
3536
* Default constructor.
36-
*
37+
*
3738
* @param expression the expression for the evaluator
3839
* @param parameters parameters that are passed to the expression
3940
* @throws DbException if there is an error compiling the expression
@@ -50,8 +51,14 @@ public ConstantEvaluator(final Expression expression, final ExpressionOperatorPa
5051
} catch (Exception e) {
5152
throw new DbException("Error when generating Java expression " + this, e);
5253
}
54+
55+
evaluator = new ExpressionEvaluator();
56+
evaluator.setParameters(new String[] {}, new Class<?>[] {});
57+
evaluator.setDefaultImports(MyriaConstants.DEFAULT_JANINO_IMPORTS);
58+
5359
try {
54-
evaluator = new ExpressionEvaluator(java, type.toJavaType(), new String[] {}, new Class<?>[] {});
60+
evaluator.setExpressionType(type.toJavaType());
61+
evaluator.cook(java);
5562
value = evaluator.evaluate(NO_ARGS);
5663
} catch (CompileException e) {
5764
throw new DbException("Error when compiling expression " + java, e);
@@ -76,7 +83,7 @@ public void compile() {
7683

7784
/**
7885
* Evaluates the {@link #getJavaExpression()} using the {@link #evaluator}.
79-
*
86+
*
8087
* @return the result from the evaluation
8188
*/
8289
public Object eval() {

src/edu/washington/escience/myria/expression/evaluate/EvalInterface.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ public interface EvalInterface {
1111
/**
1212
* The interface for applying expressions. We only need a reference to the tuple batch and a row id. The variables
1313
* will be fetched from the tuple buffer using the rowId provided in {@link VariableExpression}.
14-
*
14+
*
1515
* @param tb a tuple batch
1616
* @param rowId the row in the tb that should be used.
1717
* @param result the result column that the value should be appended to
1818
* @param state optional state that is passed during evaluation
19-
* @return the result from the evaluation
2019
*/
21-
Object evaluate(final ReadableTable tb, final int rowId, final WritableColumn result, final ReadableTable state);
20+
void evaluate(final ReadableTable tb, final int rowId, final WritableColumn result, final ReadableTable state);
2221
}

src/edu/washington/escience/myria/expression/evaluate/GenericEvaluator.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import org.codehaus.commons.compiler.CompileException;
66
import org.codehaus.commons.compiler.CompilerFactoryFactory;
7-
import org.codehaus.commons.compiler.IScriptEvaluator;
7+
import org.codehaus.commons.compiler.IExpressionEvaluator;
88

99
import com.google.common.base.Preconditions;
1010

1111
import edu.washington.escience.myria.DbException;
12+
import edu.washington.escience.myria.MyriaConstants;
1213
import edu.washington.escience.myria.Type;
1314
import edu.washington.escience.myria.column.Column;
1415
import edu.washington.escience.myria.column.builder.ColumnBuilder;
@@ -39,7 +40,7 @@ public class GenericEvaluator extends Evaluator {
3940

4041
/**
4142
* Default constructor.
42-
*
43+
*
4344
* @param expression the expression for the evaluator
4445
* @param parameters parameters that are passed to the expression
4546
*/
@@ -49,7 +50,7 @@ public GenericEvaluator(final Expression expression, final ExpressionOperatorPar
4950

5051
/**
5152
* Compiles the {@link #javaExpression}.
52-
*
53+
*
5354
* @throws DbException compilation failed
5455
*/
5556
@Override
@@ -58,14 +59,17 @@ public void compile() throws DbException {
5859
"This expression does not need to be compiled.");
5960

6061
String javaExpression = getJavaExpression();
61-
IScriptEvaluator se;
62+
IExpressionEvaluator se;
6263
try {
6364
se = CompilerFactoryFactory.getDefaultCompilerFactory().newExpressionEvaluator();
6465
} catch (Exception e) {
6566
LOGGER.error("Could not create expression evaluator", e);
6667
throw new DbException("Could not create expression evaluator", e);
6768
}
6869

70+
se.setExpressionType(Void.TYPE);
71+
se.setDefaultImports(MyriaConstants.DEFAULT_JANINO_IMPORTS);
72+
6973
try {
7074
evaluator =
7175
(EvalInterface) se.createFastEvaluator(javaExpression, EvalInterface.class, new String[] {
@@ -79,7 +83,7 @@ public void compile() throws DbException {
7983
/**
8084
* Evaluates the {@link #getJavaExpression()} using the {@link #evaluator}. Prefer to use
8185
* {@link #evaluateColumn(TupleBatch)} as it can copy data without evaluating the expression.
82-
*
86+
*
8387
* @param tb a tuple batch
8488
* @param rowIdx the row that should be used for input data
8589
* @param result the column that the result should be appended to
@@ -109,7 +113,7 @@ public String getJavaExpression() {
109113
/**
110114
* Evaluate an expression over an entire TupleBatch and return the column of results. This method cannot take state
111115
* into consideration.
112-
*
116+
*
113117
* @param tb the tuples to be input to this expression
114118
* @return a column containing the result of evaluating this expression on the entire TupleBatch
115119
* @throws InvocationTargetException exception thrown from janino

test/edu/washington/escience/myria/operator/apply/ApplySerializationTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import edu.washington.escience.myria.expression.GreaterExpression;
2828
import edu.washington.escience.myria.expression.GreaterThanExpression;
2929
import edu.washington.escience.myria.expression.GreaterThanOrEqualsExpression;
30+
import edu.washington.escience.myria.expression.HashMd5Expression;
3031
import edu.washington.escience.myria.expression.IntDivideExpression;
3132
import edu.washington.escience.myria.expression.LenExpression;
3233
import edu.washington.escience.myria.expression.LessThanExpression;
@@ -78,6 +79,7 @@ public void testJsonMapping() throws IOException {
7879
CeilExpression ceil = new CeilExpression(constant);
7980
FloorExpression floor = new FloorExpression(constant);
8081
LogExpression log = new LogExpression(constant);
82+
HashMd5Expression md5 = new HashMd5Expression(constant);
8183
NegateExpression negate = new NegateExpression(constant);
8284
CosExpression cos = new CosExpression(constant);
8385
SinExpression sin = new SinExpression(constant);
@@ -86,8 +88,8 @@ public void testJsonMapping() throws IOException {
8688
ToUpperCaseExpression upper = new ToUpperCaseExpression(constant);
8789
NotExpression not = new NotExpression(constant);
8890
LenExpression len = new LenExpression(constant);
89-
expressions.add(abs).add(ceil).add(cos).add(floor).add(log).add(negate).add(not).add(sin).add(sqrt).add(tan).add(
90-
upper).add(len);
91+
expressions.add(abs).add(ceil).add(cos).add(floor).add(log).add(md5).add(negate).add(not).add(sin).add(sqrt).add(
92+
tan).add(upper).add(len);
9193

9294
/* Binary */
9395
DivideExpression divide = new DivideExpression(constant, variable);

0 commit comments

Comments
 (0)