Search in sources :

Example 11 with Expression

use of org.immutables.criteria.expression.Expression in project immutables by immutables.

the class AggregateQueryBuilder method jsonQuery.

ObjectNode jsonQuery() {
    if (!query.groupBy().isEmpty() && query.offset().isPresent()) {
        String message = "Currently ES doesn't support generic pagination " + "with aggregations. You can still use LIMIT keyword (without OFFSET). " + "For more details see https://github.com/elastic/elasticsearch/issues/4915";
        throw new UnsupportedOperationException(message);
    }
    final ObjectNode json = nodeFactory.objectNode();
    json.put("_source", false);
    json.put("size", 0);
    // avoid fetch phase
    json.put("stored_fields", "_none_");
    query.filter().ifPresent(f -> json.set("query", Elasticsearch.constantScoreQuery(mapper, pathNaming, idPredicate).convert(f)));
    // due to ES aggregation format. fields in "order by" clause should go first
    // if "order by" is missing. order in "group by" is un-important
    final Set<Expression> orderedGroupBy = new LinkedHashSet<>();
    orderedGroupBy.addAll(query.collations().stream().map(Collation::expression).collect(Collectors.toList()));
    orderedGroupBy.addAll(query.groupBy());
    // construct nested aggregations node(s)
    ObjectNode parent = json.with(AGGREGATIONS);
    for (Expression expr : orderedGroupBy) {
        final String name = ((Path) expr).toStringPath();
        final String aggName = naming.apply(expr);
        final ObjectNode section = parent.with(aggName);
        final ObjectNode terms = section.with("terms");
        terms.put("field", name);
        mapping.missingValueFor(name).ifPresent(m -> {
            // expose missing terms. each type has a different missing value
            terms.set("missing", m);
        });
        query.limit().ifPresent(limit -> terms.put("size", limit));
        query.collations().stream().filter(c -> c.path().toStringPath().equals(name)).findAny().ifPresent(col -> terms.with("order").put("_key", col.direction().isAscending() ? "asc" : "desc"));
        parent = section.with(AGGREGATIONS);
    }
    for (Expression expr : query.projections()) {
        if (Visitors.isAggregationCall(expr)) {
            Call call = Visitors.toCall(expr);
            ObjectNode agg = nodeFactory.objectNode();
            String field = ((Path) call.arguments().get(0)).toStringPath();
            agg.with(toElasticAggregate(call)).put("field", field);
            parent.set(naming.apply(call), agg);
        }
    }
    // cleanup json. remove empty "aggregations" element (if empty)
    removeEmptyAggregation(json);
    return json;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Path(org.immutables.criteria.expression.Path) Call(org.immutables.criteria.expression.Call) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Expression(org.immutables.criteria.expression.Expression) Collation(org.immutables.criteria.expression.Collation)

Example 12 with Expression

use of org.immutables.criteria.expression.Expression 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);
}
Also used : Operator(org.immutables.criteria.expression.Operator) Operators(org.immutables.criteria.expression.Operators) ImmutableSet(com.google.common.collect.ImmutableSet) Path(org.immutables.criteria.expression.Path) OptionalOperators(org.immutables.criteria.expression.OptionalOperators) PathNaming(org.immutables.criteria.backend.PathNaming) Predicate(java.util.function.Predicate) AbstractExpressionVisitor(org.immutables.criteria.expression.AbstractExpressionVisitor) Expression(org.immutables.criteria.expression.Expression) Collection(java.util.Collection) Call(org.immutables.criteria.expression.Call) StringOperators(org.immutables.criteria.expression.StringOperators) Objects(java.util.Objects) Visitors(org.immutables.criteria.expression.Visitors) List(java.util.List) Preconditions(com.google.common.base.Preconditions) Pattern(java.util.regex.Pattern) Operator(org.immutables.criteria.expression.Operator) Collections(java.util.Collections) ComparableOperators(org.immutables.criteria.expression.ComparableOperators) Expression(org.immutables.criteria.expression.Expression)

Example 13 with Expression

use of org.immutables.criteria.expression.Expression in project immutables by immutables.

the class Elasticsearch method idPredicate.

/**
 * Predicate which checks if a path represents entity key (id). Used to generate {@code ids}
 * type of queries.
 */
static Predicate<Path> idPredicate(KeyExtractor.KeyMetadata metadata) {
    Objects.requireNonNull(metadata, "metadata");
    Predicate<Path> alwaysFalse = p -> false;
    if (!(metadata.isKeyDefined() && metadata.isExpression())) {
        return alwaysFalse;
    }
    if (metadata.keys().size() != 1) {
        return alwaysFalse;
    }
    Expression expression = Iterables.getOnlyElement(metadata.keys());
    return Visitors.maybePath(expression).map(p -> ((Predicate<Path>) p::equals)).orElse(alwaysFalse);
}
Also used : Path(org.immutables.criteria.expression.Path) Objects(java.util.Objects) KeyExtractor(org.immutables.criteria.backend.KeyExtractor) Iterables(com.google.common.collect.Iterables) Visitors(org.immutables.criteria.expression.Visitors) Path(org.immutables.criteria.expression.Path) ExpressionConverter(org.immutables.criteria.expression.ExpressionConverter) PathNaming(org.immutables.criteria.backend.PathNaming) Predicate(java.util.function.Predicate) Expression(org.immutables.criteria.expression.Expression) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Expression(org.immutables.criteria.expression.Expression)

Example 14 with Expression

use of org.immutables.criteria.expression.Expression in project immutables by immutables.

the class GeodeQueryVisitor method visit.

@Override
public Oql visit(Call call) {
    final Operator op = call.operator();
    final List<Expression> args = call.arguments();
    if (op == Operators.NOT_IN || op == IterableOperators.NOT_EMPTY) {
        // convert "foo not in [1, 2, 3]" into "not (foo in [1, 2, 3])"
        return visit(Expressions.not(Expressions.call(inverseOp(op), call.arguments())));
    }
    if (op == Operators.AND || op == Operators.OR) {
        Preconditions.checkArgument(!args.isEmpty(), "Size should be >=1 for %s but was %s", op, args.size());
        final String join = ") " + op.name() + " (";
        final String newOql = "(" + args.stream().map(a -> a.accept(this)).map(Oql::oql).collect(Collectors.joining(join)) + ")";
        return new Oql(variables, newOql);
    }
    if (op.arity() == Operator.Arity.BINARY) {
        return binaryOperator(call);
    }
    if (op.arity() == Operator.Arity.UNARY) {
        return unaryOperator(call);
    }
    throw new UnsupportedOperationException("Don't know how to handle " + call);
}
Also used : Operator(org.immutables.criteria.expression.Operator) Expression(org.immutables.criteria.expression.Expression)

Example 15 with Expression

use of org.immutables.criteria.expression.Expression in project immutables by immutables.

the class GeodeQueryVisitor method unaryOperator.

/**
 * Operator with single operator: {@code NOT}, {@code IS_PRESENT}
 */
private Oql unaryOperator(Call call) {
    final Operator op = call.operator();
    final List<Expression> args = call.arguments();
    Preconditions.checkArgument(args.size() == 1, "Size should be == 1 for unary operator %s but was %s", op, args.size());
    Expression arg0 = args.get(0);
    String path = arg0.accept(this).oql();
    if (op instanceof OptionalOperators) {
        // use IS_DEFINED / IS_UNDEFINED functions
        String expr;
        if (op == OptionalOperators.IS_PRESENT) {
            expr = String.format("is_defined(%s) AND %s != null", path, path);
        } else {
            expr = String.format("is_undefined(%s) OR %s = null", path, path);
        }
        return oql(expr);
    } else if (op == Operators.NOT) {
        return oql(String.format("NOT (%s)", path));
    } else if (op == IterableOperators.IS_EMPTY || op == StringOperators.TO_LOWER_CASE || op == StringOperators.TO_UPPER_CASE) {
        return oql(String.format("%s.%s()", path, toMethodName(op)));
    }
    throw new UnsupportedOperationException("Unknown unary operator " + call);
}
Also used : Operator(org.immutables.criteria.expression.Operator) Expression(org.immutables.criteria.expression.Expression) OptionalOperators(org.immutables.criteria.expression.OptionalOperators)

Aggregations

Expression (org.immutables.criteria.expression.Expression)32 Path (org.immutables.criteria.expression.Path)11 Call (org.immutables.criteria.expression.Call)8 Operator (org.immutables.criteria.expression.Operator)8 Pattern (java.util.regex.Pattern)5 PathNaming (org.immutables.criteria.backend.PathNaming)5 OptionalOperators (org.immutables.criteria.expression.OptionalOperators)5 Query (org.immutables.criteria.expression.Query)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Collection (java.util.Collection)4 Objects (java.util.Objects)4 ComparableOperators (org.immutables.criteria.expression.ComparableOperators)4 Constant (org.immutables.criteria.expression.Constant)4 ImmutableQuery (org.immutables.criteria.expression.ImmutableQuery)4 Operators (org.immutables.criteria.expression.Operators)4 StringOperators (org.immutables.criteria.expression.StringOperators)4 Visitors (org.immutables.criteria.expression.Visitors)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Preconditions (com.google.common.base.Preconditions)3 ArrayList (java.util.ArrayList)3