use of com.xiaomi.linden.thrift.common.LindenQuery in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitRange_predicate.
@Override
public void exitRange_predicate(BQLParser.Range_predicateContext ctx) {
// ignore unassigned value
if (ctx.val.PLACEHOLDER() != null) {
return;
}
Object val = valProperty.get(ctx.val);
String col = unescapeColumnName(ctx.column_name());
Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
LindenType type = fieldNameAndType.getValue();
col = fieldNameAndType.getKey();
if (!checkValueType(type, val)) {
throw new ParseCancellationException("Value: " + val + " in RANGE predicate doesn't correspond to field type: " + type);
}
String strVal1;
String strVal2;
boolean isStartClosed = false;
boolean isEndClosed = false;
if (ctx.op.getText().charAt(0) == '>') {
strVal1 = val.toString();
strVal2 = null;
if (">=".equals(ctx.op.getText())) {
isStartClosed = true;
}
} else {
strVal1 = null;
strVal2 = val.toString();
if ("<=".equals(ctx.op.getText())) {
isEndClosed = true;
}
}
if (inQueryWhere) {
LindenQuery query = LindenRangeQueryBuilder.buildRangeQuery(col, type, strVal1, strVal2, isStartClosed, isEndClosed);
queryProperty.put(ctx, query);
} else {
LindenFilter filter = LindenRangeFilterBuilder.buildRangeFilter(col, type, strVal1, strVal2, isStartClosed, isEndClosed);
filterProperty.put(ctx, filter);
}
}
use of com.xiaomi.linden.thrift.common.LindenQuery in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitFlexible_query_predicate.
@Override
public void exitFlexible_query_predicate(BQLParser.Flexible_query_predicateContext ctx) {
String orig = unescapeStringLiteral(ctx.STRING_LITERAL());
LindenScoreModel lindenScoreModel = new LindenScoreModel().setName(ctx.IDENT().getText()).setFunc((String) valProperty.get(ctx.score_model())).setParams((List<LindenInputParam>) valProperty.get(ctx.formal_parameters()));
if (ctx.OVERRIDE() != null) {
lindenScoreModel.setOverride(true);
}
if (ctx.PLUGIN() != null) {
lindenScoreModel.setPlugin(true);
}
LindenFlexibleQuery lindenFlexibleQuery = new LindenFlexibleQuery().setQuery(orig).setFields((List<LindenSearchField>) valProperty.get(ctx.flexible_fields()));
lindenFlexibleQuery.setModel(lindenScoreModel);
if (ctx.fm != null) {
lindenFlexibleQuery.setFullMatch(true);
} else if (ctx.mrt != null && ctx.mrt.PLACEHOLDER() == null) {
double ratio = Double.valueOf(ctx.mrt.getText());
lindenFlexibleQuery.setMatchRatio(ratio);
}
LindenQuery lindenQuery = new LindenQuery();
if (inQueryWhere) {
if (ctx.gi != null) {
lindenFlexibleQuery.setGlobalIDF(true);
if (ctx.gfd != null) {
lindenFlexibleQuery.setGlobalFields((List<LindenSearchField>) valProperty.get(ctx.global_fields()));
}
}
queryProperty.put(ctx, lindenQuery.setFlexQuery(lindenFlexibleQuery));
} else {
filterProperty.put(ctx, LindenQueryFilterBuilder.buildQueryFilter(lindenQuery.setFlexQuery(lindenFlexibleQuery)));
}
}
use of com.xiaomi.linden.thrift.common.LindenQuery 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.common.LindenQuery 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.LindenQuery 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);
}
}
Aggregations