use of org.immutables.criteria.expression.Call in project immutables by immutables.
the class ElasticsearchQueryVisitor method visit.
@Override
public QueryBuilders.QueryBuilder visit(Call call) {
final Operator op = call.operator();
final List<Expression> args = call.arguments();
if (op == OptionalOperators.IS_PRESENT || op == OptionalOperators.IS_ABSENT) {
final String field = pathNaming.name(Visitors.toPath(args.get(0)));
QueryBuilders.QueryBuilder builder = QueryBuilders.existsQuery(field);
if (op == OptionalOperators.IS_ABSENT) {
builder = QueryBuilders.boolQuery().mustNot(builder);
}
return builder;
}
if (op == Operators.AND || op == Operators.OR) {
Preconditions.checkArgument(!args.isEmpty(), "Size should be >=1 for %s but was %s", op, args.size());
final QueryBuilders.BoolQueryBuilder builder = args.stream().map(a -> a.accept(this)).reduce(QueryBuilders.boolQuery(), (a, b) -> op == Operators.AND ? a.must(b) : a.should(b), (a, b) -> b);
return builder;
}
if (op == Operators.NOT) {
Preconditions.checkArgument(args.size() == 1, "Size should be 1 for %s but was %s", op, args.size());
final QueryBuilders.QueryBuilder builder = args.get(0).accept(this);
return QueryBuilders.boolQuery().mustNot(builder);
}
if (op.arity() == Operator.Arity.BINARY) {
return binaryCall(call);
}
throw new UnsupportedOperationException("Don't know how to handle " + call);
}
use of org.immutables.criteria.expression.Call in project immutables by immutables.
the class Combiner method combineAndSimplify.
static Call combineAndSimplify(Operator operator, Expression leftExpr, Expression rightExpr) {
Objects.requireNonNull(operator, "operator");
if (!(leftExpr instanceof Call && rightExpr instanceof Call)) {
// regular call which can't be simplified
return Expressions.binaryCall(operator, leftExpr, rightExpr);
}
Call left = (Call) leftExpr;
Call right = (Call) rightExpr;
if (!(left.operator() == operator && right.operator() == operator)) {
if (left.operator() == operator) {
List<Expression> args = new ArrayList<>(left.arguments());
args.add(right);
return Expressions.call(operator, args);
}
return Expressions.binaryCall(operator, left, right);
}
// combine expressions with same operator (AND / OR) into single expression
// with multiple arguments
List<Expression> args = new ArrayList<>();
args.addAll(left.arguments());
args.addAll(right.arguments());
return Expressions.call(operator, args);
}
use of org.immutables.criteria.expression.Call in project immutables by immutables.
the class Combiner method dnfAnd.
/**
* Combines expression using <a href="https://en.wikipedia.org/wiki/Disjunctive_normal_form">DNF logic</a>
*/
static Combiner dnfAnd() {
Combiner combiner = new Combiner() {
@Override
public Expression combine(Expression left, Expression right) {
if (!(left instanceof Call)) {
// regular and
return and().combine(left, right);
}
Call root = (Call) left;
if (root.operator() == Operators.OR) {
// change right-most child
// a.is(1)
// .or()
// .b.is(2).c.is(3)
List<Expression> args = root.arguments().stream().limit(// skip last
root.arguments().size() - 1).collect(Collectors.toList());
Expression last = root.arguments().get(root.arguments().size() - 1);
Expression newLast = combine(last, right);
args.add(newLast);
return Expressions.or(args);
}
// simple 2 arg AND call
return and().combine(left, right);
}
};
return nullSafe(combiner);
}
use of org.immutables.criteria.expression.Call in project immutables by immutables.
the class AggregationQuery method extractPath.
private static Path extractPath(Expression expression) {
if (expression instanceof Path) {
return (Path) expression;
}
if (expression instanceof Call) {
Call call = (Call) expression;
if (call.operator().arity() != Operator.Arity.UNARY) {
throw new IllegalArgumentException("Expected unary operator but got " + call);
}
Expression arg = call.arguments().get(0);
Preconditions.checkArgument(arg instanceof Path, "expected path got %s", arg);
return (Path) arg;
}
throw new IllegalArgumentException("Can't extract path from " + expression);
}
Aggregations