mirror of https://github.com/apache/druid.git
1) Make tests pass
This commit is contained in:
parent
86a6d112e3
commit
4a1f5315a6
|
@ -29,6 +29,7 @@ import com.metamx.common.guava.ConcatSequence;
|
||||||
import com.metamx.common.guava.Sequence;
|
import com.metamx.common.guava.Sequence;
|
||||||
import com.metamx.common.guava.Sequences;
|
import com.metamx.common.guava.Sequences;
|
||||||
import com.metamx.druid.Query;
|
import com.metamx.druid.Query;
|
||||||
|
import com.metamx.druid.QueryGranularity;
|
||||||
import com.metamx.druid.aggregation.AggregatorFactory;
|
import com.metamx.druid.aggregation.AggregatorFactory;
|
||||||
import com.metamx.druid.index.v1.IncrementalIndex;
|
import com.metamx.druid.index.v1.IncrementalIndex;
|
||||||
import com.metamx.druid.initialization.Initialization;
|
import com.metamx.druid.initialization.Initialization;
|
||||||
|
@ -99,10 +100,11 @@ public class GroupByQueryQueryToolChest implements QueryToolChest<Row, GroupByQu
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
final QueryGranularity gran = query.getGranularity();
|
||||||
final IncrementalIndex index = runner.run(query).accumulate(
|
final IncrementalIndex index = runner.run(query).accumulate(
|
||||||
new IncrementalIndex(
|
new IncrementalIndex(
|
||||||
condensed.get(0).getStartMillis(),
|
gran.truncate(condensed.get(0).getStartMillis()),
|
||||||
query.getGranularity(),
|
gran,
|
||||||
aggs.toArray(new AggregatorFactory[aggs.size()])
|
aggs.toArray(new AggregatorFactory[aggs.size()])
|
||||||
),
|
),
|
||||||
new Accumulator<IncrementalIndex, Row>()
|
new Accumulator<IncrementalIndex, Row>()
|
||||||
|
|
|
@ -138,4 +138,33 @@ public class MapBasedRow implements Row
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o)
|
||||||
|
{
|
||||||
|
if (this == o) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (o == null || getClass() != o.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
MapBasedRow that = (MapBasedRow) o;
|
||||||
|
|
||||||
|
if (timestamp != that.timestamp) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (event != null ? !event.equals(that.event) : that.event != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode()
|
||||||
|
{
|
||||||
|
int result = (int) (timestamp ^ (timestamp >>> 32));
|
||||||
|
result = 31 * result + (event != null ? event.hashCode() : 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,6 +52,12 @@ public class Rows
|
||||||
{
|
{
|
||||||
return row.getFloatMetric(metric);
|
return row.getFloatMetric(metric);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return row.toString();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,30 @@
|
||||||
|
/*
|
||||||
|
* Druid - a distributed column store.
|
||||||
|
* Copyright (C) 2012 Metamarkets Group Inc.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
package com.metamx.druid.query.group;
|
package com.metamx.druid.query.group;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
import com.google.common.collect.ImmutableMap;
|
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
import com.metamx.common.guava.Sequence;
|
import com.metamx.common.guava.Sequence;
|
||||||
import com.metamx.common.guava.Sequences;
|
import com.metamx.common.guava.Sequences;
|
||||||
import com.metamx.druid.PeriodGranularity;
|
import com.metamx.druid.PeriodGranularity;
|
||||||
|
@ -33,6 +53,7 @@ import java.nio.ByteBuffer;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@RunWith(Parameterized.class)
|
@RunWith(Parameterized.class)
|
||||||
public class GroupByQueryRunnerTest
|
public class GroupByQueryRunnerTest
|
||||||
|
@ -88,79 +109,58 @@ public class GroupByQueryRunnerTest
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGroupBy() {
|
public void testGroupBy() {
|
||||||
GroupByQuery query = GroupByQuery.builder()
|
GroupByQuery query = GroupByQuery
|
||||||
|
.builder()
|
||||||
.setDataSource(QueryRunnerTestHelper.dataSource)
|
.setDataSource(QueryRunnerTestHelper.dataSource)
|
||||||
.setQuerySegmentSpec(QueryRunnerTestHelper.firstToThird)
|
.setQuerySegmentSpec(QueryRunnerTestHelper.firstToThird)
|
||||||
.setDimensions(
|
.setDimensions(Lists.<DimensionSpec>newArrayList(new DefaultDimensionSpec("quality", "alias")))
|
||||||
Lists.newArrayList(
|
|
||||||
(DimensionSpec)new DefaultDimensionSpec(
|
|
||||||
"quality",
|
|
||||||
"alias"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.setAggregatorSpecs(
|
.setAggregatorSpecs(
|
||||||
Arrays.<AggregatorFactory>asList(
|
Arrays.<AggregatorFactory>asList(
|
||||||
QueryRunnerTestHelper.rowsCount,
|
QueryRunnerTestHelper.rowsCount,
|
||||||
new LongSumAggregatorFactory(
|
new LongSumAggregatorFactory("idx", "index")
|
||||||
"idx",
|
|
||||||
"index"
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.setGranularity(QueryRunnerTestHelper.dayGran)
|
.setGranularity(QueryRunnerTestHelper.dayGran)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
List<Row> expectedResults = Arrays.asList(
|
List<Row> expectedResults = Arrays.<Row>asList(
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-01"),ImmutableMap.<String, Object>of("alias", "automotive", "rows", 1L, "idx", 135L)),
|
createExpectedRow("2011-04-01", "alias", "automotive", "rows", 1L, "idx", 135L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-01"),ImmutableMap.<String, Object>of("alias", "business", "rows", 1L, "idx", 118L)),
|
createExpectedRow("2011-04-01", "alias", "business", "rows", 1L, "idx", 118L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-01"),ImmutableMap.<String, Object>of("alias", "entertainment", "rows", 1L, "idx", 158L)),
|
createExpectedRow("2011-04-01", "alias", "entertainment", "rows", 1L, "idx", 158L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-01"),ImmutableMap.<String, Object>of("alias", "health", "rows", 1L, "idx", 120L)),
|
createExpectedRow("2011-04-01", "alias", "health", "rows", 1L, "idx", 120L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-01"),ImmutableMap.<String, Object>of("alias", "mezzanine", "rows", 3L, "idx", 2870L)),
|
createExpectedRow("2011-04-01", "alias", "mezzanine", "rows", 3L, "idx", 2870L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-01"),ImmutableMap.<String, Object>of("alias", "news", "rows", 1L, "idx", 121L)),
|
createExpectedRow("2011-04-01", "alias", "news", "rows", 1L, "idx", 121L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-01"),ImmutableMap.<String, Object>of("alias", "premium", "rows", 3L, "idx", 2900L)),
|
createExpectedRow("2011-04-01", "alias", "premium", "rows", 3L, "idx", 2900L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-01"),ImmutableMap.<String, Object>of("alias", "technology", "rows", 1L, "idx", 78L)),
|
createExpectedRow("2011-04-01", "alias", "technology", "rows", 1L, "idx", 78L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-01"),ImmutableMap.<String, Object>of("alias", "travel", "rows", 1L, "idx", 119L)),
|
createExpectedRow("2011-04-01", "alias", "travel", "rows", 1L, "idx", 119L),
|
||||||
|
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-02"),ImmutableMap.<String, Object>of("alias", "automotive", "rows", 1L, "idx", 147L)),
|
createExpectedRow("2011-04-02", "alias", "automotive", "rows", 1L, "idx", 147L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-02"),ImmutableMap.<String, Object>of("alias", "business", "rows", 1L, "idx", 112L)),
|
createExpectedRow("2011-04-02", "alias", "business", "rows", 1L, "idx", 112L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-02"),ImmutableMap.<String, Object>of("alias", "entertainment", "rows", 1L, "idx", 166L)),
|
createExpectedRow("2011-04-02", "alias", "entertainment", "rows", 1L, "idx", 166L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-02"),ImmutableMap.<String, Object>of("alias", "health", "rows", 1L, "idx", 113L)),
|
createExpectedRow("2011-04-02", "alias", "health", "rows", 1L, "idx", 113L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-02"),ImmutableMap.<String, Object>of("alias", "mezzanine", "rows", 3L, "idx", 2447L)),
|
createExpectedRow("2011-04-02", "alias", "mezzanine", "rows", 3L, "idx", 2447L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-02"),ImmutableMap.<String, Object>of("alias", "news", "rows", 1L, "idx", 114L)),
|
createExpectedRow("2011-04-02", "alias", "news", "rows", 1L, "idx", 114L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-02"),ImmutableMap.<String, Object>of("alias", "premium", "rows", 3L, "idx", 2505L)),
|
createExpectedRow("2011-04-02", "alias", "premium", "rows", 3L, "idx", 2505L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-02"),ImmutableMap.<String, Object>of("alias", "technology", "rows", 1L, "idx", 97L)),
|
createExpectedRow("2011-04-02", "alias", "technology", "rows", 1L, "idx", 97L),
|
||||||
(Row) new MapBasedRow(new DateTime("2011-04-02"),ImmutableMap.<String, Object>of("alias", "travel", "rows", 1L, "idx", 126L))
|
createExpectedRow("2011-04-02", "alias", "travel", "rows", 1L, "idx", 126L)
|
||||||
);
|
);
|
||||||
|
|
||||||
Iterable<Row> results = Sequences.toList(
|
Iterable<Row> results = Sequences.toList(runner.run(query), Lists.<Row>newArrayList());
|
||||||
runner.run(query),
|
|
||||||
Lists.<Row>newArrayList()
|
|
||||||
);
|
|
||||||
|
|
||||||
TestHelper.assertExpectedObjects(expectedResults, results, "");
|
TestHelper.assertExpectedObjects(expectedResults, results, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMergeResults() {
|
public void testMergeResults() {
|
||||||
GroupByQuery.Builder builder = GroupByQuery.builder()
|
GroupByQuery.Builder builder = GroupByQuery
|
||||||
|
.builder()
|
||||||
.setDataSource(QueryRunnerTestHelper.dataSource)
|
.setDataSource(QueryRunnerTestHelper.dataSource)
|
||||||
.setInterval("2011-04-02/2011-04-04")
|
.setInterval("2011-04-02/2011-04-04")
|
||||||
.setDimensions(
|
.setDimensions(Lists.<DimensionSpec>newArrayList(new DefaultDimensionSpec("quality", "alias")))
|
||||||
Lists.newArrayList(
|
|
||||||
(DimensionSpec)new DefaultDimensionSpec(
|
|
||||||
"quality",
|
|
||||||
"alias"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.setAggregatorSpecs(
|
.setAggregatorSpecs(
|
||||||
Arrays.<AggregatorFactory>asList(
|
Arrays.<AggregatorFactory>asList(
|
||||||
QueryRunnerTestHelper.rowsCount,
|
QueryRunnerTestHelper.rowsCount,
|
||||||
new LongSumAggregatorFactory(
|
new LongSumAggregatorFactory("idx", "index")
|
||||||
"idx",
|
|
||||||
"index"
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
.setGranularity(new PeriodGranularity(new Period("P1M"), null, null));
|
.setGranularity(new PeriodGranularity(new Period("P1M"), null, null));
|
||||||
|
@ -174,57 +174,42 @@ public class GroupByQueryRunnerTest
|
||||||
public Sequence run(Query<Row> query)
|
public Sequence run(Query<Row> query)
|
||||||
{
|
{
|
||||||
// simulate two daily segments
|
// simulate two daily segments
|
||||||
final Query query1 = query.withQuerySegmentSpec(new MultipleIntervalSegmentSpec(Lists.newArrayList(new Interval("2011-04-02/2011-04-03"))));
|
final Query query1 = query.withQuerySegmentSpec(
|
||||||
final Query query2 = query.withQuerySegmentSpec(new MultipleIntervalSegmentSpec(Lists.newArrayList(new Interval("2011-04-03/2011-04-04"))));
|
new MultipleIntervalSegmentSpec(Lists.newArrayList(new Interval("2011-04-02/2011-04-03")))
|
||||||
return Sequences.<Row>concat(runner.run(query1), runner.run(query2));
|
);
|
||||||
|
final Query query2 = query.withQuerySegmentSpec(
|
||||||
|
new MultipleIntervalSegmentSpec(Lists.newArrayList(new Interval("2011-04-03/2011-04-04")))
|
||||||
|
);
|
||||||
|
return Sequences.concat(runner.run(query1), runner.run(query2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
List<Row> expectedResults = Arrays.asList(
|
List<Row> expectedResults = Arrays.<Row>asList(
|
||||||
(Row) new MapBasedRow(
|
createExpectedRow("2011-04-01", "alias", "automotive", "rows", 2L, "idx", 269L),
|
||||||
new DateTime("2011-04-01"),
|
createExpectedRow("2011-04-01", "alias", "business", "rows", 2L, "idx", 217L),
|
||||||
ImmutableMap.<String, Object>of("alias", "automotive", "rows", 1L, "idx", 269L)
|
createExpectedRow("2011-04-01", "alias", "entertainment", "rows", 2L, "idx", 319L),
|
||||||
),
|
createExpectedRow("2011-04-01", "alias", "health", "rows", 2L, "idx", 216L),
|
||||||
(Row) new MapBasedRow(
|
createExpectedRow("2011-04-01", "alias", "mezzanine", "rows", 6L, "idx", 4420L),
|
||||||
new DateTime("2011-04-01"),
|
createExpectedRow("2011-04-01", "alias", "news", "rows", 2L, "idx", 221L),
|
||||||
ImmutableMap.<String, Object>of("alias", "business", "rows", 1L, "idx", 217L)
|
createExpectedRow("2011-04-01", "alias", "premium", "rows", 6L, "idx", 4416L),
|
||||||
),
|
createExpectedRow("2011-04-01", "alias", "technology", "rows", 2L, "idx", 177L),
|
||||||
(Row) new MapBasedRow(
|
createExpectedRow("2011-04-01", "alias", "travel", "rows", 2L, "idx", 243L)
|
||||||
new DateTime("2011-04-01"),
|
|
||||||
ImmutableMap.<String, Object>of("alias", "entertainment", "rows", 1L, "idx", 319L)
|
|
||||||
),
|
|
||||||
(Row) new MapBasedRow(
|
|
||||||
new DateTime("2011-04-01"),
|
|
||||||
ImmutableMap.<String, Object>of("alias", "health", "rows", 1L, "idx", 216L)
|
|
||||||
),
|
|
||||||
(Row) new MapBasedRow(
|
|
||||||
new DateTime("2011-04-01"),
|
|
||||||
ImmutableMap.<String, Object>of("alias", "mezzanine", "rows", 3L, "idx", 4420L)
|
|
||||||
),
|
|
||||||
(Row) new MapBasedRow(
|
|
||||||
new DateTime("2011-04-01"),
|
|
||||||
ImmutableMap.<String, Object>of("alias", "news", "rows", 1L, "idx", 221L)
|
|
||||||
),
|
|
||||||
(Row) new MapBasedRow(
|
|
||||||
new DateTime("2011-04-01"),
|
|
||||||
ImmutableMap.<String, Object>of("alias", "premium", "rows", 3L, "idx", 4416L)
|
|
||||||
),
|
|
||||||
(Row) new MapBasedRow(
|
|
||||||
new DateTime("2011-04-01"),
|
|
||||||
ImmutableMap.<String, Object>of("alias", "technology", "rows", 1L, "idx", 177L)
|
|
||||||
),
|
|
||||||
(Row) new MapBasedRow(
|
|
||||||
new DateTime("2011-04-01"),
|
|
||||||
ImmutableMap.<String, Object>of("alias", "travel", "rows", 1L, "idx", 243L)
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
Iterable<Row> results = Sequences.toList(
|
TestHelper.assertExpectedObjects(expectedResults, runner.run(fullQuery), "direct");
|
||||||
mergedRunner.run(fullQuery),
|
TestHelper.assertExpectedObjects(expectedResults, mergedRunner.run(fullQuery), "merged");
|
||||||
Lists.<Row>newArrayList()
|
}
|
||||||
);
|
|
||||||
|
|
||||||
TestHelper.assertExpectedObjects(expectedResults, results, "");
|
private MapBasedRow createExpectedRow(final String timestamp, Object... vals)
|
||||||
|
{
|
||||||
|
Preconditions.checkArgument(vals.length % 2 == 0);
|
||||||
|
|
||||||
|
Map<String, Object> theVals = Maps.newHashMap();
|
||||||
|
for (int i = 0; i < vals.length; i+=2) {
|
||||||
|
theVals.put(vals[i].toString(), vals[i+1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new MapBasedRow(new DateTime(timestamp), theVals);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue