mirror of
https://github.com/apache/druid.git
synced 2025-02-09 03:24:55 +00:00
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:
parent
42a7b8849a
commit
0344a020bb
@ -29,25 +29,18 @@ import org.apache.druid.segment.DimensionSelector;
|
|||||||
import org.apache.druid.segment.DimensionSelectorUtils;
|
import org.apache.druid.segment.DimensionSelectorUtils;
|
||||||
import org.apache.druid.segment.IdLookup;
|
import org.apache.druid.segment.IdLookup;
|
||||||
import org.apache.druid.segment.data.IndexedInts;
|
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;
|
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
|
public class SingleStringInputDimensionSelector implements DimensionSelector
|
||||||
{
|
{
|
||||||
private final DimensionSelector selector;
|
private final DimensionSelector selector;
|
||||||
private final Expr expression;
|
private final Expr expression;
|
||||||
private final SingleInputBindings bindings = new SingleInputBindings();
|
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(
|
public SingleStringInputDimensionSelector(
|
||||||
final DimensionSelector selector,
|
final DimensionSelector selector,
|
||||||
@ -67,7 +60,6 @@ public class SingleStringInputDimensionSelector implements DimensionSelector
|
|||||||
|
|
||||||
this.selector = Preconditions.checkNotNull(selector, "selector");
|
this.selector = Preconditions.checkNotNull(selector, "selector");
|
||||||
this.expression = Preconditions.checkNotNull(expression, "expression");
|
this.expression = Preconditions.checkNotNull(expression, "expression");
|
||||||
this.nullAdjustment = selector.getValueCardinality() == 0 || selector.lookupName(0) != null ? 1 : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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
|
* Get the underlying selector {@link IndexedInts} row, or the null adjusted row.
|
||||||
* other expression selectors. See also {@link ExpressionSelectors#supplierFromDimensionSelector} for similar
|
|
||||||
* behavior.
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public IndexedInts getRow()
|
public IndexedInts getRow()
|
||||||
{
|
{
|
||||||
final IndexedInts row = selector.getRow();
|
final IndexedInts row = selector.getRow();
|
||||||
|
|
||||||
if (row.size() == 1) {
|
assert row.size() <= 1;
|
||||||
if (nullAdjustment == 0) {
|
return row;
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -116,7 +96,7 @@ public class SingleStringInputDimensionSelector implements DimensionSelector
|
|||||||
@Override
|
@Override
|
||||||
public int getValueCardinality()
|
public int getValueCardinality()
|
||||||
{
|
{
|
||||||
return selector.getValueCardinality() + nullAdjustment;
|
return selector.getValueCardinality();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -124,12 +104,7 @@ public class SingleStringInputDimensionSelector implements DimensionSelector
|
|||||||
{
|
{
|
||||||
final String value;
|
final String value;
|
||||||
|
|
||||||
if (id == 0) {
|
value = selector.lookupName(id);
|
||||||
// id 0 is always null for this selector impl.
|
|
||||||
value = null;
|
|
||||||
} else {
|
|
||||||
value = selector.lookupName(id - nullAdjustment);
|
|
||||||
}
|
|
||||||
|
|
||||||
bindings.set(value);
|
bindings.set(value);
|
||||||
return expression.eval(bindings).asString();
|
return expression.eval(bindings).asString();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user