Search in sources :

Example 16 with LindenType

use of com.xiaomi.linden.thrift.common.LindenType 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 LindenType

use of com.xiaomi.linden.thrift.common.LindenType in project linden by XiaoMi.

the class BQLCompilerAnalyzer method getFieldNameAndType.

private Map.Entry<String, LindenType> getFieldNameAndType(String col) {
    LindenType type = fieldTypeMap.get(col);
    if (type != null) {
        return new AbstractMap.SimpleEntry<>(col, type);
    }
    LindenFieldSchema fieldSchema = LindenUtil.parseDynamicFieldSchema(col);
    return new AbstractMap.SimpleEntry<>(fieldSchema.getName(), fieldSchema.getType());
}
Also used : LindenFieldSchema(com.xiaomi.linden.thrift.common.LindenFieldSchema) LindenType(com.xiaomi.linden.thrift.common.LindenType)

Example 18 with LindenType

use of com.xiaomi.linden.thrift.common.LindenType 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 19 with LindenType

use of com.xiaomi.linden.thrift.common.LindenType in project linden by XiaoMi.

the class RangeFilterConstructor method construct.

@Override
protected Filter construct(LindenFilter lindenFilter, LindenConfig config) throws IOException {
    LindenRangeFilter lindenRangeFilter = lindenFilter.getRangeFilter();
    LindenRange range = lindenRangeFilter.getRange();
    LindenType type = range.getType();
    String start = range.getStartValue();
    String end = range.getEndValue();
    String fieldName = range.getField();
    boolean startClose = range.isStartClosed();
    boolean endClose = range.isEndClosed();
    Filter filter = null;
    switch(type) {
        case STRING:
        case FACET:
            filter = new TermRangeFilter(fieldName, bytesRefVal(start), bytesRefVal(end), startClose, endClose);
            break;
        case INTEGER:
            filter = NumericRangeFilter.newIntRange(fieldName, intVal(start), intVal(end), startClose, endClose);
            break;
        case LONG:
            filter = NumericRangeFilter.newLongRange(fieldName, longVal(start), longVal(end), startClose, endClose);
            break;
        case DOUBLE:
            filter = NumericRangeFilter.newDoubleRange(fieldName, doubleVal(start), doubleVal(end), startClose, endClose);
            break;
        case FLOAT:
            filter = NumericRangeFilter.newFloatRange(fieldName, floatVal(start), floatVal(end), startClose, endClose);
            break;
    }
    return filter;
}
Also used : LindenRangeFilter(com.xiaomi.linden.thrift.common.LindenRangeFilter) LindenRangeFilter(com.xiaomi.linden.thrift.common.LindenRangeFilter) LindenFilter(com.xiaomi.linden.thrift.common.LindenFilter) TermRangeFilter(org.apache.lucene.search.TermRangeFilter) Filter(org.apache.lucene.search.Filter) NumericRangeFilter(org.apache.lucene.search.NumericRangeFilter) LindenType(com.xiaomi.linden.thrift.common.LindenType) LindenRange(com.xiaomi.linden.thrift.common.LindenRange) TermRangeFilter(org.apache.lucene.search.TermRangeFilter)

Example 20 with LindenType

use of com.xiaomi.linden.thrift.common.LindenType in project linden by XiaoMi.

the class RangeQueryConstructor method construct.

@Override
protected Query construct(LindenQuery lindenQuery, LindenConfig config) throws IOException {
    if (lindenQuery.isSetRangeQuery()) {
        LindenRangeQuery lindenRangeQuery = lindenQuery.getRangeQuery();
        LindenRange range = lindenRangeQuery.getRange();
        String fieldName = range.getField();
        LindenType type = range.getType();
        String start = range.getStartValue();
        String end = range.getEndValue();
        boolean startClose = range.isStartClosed();
        boolean endClose = range.isEndClosed();
        Query query = null;
        switch(type) {
            case STRING:
            case FACET:
                query = new TermRangeQuery(fieldName, bytesRefVal(start), bytesRefVal(end), startClose, endClose);
                break;
            case INTEGER:
                query = NumericRangeQuery.newIntRange(fieldName, intVal(start), intVal(end), startClose, endClose);
                break;
            case LONG:
                query = NumericRangeQuery.newLongRange(fieldName, longVal(start), longVal(end), startClose, endClose);
                break;
            case DOUBLE:
                query = NumericRangeQuery.newDoubleRange(fieldName, doubleVal(start), doubleVal(end), startClose, endClose);
                break;
            case FLOAT:
                query = NumericRangeQuery.newFloatRange(fieldName, floatVal(start), floatVal(end), startClose, endClose);
                break;
        }
        return query;
    }
    // todo throw exception.
    return null;
}
Also used : Query(org.apache.lucene.search.Query) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) LindenQuery(com.xiaomi.linden.thrift.common.LindenQuery) LindenRangeQuery(com.xiaomi.linden.thrift.common.LindenRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) LindenRangeQuery(com.xiaomi.linden.thrift.common.LindenRangeQuery) LindenType(com.xiaomi.linden.thrift.common.LindenType) LindenRange(com.xiaomi.linden.thrift.common.LindenRange)

Aggregations

LindenType (com.xiaomi.linden.thrift.common.LindenType)20 AbstractMap (java.util.AbstractMap)14 HashMap (java.util.HashMap)14 Map (java.util.Map)14 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)10 LindenFilter (com.xiaomi.linden.thrift.common.LindenFilter)7 LindenQuery (com.xiaomi.linden.thrift.common.LindenQuery)7 LindenBooleanQueryBuilder (com.xiaomi.linden.thrift.builder.query.LindenBooleanQueryBuilder)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 LindenBooleanFilterBuilder (com.xiaomi.linden.thrift.builder.filter.LindenBooleanFilterBuilder)3 Aggregation (com.xiaomi.linden.thrift.common.Aggregation)2 Bucket (com.xiaomi.linden.thrift.common.Bucket)2 LindenFacetDimAndPath (com.xiaomi.linden.thrift.common.LindenFacetDimAndPath)2 LindenFacetParam (com.xiaomi.linden.thrift.common.LindenFacetParam)2 LindenFieldSchema (com.xiaomi.linden.thrift.common.LindenFieldSchema)2 LindenRange (com.xiaomi.linden.thrift.common.LindenRange)2 NumericRangeQuery (org.apache.lucene.search.NumericRangeQuery)2 Query (org.apache.lucene.search.Query)2 AggregationResult (com.xiaomi.linden.thrift.common.AggregationResult)1