inject @Json ObjectMapper for to_json_string and parse_json expressions (#12900)

* inject @Json ObjectMapper for to_json_string and parse_json expressions

* fix npe

* better
This commit is contained in:
Clint Wylie 2022-08-15 08:44:24 -07:00 committed by GitHub
parent 28836dfa71
commit e42e025296
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 13 deletions

View File

@ -24,7 +24,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.guice.annotations.Json;
import org.apache.druid.java.util.common.IAE;
import org.apache.druid.math.expr.Expr;
import org.apache.druid.math.expr.ExprEval;
@ -38,6 +38,7 @@ import org.apache.druid.segment.nested.StructuredData;
import org.apache.druid.segment.nested.StructuredDataProcessor;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -45,8 +46,6 @@ import java.util.stream.Collectors;
public class NestedDataExpressions
{
private static final ObjectMapper JSON_MAPPER = new DefaultObjectMapper();
public static final ExpressionType TYPE = Preconditions.checkNotNull(
ExpressionType.fromColumnType(NestedDataComplexTypeSerde.TYPE)
);
@ -108,7 +107,7 @@ public class NestedDataExpressions
public static class JsonObjectExprMacro extends StructExprMacro
{
public static final String NAME = "json_object";
@Override
public String name()
{
@ -168,6 +167,16 @@ public class NestedDataExpressions
{
public static final String NAME = "to_json_string";
private final ObjectMapper jsonMapper;
@Inject
public ToJsonStringExprMacro(
@Json ObjectMapper jsonMapper
)
{
this.jsonMapper = jsonMapper;
}
@Override
public String name()
{
@ -190,7 +199,7 @@ public class NestedDataExpressions
ExprEval input = args.get(0).eval(bindings);
try {
final Object unwrapped = maybeUnwrapStructuredData(input);
final String stringify = unwrapped == null ? null : JSON_MAPPER.writeValueAsString(unwrapped);
final String stringify = unwrapped == null ? null : jsonMapper.writeValueAsString(unwrapped);
return ExprEval.ofType(
ExpressionType.STRING,
stringify
@ -223,6 +232,16 @@ public class NestedDataExpressions
{
public static final String NAME = "parse_json";
private final ObjectMapper jsonMapper;
@Inject
public ParseJsonExprMacro(
@Json ObjectMapper jsonMapper
)
{
this.jsonMapper = jsonMapper;
}
@Override
public String name()
{
@ -246,7 +265,7 @@ public class NestedDataExpressions
Object parsed = maybeUnwrapStructuredData(arg);
if (arg.type().is(ExprType.STRING) && arg.value() != null && maybeJson(arg.asString())) {
try {
parsed = JSON_MAPPER.readValue(arg.asString(), Object.class);
parsed = jsonMapper.readValue(arg.asString(), Object.class);
}
catch (JsonProcessingException e) {
throw new IAE("Bad string input [%s] to [%s]", arg.asString(), name());

View File

@ -271,7 +271,11 @@ public class NestedDataTestUtils
return createIncrementalIndex(Granularities.DAY, true, true, 1000);
}
public static List<Segment> createDefaultHourlySegments(AggregationTestHelper helper, TemporaryFolder tempFolder, Closer closer)
public static List<Segment> createDefaultHourlySegments(
AggregationTestHelper helper,
TemporaryFolder tempFolder,
Closer closer
)
throws Exception
{
return createSegments(
@ -284,7 +288,11 @@ public class NestedDataTestUtils
);
}
public static List<Segment> createDefaultHourlySegmentsTsv(AggregationTestHelper helper, TemporaryFolder tempFolder, Closer closer)
public static List<Segment> createDefaultHourlySegmentsTsv(
AggregationTestHelper helper,
TemporaryFolder tempFolder,
Closer closer
)
throws Exception
{
return createTsvSegments(
@ -297,7 +305,11 @@ public class NestedDataTestUtils
);
}
public static List<Segment> createDefaultDaySegments(AggregationTestHelper helper, TemporaryFolder tempFolder, Closer closer)
public static List<Segment> createDefaultDaySegments(
AggregationTestHelper helper,
TemporaryFolder tempFolder,
Closer closer
)
throws Exception
{
return createSegments(

View File

@ -19,9 +19,11 @@
package org.apache.druid.query.expression;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.java.util.common.Pair;
import org.apache.druid.math.expr.Expr;
import org.apache.druid.math.expr.ExprEval;
@ -38,6 +40,7 @@ import java.util.Map;
public class NestedDataExpressionsTest extends InitializedNullHandlingTest
{
private static final ObjectMapper JSON_MAPPER = new DefaultObjectMapper();
private static final ExprMacroTable MACRO_TABLE = new ExprMacroTable(
ImmutableList.of(
new NestedDataExpressions.StructExprMacro(),
@ -50,8 +53,8 @@ public class NestedDataExpressionsTest extends InitializedNullHandlingTest
new NestedDataExpressions.JsonValueExprMacro(),
new NestedDataExpressions.JsonQueryExprMacro(),
new NestedDataExpressions.ToJsonExprMacro(),
new NestedDataExpressions.ToJsonStringExprMacro(),
new NestedDataExpressions.ParseJsonExprMacro()
new NestedDataExpressions.ToJsonStringExprMacro(JSON_MAPPER),
new NestedDataExpressions.ParseJsonExprMacro(JSON_MAPPER)
)
);
private static final Map<String, Object> NEST = ImmutableMap.of(

View File

@ -19,7 +19,9 @@
package org.apache.druid.query.expression;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.math.expr.ExprMacroTable;
public class TestExprMacroTable extends ExprMacroTable
@ -27,6 +29,11 @@ public class TestExprMacroTable extends ExprMacroTable
public static final ExprMacroTable INSTANCE = new TestExprMacroTable();
private TestExprMacroTable()
{
this(new DefaultObjectMapper());
}
private TestExprMacroTable(ObjectMapper jsonMapper)
{
super(
ImmutableList.of(
@ -56,8 +63,8 @@ public class TestExprMacroTable extends ExprMacroTable
new NestedDataExpressions.JsonValueExprMacro(),
new NestedDataExpressions.JsonQueryExprMacro(),
new NestedDataExpressions.ToJsonExprMacro(),
new NestedDataExpressions.ToJsonStringExprMacro(),
new NestedDataExpressions.ParseJsonExprMacro()
new NestedDataExpressions.ToJsonStringExprMacro(jsonMapper),
new NestedDataExpressions.ParseJsonExprMacro(jsonMapper)
)
);
}