Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/main/java/com/yahoo/bullet/aggregations/GroupBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import java.util.Optional;
import java.util.Set;

import static com.yahoo.bullet.common.Utilities.extractField;

/**
* This {@link Strategy} implements a Tuple Sketch based approach to doing a group by. In particular, it
* provides a uniform sample of the groups if the number of unique groups exceed the Sketch size. Metrics like
Expand Down Expand Up @@ -104,7 +102,7 @@ private Map<String, String> getFields(BulletRecord record) {
Map<String, String> fieldValues = new HashMap<>();
for (String field : fields) {
// This explicitly does not do a TypedObject checking. Nulls (and everything else) turn into Strings
String value = Objects.toString(extractField(field, record));
String value = Objects.toString(record.extractField(field));
fieldValues.put(field, value);
}
return fieldValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.yahoo.bullet.common.Utilities.extractField;

/**
* The parent class for all {@link Strategy} that use Sketches.
*
Expand Down Expand Up @@ -99,7 +97,7 @@ public void reset() {
* @return A string representing the composite field.
*/
String composeField(BulletRecord record) {
return composeField(fields.stream().map(field -> Objects.toString(extractField(field, record))));
return composeField(fields.stream().map(field -> Objects.toString(record.extractField(field))));
}

/**
Expand Down
41 changes: 11 additions & 30 deletions src/main/java/com/yahoo/bullet/common/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,44 +94,25 @@ public static double round(double value, int places) {
}

/**
* Extracts the field from the given {@link BulletRecord}.
* Extracts this identifier as a {@link TypedObject}.
*
* @param field The field to get. It can be "." separated to look inside maps.
* @param record The record containing the field.
* @return The extracted field or null if error or not found.
*/
public static Object extractField(String field, BulletRecord record) {
if (field == null) {
return null;
}
String[] split = splitField(field);
try {
return split.length > 1 ? record.get(split[0], split[1]) : record.get(field);
} catch (ClassCastException cce) {
return null;
}
}

