use of org.immutables.criteria.expression.Expression in project immutables by immutables.
the class GeodeQueryVisitorTest method oqlChecker.
private static StringChecker oqlChecker(Criterion<?> criteria, boolean useBindVariables) {
final PathNaming pathNaming = ReservedWordNaming.of(PathNaming.defaultNaming());
final GeodeQueryVisitor queryVisitor = new GeodeQueryVisitor(useBindVariables, pathNaming);
Expression filter = Criterias.toQuery(criteria).filter().orElseThrow(() -> new IllegalStateException("no filter"));
return check(filter.accept(queryVisitor).oql());
}
use of org.immutables.criteria.expression.Expression in project immutables by immutables.
the class KeyLookupAnalyzerTest method ids.
private static Iterable<String> ids(StringHolderCriteria crit) {
Expression filter = Criterias.toQuery(crit).filter().orElseThrow(() -> new AssertionError("No filter present"));
KeyExtractor extractor = KeyExtractor.defaultFactory().create(TypeHolder.StringHolder.class);
KeyLookupAnalyzer analyzer = KeyLookupAnalyzer.fromExtractor(extractor);
KeyLookupAnalyzer.Result result = analyzer.analyze(filter);
if (!result.isOptimizable()) {
return Collections.emptyList();
}
return (Iterable<String>) result.values();
}
use of org.immutables.criteria.expression.Expression in project immutables by immutables.
the class InMemory method toPredicate.
/**
* Converts criteria to a predicate. Note that filter must be set on criteria.
* For instance {@code person.name.is("John")} is a valid filter but {@code person.name} is not.
*
* @throws NullPointerException if argument is null
* @throws IllegalArgumentException if filter is not set on criteria
* @return predicate which reflects criteria filter expression
*/
public static <T> Predicate<T> toPredicate(Criterion<T> criterion) {
Objects.requireNonNull(criterion, "criterion");
Expression expression = Criterias.toQuery(criterion).filter().orElseThrow(() -> new IllegalArgumentException("Filter not set"));
@SuppressWarnings("unchecked") Predicate<T> predicate = (Predicate<T>) ExpressionInterpreter.of(expression).asPredicate();
return predicate;
}
use of org.immutables.criteria.expression.Expression in project immutables by immutables.
the class TupleExtractor method extract.
ProjectedTuple extract(Object instance) {
List<Object> values = new ArrayList<>();
for (Expression expr : query.projections()) {
Path path = (Path) expr;
Object value = pathExtractor.extract(path, instance);
value = maybeWrapOptional(value, path);
values.add(value);
}
return ProjectedTuple.of(query.projections(), values);
}
use of org.immutables.criteria.expression.Expression in project immutables by immutables.
the class AggregateQueryBuilder method processResult.
List<ProjectedTuple> processResult(Json.Result result) {
final List<ProjectedTuple> tuples = new ArrayList<>();
if (result.aggregations() != null) {
Converter<String, Expression> converter = naming.asConverter().reverse();
// collect values
Json.visitValueNodes(result.aggregations(), m -> {
Map<Expression, Object> values = Maps.newHashMapWithExpectedSize(query.projections().size());
for (String field : m.keySet()) {
Expression expression = converter.convert(field);
Object value = m.get(field);
if (value == null) {
// otherwise jackson returns null even for optionals
value = NullNode.getInstance();
} else if (value instanceof Number && (expression.returnType() == LocalDate.class || expression.returnType() == LocalDateTime.class)) {
// hack/work-around because JavaTimeModule doesn't handle epoch millis for LocalDate and LocalDateTime
// and elastic always returns epoch millis
// this ideally should be handled directly by Deserializer
Instant instant = Instant.ofEpochMilli(((Number) value).longValue());
value = nodeFactory.textNode(instant.toString());
}
values.put(expression, mapper.convertValue(value, mapper.getTypeFactory().constructType(expression.returnType())));
}
List<Object> projections = query.projections().stream().map(values::get).collect(Collectors.toList());
tuples.add(ProjectedTuple.of(query.projections(), projections));
});
}
// elastic exposes total number of documents matching a query in "/hits/total" path
// this can be used for simple "select count(*) from table"
final long total = result.searchHits().total().value();
return tuples;
}
Aggregations