use of org.apache.carbondata.core.scan.expression.conditional.EqualToExpression in project carbondata by apache.
the class PrestoFilterUtil method parseFilterExpression.
/**
* Convert presto-TupleDomain predication into Carbon scan express condition
*
* @param originalConstraint presto-TupleDomain
* @return
*/
static Expression parseFilterExpression(TupleDomain<ColumnHandle> originalConstraint) {
ImmutableList.Builder<Expression> filters = ImmutableList.builder();
Domain domain;
for (ColumnHandle c : originalConstraint.getDomains().get().keySet()) {
// Build ColumnExpression for Expression(Carbondata)
CarbondataColumnHandle cdch = (CarbondataColumnHandle) c;
Type type = cdch.getColumnType();
DataType coltype = Spi2CarbondataTypeMapper(cdch);
Expression colExpression = new ColumnExpression(cdch.getColumnName(), coltype);
domain = originalConstraint.getDomains().get().get(c);
checkArgument(domain.getType().isOrderable(), "Domain type must be orderable");
List<Object> singleValues = new ArrayList<>();
Map<Object, List<Expression>> valueExpressionMap = new HashMap<>();
for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
if (range.isSingleValue()) {
Object value = ConvertDataByType(range.getLow().getValue(), type);
singleValues.add(value);
} else {
if (!range.getLow().isLowerUnbounded()) {
Object value = ConvertDataByType(range.getLow().getValue(), type);
switch(range.getLow().getBound()) {
case ABOVE:
if (type == TimestampType.TIMESTAMP) {
// todo not now
} else {
GreaterThanExpression greater = new GreaterThanExpression(colExpression, new LiteralExpression(value, coltype));
valueExpressionMap.computeIfAbsent(value, key -> new ArrayList<>()).add(greater);
}
break;
case EXACTLY:
GreaterThanEqualToExpression greater = new GreaterThanEqualToExpression(colExpression, new LiteralExpression(value, coltype));
valueExpressionMap.computeIfAbsent(value, key -> new ArrayList<>()).add(greater);
break;
case BELOW:
throw new IllegalArgumentException("Low marker should never use BELOW bound");
default:
throw new AssertionError("Unhandled bound: " + range.getLow().getBound());
}
}
if (!range.getHigh().isUpperUnbounded()) {
Object value = ConvertDataByType(range.getHigh().getValue(), type);
switch(range.getHigh().getBound()) {
case ABOVE:
throw new IllegalArgumentException("High marker should never use ABOVE bound");
case EXACTLY:
LessThanEqualToExpression less = new LessThanEqualToExpression(colExpression, new LiteralExpression(value, coltype));
valueExpressionMap.computeIfAbsent(value, key -> new ArrayList<>()).add(less);
break;
case BELOW:
LessThanExpression less2 = new LessThanExpression(colExpression, new LiteralExpression(value, coltype));
valueExpressionMap.computeIfAbsent(value, key -> new ArrayList<>()).add(less2);
break;
default:
throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
}
}
}
}
if (singleValues.size() == 1) {
Expression ex;
if (coltype.equals(DataTypes.STRING)) {
ex = new EqualToExpression(colExpression, new LiteralExpression(singleValues.get(0), coltype));
} else if (coltype.equals(DataTypes.TIMESTAMP) || coltype.equals(DataTypes.DATE)) {
Long value = (Long) singleValues.get(0);
ex = new EqualToExpression(colExpression, new LiteralExpression(value, coltype));
} else
ex = new EqualToExpression(colExpression, new LiteralExpression(singleValues.get(0), coltype));
filters.add(ex);
} else if (singleValues.size() > 1) {
ListExpression candidates = null;
List<Expression> exs = singleValues.stream().map((a) -> new LiteralExpression(a, coltype)).collect(Collectors.toList());
candidates = new ListExpression(exs);
filters.add(new InExpression(colExpression, candidates));
} else if (valueExpressionMap.size() > 0) {
List<Expression> valuefilters = new ArrayList<>();
Expression finalFilters = null;
List<Expression> expressions;
for (Map.Entry<Object, List<Expression>> entry : valueExpressionMap.entrySet()) {
expressions = valueExpressionMap.get(entry.getKey());
if (expressions.size() == 1) {
finalFilters = expressions.get(0);
} else if (expressions.size() >= 2) {
finalFilters = new OrExpression(expressions.get(0), expressions.get(1));
for (int i = 2; i < expressions.size(); i++) {
finalFilters = new OrExpression(finalFilters, expressions.get(i));
}
}
valuefilters.add(finalFilters);
}
if (valuefilters.size() == 1) {
finalFilters = valuefilters.get(0);
} else if (valuefilters.size() >= 2) {
finalFilters = new AndExpression(valuefilters.get(0), valuefilters.get(1));
for (int i = 2; i < valuefilters.size(); i++) {
finalFilters = new AndExpression(finalFilters, valuefilters.get(i));
}
}
filters.add(finalFilters);
}
}
Expression finalFilters;
List<Expression> tmp = filters.build();
if (tmp.size() > 1) {
finalFilters = new AndExpression(tmp.get(0), tmp.get(1));
if (tmp.size() > 2) {
for (int i = 2; i < tmp.size(); i++) {
finalFilters = new AndExpression(finalFilters, tmp.get(i));
}
}
} else if (tmp.size() == 1)
finalFilters = tmp.get(0);
else
return null;
return finalFilters;
}
Aggregations