Skip to content

Commit a454bdd

Browse files
authored
Add hash operation (#110)
1 parent 006573f commit a454bdd

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

src/main/java/com/yahoo/bullet/query/expressions/Operation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public enum Operation {
6565
SUBSTRING("SUBSTRING"),
6666
UNIX_TIMESTAMP("UNIXTIMESTAMP"),
6767
LOWER("LOWER"),
68-
UPPER("UPPER");
68+
UPPER("UPPER"),
69+
HASH("HASH");
6970

7071
public static final Set<Operation> BINARY_OPERATIONS =
7172
new HashSet<>(asList(ADD, SUB, MUL, DIV, MOD,
@@ -75,7 +76,7 @@ public enum Operation {
7576
LESS_THAN_OR_EQUALS, LESS_THAN_OR_EQUALS_ANY, LESS_THAN_OR_EQUALS_ALL, REGEX_LIKE, REGEX_LIKE_ANY,
7677
NOT_REGEX_LIKE, NOT_REGEX_LIKE_ANY, SIZE_IS, CONTAINS_KEY, CONTAINS_VALUE, IN, NOT_IN,
7778
AND, OR, XOR, FILTER));
78-
public static final Set<Operation> UNARY_OPERATIONS = new HashSet<>(asList(NOT, SIZE_OF, IS_NULL, IS_NOT_NULL, TRIM, ABS, LOWER, UPPER));
79+
public static final Set<Operation> UNARY_OPERATIONS = new HashSet<>(asList(NOT, SIZE_OF, IS_NULL, IS_NOT_NULL, TRIM, ABS, LOWER, UPPER, HASH));
7980
public static final Set<Operation> N_ARY_OPERATIONS = new HashSet<>(asList(AND, OR, IF, BETWEEN, NOT_BETWEEN, SUBSTRING, UNIX_TIMESTAMP));
8081

8182
private String name;

src/main/java/com/yahoo/bullet/querying/evaluators/UnaryOperations.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.Serializable;
1515
import java.util.EnumMap;
1616
import java.util.Map;
17+
import java.util.Objects;
1718
import java.util.function.Function;
1819

1920
/**
@@ -36,6 +37,7 @@ public interface UnaryOperator extends Serializable {
3637
UNARY_OPERATORS.put(Operation.ABS, UnaryOperations::abs);
3738
UNARY_OPERATORS.put(Operation.LOWER, UnaryOperations::lower);
3839
UNARY_OPERATORS.put(Operation.UPPER, UnaryOperations::upper);
40+
UNARY_OPERATORS.put(Operation.HASH, UnaryOperations::hash);
3941
}
4042

4143
static TypedObject not(Evaluator evaluator, BulletRecord record) {
@@ -61,20 +63,6 @@ static TypedObject trim(Evaluator evaluator, BulletRecord record) {
6163
});
6264
}
6365

64-
static TypedObject lower(Evaluator evaluator, BulletRecord record) {
65-
return checkNull(evaluator, record, value -> {
66-
String str = (String) value.getValue();
67-
return TypedObject.valueOf(str.toLowerCase());
68-
});
69-
}
70-
71-
static TypedObject upper(Evaluator evaluator, BulletRecord record) {
72-
return checkNull(evaluator, record, value -> {
73-
String str = (String) value.getValue();
74-
return TypedObject.valueOf(str.toUpperCase());
75-
});
76-
}
77-
7866
static TypedObject abs(Evaluator evaluator, BulletRecord record) {
7967
return checkNull(evaluator, record, value -> {
8068
Number number = (Number) value.getValue();
@@ -91,6 +79,24 @@ static TypedObject abs(Evaluator evaluator, BulletRecord record) {
9179
});
9280
}
9381

82+
static TypedObject lower(Evaluator evaluator, BulletRecord record) {
83+
return checkNull(evaluator, record, value -> {
84+
String str = (String) value.getValue();
85+
return TypedObject.valueOf(str.toLowerCase());
86+
});
87+
}
88+
89+
static TypedObject upper(Evaluator evaluator, BulletRecord record) {
90+
return checkNull(evaluator, record, value -> {
91+
String str = (String) value.getValue();
92+
return TypedObject.valueOf(str.toUpperCase());
93+
});
94+
}
95+
96+
static TypedObject hash(Evaluator evaluator, BulletRecord record) {
97+
return TypedObject.valueOf(Objects.hashCode(evaluator.evaluate(record).getValue()));
98+
}
99+
94100
private static TypedObject checkNull(Evaluator evaluator, BulletRecord record, Function<TypedObject, TypedObject> operator) {
95101
TypedObject value = evaluator.evaluate(record);
96102
if (Utilities.isNull(value)) {

src/test/java/com/yahoo/bullet/querying/evaluators/UnaryOperationsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.testng.Assert;
1111
import org.testng.annotations.Test;
1212

13+
import java.util.Objects;
14+
1315
import static com.yahoo.bullet.querying.evaluators.EvaluatorUtils.listEvaluator;
1416
import static com.yahoo.bullet.querying.evaluators.EvaluatorUtils.valueEvaluator;
1517

@@ -83,4 +85,10 @@ public void testUpper() {
8385
Assert.assertEquals(UnaryOperations.upper(valueEvaluator("hello"), null), TypedObject.valueOf("HELLO"));
8486
Assert.assertEquals(UnaryOperations.upper(valueEvaluator(null), null), TypedObject.NULL);
8587
}
88+
89+
@Test
90+
public void testHash() {
91+
Assert.assertEquals(UnaryOperations.hash(valueEvaluator(null), null), TypedObject.valueOf(Objects.hashCode(null)));
92+
Assert.assertEquals(UnaryOperations.hash(valueEvaluator("hello"), null), TypedObject.valueOf(Objects.hashCode("hello")));
93+
}
8694
}

0 commit comments

Comments
 (0)