From 405ebdcaafba5237530831f5e559cdbc9ad78fb4 Mon Sep 17 00:00:00 2001 From: xiangqiao123 <572469094@qq.com> Date: Sat, 27 Jun 2020 07:52:04 +0800 Subject: [PATCH] fix MaterializedView gropuby query return arry result by default (#9936) * fix bug:MaterializedView gropuby query return map result by default * add unit test * add unit test * add unit test * fix bug:MaterializedView gropuby query return map result by default * add unit test * add unit test * add unit test * update pr * update pr Co-authored-by: xiangqiao --- .../MaterializedViewQueryQueryToolChest.java | 8 + ...terializedViewQueryQueryToolChestTest.java | 162 ++++++++++++++++++ 2 files changed, 170 insertions(+) diff --git a/extensions-contrib/materialized-view-selection/src/main/java/org/apache/druid/query/materializedview/MaterializedViewQueryQueryToolChest.java b/extensions-contrib/materialized-view-selection/src/main/java/org/apache/druid/query/materializedview/MaterializedViewQueryQueryToolChest.java index ca02de7156d..d6916e719ef 100644 --- a/extensions-contrib/materialized-view-selection/src/main/java/org/apache/druid/query/materializedview/MaterializedViewQueryQueryToolChest.java +++ b/extensions-contrib/materialized-view-selection/src/main/java/org/apache/druid/query/materializedview/MaterializedViewQueryQueryToolChest.java @@ -20,6 +20,7 @@ package org.apache.druid.query.materializedview; import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Function; import com.google.inject.Inject; import org.apache.druid.java.util.common.guava.Sequence; @@ -96,6 +97,13 @@ public class MaterializedViewQueryQueryToolChest extends QueryToolChest return warehouse.getToolChest(realQuery).makePostComputeManipulatorFn(realQuery, fn); } + @Override + public ObjectMapper decorateObjectMapper(final ObjectMapper objectMapper, final Query query) + { + Query realQuery = getRealQuery(query); + return warehouse.getToolChest(realQuery).decorateObjectMapper(objectMapper, realQuery); + } + @Override public TypeReference getResultTypeReference() { diff --git a/extensions-contrib/materialized-view-selection/src/test/java/org/apache/druid/query/materializedview/MaterializedViewQueryQueryToolChestTest.java b/extensions-contrib/materialized-view-selection/src/test/java/org/apache/druid/query/materializedview/MaterializedViewQueryQueryToolChestTest.java index cf1649c9102..b96ff17f670 100644 --- a/extensions-contrib/materialized-view-selection/src/test/java/org/apache/druid/query/materializedview/MaterializedViewQueryQueryToolChestTest.java +++ b/extensions-contrib/materialized-view-selection/src/test/java/org/apache/druid/query/materializedview/MaterializedViewQueryQueryToolChestTest.java @@ -19,8 +19,12 @@ package org.apache.druid.query.materializedview; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Function; import com.google.common.collect.ImmutableMap; +import org.apache.druid.data.input.MapBasedRow; +import org.apache.druid.jackson.DefaultObjectMapper; import org.apache.druid.java.util.common.DateTimes; import org.apache.druid.query.Druids; import org.apache.druid.query.MapQueryToolChestWarehouse; @@ -29,17 +33,30 @@ import org.apache.druid.query.QueryRunnerTestHelper; import org.apache.druid.query.QueryToolChest; import org.apache.druid.query.Result; import org.apache.druid.query.aggregation.AggregatorFactory; +import org.apache.druid.query.aggregation.LongSumAggregatorFactory; import org.apache.druid.query.aggregation.MetricManipulationFn; +import org.apache.druid.query.dimension.DefaultDimensionSpec; +import org.apache.druid.query.groupby.GroupByQuery; +import org.apache.druid.query.groupby.GroupByQueryConfig; +import org.apache.druid.query.groupby.GroupByQueryQueryToolChest; +import org.apache.druid.query.groupby.GroupByQueryRunnerTestHelper; +import org.apache.druid.query.groupby.ResultRow; import org.apache.druid.query.timeseries.TimeseriesQuery; import org.apache.druid.query.timeseries.TimeseriesQueryQueryToolChest; import org.apache.druid.query.timeseries.TimeseriesResultValue; import org.junit.Assert; import org.junit.Test; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public class MaterializedViewQueryQueryToolChestTest { + private static final ObjectMapper JSON_MAPPER = new DefaultObjectMapper(); + @Test public void testMakePostComputeManipulatorFn() { @@ -85,4 +102,149 @@ public class MaterializedViewQueryQueryToolChestTest Assert.assertEquals(postResultMap.get(QueryRunnerTestHelper.ROWS_COUNT.getName()), "metricvalue1"); Assert.assertEquals(postResultMap.get("dim1"), "dimvalue1"); } + + @Test + public void testDecorateObjectMapper() throws IOException + { + GroupByQuery realQuery = GroupByQuery.builder() + .setDataSource(QueryRunnerTestHelper.DATA_SOURCE) + .setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD) + .setDimensions(new DefaultDimensionSpec("quality", "alias")) + .setAggregatorSpecs( + QueryRunnerTestHelper.ROWS_COUNT, + new LongSumAggregatorFactory("idx", "index") + ) + .setGranularity(QueryRunnerTestHelper.DAY_GRAN) + .setContext(ImmutableMap.of(GroupByQueryConfig.CTX_KEY_ARRAY_RESULT_ROWS, false)) + .build(); + + QueryToolChest queryToolChest = + new MaterializedViewQueryQueryToolChest(new MapQueryToolChestWarehouse( + ImmutableMap., QueryToolChest>builder() + .put(GroupByQuery.class, new GroupByQueryQueryToolChest(null)) + .build() + )); + + ObjectMapper objectMapper = queryToolChest.decorateObjectMapper(JSON_MAPPER, realQuery); + + List results = Arrays.asList( + GroupByQueryRunnerTestHelper.createExpectedRow( + realQuery, + "2011-04-01", + "alias", + "automotive", + "rows", + 1L, + "idx", + 135L + + ), + GroupByQueryRunnerTestHelper.createExpectedRow( + realQuery, + "2011-04-01", + "alias", + "business", + "rows", + 1L, + "idx", + 118L + ) + ); + List expectedResults = results.stream() + .map(resultRow -> resultRow.toMapBasedRow(realQuery)) + .collect(Collectors.toList()); + + Assert.assertEquals( + "decorate-object-mapper", + JSON_MAPPER.writerFor(new TypeReference>(){}).writeValueAsString(expectedResults), + objectMapper.writeValueAsString(results) + ); + } + + @Test + public void testDecorateObjectMapperMaterializedViewQuery() throws IOException + { + GroupByQuery realQuery = GroupByQuery.builder() + .setDataSource(QueryRunnerTestHelper.DATA_SOURCE) + .setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD) + .setDimensions(new DefaultDimensionSpec("quality", "alias")) + .setAggregatorSpecs( + QueryRunnerTestHelper.ROWS_COUNT, + new LongSumAggregatorFactory("idx", "index") + ) + .setGranularity(QueryRunnerTestHelper.DAY_GRAN) + .setContext(ImmutableMap.of(GroupByQueryConfig.CTX_KEY_ARRAY_RESULT_ROWS, false)) + .build(); + MaterializedViewQuery materializedViewQuery = new MaterializedViewQuery(realQuery, null); + + QueryToolChest materializedViewQueryQueryToolChest = + new MaterializedViewQueryQueryToolChest(new MapQueryToolChestWarehouse( + ImmutableMap., QueryToolChest>builder() + .put(GroupByQuery.class, new GroupByQueryQueryToolChest(null)) + .build() + )); + + ObjectMapper objectMapper = materializedViewQueryQueryToolChest.decorateObjectMapper(JSON_MAPPER, materializedViewQuery); + + List results = Arrays.asList( + GroupByQueryRunnerTestHelper.createExpectedRow( + realQuery, + "2011-04-01", + "alias", + "automotive", + "rows", + 1L, + "idx", + 135L + + ), + GroupByQueryRunnerTestHelper.createExpectedRow( + realQuery, + "2011-04-01", + "alias", + "business", + "rows", + 1L, + "idx", + 118L + ) + ); + List expectedResults = results.stream() + .map(resultRow -> resultRow.toMapBasedRow(realQuery)) + .collect(Collectors.toList()); + + Assert.assertEquals( + "decorate-object-mapper", + JSON_MAPPER.writerFor(new TypeReference>(){}).writeValueAsString(expectedResults), + objectMapper.writeValueAsString(results) + ); + } + + @Test + public void testGetRealQuery() + { + GroupByQuery realQuery = GroupByQuery.builder() + .setDataSource(QueryRunnerTestHelper.DATA_SOURCE) + .setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD) + .setDimensions(new DefaultDimensionSpec("quality", "alias")) + .setAggregatorSpecs( + QueryRunnerTestHelper.ROWS_COUNT, + new LongSumAggregatorFactory("idx", "index") + ) + .setGranularity(QueryRunnerTestHelper.DAY_GRAN) + .setContext(ImmutableMap.of(GroupByQueryConfig.CTX_KEY_ARRAY_RESULT_ROWS, false)) + .build(); + MaterializedViewQuery materializedViewQuery = new MaterializedViewQuery(realQuery, null); + + MaterializedViewQueryQueryToolChest materializedViewQueryQueryToolChest = + new MaterializedViewQueryQueryToolChest(new MapQueryToolChestWarehouse( + ImmutableMap., QueryToolChest>builder() + .put(GroupByQuery.class, new GroupByQueryQueryToolChest(null)) + .build() + )); + + Assert.assertEquals(realQuery, materializedViewQueryQueryToolChest.getRealQuery(materializedViewQuery)); + + } + }