Skip to content

Commit

Permalink
added concatExpression and test
Browse files Browse the repository at this point in the history
  • Loading branch information
orzikhd committed Aug 29, 2017
1 parent 5732f48 commit 27364ad
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
39 changes: 39 additions & 0 deletions src/edu/washington/escience/myria/expression/ConcatExpression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package edu.washington.escience.myria.expression;

import com.google.common.base.Preconditions;

import edu.washington.escience.myria.Type;
import edu.washington.escience.myria.expression.evaluate.ExpressionOperatorParameter;

public class ConcatExpression extends BinaryExpression {
/***/
private static final long serialVersionUID = 1L;

/**
* This is not really unused, it's used automagically by Jackson deserialization.
*/
@SuppressWarnings("unused")
private ConcatExpression() {}

public ConcatExpression(final ExpressionOperator left, final ExpressionOperator right) {
super(left, right);
}

@Override
public Type getOutputType(ExpressionOperatorParameter parameters) {
Preconditions.checkArgument(
getChildren().size() == 2, "Concat function has to take 2 arguments.");
Preconditions.checkArgument(
getChildren().get(0).getOutputType(parameters) == Type.STRING_TYPE,
"The first argument of concat has to be STRING.");
Preconditions.checkArgument(
getChildren().get(1).getOutputType(parameters) == Type.STRING_TYPE,
"The second argument of concat has to be STRING.");
return Type.STRING_TYPE;
}

@Override
public String getJavaString(ExpressionOperatorParameter parameters) {
return getInfixBinaryString("+", parameters);
}
}
26 changes: 18 additions & 8 deletions test/edu/washington/escience/myria/operator/apply/ApplyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import edu.washington.escience.myria.expression.AbsExpression;
import edu.washington.escience.myria.expression.AndExpression;
import edu.washington.escience.myria.expression.CeilExpression;
import edu.washington.escience.myria.expression.ConcatExpression;
import edu.washington.escience.myria.expression.ConditionalExpression;
import edu.washington.escience.myria.expression.ConstantExpression;
import edu.washington.escience.myria.expression.CosExpression;
Expand Down Expand Up @@ -309,6 +310,12 @@ public void testApply() throws DbException {
Expressions.add(expr);
}

{
ConcatExpression concat = new ConcatExpression(vard, new ConstantExpression("t"));
Expression expr = new Expression("concat", concat);
Expressions.add(expr);
}

{
// Expression: hash(a);
HashMd5Expression hash = new HashMd5Expression(vara);
Expand Down Expand Up @@ -340,7 +347,7 @@ public void testApply() throws DbException {
while (!apply.eos()) {
result = apply.nextReady();
if (result != null) {
assertEquals(24, result.getSchema().numColumns());
assertEquals(25, result.getSchema().numColumns());
assertEquals(Type.DOUBLE_TYPE, result.getSchema().getColumnType(0));
assertEquals(Type.LONG_TYPE, result.getSchema().getColumnType(1));
assertEquals(Type.DOUBLE_TYPE, result.getSchema().getColumnType(2));
Expand All @@ -362,9 +369,10 @@ public void testApply() throws DbException {
assertEquals(Type.LONG_TYPE, result.getSchema().getColumnType(18));
assertEquals(Type.LONG_TYPE, result.getSchema().getColumnType(19));
assertEquals(Type.STRING_TYPE, result.getSchema().getColumnType(20));
assertEquals(Type.LONG_TYPE, result.getSchema().getColumnType(21));
assertEquals(Type.STRING_TYPE, result.getSchema().getColumnType(21));
assertEquals(Type.LONG_TYPE, result.getSchema().getColumnType(22));
assertEquals(Type.LONG_TYPE, result.getSchema().getColumnType(23));
assertEquals(Type.LONG_TYPE, result.getSchema().getColumnType(24));

assertEquals("sqrt", result.getSchema().getColumnName(0));
assertEquals("simpleNestedExpression", result.getSchema().getColumnName(1));
Expand All @@ -387,9 +395,10 @@ public void testApply() throws DbException {
assertEquals("max", result.getSchema().getColumnName(18));
assertEquals("min", result.getSchema().getColumnName(19));
assertEquals("substr", result.getSchema().getColumnName(20));
assertEquals("hashA", result.getSchema().getColumnName(21));
assertEquals("hashC", result.getSchema().getColumnName(22));
assertEquals("hashD", result.getSchema().getColumnName(23));
assertEquals("concat", result.getSchema().getColumnName(21));
assertEquals("hashA", result.getSchema().getColumnName(22));
assertEquals("hashC", result.getSchema().getColumnName(23));
assertEquals("hashD", result.getSchema().getColumnName(24));

for (int curI = 0; curI < result.numTuples(); curI++) {
long i = curI + resultSize;
Expand Down Expand Up @@ -428,11 +437,12 @@ public void testApply() throws DbException {
assertEquals(Math.max(a, c), result.getLong(18, curI));
assertEquals(Math.min(a, c), result.getLong(19, curI));
assertEquals(d.substring(0, 4), result.getString(20, curI));
assertEquals(Hashing.md5().hashLong(a).asLong(), result.getLong(21, curI));
assertEquals(Hashing.md5().hashInt(c).asLong(), result.getLong(22, curI));
assertEquals(d + "t", result.getString(21, curI));
assertEquals(Hashing.md5().hashLong(a).asLong(), result.getLong(22, curI));
assertEquals(Hashing.md5().hashInt(c).asLong(), result.getLong(23, curI));
assertEquals(
Hashing.md5().hashString(d, Charset.defaultCharset()).asLong(),
result.getLong(23, curI));
result.getLong(24, curI));
}
resultSize += result.numTuples();
}
Expand Down

0 comments on commit 27364ad

Please sign in to comment.