Search in sources :

Example 1 with LindenBooleanQueryBuilder

use of com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder in project linden by XiaoMi.

the class TestBQL method testRangePred.

@Test
public void testRangePred() throws Exception {
    String bql = "SELECT * FROM linden WHERE id > 1999";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter expectedFilter = LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "1999", null, false, false);
    Assert.assertEquals(expectedFilter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden  WHERE id <= 2000";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    expectedFilter = LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, null, "2000", false, true);
    Assert.assertEquals(expectedFilter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE id <= $e";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertEquals(null, lindenRequest.getFilter());
    bql = "SELECT * FROM linden by id <= 2000";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query = LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, null, "2000", false, true);
    Assert.assertEquals(query, lindenRequest.getQuery());
    bql = "SELECT * FROM linden by id <= 2000 and id > 1000 subBoost by 0.5";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query1 = LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, null, "2000", false, true);
    LindenQuery query2 = LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "1000", null, false, false);
    query2.setBoost(0.5);
    LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
    builder.addQuery(query1, LindenBooleanClause.MUST);
    builder.addQuery(query2, LindenBooleanClause.MUST);
    Assert.assertEquals(builder.build(), lindenRequest.getQuery());
    bql = "SELECT * FROM linden WHERE color > 're''d'";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    expectedFilter = LindenRangeFilterBuilder.buildRangeFilter("color", LindenType.FACET, "re'd", null, false, false);
    Assert.assertEquals(expectedFilter, lindenRequest.getFilter());
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 2 with LindenBooleanQueryBuilder

use of com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder in project linden by XiaoMi.

the class TestBQL method testLikePredicate.

@Test
public void testLikePredicate() throws Exception {
    String bql = "SELECT * FROM linden WHERE title LIKE 's?d*'";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter filter = new LindenFilter().setQueryFilter(new LindenQueryFilter().setQuery(new LindenQuery().setWildcardQuery(new LindenWildcardQuery().setQuery("s?d*").setField("title"))));
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title NOT LIKE 'bl*'";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query = new LindenQuery();
    query.setWildcardQuery(new LindenWildcardQuery().setQuery("bl*").setField("title"));
    filter = new LindenFilter().setQueryFilter(new LindenQueryFilter().setQuery(query));
    LindenBooleanFilter booleanFilter = new LindenBooleanFilter();
    booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(filter).setClause(LindenBooleanClause.MUST_NOT));
    Assert.assertEquals(booleanFilter, lindenRequest.getFilter().getBooleanFilter());
    bql = "SELECT * FROM linden WHERE title NOT LIKE $title";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertEquals(null, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title LIKE $title";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertEquals(null, lindenRequest.getFilter());
    bql = "SELECT * FROM linden BY title LIKE 'sed*'";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    query = new LindenQuery();
    query.setWildcardQuery(new LindenWildcardQuery().setQuery("sed*").setField("title"));
    Assert.assertEquals(query, lindenRequest.getQuery());
    bql = "SELECT * FROM linden BY title NOT LIKE  'sed*'";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
    builder.addQuery(LindenRangeQueryBuilder.buildMatchAllQuery(), LindenBooleanClause.MUST);
    builder.addQuery(new LindenQuery().setWildcardQuery(new LindenWildcardQuery().setQuery("sed*").setField("title")), LindenBooleanClause.MUST_NOT);
    Assert.assertEquals(builder.build(), lindenRequest.getQuery());
    bql = "SELECT * FROM linden BY title LIKE 'sed*' subBoost by 3";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    query = new LindenQuery();
    query.setWildcardQuery(new LindenWildcardQuery().setQuery("sed*").setField("title"));
    query.setBoost(3);
    Assert.assertEquals(query, lindenRequest.getQuery());
}
Also used : LindenQueryFilter(com.xiaomi.linden.thrift.common.LindenQueryFilter) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenWildcardQuery(com.xiaomi.linden.thrift.common.LindenWildcardQuery) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanFilter(com.xiaomi.linden.thrift.common.LindenBooleanFilter) LindenBooleanSubFilter(com.xiaomi.linden.thrift.common.LindenBooleanSubFilter) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 3 with LindenBooleanQueryBuilder

use of com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder in project linden by XiaoMi.

the class TestBQL method testBetweenPred.

