mirror of https://github.com/apache/druid.git
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 <xiangqiao@kuaishou.com>
This commit is contained in:
parent
4b99c6d3ef
commit
405ebdcaaf
|
@ -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()
|
||||
{
|
||||
|
|
|
@ -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.<Class<? extends Query>, QueryToolChest>builder()
|
||||
.put(GroupByQuery.class, new GroupByQueryQueryToolChest(null))
|
||||
.build()
|
||||
));
|
||||
|
||||
ObjectMapper objectMapper = queryToolChest.decorateObjectMapper(JSON_MAPPER, realQuery);
|
||||
|
||||
List<ResultRow> 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<MapBasedRow> expectedResults = results.stream()
|
||||
.map(resultRow -> resultRow.toMapBasedRow(realQuery))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.assertEquals(
|
||||
"decorate-object-mapper",
|
||||
JSON_MAPPER.writerFor(new TypeReference<List<MapBasedRow>>(){}).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.<Class<? extends Query>, QueryToolChest>builder()
|
||||
.put(GroupByQuery.class, new GroupByQueryQueryToolChest(null))
|
||||
.build()
|
||||
));
|
||||
|
||||
ObjectMapper objectMapper = materializedViewQueryQueryToolChest.decorateObjectMapper(JSON_MAPPER, materializedViewQuery);
|
||||
|
||||
List<ResultRow> 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<MapBasedRow> expectedResults = results.stream()
|
||||
.map(resultRow -> resultRow.toMapBasedRow(realQuery))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
Assert.assertEquals(
|
||||
"decorate-object-mapper",
|
||||
JSON_MAPPER.writerFor(new TypeReference<List<MapBasedRow>>(){}).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.<Class<? extends Query>, QueryToolChest>builder()
|
||||
.put(GroupByQuery.class, new GroupByQueryQueryToolChest(null))
|
||||
.build()
|
||||
));
|
||||
|
||||
Assert.assertEquals(realQuery, materializedViewQueryQueryToolChest.getRealQuery(materializedViewQuery));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue