/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.mongodb;
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.predicate.Domain;
import com.facebook.presto.spi.predicate.TupleDomain;
import com.facebook.presto.spi.predicate.ValueSet;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.bson.Document;
import org.testng.annotations.Test;
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.lessThan;
import static com.facebook.presto.spi.predicate.Range.range;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType;
import static io.airlift.slice.Slices.utf8Slice;
import static java.util.Arrays.asList;
import static org.testng.Assert.assertEquals;
public class TestMongoSession
{
private static final MongoColumnHandle COL1 = new MongoColumnHandle("col1", BIGINT, false);
private static final MongoColumnHandle COL2 = new MongoColumnHandle("col2", createUnboundedVarcharType(), false);
@Test
public void testBuildQuery()
{
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
COL1, Domain.create(ValueSet.ofRanges(range(BIGINT, 100L, false, 200L, true)), false),
COL2, Domain.singleValue(createUnboundedVarcharType(), utf8Slice("a value"))
));
Document query = MongoSession.buildQuery(tupleDomain);
Document expected = new Document()
.append(COL1.getName(), new Document().append("$gt", 100L).append("$lte", 200L))
.append(COL2.getName(), new Document("$eq", "a value"));
assertEquals(query, expected);
}
@Test
public void testBuildQueryIn()
{
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
COL2, Domain.create(ValueSet.ofRanges(equal(createUnboundedVarcharType(), utf8Slice("hello")), equal(createUnboundedVarcharType(), utf8Slice("world"))), false)
));
Document query = MongoSession.buildQuery(tupleDomain);
Document expected = new Document(COL2.getName(), new Document("$in", ImmutableList.of("hello", "world")));
assertEquals(query, expected);
}
@Test
public void testBuildQueryOr()
{
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
COL1, Domain.create(ValueSet.ofRanges(lessThan(BIGINT, 100L), greaterThan(BIGINT, 200L)), false)
));
Document query = MongoSession.buildQuery(tupleDomain);
Document expected = new Document("$or", asList(
new Document(COL1.getName(), new Document("$lt", 100L)),
new Document(COL1.getName(), new Document("$gt", 200L))
));
assertEquals(query, expected);
}
@Test
public void testBuildQueryNull()
{
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
COL1, Domain.create(ValueSet.ofRanges(greaterThan(BIGINT, 200L)), true)
));
Document query = MongoSession.buildQuery(tupleDomain);
Document expected = new Document("$or", asList(
new Document(COL1.getName(), new Document("$gt", 200L)),
new Document(COL1.getName(), new Document("$exists", true).append("$eq", null))
));
assertEquals(query, expected);
}
}