@Test
public void testBetweenPred() throws Exception {
    String bql = "SELECT * FROM linden WHERE id BETWEEN 2000 AND 2001";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter filter = LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "2000", "2001", true, true);
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title BETWEEN 'black' AND \"yellow\"";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filter = LindenRangeFilterBuilder.buildRangeFilter("title", LindenType.STRING, "black", "yellow", true, true);
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE id NOT BETWEEN 2000 AND 2002";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenFilter filter1 = LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, null, "2000", false, false);
    LindenFilter filter2 = LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "2002", null, false, false);
    LindenBooleanFilter booleanFilter = new LindenBooleanFilter();
    booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(filter1).setClause(LindenBooleanClause.SHOULD));
    booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(filter2).setClause(LindenBooleanClause.SHOULD));
    LindenFilter expectedFilter = new LindenFilter().setBooleanFilter(booleanFilter);
    Assert.assertEquals(expectedFilter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title BETWEEN $a AND $b";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    Assert.assertEquals(null, lindenRequest.getFilter());
    bql = "SELECT * FROM linden WHERE title BETWEEN 'black' AND \"yellow\"";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filter = LindenRangeFilterBuilder.buildRangeFilter("title", LindenType.STRING, "black", "yellow", true, true);
    Assert.assertEquals(filter, lindenRequest.getFilter());
    bql = "SELECT * FROM linden by id NOT BETWEEN 2000 AND 2002";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query1 = LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, null, "2000", false, false);
    LindenQuery query2 = LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "2002", null, false, false);
    LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
    builder.addQuery(query1, LindenBooleanClause.SHOULD);
    builder.addQuery(query2, LindenBooleanClause.SHOULD);
    Assert.assertEquals(builder.build(), lindenRequest.getQuery());
    bql = "SELECT * FROM linden by title BETWEEN 'black' AND \"yel\"\"low\"";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenQuery query = LindenRangeQueryBuilder.buildRangeQuery("title", LindenType.STRING, "black", "yel\"low", true, true);
    Assert.assertEquals(query, lindenRequest.getQuery());
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenBooleanFilter(com.xiaomi.linden.thrift.common.LindenBooleanFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanSubFilter(com.xiaomi.linden.thrift.common.LindenBooleanSubFilter) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 4 with LindenBooleanQueryBuilder

use of com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder in project linden by XiaoMi.

the class TestBQL method testInPredicate.

@Test
public void testInPredicate() {
    String bql = "select * from linden where id in (1, 2, 3)";
    LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenBooleanFilterBuilder filterBuilder = new LindenBooleanFilterBuilder();
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "1", "1", true, true), LindenBooleanClause.SHOULD);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.SHOULD);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "3", "3", true, true), LindenBooleanClause.SHOULD);
    LindenFilter expected = filterBuilder.build();
    Assert.assertEquals(expected, lindenRequest.getFilter());
    bql = "select * from linden where id not in (1, 2, 3)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filterBuilder = new LindenBooleanFilterBuilder();
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "1", "1", true, true), LindenBooleanClause.MUST_NOT);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.MUST_NOT);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "3", "3", true, true), LindenBooleanClause.MUST_NOT);
    expected = filterBuilder.build();
    Assert.assertEquals(expected, lindenRequest.getFilter());
    bql = "select * from linden where id in (1) except (2, 3)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filterBuilder = new LindenBooleanFilterBuilder();
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "1", "1", true, true), LindenBooleanClause.SHOULD);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.MUST_NOT);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("id", LindenType.LONG, "3", "3", true, true), LindenBooleanClause.MUST_NOT);
    expected = filterBuilder.build();
    Assert.assertEquals(expected, lindenRequest.getFilter());
    bql = "select * from linden where dy.long in (1) except (2, 3)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    filterBuilder = new LindenBooleanFilterBuilder();
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("dy", LindenType.LONG, "1", "1", true, true), LindenBooleanClause.SHOULD);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("dy", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.MUST_NOT);
    filterBuilder.addFilter(LindenRangeFilterBuilder.buildRangeFilter("dy", LindenType.LONG, "3", "3", true, true), LindenBooleanClause.MUST_NOT);
    expected = filterBuilder.build();
    Assert.assertEquals(expected, lindenRequest.getFilter());
    bql = "select * from linden by id in (1) except (2, 3)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    LindenBooleanQueryBuilder queryBuilder = new LindenBooleanQueryBuilder();
    queryBuilder.addQuery(LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "1", "1", true, true), LindenBooleanClause.SHOULD);
    queryBuilder.addQuery(LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.MUST_NOT);
    queryBuilder.addQuery(LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "3", "3", true, true), LindenBooleanClause.MUST_NOT);
    LindenQuery query = queryBuilder.build();
    Assert.assertEquals(query, lindenRequest.getQuery());
    bql = "select * from linden by id in ($a) except (2, $b)";
    lindenRequest = compiler.compile(bql).getSearchRequest();
    queryBuilder = new LindenBooleanQueryBuilder();
    queryBuilder.addQuery(LindenRangeQueryBuilder.buildRangeQuery("id", LindenType.LONG, "2", "2", true, true), LindenBooleanClause.MUST_NOT);
    query = queryBuilder.build();
    Assert.assertEquals(query, lindenRequest.getQuery());
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenSearchRequest(com.xiaomi.linden.thrift.common.LindenSearchRequest) Test(org.junit.Test)

