Skip to content

Commit

Permalink
Created a failing test to reproduce painless bug
Browse files Browse the repository at this point in the history
Signed-off-by: Shivansh Arora <shivansh.arora@protonmail.com>
  • Loading branch information
shiv0408 committed Jun 28, 2023
1 parent 03bc192 commit 4c711f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
import org.opensearch.script.ScriptContext;
import org.opensearch.script.ScriptService;
import org.opensearch.script.ScriptType;
import org.opensearch.script.UpdateScript;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.TransportService;

Expand Down Expand Up @@ -138,6 +139,7 @@ public static class Request extends SingleShardRequest<Request> implements ToXCo
supportedContexts.put("painless_test", PainlessTestScript.CONTEXT);
supportedContexts.put("filter", FilterScript.CONTEXT);
supportedContexts.put("score", ScoreScript.CONTEXT);
supportedContexts.put("update", UpdateScript.CONTEXT);
SUPPORTED_CONTEXTS = Collections.unmodifiableMap(supportedContexts);
}

Expand Down Expand Up @@ -577,6 +579,13 @@ static Response innerShardOperation(Request request, ScriptService scriptService
double result = scoreScript.execute(null);
return new Response(result);
}, indexService);
} else if (scriptContext == UpdateScript.CONTEXT) {
return prepareRamIndex(request, (context, leafReaderContext) -> {
UpdateScript.Factory factory = scriptService.compile(request.script, UpdateScript.CONTEXT);
UpdateScript updateScript = factory.newInstance(request.getScript().getParams(), null);
updateScript.execute();
return new Response(null);
}, indexService);
} else {
throw new UnsupportedOperationException("unsupported context [" + scriptContext.name + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.opensearch.script.ScriptException;
import org.opensearch.script.ScriptService;
import org.opensearch.script.ScriptType;
import org.opensearch.script.UpdateScript;
import org.opensearch.test.OpenSearchSingleNodeTestCase;

import java.io.IOException;
Expand Down Expand Up @@ -142,4 +143,14 @@ public void testScoreExecutionContext() throws IOException {
assertThat(response.getResult(), equalTo(0.93D));
}

public void testUpdateScriptThrowsScriptExceptionWhenDefReturned() throws Exception {
ScriptService scriptService = getInstanceFromNode(ScriptService.class);
IndexService indexService = createIndex("index", Settings.EMPTY, "doc");
Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 3}"), null);
contextSetup.setXContentType(XContentType.JSON);
Request request = new Request(new Script("def x=1;return x;"), "update", contextSetup);
Exception ex = expectThrows(ScriptException.class, () -> innerShardOperation(request, scriptService, indexService));
assertThat(ex.getCause().getClass(), equalTo(ClassCastException.class));
}

}

0 comments on commit 4c711f0

Please sign in to comment.