Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix null exception in text docs data set #1403

Merged
merged 1 commit into from
Sep 28, 2023
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
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));
}
}
Loading