aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo
diff options
context:
space:
mode:
authorGeir Storli <geirst@yahooinc.com>2023-08-25 10:07:35 +0000
committerGeir Storli <geirst@yahooinc.com>2023-08-25 10:07:35 +0000
commit6fd175bcb3e1b61323214380d6f23324a3056043 (patch)
tree6644e278e75daff81491a265ced5ccdc3cd4413a /document/src/test/java/com/yahoo
parent39420e6f2331825568605cfeb2975844de99de3a (diff)
Add "create non-existing cells" flag to TensorModifyUpdate.
When this is true, non-existing cells in the input tensor is created before applying the modify update. The default cell value is 0.0 for REPLACE and ADD operations, and 1.0 for MULTIPLY operation.
Diffstat (limited to 'document/src/test/java/com/yahoo')
-rw-r--r--document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java5
-rw-r--r--document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java31
-rw-r--r--document/src/test/java/com/yahoo/document/update/TensorModifyUpdateTest.java31
3 files changed, 48 insertions, 19 deletions
diff --git a/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java b/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java
index 9733cd41a88..9d4d1e8f3aa 100644
--- a/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java
+++ b/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java
@@ -822,7 +822,10 @@ public class DocumentUpdateTestCase {
result.addFieldUpdate(FieldUpdate.create(getField("dense_tensor"))
.addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.REPLACE, createTensor()))
.addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.ADD, createTensor()))
- .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.MULTIPLY, createTensor())));
+ .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.MULTIPLY, createTensor()))
+ .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.REPLACE, createTensor(), true))
+ .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.ADD, createTensor(), true))
+ .addValueUpdate(new TensorModifyUpdate(TensorModifyUpdate.Operation.MULTIPLY, createTensor(), true)));
return result;
}
diff --git a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java
index 96b5d2c1fb5..4140a9eee02 100644
--- a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java
+++ b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java
@@ -1681,6 +1681,26 @@ public class JsonReaderTestCase {
}
@Test
+ public void tensor_modify_update_with_create_non_existing_cells_true() {
+ assertTensorModifyUpdate("{{x:a,y:b}:2.0}", TensorModifyUpdate.Operation.ADD, true, "sparse_tensor",
+ inputJson("{",
+ " 'operation': 'add',",
+ " 'create': true,",
+ " 'cells': [",
+ " { 'address': { 'x': 'a', 'y': 'b' }, 'value': 2.0 } ]}"));
+ }
+
+ @Test
+ public void tensor_modify_update_with_create_non_existing_cells_false() {
+ assertTensorModifyUpdate("{{x:a,y:b}:2.0}", TensorModifyUpdate.Operation.ADD, false, "sparse_tensor",
+ inputJson("{",
+ " 'operation': 'add',",
+ " 'create': false,",
+ " 'cells': [",
+ " { 'address': { 'x': 'a', 'y': 'b' }, 'value': 2.0 } ]}"));
+ }
+
+ @Test
public void tensor_modify_update_treats_the_input_tensor_as_sparse() {
// Note that the type of the tensor in the modify update is sparse (it only has mapped dimensions).
assertTensorModifyUpdate("tensor(x{},y{}):{{x:0,y:0}:2.0, {x:1,y:2}:3.0}",
@@ -2155,16 +2175,25 @@ public class JsonReaderTestCase {
private void assertTensorModifyUpdate(String expectedTensor, TensorModifyUpdate.Operation expectedOperation,
String tensorFieldName, String modifyJson) {
- assertTensorModifyUpdate(expectedTensor, expectedOperation, tensorFieldName,
+ assertTensorModifyUpdate(expectedTensor, expectedOperation, false, tensorFieldName,
+ createTensorModifyUpdate(modifyJson, tensorFieldName));
+ }
+
+ private void assertTensorModifyUpdate(String expectedTensor, TensorModifyUpdate.Operation expectedOperation,
+ boolean expectedCreateNonExistingCells,
+ String tensorFieldName, String modifyJson) {
+ assertTensorModifyUpdate(expectedTensor, expectedOperation, expectedCreateNonExistingCells, tensorFieldName,
createTensorModifyUpdate(modifyJson, tensorFieldName));
}
private static void assertTensorModifyUpdate(String expectedTensor, TensorModifyUpdate.Operation expectedOperation,
+ boolean expectedCreateNonExistingCells,
String tensorFieldName, DocumentUpdate update) {
assertTensorFieldUpdate(update, tensorFieldName);
TensorModifyUpdate modifyUpdate = (TensorModifyUpdate) update.getFieldUpdate(tensorFieldName).getValueUpdate(0);
assertEquals(expectedOperation, modifyUpdate.getOperation());
assertEquals(Tensor.from(expectedTensor), modifyUpdate.getValue().getTensor().get());
+ assertEquals(expectedCreateNonExistingCells, modifyUpdate.getCreateNonExistingCells());
}
private DocumentUpdate createTensorModifyUpdate(String modifyJson, String tensorFieldName) {
diff --git a/document/src/test/java/com/yahoo/document/update/TensorModifyUpdateTest.java b/document/src/test/java/com/yahoo/document/update/TensorModifyUpdateTest.java
index 55b9090cce8..d0b04cf5449 100644
--- a/document/src/test/java/com/yahoo/document/update/TensorModifyUpdateTest.java
+++ b/document/src/test/java/com/yahoo/document/update/TensorModifyUpdateTest.java
@@ -51,35 +51,32 @@ public class TensorModifyUpdateTest {
@Test
public void apply_modify_update_operations_with_default_cell_value() {
- assertApplyTo("tensor(x{})", "tensor(x{})", Operation.ADD, Optional.of(0.0),
- "{{x:a}:1,{x:b}:2}", "{{x:b}:3}", "{{x:a}:1,{x:b}:5}");
+ assertApplyTo("tensor(x{})", "tensor(x{})", Operation.MULTIPLY, true,
+ "{{x:a}:1,{x:b}:2}", "{{x:b}:3}", "{{x:a}:1,{x:b}:6}");
- assertApplyTo("tensor(x{})", "tensor(x{})", Operation.ADD, Optional.of(0.0),
- "{{x:a}:1,{x:b}:2}", "{{x:b}:3,{x:c}:4}", "{{x:a}:1,{x:b}:5,{x:c}:4}");
+ assertApplyTo("tensor(x{})", "tensor(x{})", Operation.MULTIPLY, true,
+ "{{x:a}:1,{x:b}:2}", "{{x:b}:3,{x:c}:4}", "{{x:a}:1,{x:b}:6,{x:c}:4}");
- assertApplyTo("tensor(x{},y[3])", "tensor(x{},y{})", Operation.ADD, Optional.of(1.0),
+ assertApplyTo("tensor(x{},y[3])", "tensor(x{},y{})", Operation.ADD, true,
"{{x:a,y:0}:3,{x:a,y:1}:4,{x:a,y:2}:5}",
"{{x:a,y:0}:6,{x:b,y:1}:7,{x:b,y:2}:8,{x:c,y:0}:9}",
"{{x:a,y:0}:9,{x:a,y:1}:4,{x:a,y:2}:5," +
- "{x:b,y:0}:1,{x:b,y:1}:8,{x:b,y:2}:9," +
- "{x:c,y:0}:10,{x:c,y:1}:1,{x:c,y:2}:1}");
+ "{x:b,y:0}:0,{x:b,y:1}:7,{x:b,y:2}:8," +
+ "{x:c,y:0}:9,{x:c,y:1}:0,{x:c,y:2}:0}");
- // NOTE: The specified default cell value doesn't have any effect for tensors with only indexed dimensions,
- // as the dense subspace is always represented (with default cell value 0.0).
- assertApplyTo("tensor(x[3])", "tensor(x{})", Operation.ADD, Optional.of(2.0),
- "{{x:0}:2}", "{{x:1}:3}", "{{x:0}:2,{x:1}:3,{x:2}:0}");
+ // NOTE: The default cell value (1.0) used for MULTIPLY operation doesn't have any effect for tensors
+ // with only indexed dimensions, as the dense subspace is always represented (with default cell value 0.0).
+ assertApplyTo("tensor(x[3])", "tensor(x{})", Operation.MULTIPLY, true,
+ "{{x:0}:2}", "{{x:1}:3}", "{{x:0}:2,{x:1}:0,{x:2}:0}");
}
private void assertApplyTo(String spec, Operation op, String input, String update, String expected) {
- assertApplyTo(spec, "tensor(x{},y{})", op, Optional.empty(), input, update, expected);
+ assertApplyTo(spec, "tensor(x{},y{})", op, false, input, update, expected);
}
- private void assertApplyTo(String inputSpec, String updateSpec, Operation op, Optional<Double> defaultCellValue, String input, String update, String expected) {
+ private void assertApplyTo(String inputSpec, String updateSpec, Operation op, boolean createNonExistingCells, String input, String update, String expected) {
TensorFieldValue inputFieldValue = new TensorFieldValue(Tensor.from(inputSpec, input));
- TensorModifyUpdate modifyUpdate = new TensorModifyUpdate(op, new TensorFieldValue(Tensor.from(updateSpec, update)));
- if (defaultCellValue.isPresent()) {
- modifyUpdate.setDefaultCellValue(defaultCellValue.get());
- }
+ TensorModifyUpdate modifyUpdate = new TensorModifyUpdate(op, new TensorFieldValue(Tensor.from(updateSpec, update)), createNonExistingCells);
TensorFieldValue updatedFieldValue = (TensorFieldValue) modifyUpdate.applyTo(inputFieldValue);
assertEquals(Tensor.from(inputSpec, expected), updatedFieldValue.getTensor().get());
}