Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More consistent empty-set filtering behavior on multi-value columns. #2753

Merged
merged 1 commit into from
Mar 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions docs/content/querying/multi-value-dimensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,72 @@ called `tags`.
{"timestamp": "2011-01-12T00:00:00.000Z", "tags": ["t1","t2","t3"]} #row1
{"timestamp": "2011-01-13T00:00:00.000Z", "tags": ["t3","t4","t5"]} #row2
{"timestamp": "2011-01-14T00:00:00.000Z", "tags": ["t5","t6","t7"]} #row3
{"timestamp": "2011-01-14T00:00:00.000Z", "tags": []} #row4
```

All query types can filter on multi-value dimensions. Filters operate independently on each value of a multi-value
dimension. For example, a `"t1" OR "t3"` filter would match row1 and row2 but not row3. A `"t1" AND "t3"` filter
would only match row1.
### Filtering

All query types, as well as [filtered aggregators](aggregations.html#filtered-aggregator), can filter on multi-value
dimensions. Filters follow these rules on multi-value dimensions:

- Value filters (like "selector", "bound", and "in") match a row if any of the values of a multi-value dimension match
the filter.
- Value filters that match `null` or `""` (empty string) will match empty cells in a multi-value dimension.
- Logical expression filters behave the same way they do on single-value dimensions: "and" matches a row if all
underlying filters match that row; "or" matches a row if any underlying filters match that row; "not" matches a row
if the underlying filter does not match the row.

For example, this "or" filter would match row1 and row2 of the dataset above, but not row3:

```
{
"type": "or",
"fields": [
{
"type": "selector",
"dimension": "tags",
"value": "t1"
},
{
"type": "selector",
"dimension": "tags",
"value": "t3"
}
]
}
```

This "and" filter would match only row1 of the dataset above:

```
{
"type": "and",
"fields": [
{
"type": "selector",
"dimension": "tags",
"value": "t1"
},
{
"type": "selector",
"dimension": "tags",
"value": "t3"
}
]
}
```

This "selector" filter would match row4 of the dataset above:

```
{
"type": "selector",
"dimension": "tags",
"value": null
}
```

### Grouping

topN and groupBy queries can group on multi-value dimensions. When grouping on a multi-value dimension, _all_ values
from matching rows will be used to generate one group per value. It's possible for a query to return more groups than
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import io.druid.query.dimension.DefaultDimensionSpec;
import io.druid.query.filter.DimFilter;
import io.druid.query.filter.ValueMatcher;
Expand Down Expand Up @@ -225,12 +226,11 @@ public ValueMatcher makeValueMatcher(final String dimension, final Comparable va
);

// Compare "value" as a String.
final String valueString = value == null ? null : value.toString();
final boolean isNullOrEmpty = valueString == null || valueString.isEmpty();
final String valueString = value == null ? null : Strings.emptyToNull(value.toString());

// Missing columns match a null or empty string value, and don't match anything else.
if (selector == null) {
return new BooleanValueMatcher(isNullOrEmpty);
return new BooleanValueMatcher(valueString == null);
}

final int valueId = selector.lookupId(valueString);
Expand All @@ -241,12 +241,17 @@ public boolean matches()
{
final IndexedInts row = selector.getRow();
final int size = row.size();
for (int i = 0; i < size; ++i) {
if (row.get(i) == valueId) {
return true;
if (size == 0) {
// null should match empty rows in multi-value columns
return valueString == null;
} else {
for (int i = 0; i < size; ++i) {
if (row.get(i) == valueId) {
return true;
}
}
return false;
}
return false;
}
};
}
Expand All @@ -258,8 +263,10 @@ public ValueMatcher makeValueMatcher(final String dimension, final Predicate pre
new DefaultDimensionSpec(dimension, dimension)
);

final boolean doesMatchNull = predicate.apply(null);

if (selector == null) {
return new BooleanValueMatcher(predicate.apply(null));
return new BooleanValueMatcher(doesMatchNull);
}

// Check every value in the dimension, as a String.
Expand All @@ -278,12 +285,17 @@ public boolean matches()
{
final IndexedInts row = selector.getRow();
final int size = row.size();
for (int i = 0; i < size; ++i) {
if (valueIds.get(row.get(i))) {
return true;
if (size == 0) {
// null should match empty rows in multi-value columns
return doesMatchNull;
} else {
for (int i = 0; i < size; ++i) {
if (valueIds.get(row.get(i))) {
return true;
}
}
return false;
}
return false;
}
};
}
Expand Down
25 changes: 22 additions & 3 deletions processing/src/main/java/io/druid/segment/IndexMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -659,16 +659,20 @@ public void close() throws IOException
progress.progress();
startTime = System.currentTimeMillis();

ArrayList<FileOutputSupplier> dimOuts = Lists.newArrayListWithCapacity(mergedDimensions.size());
Map<String, Integer> dimensionCardinalities = Maps.newHashMap();
ArrayList<Map<String, IntBuffer>> dimConversions = Lists.newArrayListWithCapacity(indexes.size());
final ArrayList<FileOutputSupplier> dimOuts = Lists.newArrayListWithCapacity(mergedDimensions.size());
final Map<String, Integer> dimensionCardinalities = Maps.newHashMap();
final ArrayList<Map<String, IntBuffer>> dimConversions = Lists.newArrayListWithCapacity(indexes.size());
final ArrayList<Boolean> convertMissingDimsFlags = Lists.newArrayListWithCapacity(mergedDimensions.size());
final ArrayList<MutableBitmap> nullRowsList = Lists.newArrayListWithCapacity(mergedDimensions.size());
final ArrayList<Boolean> dimHasNullFlags = Lists.newArrayListWithCapacity(mergedDimensions.size());

for (int i = 0; i < indexes.size(); ++i) {
dimConversions.add(Maps.<String, IntBuffer>newHashMap());
}

for (String dimension : mergedDimensions) {
nullRowsList.add(indexSpec.getBitmapSerdeFactory().getBitmapFactory().makeEmptyMutableBitmap());

final GenericIndexedWriter<String> writer = new GenericIndexedWriter<String>(
ioPeon, dimension, GenericIndexed.STRING_STRATEGY
);
Expand Down Expand Up @@ -704,6 +708,7 @@ public void close() throws IOException
* rows from indexes with null/empty str values for that dimension.
*/
if (convertMissingDims && !dimHasNull) {
dimHasNull = true;
dimValueLookups[indexes.size()] = dimValueLookup = EMPTY_STR_DIM_VAL;
numMergeIndex++;
}
Expand Down Expand Up @@ -731,6 +736,9 @@ public void close() throws IOException

dimensionCardinalities.put(dimension, cardinality);

// Mark if this dim has the null/empty str value in its dictionary, used for determining nullRowsList later.
dimHasNullFlags.add(dimHasNull);

FileOutputSupplier dimOut = new FileOutputSupplier(IndexIO.makeDimFile(v8OutDir, dimension), true);
dimOuts.add(dimOut);

Expand Down Expand Up @@ -821,6 +829,14 @@ public void close() throws IOException
? null
: Ints.asList(dims[i]);
forwardDimWriters.get(i).write(listToWrite);
if (listToWrite == null || listToWrite.isEmpty()) {
// empty row; add to the nullRows bitmap
nullRowsList.get(i).add(rowCount);
} else if (dimHasNullFlags.get(i) && listToWrite.size() == 1 && listToWrite.get(0) == 0) {
// If this dimension has the null/empty str in its dictionary, a row with a single-valued dimension
// that matches the null/empty str's dictionary ID should also be added to nullRowsList.
nullRowsList.get(i).add(rowCount);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add(..) on MutableBitmap is expensive and leads to a performance regression here because this will, in many cases, get called for num_rows * num_dimensions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this code from IndexMergerV9 so it is probably an issue there too.

Although now I wonder if this check is really necessary. nullRowsList is just used to add empty rows to the inverted index for null. Those rows already containing explicit nulls should already be in the inverted index – so this might be duplicating work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this and a bunch of filter tests failed so it does seem like it's doing something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in IndexMergerV9, nullRowsList was analogous to this part of IndexIO, in convertV8toV9():
https://github.com/druid-io/druid/blob/master/processing/src/main/java/io/druid/segment/IndexIO.java#L621

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked into this a bit, I think this "null bitmap" code in IndexMerger/IndexMergerV9 is handling two things:

  • IncrementalIndexAdapter doesn't build bitmap indexes for null fully, it doesn't take into account empty or missing rows:
    jon-wei@59e1090. You can see that if you disable all of the "null bitmap" logic and run IndexMergerTest.testPersistWithDifferentDims()
  • The "null bitmap" code also handles cases where two segments are merged and a dimension was only present in one, you can see that behavior by disable the "null bitmap" code and running IndexMergerTest.testDisjointDimMerge()

If I disable the code pointed to by this comment thread in IndexMerger, I think some of the recently introduced filter tests on multival dimensions fails; enabling the "null bitmap" adjustment code in IndexIO.convertV8toV9() for multivalued dimensions as well as single valued dimensions seems to have the same effect as this change in question (but I haven't tested this extensively, only ran SelectorFilterTest)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think this code may be redundant for the single value dimension case

}

for (Map.Entry<Integer, TreeSet<Integer>> comprisedRow : theRow.getComprisedRows().entrySet()) {
Expand Down Expand Up @@ -930,6 +946,9 @@ public void close() throws IOException
}

MutableBitmap bitset = bitmapSerdeFactory.getBitmapFactory().makeEmptyMutableBitmap();
if ((dictId == 0) && (Iterables.getFirst(dimVals, "") == null)) {
bitset.or(nullRowsList.get(i));
}
for (Integer row : CombiningIterable.createSplatted(
convertedInverteds,
Ordering.<Integer>natural().nullsFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
*/
public class IndexBuilder
{
private static final int ROWS_PER_INDEX_FOR_MERGING = 2;
private static final int ROWS_PER_INDEX_FOR_MERGING = 1;
private static final int MAX_ROWS = 50_000;

private IncrementalIndexSchema schema = new IncrementalIndexSchema.Builder().withMetrics(new AggregatorFactory[]{
Expand Down
Loading