optimize single string dimension expression selector (#8014)

* optimize single string dimension expression selector

* more javadoc

* oops

* fix

* fix it

* import
This commit is contained in:
Clint Wylie 2019-07-04 16:26:10 -07:00 committed by Gian Merlino
parent 42a7b8849a
commit 0344a020bb
1 changed files with 7 additions and 32 deletions

View File

@ -29,25 +29,18 @@ import org.apache.druid.segment.DimensionSelector;
import org.apache.druid.segment.DimensionSelectorUtils;
import org.apache.druid.segment.IdLookup;
import org.apache.druid.segment.data.IndexedInts;
import org.apache.druid.segment.data.SingleIndexedInt;
import org.apache.druid.segment.data.ZeroIndexedInts;
import javax.annotation.Nullable;
/**
* A DimensionSelector decorator that computes an expression on top of it.
* A DimensionSelector decorator that computes an expression on top of it. See {@link ExpressionSelectors} for details
* on how expression selectors are constructed.
*/
public class SingleStringInputDimensionSelector implements DimensionSelector
{
private final DimensionSelector selector;
private final Expr expression;
private final SingleInputBindings bindings = new SingleInputBindings();
private final SingleIndexedInt nullAdjustedRow = new SingleIndexedInt();
/**
* 0 if selector has null as a value; 1 if it doesn't.
*/
private final int nullAdjustment;
public SingleStringInputDimensionSelector(
final DimensionSelector selector,
@ -67,7 +60,6 @@ public class SingleStringInputDimensionSelector implements DimensionSelector
this.selector = Preconditions.checkNotNull(selector, "selector");
this.expression = Preconditions.checkNotNull(expression, "expression");
this.nullAdjustment = selector.getValueCardinality() == 0 || selector.lookupName(0) != null ? 1 : 0;
}
@Override
@ -78,27 +70,15 @@ public class SingleStringInputDimensionSelector implements DimensionSelector
}
/**
* Treats any non-single-valued row as a row containing a single null value, to ensure consistency with
* other expression selectors. See also {@link ExpressionSelectors#supplierFromDimensionSelector} for similar
* behavior.
* Get the underlying selector {@link IndexedInts} row, or the null adjusted row.
*/
@Override
public IndexedInts getRow()
{
final IndexedInts row = selector.getRow();
if (row.size() == 1) {
if (nullAdjustment == 0) {
return row;
} else {
nullAdjustedRow.setValue(row.get(0) + nullAdjustment);
return nullAdjustedRow;
}
} else {
// Can't handle non-singly-valued rows in expressions.
// Treat them as nulls until we think of something better to do.
return ZeroIndexedInts.instance();
}
assert row.size() <= 1;
return row;
}
@Override
@ -116,7 +96,7 @@ public class SingleStringInputDimensionSelector implements DimensionSelector
@Override
public int getValueCardinality()
{
return selector.getValueCardinality() + nullAdjustment;
return selector.getValueCardinality();
}
@Override
@ -124,12 +104,7 @@ public class SingleStringInputDimensionSelector implements DimensionSelector
{
final String value;
if (id == 0) {
// id 0 is always null for this selector impl.
value = null;
} else {
value = selector.lookupName(id - nullAdjustment);
}
value = selector.lookupName(id);
bindings.set(value);
return expression.eval(bindings).asString();