Skip to content

Commit

Permalink
fix null exception in text docs data set (opensearch-project#1403)
Browse files Browse the repository at this point in the history
Signed-off-by: Yaliang Wu <ylwu@amazon.com>
  • Loading branch information
ylwu-amzn authored Sep 28, 2023
1 parent f1dd56b commit cda89ff
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.opensearch.ml.common.output.model.ModelResultFilter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

Expand All @@ -40,7 +41,11 @@ public TextDocsInputDataSet(List<String> docs, ModelResultFilter resultFilter) {

public TextDocsInputDataSet(StreamInput streamInput) throws IOException {
super(MLInputDataType.TEXT_DOCS);
docs = streamInput.readStringList();
docs = new ArrayList<>();
int size = streamInput.readInt();
for (int i=0; i<size; i++) {
docs.add(streamInput.readOptionalString());
}
if (streamInput.readBoolean()) {
resultFilter = new ModelResultFilter(streamInput);
} else {
Expand All @@ -51,7 +56,10 @@ public TextDocsInputDataSet(StreamInput streamInput) throws IOException {
@Override
public void writeTo(StreamOutput streamOutput) throws IOException {
super.writeTo(streamOutput);
streamOutput.writeStringCollection(docs);
streamOutput.writeInt(docs.size());
for (String doc : docs) {
streamOutput.writeOptionalString(doc);
}
if (resultFilter != null) {
streamOutput.writeBoolean(true);
resultFilter.writeTo(streamOutput);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.ml.common.dataset;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;

import java.io.IOException;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class TextDocsInputDataSetTest {

@Rule
public ExpectedException exceptionRule = ExpectedException.none();

@Test
public void writeTo_Success() throws IOException {
TextDocsInputDataSet inputDataSet = TextDocsInputDataSet.builder().docs(Arrays.asList("doc1", "doc2")).build();
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
inputDataSet.writeTo(bytesStreamOutput);

StreamInput streamInput = bytesStreamOutput.bytes().streamInput();
MLInputDataType inputDataType = streamInput.readEnum(MLInputDataType.class);
assertEquals(MLInputDataType.TEXT_DOCS, inputDataType);
TextDocsInputDataSet inputDataSet1 = new TextDocsInputDataSet((streamInput));
assertEquals(2, inputDataSet1.getDocs().size());
assertEquals("doc1", inputDataSet1.getDocs().get(0));
assertEquals("doc2", inputDataSet1.getDocs().get(1));
}

@Test
public void writeTo_Success_NullValue() throws IOException {
TextDocsInputDataSet inputDataSet = TextDocsInputDataSet.builder().docs(Arrays.asList("doc1", null)).build();
BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
inputDataSet.writeTo(bytesStreamOutput);

StreamInput streamInput = bytesStreamOutput.bytes().streamInput();
MLInputDataType inputDataType = streamInput.readEnum(MLInputDataType.class);
assertEquals(MLInputDataType.TEXT_DOCS, inputDataType);
TextDocsInputDataSet inputDataSet1 = new TextDocsInputDataSet((streamInput));
assertEquals(2, inputDataSet1.getDocs().size());
assertEquals("doc1", inputDataSet1.getDocs().get(0));
assertEquals(null, inputDataSet1.getDocs().get(1));
}
}

0 comments on commit cda89ff

Please sign in to comment.