Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,18 @@
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,
Expand All @@ -67,7 +60,6 @@ public SingleStringInputDimensionSelector(

this.selector = Preconditions.checkNotNull(selector, "selector");
this.expression = Preconditions.checkNotNull(expression, "expression");
this.nullAdjustment = selector.getValueCardinality() == 0 || selector.lookupName(0) != null ? 1 : 0;
}

@Override
Expand All @@ -78,27 +70,15 @@ public void inspectRuntimeShape(final RuntimeShapeInspector inspector)
}

/**
* 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
Expand All @@ -116,20 +96,15 @@ public ValueMatcher makeValueMatcher(final Predicate<String> predicate)
@Override
public int getValueCardinality()
{
return selector.getValueCardinality() + nullAdjustment;
return selector.getValueCardinality();
}

@Override
public String lookupName(final int id)
{
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();
Expand Down