mirror of https://github.com/apache/druid.git
1) Fix bug with NPE thrown when requesting a dimension on a GroupBy query that is not dictionary encoded
This commit is contained in:
parent
36d02ab943
commit
ba5ed4c9d4
|
@ -1,118 +0,0 @@
|
|||
/*
|
||||
* 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.index.column;
|
||||
|
||||
import com.metamx.druid.kv.Indexed;
|
||||
import com.metamx.druid.kv.IndexedInts;
|
||||
import it.uniroma3.mat.extendedset.intset.ImmutableConciseSet;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class StringMultiValueColumn extends AbstractColumn
|
||||
{
|
||||
private static final ImmutableConciseSet emptySet = new ImmutableConciseSet();
|
||||
private static final ColumnCapabilitiesImpl CAPABILITIES = new ColumnCapabilitiesImpl()
|
||||
.setType(ValueType.STRING)
|
||||
.setDictionaryEncoded(true)
|
||||
.setHasBitmapIndexes(true)
|
||||
.setHasMultipleValues(true);
|
||||
|
||||
private final Indexed<String> lookups;
|
||||
private final Indexed<? extends IndexedInts> column;
|
||||
private final Indexed<ImmutableConciseSet> bitmapIndexes;
|
||||
|
||||
public StringMultiValueColumn(
|
||||
Indexed<String> lookups,
|
||||
Indexed<? extends IndexedInts> column,
|
||||
Indexed<ImmutableConciseSet> bitmapIndexes
|
||||
)
|
||||
{
|
||||
this.lookups = lookups;
|
||||
this.column = column;
|
||||
this.bitmapIndexes = bitmapIndexes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ColumnCapabilities getCapabilities()
|
||||
{
|
||||
return CAPABILITIES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength()
|
||||
{
|
||||
return column.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DictionaryEncodedColumn getDictionaryEncoding()
|
||||
{
|
||||
return new DictionaryEncodedColumn()
|
||||
{
|
||||
@Override
|
||||
public int length()
|
||||
{
|
||||
return column.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMultipleValues()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSingleValueRow(int rowNum)
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IndexedInts getMultiValueRow(int rowNum)
|
||||
{
|
||||
return column.get(rowNum);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String lookupName(int id)
|
||||
{
|
||||
return lookups.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lookupId(String name)
|
||||
{
|
||||
return lookups.indexOf(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCardinality()
|
||||
{
|
||||
return lookups.size();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public BitmapIndex getBitmapIndex()
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.metamx.druid.kv;
|
||||
|
||||
import com.google.common.collect.Iterators;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class SingleIndexedInts implements IndexedInts
|
||||
{
|
||||
private final int value;
|
||||
|
||||
public SingleIndexedInts(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int get(int index)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Integer> iterator()
|
||||
{
|
||||
return Iterators.singletonIterator(value);
|
||||
}
|
||||
}
|
|
@ -22,7 +22,6 @@ package com.metamx.druid.index.v1;
|
|||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Functions;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Iterators;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.io.Closeables;
|
||||
import com.metamx.collections.spatial.ImmutableRTree;
|
||||
|
@ -48,6 +47,7 @@ import com.metamx.druid.index.v1.processing.Offset;
|
|||
import com.metamx.druid.kv.Indexed;
|
||||
import com.metamx.druid.kv.IndexedInts;
|
||||
import com.metamx.druid.kv.IndexedIterable;
|
||||
import com.metamx.druid.kv.SingleIndexedInts;
|
||||
import com.metamx.druid.processing.ComplexMetricSelector;
|
||||
import com.metamx.druid.processing.FloatMetricSelector;
|
||||
import com.metamx.druid.processing.ObjectColumnSelector;
|
||||
|
@ -350,7 +350,10 @@ public class QueryableIndexStorageAdapter extends BaseStorageAdapter
|
|||
|
||||
final DictionaryEncodedColumn column = columnDesc.getDictionaryEncoding();
|
||||
|
||||
if (columnDesc.getCapabilities().hasMultipleValues()) {
|
||||
if (column == null) {
|
||||
return null;
|
||||
}
|
||||
else if (columnDesc.getCapabilities().hasMultipleValues()) {
|
||||
return new DimensionSelector()
|
||||
{
|
||||
@Override
|
||||
|
@ -384,27 +387,7 @@ public class QueryableIndexStorageAdapter extends BaseStorageAdapter
|
|||
@Override
|
||||
public IndexedInts getRow()
|
||||
{
|
||||
final int value = column.getSingleValueRow(cursorOffset.getOffset());
|
||||
return new IndexedInts()
|
||||
{
|
||||
@Override
|
||||
public int size()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int get(int index)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Integer> iterator()
|
||||
{
|
||||
return Iterators.singletonIterator(value);
|
||||
}
|
||||
};
|
||||
return new SingleIndexedInts(column.getSingleValueRow(cursorOffset.getOffset()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -779,39 +762,42 @@ public class QueryableIndexStorageAdapter extends BaseStorageAdapter
|
|||
public DimensionSelector makeDimensionSelector(String dimension)
|
||||
{
|
||||
final String dimensionName = dimension.toLowerCase();
|
||||
final Column columnDesc = index.getColumn(dimensionName);
|
||||
if (columnDesc == null) {
|
||||
final Column column = index.getColumn(dimensionName);
|
||||
if (column == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final DictionaryEncodedColumn column = columnDesc.getDictionaryEncoding();
|
||||
final DictionaryEncodedColumn dict = column.getDictionaryEncoding();
|
||||
|
||||
if (columnDesc.getCapabilities().hasMultipleValues()) {
|
||||
if (dict == null) {
|
||||
return null;
|
||||
}
|
||||
else if (column.getCapabilities().hasMultipleValues()) {
|
||||
return new DimensionSelector()
|
||||
{
|
||||
@Override
|
||||
public IndexedInts getRow()
|
||||
{
|
||||
return column.getMultiValueRow(currRow);
|
||||
return dict.getMultiValueRow(currRow);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getValueCardinality()
|
||||
{
|
||||
return column.getCardinality();
|
||||
return dict.getCardinality();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String lookupName(int id)
|
||||
{
|
||||
final String retVal = column.lookupName(id);
|
||||
final String retVal = dict.lookupName(id);
|
||||
return retVal == null ? "" : retVal;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lookupId(String name)
|
||||
{
|
||||
return column.lookupId(name);
|
||||
return dict.lookupId(name);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
|
@ -820,45 +806,25 @@ public class QueryableIndexStorageAdapter extends BaseStorageAdapter
|
|||
@Override
|
||||
public IndexedInts getRow()
|
||||
{
|
||||
final int value = column.getSingleValueRow(currRow);
|
||||
return new IndexedInts()
|
||||
{
|
||||
@Override
|
||||
public int size()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int get(int index)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Integer> iterator()
|
||||
{
|
||||
return Iterators.singletonIterator(value);
|
||||
}
|
||||
};
|
||||
return new SingleIndexedInts(dict.getSingleValueRow(currRow));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getValueCardinality()
|
||||
{
|
||||
return column.getCardinality();
|
||||
return dict.getCardinality();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String lookupName(int id)
|
||||
{
|
||||
return column.lookupName(id);
|
||||
return dict.lookupName(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lookupId(String name)
|
||||
{
|
||||
return column.lookupId(name);
|
||||
return dict.lookupId(name);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1089,6 +1055,33 @@ public class QueryableIndexStorageAdapter extends BaseStorageAdapter
|
|||
}
|
||||
}
|
||||
|
||||
private static class NullDimensionSelector implements DimensionSelector
|
||||
{
|
||||
@Override
|
||||
public IndexedInts getRow()
|
||||
{
|
||||
return new SingleIndexedInts(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getValueCardinality()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String lookupName(int id)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lookupId(String name)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private class MMappedBitmapIndexSelector implements BitmapIndexSelector
|
||||
{
|
||||
private final ColumnSelector index;
|
||||
|
|
|
@ -654,12 +654,47 @@ public class GroupByQueryRunnerTest
|
|||
|
||||
List<Row> expectedResults = Arrays.asList(
|
||||
createExpectedRow("2011-04-01", "quality", "automotive", "rows", 2L)
|
||||
);
|
||||
);
|
||||
|
||||
QueryRunner<Row> mergeRunner = new GroupByQueryQueryToolChest().mergeResults(runner);
|
||||
TestHelper.assertExpectedObjects(expectedResults, mergeRunner.run(query), "no-limit");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGroupByWithMetricColumnDisappears() throws Exception
|
||||
{
|
||||
GroupByQuery.Builder builder = GroupByQuery
|
||||
.builder()
|
||||
.setDataSource(QueryRunnerTestHelper.dataSource)
|
||||
.setInterval("2011-04-02/2011-04-04")
|
||||
.addDimension("quality")
|
||||
.addDimension("index")
|
||||
.setAggregatorSpecs(
|
||||
Arrays.<AggregatorFactory>asList(
|
||||
QueryRunnerTestHelper.rowsCount
|
||||
)
|
||||
)
|
||||
.setGranularity(new PeriodGranularity(new Period("P1M"), null, null));
|
||||
|
||||
final GroupByQuery query = builder.build();
|
||||
|
||||
List<Row> expectedResults = Arrays.asList(
|
||||
createExpectedRow("2011-04-01", "quality", "automotive", "rows", 2L),
|
||||
createExpectedRow("2011-04-01", "quality", "business", "rows", 2L),
|
||||
createExpectedRow("2011-04-01", "quality", "entertainment", "rows", 2L),
|
||||
createExpectedRow("2011-04-01", "quality", "health", "rows", 2L),
|
||||
createExpectedRow("2011-04-01", "quality", "mezzanine", "rows", 6L),
|
||||
createExpectedRow("2011-04-01", "quality", "news", "rows", 2L),
|
||||
createExpectedRow("2011-04-01", "quality", "premium", "rows", 6L),
|
||||
createExpectedRow("2011-04-01", "quality", "technology", "rows", 2L),
|
||||
createExpectedRow("2011-04-01", "quality", "travel", "rows", 2L)
|
||||
);
|
||||
|
||||
TestHelper.assertExpectedObjects(expectedResults, runner.run(query), "normal");
|
||||
QueryRunner<Row> mergeRunner = new GroupByQueryQueryToolChest().mergeResults(runner);
|
||||
TestHelper.assertExpectedObjects(expectedResults, mergeRunner.run(query), "no-limit");
|
||||
}
|
||||
|
||||
private Row createExpectedRow(final String timestamp, Object... vals)
|
||||
{
|
||||
return createExpectedRow(new DateTime(timestamp), vals);
|
||||
|
|
Loading…
Reference in New Issue