Search in sources :

Example 16 with LindenFilter

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());
    }
}
Also used : LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) ArrayList(java.util.ArrayList) LindenType(com.xiaomi.linden.thrift.common.LindenType) AbstractMap(java.util.AbstractMap) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) List(java.util.List) ArrayList(java.util.ArrayList) LindenBooleanClause(com.xiaomi.linden.thrift.common.LindenBooleanClause) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 17 with LindenFilter

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);
    }
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenQueryStringQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenQueryStringQueryBuilder)

Example 18 with LindenFilter

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);
    }
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) ArrayList(java.util.ArrayList)

Example 19 with 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));
    }
}
Also used : LindenDeleteRequest(com.xiaomi.linden.thrift.common.LindenDeleteRequest) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException)

Example 20 with LindenFilter

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);
    }
}
Also used : LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenBooleanQueryBuilder(com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder) LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) ArrayList(java.util.ArrayList)

Aggregations

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