Search in sources :

Example 6 with LindenBooleanQueryBuilder

use of com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder 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);
        }
    }
}
Also used : 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) LindenBooleanFilterBuilder(com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder) LindenType(com.xiaomi.linden.thrift.common.LindenType) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 7 with LindenBooleanQueryBuilder

use of com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder 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 8 with LindenBooleanQueryBuilder

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

Example 9 with LindenBooleanQueryBuilder

use of com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder 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 10 with LindenBooleanQueryBuilder

use of com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder 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

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