Skip to content

Commit

Permalink
Fix inequality operators for string types in mongodb connector
Browse files Browse the repository at this point in the history
The values weren't being properly translated from the Slice
representation to the corresponding String representation.
  • Loading branch information
visualage authored and martint committed Dec 18, 2017
1 parent 02868fe commit 7a0b519
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Expand Up @@ -277,17 +277,17 @@ private static Document buildPredicate(MongoColumnHandle column, Domain domain)
List<Document> disjuncts = new ArrayList<>();
for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
if (range.isSingleValue()) {
singleValues.add(range.getSingleValue());
singleValues.add(translateValue(range.getSingleValue(), type));
}
else {
Document rangeConjuncts = new Document();
if (!range.getLow().isLowerUnbounded()) {
switch (range.getLow().getBound()) {
case ABOVE:
rangeConjuncts.put(GT_OP, range.getLow().getValue());
rangeConjuncts.put(GT_OP, translateValue(range.getLow().getValue(), type));
break;
case EXACTLY:
rangeConjuncts.put(GTE_OP, range.getLow().getValue());
rangeConjuncts.put(GTE_OP, translateValue(range.getLow().getValue(), type));
break;
case BELOW:
throw new IllegalArgumentException("Low Marker should never use BELOW bound: " + range);
Expand All @@ -300,10 +300,10 @@ private static Document buildPredicate(MongoColumnHandle column, Domain domain)
case ABOVE:
throw new IllegalArgumentException("High Marker should never use ABOVE bound: " + range);
case EXACTLY:
rangeConjuncts.put(LTE_OP, range.getHigh().getValue());
rangeConjuncts.put(LTE_OP, translateValue(range.getHigh().getValue(), type));
break;
case BELOW:
rangeConjuncts.put(LT_OP, range.getHigh().getValue());
rangeConjuncts.put(LT_OP, translateValue(range.getHigh().getValue(), type));
break;
default:
throw new AssertionError("Unhandled bound: " + range.getHigh().getBound());
Expand All @@ -317,12 +317,10 @@ private static Document buildPredicate(MongoColumnHandle column, Domain domain)

// Add back all of the possible single values either as an equality or an IN predicate
if (singleValues.size() == 1) {
disjuncts.add(documentOf(EQ_OP, translateValue(singleValues.get(0), type)));
disjuncts.add(documentOf(EQ_OP, singleValues.get(0)));
}
else if (singleValues.size() > 1) {
disjuncts.add(documentOf(IN_OP, singleValues.stream()
.map(value -> translateValue(value, type))
.collect(toList())));
disjuncts.add(documentOf(IN_OP, singleValues));
}

if (domain.isNullAllowed()) {
Expand Down
Expand Up @@ -24,6 +24,7 @@

import static com.facebook.presto.spi.predicate.Range.equal;
import static com.facebook.presto.spi.predicate.Range.greaterThan;
import static com.facebook.presto.spi.predicate.Range.greaterThanOrEqual;
import static com.facebook.presto.spi.predicate.Range.lessThan;
import static com.facebook.presto.spi.predicate.Range.range;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
Expand Down Expand Up @@ -51,6 +52,20 @@ public void testBuildQuery()
assertEquals(query, expected);
}

@Test
public void testBuildQueryStringType()
{
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
COL1, Domain.create(ValueSet.ofRanges(range(createUnboundedVarcharType(), utf8Slice("hello"), false, utf8Slice("world"), true)), false),
COL2, Domain.create(ValueSet.ofRanges(greaterThanOrEqual(createUnboundedVarcharType(), utf8Slice("a value"))), false)));

Document query = MongoSession.buildQuery(tupleDomain);
Document expected = new Document()
.append(COL1.getName(), new Document().append("$gt", "hello").append("$lte", "world"))
.append(COL2.getName(), new Document("$gte", "a value"));
assertEquals(query, expected);
}

@Test
public void testBuildQueryIn()
{
Expand Down

0 comments on commit 7a0b519

Please sign in to comment.