use of com.xiaomi.linden.thrift.common.LindenFilter 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.common.LindenFilter in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitQuery_predicate.
@Override
public void exitQuery_predicate(BQLParser.Query_predicateContext ctx) {
String orig = unescapeStringLiteral(ctx.STRING_LITERAL());
LindenQueryStringQueryBuilder builder = new LindenQueryStringQueryBuilder().setQuery(orig);
if (ctx.disable_coord != null) {
builder.setDisableCoord((Boolean) valProperty.get(ctx.disable_coord));
}
if (ctx.AND() != null) {
builder.setOperator(Operator.AND);
}
LindenQuery stringQuery = builder.build();
if (inQueryWhere) {
queryProperty.put(ctx, stringQuery);
} else {
LindenFilter filter = LindenQueryFilterBuilder.buildQueryFilter(stringQuery);
filterProperty.put(ctx, filter);
}
}
use of com.xiaomi.linden.thrift.common.LindenFilter in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitSearch_expr.
@Override
public void exitSearch_expr(BQLParser.Search_exprContext ctx) {
if (inQueryWhere) {
List<LindenQuery> lindenQueries = new ArrayList<>();
for (BQLParser.Term_exprContext f : ctx.term_expr()) {
LindenQuery query = queryProperty.get(f);
if (query == null) {
continue;
}
lindenQueries.add(query);
}
if (lindenQueries.size() == 0) {
return;
}
if (lindenQueries.size() == 1) {
queryProperty.put(ctx, lindenQueries.get(0));
return;
}
boolean disableCoord = ctx.disable_coord == null ? false : (boolean) valProperty.get(ctx.disable_coord);
LindenBooleanQueryBuilder booleanQueryBuilder = new LindenBooleanQueryBuilder();
booleanQueryBuilder.setDisableCoord(disableCoord);
if (ctx.boost_by != null && ctx.boost_by.numeric_value().PLACEHOLDER() == null) {
booleanQueryBuilder.setBoost(Double.valueOf(ctx.boost_by.numeric_value().getText()));
}
for (int i = 0; i < lindenQueries.size(); ++i) {
// term_op indicates first clause is must
if (ctx.term_op != null && i == 0) {
booleanQueryBuilder.addQuery(lindenQueries.get(i), LindenBooleanClause.MUST);
} else {
booleanQueryBuilder.addQuery(lindenQueries.get(i), LindenBooleanClause.SHOULD);
}
}
LindenQuery lindenQuery = booleanQueryBuilder.build();
queryProperty.put(ctx, lindenQuery);
} else {
List<LindenFilter> lindenFilters = new ArrayList<>();
for (BQLParser.Term_exprContext f : ctx.term_expr()) {
LindenFilter filter = filterProperty.get(f);
if (filter == null) {
continue;
}
lindenFilters.add(filter);
}
if (lindenFilters.size() == 0) {
return;
}
if (lindenFilters.size() == 1) {
filterProperty.put(ctx, lindenFilters.get(0));
return;
}
LindenBooleanFilterBuilder booleanFilterBuilder = new LindenBooleanFilterBuilder();
for (int i = 0; i < lindenFilters.size(); ++i) {
// term_op indicates first clause is must
if (ctx.term_op != null && i == 0) {
booleanFilterBuilder.addFilter(lindenFilters.get(i), LindenBooleanClause.MUST);
} else {
booleanFilterBuilder.addFilter(lindenFilters.get(i), LindenBooleanClause.SHOULD);
}
}
LindenFilter lindenFilter = booleanFilterBuilder.build();
filterProperty.put(ctx, lindenFilter);
}
}
use of com.xiaomi.linden.thrift.common.LindenFilter in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitDelete_stmt.
@Override
public void exitDelete_stmt(BQLParser.Delete_stmtContext ctx) {
deleteRequest = new LindenDeleteRequest();
if (ctx.dw != null) {
LindenFilter filter = filterProperty.get(ctx.dw);
if (filter == null) {
throw new ParseCancellationException(new SemanticException(ctx, "Filter parse failed"));
}
LindenQuery query = LindenQueryBuilder.buildMatchAllQuery();
query = LindenQueryBuilder.buildFilteredQuery(query, filter);
deleteRequest.setQuery(query);
} else {
deleteRequest.setQuery(LindenQueryBuilder.buildMatchAllQuery());
}
if (ctx.route_param != null) {
deleteRequest.setRouteParam((SearchRouteParam) valProperty.get(ctx.route_param));
}
if (ctx.indexes != null) {
deleteRequest.setIndexNames((List<String>) valProperty.get(ctx.indexes));
}
}
use of com.xiaomi.linden.thrift.common.LindenFilter in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitTerm_expr.
@Override
public void exitTerm_expr(BQLParser.Term_exprContext ctx) {
if (inQueryWhere) {
List<LindenQuery> lindenQueries = new ArrayList<>();
for (BQLParser.Factor_exprContext f : ctx.factor_expr()) {
LindenQuery query = queryProperty.get(f);
if (query == null) {
continue;
}
lindenQueries.add(query);
}
if (lindenQueries.size() == 0) {
return;
}
if (lindenQueries.size() == 1) {
queryProperty.put(ctx, lindenQueries.get(0));
return;
}
boolean disableCoord = ctx.disable_coord == null ? false : (boolean) valProperty.get(ctx.disable_coord);
LindenBooleanQueryBuilder booleanQueryBuilder = new LindenBooleanQueryBuilder();
booleanQueryBuilder.setDisableCoord(disableCoord);
if (ctx.boost_by != null && ctx.boost_by.numeric_value().PLACEHOLDER() == null) {
booleanQueryBuilder.setBoost(Double.valueOf(ctx.boost_by.numeric_value().getText()));
}
for (int i = 0; i < lindenQueries.size(); ++i) {
booleanQueryBuilder.addQuery(lindenQueries.get(i), LindenBooleanClause.MUST);
}
LindenQuery lindenQuery = booleanQueryBuilder.build();
queryProperty.put(ctx, lindenQuery);
} else {
List<LindenFilter> lindenFilters = new ArrayList<>();
for (BQLParser.Factor_exprContext f : ctx.factor_expr()) {
LindenFilter filter = filterProperty.get(f);
if (filter == null) {
continue;
}
lindenFilters.add(filter);
}
if (lindenFilters.size() == 0) {
return;
}
if (lindenFilters.size() == 1) {
filterProperty.put(ctx, lindenFilters.get(0));
return;
}
LindenBooleanFilterBuilder booleanFilterBuilder = new LindenBooleanFilterBuilder();
for (int i = 0; i < lindenFilters.size(); ++i) {
booleanFilterBuilder.addFilter(lindenFilters.get(i), LindenBooleanClause.MUST);
}
LindenFilter lindenFilter = booleanFilterBuilder.build();
filterProperty.put(ctx, lindenFilter);
}
}
Aggregations