Skip to content

Commit

Permalink
fix: Allow multiple EXTRACTJSONFIELD invocations on different paths (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanfrehse authored Sep 21, 2021
1 parent cc5c02b commit 7f1d407
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
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"));
}
}

0 comments on commit 7f1d407

Please sign in to comment.