Example 5 with LindenBooleanQueryBuilder

use of com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitLike_predicate.

@Override
public void exitLike_predicate(BQLParser.Like_predicateContext ctx) {
    if (ctx.PLACEHOLDER() != null) {
        return;
    }
    String col = unescapeColumnName(ctx.column_name());
    Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
    LindenType type = fieldNameAndType.getValue();
    if (type != LindenType.STRING && type != LindenType.FACET) {
        throw new ParseCancellationException(new SemanticException(ctx.column_name(), "Non-string type column \"" + col + "\" can not be used in LIKE predicates."));
    }
    col = fieldNameAndType.getKey();
    String likeString = unescapeStringLiteral(ctx.STRING_LITERAL());
    LindenWildcardQuery wildcardQuery = new LindenWildcardQuery().setField(col).setQuery(likeString);
    if (inQueryWhere) {
        if (ctx.NOT() != null) {
            LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
            builder.addQuery(LindenRangeQueryBuilder.buildMatchAllQuery(), LindenBooleanClause.MUST);
            builder.addQuery(new LindenQuery().setWildcardQuery(wildcardQuery), LindenBooleanClause.MUST_NOT);
            queryProperty.put(ctx, builder.build());
        } else {
            queryProperty.put(ctx, new LindenQuery().setWildcardQuery(wildcardQuery));
        }
    } else {
        LindenFilter filter = new LindenFilter().setQueryFilter(new LindenQueryFilter().setQuery(new LindenQuery().setWildcardQuery(wildcardQuery)));
        if (ctx.NOT() != null) {
            LindenBooleanFilter booleanFilter = new LindenBooleanFilter();
            booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(filter).setClause(LindenBooleanClause.MUST_NOT));
            filter = new LindenFilter().setBooleanFilter(booleanFilter);
        }
        filterProperty.put(ctx, filter);
    }
}
Also used : LindenQueryFilter(com.xiaomi.linden.thrift.common.LindenQueryFilter) LindenWildcardQuery(com.xiaomi.linden.thrift.common.LindenWildcardQuery) LindenBooleanFilter(com.xiaomi.linden.thrift.common.LindenBooleanFilter) LindenType(com.xiaomi.linden.thrift.common.LindenType) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LindenBooleanSubFilter(com.xiaomi.linden.thrift.common.LindenBooleanSubFilter) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Aggregations

LindenBooleanQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder)11 LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)10 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)9 LindenBooleanFilterBuilder (com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder)6 LindenSearchRequest (com.xiaomi.linden.thrift.common.LindenSearchRequest)5 Test (org.junit.Test)5 LindenBooleanFilter (com.xiaomi.linden.thrift.common.LindenBooleanFilter)4 LindenBooleanSubFilter (com.xiaomi.linden.thrift.common.LindenBooleanSubFilter)4 LindenType (com.xiaomi.linden.thrift.common.LindenType)4 AbstractMap (java.util.AbstractMap)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)4 ArrayList (java.util.ArrayList)3 LindenQueryFilter (com.xiaomi.linden.thrift.common.LindenQueryFilter)2 LindenWildcardQuery (com.xiaomi.linden.thrift.common.LindenWildcardQuery)2 LindenBooleanClause (com.xiaomi.linden.thrift.common.LindenBooleanClause)1 List (java.util.List)1