/**
* Extracts this field as a {@link TypedObject}.
*
* @param field The field name to extract.
* @param identifier The identifier name to extract. It can be "." separated to look inside maps and arrays.
* @param record The {@link BulletRecord} to extract it from.
* @return The created TypedObject from the value for the field in the record.
* @return The created TypedObject from the value for the identifier in the record.
*/
public static TypedObject extractTypedObject(String field, BulletRecord record) {
return new TypedObject(extractField(field, record));
public static TypedObject extractTypedObject(String identifier, BulletRecord record) {
return new TypedObject(record.extractField(identifier));
}

/**
* Extracts the field from the given (@link BulletRecord} as a {@link Number}, if possible.
* Extracts the identifier from the given (@link BulletRecord} as a {@link Number}, if possible.
*
* @param field The field containing a numeric value to get. It can be "." separated to look inside maps.
* @param record The record containing the field.
* @return The value of the field as a {@link Number} or null if it cannot be forced to one.
* @param identifier The identifier of a number to get. It can be "." separated to look inside maps and arrays.
* @param record The record containing the identifier.
* @return The value of the identifier as a {@link Number} or null if it cannot be forced to one.
*/
public static Number extractFieldAsNumber(String field, BulletRecord record) {
Object value = extractField(field, record);
public static Number extractFieldAsNumber(String identifier, BulletRecord record) {
Object value = record.extractField(identifier);
// Also checks for null
if (value instanceof Number) {
return (Number) value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static com.yahoo.bullet.common.Utilities.extractField;
import static com.yahoo.bullet.common.Utilities.extractTypedObject;
import static com.yahoo.bullet.common.Utilities.isEmpty;
import static com.yahoo.bullet.typesystem.TypedObject.IS_NOT_NULL;
Expand Down Expand Up @@ -110,7 +109,7 @@ private static TypedObject getTypedValue(BulletRecord record, Type type, Value v
Type newType = value.getType();
switch (value.getKind()) {
case FIELD:
result = newType == null ? TypedObject.typeCastFromObject(type, extractField(valueString, record))
result = newType == null ? TypedObject.typeCastFromObject(type, record.extractField(valueString))
: extractTypedObject(valueString, record).forceCast(newType);
break;
case VALUE:
Expand Down
15 changes: 0 additions & 15 deletions src/test/java/com/yahoo/bullet/common/UtilitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,6 @@ public void testRounding() {
Assert.assertEquals(String.valueOf(Utilities.round(1.4499999999999, 1)), "1.4");
}


@Test
public void testExtractField() {
BulletRecord record = RecordBox.get().add("field", "foo").add("map_field.foo", "bar")
.addMap("map_field", Pair.of("foo", "baz"))
.addList("list_field", singletonMap("foo", "baz"))
.getRecord();

Assert.assertNull(Utilities.extractField(null, record));
Assert.assertNull(Utilities.extractField("", record));
Assert.assertNull(Utilities.extractField("id", record));
Assert.assertEquals(Utilities.extractField("map_field.foo", record), "baz");
Assert.assertNull(Utilities.extractField("list_field.bar", record));
}

@Test
public void testNumericExtraction() {
BulletRecord record = RecordBox.get().add("foo", "1.20").add("bar", 42L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,9 @@ public void testSizeOf() {
Assert.assertTrue(FilterOperations.perform(RecordBox.get().add("id", "12").getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().add("id", "123").getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().getRecord().setListOfStringMap("id", new ArrayList<>()), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addList("id", singletonMap("1", 1)).getRecord(), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addList("id", singletonMap("1", 1), singletonMap("2", 2)).getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().addList("id", singletonMap("1", 1), singletonMap("2", 2), singletonMap("3", 3)).getRecord(), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addListOfMaps("id", singletonMap("1", 1)).getRecord(), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addListOfMaps("id", singletonMap("1", 1), singletonMap("2", 2)).getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().addListOfMaps("id", singletonMap("1", 1), singletonMap("2", 2), singletonMap("3", 3)).getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().getRecord().setStringMap("id", new HashMap<>()), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addMap("id", Pair.of("1", 1), Pair.of("2", 2)).getRecord(), clause));
}
Expand All @@ -381,8 +381,8 @@ public void testContainsKey() {
FilterClause clause = getFieldFilter("id", CONTAINS_KEY, "1", "2");
Assert.assertFalse(FilterOperations.perform(RecordBox.get().getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().add("id", "1").getRecord(), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addList("id", singletonMap("1", 1)).getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().addList("id", singletonMap("3", 1)).getRecord(), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addListOfMaps("id", singletonMap("1", 1)).getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().addListOfMaps("id", singletonMap("3", 1)).getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().getRecord().setListOfStringMap("id", new ArrayList<>()), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().getRecord().setStringMap("id", new HashMap<>()), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addMap("id", Pair.of("1", 1), Pair.of("2", 2)).getRecord(), clause));
Expand All @@ -395,9 +395,9 @@ public void testContainsValue() {
FilterClause clause = getFieldFilter("id", CONTAINS_VALUE, "1", "2");
Assert.assertFalse(FilterOperations.perform(RecordBox.get().getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().add("id", "1").getRecord(), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addList("id", singletonMap("1", "1")).getRecord(), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addList("id", singletonMap("1", 1)).getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().addList("id", singletonMap("3", "3")).getRecord(), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addListOfMaps("id", singletonMap("1", "1")).getRecord(), clause));
Assert.assertTrue(FilterOperations.perform(RecordBox.get().addListOfMaps("id", singletonMap("1", 1)).getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().addListOfMaps("id", singletonMap("3", "3")).getRecord(), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().getRecord().setListOfStringMap("id", new ArrayList<>()), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().getRecord().setListOfStringMap("id", singletonList(new HashMap<>())), clause));
Assert.assertFalse(FilterOperations.perform(RecordBox.get().getRecord().setStringMap("id", new HashMap<>()), clause));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testProjection() {
public void testUnsupportedProjection() {
Projection projection = makeProjection(ImmutablePair.of("list_field.1.foo", "bar"),
ImmutablePair.of("field", "foo"));
BulletRecord record = RecordBox.get().addList("list_field", emptyMap(), singletonMap("foo", "bar"))
BulletRecord record = RecordBox.get().addListOfMaps("list_field", emptyMap(), singletonMap("foo", "bar"))
.add("field", "123")
.getRecord();
BulletRecord actual = ProjectionOperations.project(record, projection, null, provider);
Expand All @@ -58,9 +58,9 @@ public void testUnsupportedProjection() {
public void testMapList() {
Projection projection = makeProjection("list_field", "foo");

BulletRecord record = RecordBox.get().addList("list_field", emptyMap(), singletonMap("foo", "baz")).getRecord();
BulletRecord record = RecordBox.get().addListOfMaps("list_field", emptyMap(), singletonMap("foo", "baz")).getRecord();

BulletRecord expected = RecordBox.get().addList("foo", emptyMap(), singletonMap("foo", "baz")).getRecord();
BulletRecord expected = RecordBox.get().addListOfMaps("foo", emptyMap(), singletonMap("foo", "baz")).getRecord();

BulletRecord actual = ProjectionOperations.project(record, projection, null, provider);
Assert.assertEquals(actual, expected);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/yahoo/bullet/result/ClipTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public void testNullValueInRecord() {
@Test
public void testRecordAddition() {
BulletRecord record = new RecordBox().add("field", "sample").addMap("map_field", Pair.of("foo", "bar"))
.addList("list_field", new HashMap<>(), singletonMap("foo", 1L))
.addListOfMaps("list_field", new HashMap<>(), singletonMap("foo", 1L))
.getRecord();
assertJSONEquals(Clip.of(record).asJSON(), makeJSON("[{'list_field':[{},{'foo':1}],'field':'sample','map_field':{'foo':'bar'}}]"));
}

@Test
public void testRecordsAddition() {
BulletRecord record = new RecordBox().add("field", "sample").addMap("map_field", Pair.of("foo", "bar"))
.addList("list_field", new HashMap<>(), singletonMap("foo", 1L))
.addListOfMaps("list_field", new HashMap<>(), singletonMap("foo", 1L))
.getRecord();

BulletRecord another = new RecordBox().add("field", "another").getRecord();
Expand Down
Loading