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: Allow multiple EXTRACTJSONFIELD invocations on different paths (#8122) #8123

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 @@ -37,7 +37,8 @@ public class JsonExtractString {

private static final ObjectReader OBJECT_READER = UdfJsonMapper.INSTANCE.get().reader();

private List<String> tokens = null;
private String latestPath = null;
private List<String> latestTokens = null;

@Udf
public String extract(
Expand All @@ -48,13 +49,15 @@ public String extract(
return null;
}

if (tokens == null) {

if (latestPath == null || !latestPath.equals(path)) {
final JsonPathTokenizer tokenizer = new JsonPathTokenizer(path);
tokens = ImmutableList.copyOf(tokenizer);
latestTokens = ImmutableList.copyOf(tokenizer);
latestPath = path;
}

JsonNode currentNode = parseJsonDoc(input);
for (final String token : tokens) {
for (final String token : latestTokens) {
if (currentNode instanceof ArrayNode) {
try {
final int index = Integer.parseInt(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,13 @@ public void shouldBeThreadSafe() {
.parallel()
.forEach(idx -> shouldExtractJsonField());
}

@Test
public void shouldParseCorrectlyDifferentPathsOnSameInstance() {
final String thing1 = udf.extract(JSON_DOC, "$.thing1");
assertThat(thing1, is("{\"thing2\":\"hello\"}"));

final String array = udf.extract(JSON_DOC, "$.array.1");
assertThat(array, is("102"));
}
}