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

[Backport] Fix filtering on boolean values in transformation #9828

Merged
merged 1 commit into from
May 5, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public PeriodGranularity(
)
{
this.period = Preconditions.checkNotNull(period, "period can't be null!");
Preconditions.checkArgument(!Period.ZERO.equals(period), "zero period is not acceptable in QueryGranularity!");
Preconditions.checkArgument(!Period.ZERO.equals(period), "zero period is not acceptable in PeriodGranularity!");
this.chronology = tz == null ? ISOChronology.getInstanceUTC() : ISOChronology.getInstance(tz);
if (origin == null) {
// default to origin in given time zone when aligning multi-period granularities
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/org/apache/druid/math/expr/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,6 @@ public ExprEval eval(ObjectBinding bindings)
return ExprEval.of(null);
}


if (leftVal.type() == ExprType.STRING && rightVal.type() == ExprType.STRING) {
return evalString(leftVal.asString(), rightVal.asString());
} else if (leftVal.type() == ExprType.LONG && rightVal.type() == ExprType.LONG) {
Expand Down
39 changes: 36 additions & 3 deletions core/src/main/java/org/apache/druid/math/expr/ExprEval.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static ExprEval bestEffortOf(@Nullable Object val)
}

// Cached String values
private boolean stringValueValid = false;
private boolean stringValueCached = false;
@Nullable
private String stringValue;

Expand All @@ -137,17 +137,35 @@ public T value()
return value;
}

void cacheStringValue(@Nullable String value)
{
stringValue = value;
stringValueCached = true;
}

@Nullable
String getCachedStringValue()
{
assert stringValueCached;
return stringValue;
}

boolean isStringValueCached()
{
return stringValueCached;
}

@Nullable
public String asString()
{
if (!stringValueValid) {
if (!stringValueCached) {
if (value == null) {
stringValue = null;
} else {
stringValue = String.valueOf(value);
}

stringValueValid = true;
stringValueCached = true;
}

return stringValue;
Expand Down Expand Up @@ -567,6 +585,21 @@ private ArrayExprEval(@Nullable T[] value)
super(value);
}

@Override
@Nullable
public String asString()
{
if (!isStringValueCached()) {
if (value == null) {
cacheStringValue(null);
} else {
cacheStringValue(Arrays.toString(value));
}
}

return getCachedStringValue();
}

@Override
public boolean isNumericNull()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ public boolean matches()
} else if (rowValue instanceof Number) {
// Double or some other non-int, non-long, non-float number.
return getDoublePredicate().applyDouble((double) rowValue);
} else if (rowValue instanceof String || rowValue instanceof List) {
// String or list-of-something. Cast to list of strings and evaluate them as strings.
} else {
// Other types. Cast to list of strings and evaluate them as strings.
// Boolean values are handled here as well since it is not a known type in Druid.
final List<String> rowValueStrings = Rows.objectToStrings(rowValue);

if (rowValueStrings.isEmpty()) {
// Empty list is equivalent to null.
return getStringPredicate().apply(null);
}

Expand All @@ -131,9 +133,6 @@ public boolean matches()
}

return false;
} else {
// Unfilterable type. Treat as null.
return getStringPredicate().apply(null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ class DefaultExpressionDimensionSelector extends BaseSingleValueDimensionSelecto
@Override
protected String getValue()
{

return NullHandling.emptyToNullIfNeeded(baseSelector.getObject().asString());
}

Expand Down
Loading