use of com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder in project linden by XiaoMi.
the class TestBQL method testEscapedColumnName.
@Test
public void testEscapedColumnName() {
String bql = "select * from linden where `select` = \"qq\" and id = 211 limit 10, 50";
LindenSearchRequest lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertTrue(lindenRequest.getFilter().isSetBooleanFilter());
LindenBooleanFilter booleanFilter = lindenRequest.getFilter().getBooleanFilter();
Assert.assertEquals(2, booleanFilter.getFiltersSize());
Assert.assertEquals(LindenBooleanClause.MUST, booleanFilter.getFilters().get(0).getClause());
Assert.assertEquals(new LindenTerm("select", "qq"), booleanFilter.getFilters().get(0).getFilter().getTermFilter().getTerm());
bql = "SELECT * FROM linden WHERE `source` <> 'red'";
lindenRequest = compiler.compile(bql).getSearchRequest();
booleanFilter = new LindenBooleanFilter();
LindenFilter termFilter = LindenTermFilterBuilder.buildTermFilter("source", "red");
booleanFilter.addToFilters(new LindenBooleanSubFilter().setFilter(termFilter).setClause(LindenBooleanClause.MUST_NOT));
Assert.assertEquals(booleanFilter, lindenRequest.getFilter().getBooleanFilter());
bql = "select * from linden where `explain` in (1, 2, 3)";
lindenRequest = compiler.compile(bql).getSearchRequest();
LindenBooleanFilterBuilder builder = new LindenBooleanFilterBuilder();
builder.addFilter(LindenTermFilterBuilder.buildTermFilter("explain", "1"), LindenBooleanClause.SHOULD);
builder.addFilter(LindenTermFilterBuilder.buildTermFilter("explain", "2"), LindenBooleanClause.SHOULD);
builder.addFilter(LindenTermFilterBuilder.buildTermFilter("explain", "3"), LindenBooleanClause.SHOULD);
LindenFilter expected = builder.build();
Assert.assertEquals(expected, lindenRequest.getFilter());
bql = "SELECT * FROM linden WHERE `route` BETWEEN 'black' AND \"yellow\"";
lindenRequest = compiler.compile(bql).getSearchRequest();
LindenFilter filter = LindenRangeFilterBuilder.buildRangeFilter("route", LindenType.STRING, "black", "yellow", true, true);
Assert.assertEquals(filter, lindenRequest.getFilter());
bql = "SELECT `delete`, `score` FROM linden where title = 'sed' route by 0, 1, 2";
lindenRequest = compiler.compile(bql).getSearchRequest();
Assert.assertEquals(2, lindenRequest.getSourceFieldsSize());
Assert.assertEquals("delete", lindenRequest.getSourceFields().get(0));
Assert.assertEquals("score", lindenRequest.getSourceFields().get(1));
bql = "SELECT * FROM linden QUERY `FROM` LIKE 'sed*'";
lindenRequest = compiler.compile(bql).getSearchRequest();
LindenQuery query = new LindenQuery();
query.setWildcardQuery(new LindenWildcardQuery().setQuery("sed*").setField("FROM"));
Assert.assertEquals(query, lindenRequest.getQuery());
bql = "SELECT * FROM linden browse by `group`(5)";
lindenRequest = compiler.compile(bql).getSearchRequest();
LindenFacet facetRequest = new LindenFacet();
facetRequest.addToFacetParams(new LindenFacetParam().setTopN(5).setFacetDimAndPath(new LindenFacetDimAndPath().setDim("group")));
Assert.assertEquals(facetRequest, lindenRequest.getFacet());
bql = "SELECT title, rank FROM linden drill down `group`('big')";
lindenRequest = compiler.compile(bql).getSearchRequest();
facetRequest = new LindenFacet();
facetRequest.addToDrillDownDimAndPaths(new LindenFacetDimAndPath().setDim("group").setPath("big"));
Assert.assertEquals(facetRequest, lindenRequest.getFacet());
}
use of com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder 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());
}
use of com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitBetween_predicate.
@Override
public void exitBetween_predicate(BQLParser.Between_predicateContext ctx) {
if (ctx.val1.PLACEHOLDER() != null || ctx.val2.PLACEHOLDER() != null) {
return;
}
String val1 = valProperty.get(ctx.val1).toString();
String val2 = valProperty.get(ctx.val2).toString();
String col = unescapeColumnName(ctx.column_name());
Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
LindenType type = fieldNameAndType.getValue();
col = fieldNameAndType.getKey();
if (!validateValueString(type, val1)) {
throw new ParseCancellationException("Filed value: " + val1 + " doesn't corresponds to field type: " + type);
}
if (!validateValueString(type, val2)) {
throw new ParseCancellationException("Filed value: " + val2 + " doesn't corresponds to field type: " + type);
}
if (inQueryWhere) {
LindenQuery query;
if (ctx.not == null) {
query = LindenRangeQueryBuilder.buildRangeQuery(col, type, val1, val2, true, true);
} else {
LindenQuery query1 = LindenRangeQueryBuilder.buildRangeQuery(col, type, null, val1, false, false);
LindenQuery query2 = LindenRangeQueryBuilder.buildRangeQuery(col, type, val2, null, false, false);
LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
builder.addQuery(query1, LindenBooleanClause.SHOULD);
builder.addQuery(query2, LindenBooleanClause.SHOULD);
query = builder.build();
}
queryProperty.put(ctx, query);
} else {
LindenFilter filter;
if (ctx.not == null) {
filter = LindenRangeFilterBuilder.buildRangeFilter(col, type, val1, val2, true, true);
} else {
LindenFilter filter1 = LindenRangeFilterBuilder.buildRangeFilter(col, type, null, val1, false, false);
LindenFilter filter2 = LindenRangeFilterBuilder.buildRangeFilter(col, type, val2, null, false, false);
LindenBooleanFilterBuilder builder = new LindenBooleanFilterBuilder();
builder.addFilter(filter1, LindenBooleanClause.SHOULD);
builder.addFilter(filter2, LindenBooleanClause.SHOULD);
filter = builder.build();
}
if (filter != null) {
filterProperty.put(ctx, filter);
}
}
}
use of com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitIn_predicate.
@Override
public void exitIn_predicate(BQLParser.In_predicateContext ctx) {
String col = unescapeColumnName(ctx.column_name());
Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
LindenType type = fieldNameAndType.getValue();
col = fieldNameAndType.getKey();
List<Object> values = (List<Object>) valProperty.get(ctx.value_list());
List<Object> excludes = (List<Object>) valProperty.get(ctx.except_clause());
List<Map.Entry<String, LindenBooleanClause>> subClauses = new ArrayList<>();
LindenBooleanClause clause = LindenBooleanClause.SHOULD;
if (ctx.not != null) {
clause = LindenBooleanClause.MUST_NOT;
}
for (int i = 0; values != null && i < values.size(); ++i) {
if (values.get(i) == null) {
continue;
}
subClauses.add(new AbstractMap.SimpleEntry<>(values.get(i).toString(), clause));
}
clause = LindenBooleanClause.MUST_NOT;
if (ctx.not != null) {
clause = LindenBooleanClause.SHOULD;
}
for (int i = 0; excludes != null && i < excludes.size(); ++i) {
if (excludes.get(i) == null) {
continue;
}
subClauses.add(new AbstractMap.SimpleEntry<>(excludes.get(i).toString(), clause));
}
if (subClauses.size() == 0) {
return;
}
if (inQueryWhere) {
LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
for (Map.Entry<String, LindenBooleanClause> clauseEntry : subClauses) {
LindenQuery query;
switch(type) {
case STRING:
case FACET:
query = LindenQueryBuilder.buildTermQuery(col, clauseEntry.getKey());
break;
case LONG:
case INTEGER:
case FLOAT:
case DOUBLE:
query = LindenRangeQueryBuilder.buildRangeQuery(col, type, clauseEntry.getKey(), clauseEntry.getKey(), true, true);
break;
default:
throw new ParseCancellationException("IN predicate doesn't support this type " + type);
}
builder.addQuery(query, clauseEntry.getValue());
}
queryProperty.put(ctx, builder.build());
} else {
LindenBooleanFilterBuilder builder = new LindenBooleanFilterBuilder();
for (Map.Entry<String, LindenBooleanClause> clauseEntry : subClauses) {
LindenFilter filter;
switch(type) {
case STRING:
case FACET:
filter = LindenTermFilterBuilder.buildTermFilter(col, clauseEntry.getKey());
break;
case LONG:
case INTEGER:
case FLOAT:
case DOUBLE:
filter = LindenRangeFilterBuilder.buildRangeFilter(col, type, clauseEntry.getKey(), clauseEntry.getKey(), true, true);
break;
default:
throw new ParseCancellationException("IN predicate doesn't support this type " + type);
}
builder.addFilter(filter, clauseEntry.getValue());
}
filterProperty.put(ctx, builder.build());
}
}
use of com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitNot_equal_predicate.
@Override
public void exitNot_equal_predicate(BQLParser.Not_equal_predicateContext ctx) {
if (ctx.value().PLACEHOLDER() != null) {
return;
}
String value = valProperty.get(ctx.value()).toString();
String col = unescapeColumnName(ctx.column_name());
Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
LindenType type = fieldNameAndType.getValue();
col = fieldNameAndType.getKey();
if (!validateValueString(type, value)) {
throw new ParseCancellationException("Filed value: " + value + " doesn't corresponds to field type: " + type);
}
if (inQueryWhere) {
LindenBooleanQueryBuilder builder = new LindenBooleanQueryBuilder();
builder.addQuery(LindenQueryBuilder.buildMatchAllQuery(), LindenBooleanClause.MUST);
switch(type) {
case STRING:
case FACET:
builder.addQuery(LindenRangeQueryBuilder.buildTermQuery(col, value), LindenBooleanClause.MUST_NOT);
break;
case INTEGER:
case LONG:
case FLOAT:
case DOUBLE:
builder.addQuery(LindenRangeQueryBuilder.buildRangeQuery(col, type, value, value, true, true), LindenBooleanClause.MUST_NOT);
break;
default:
throw new ParseCancellationException("NOT EQUAL predicate doesn't support this type " + type);
}
queryProperty.put(ctx, builder.build());
} else {
LindenBooleanFilterBuilder builder = new LindenBooleanFilterBuilder();
switch(type) {
case STRING:
case FACET:
builder.addFilter(LindenTermFilterBuilder.buildTermFilter(col, value), LindenBooleanClause.MUST_NOT);
break;
case INTEGER:
case LONG:
case FLOAT:
case DOUBLE:
builder.addFilter(LindenRangeFilterBuilder.buildRangeFilter(col, type, value, value, true, true), LindenBooleanClause.MUST_NOT);
break;
default:
throw new ParseCancellationException("NOT EQUAL predicate doesn't support this type " + type);
}
filterProperty.put(ctx, builder.build());
}
}
Aggregations