use of com.apple.foundationdb.record.query.expressions.BaseField in project fdb-record-layer by FoundationDB.
the class InExtractor method extractInClauses.
@SuppressWarnings("unchecked")
private QueryComponent extractInClauses() {
final AtomicInteger bindingIndex = new AtomicInteger();
return mapClauses(filter, (withComparison, fields) -> {
if (withComparison.getComparison().getType() == Comparisons.Type.IN) {
String bindingName = Bindings.Internal.IN.bindingName(withComparison.getName() + "__" + bindingIndex.getAndIncrement());
List<FieldKeyExpression> nestedFields = null;
if (fields != null && (withComparison instanceof FieldWithComparison || withComparison instanceof OneOfThemWithComparison)) {
nestedFields = new ArrayList<>(fields);
nestedFields.add(Key.Expressions.field(((BaseField) withComparison).getFieldName(), withComparison instanceof FieldWithComparison ? KeyExpression.FanType.None : KeyExpression.FanType.FanOut));
}
KeyExpression orderingKey = getOrderingKey(nestedFields);
if (withComparison.getComparison() instanceof Comparisons.ComparisonWithParameter) {
final String parameterName = ((Comparisons.ComparisonWithParameter) withComparison.getComparison()).getParameter();
inClauses.add(new InParameterClause(bindingName, parameterName, orderingKey));
} else {
final List<Object> comparand = (List<Object>) withComparison.getComparison().getComparand();
// ListComparison does not allow empty/null
if (comparand != null && comparand.size() == 1) {
return withComparison.withOtherComparison(new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, comparand.get(0)));
}
inClauses.add(new InValuesClause(bindingName, comparand, orderingKey));
}
return withComparison.withOtherComparison(new Comparisons.ParameterComparison(Comparisons.Type.EQUALS, bindingName, Bindings.Internal.IN));
} else {
return withComparison;
}
}, Collections.emptyList());
}
use of com.apple.foundationdb.record.query.expressions.BaseField in project fdb-record-layer by FoundationDB.
the class InExtractor method mapClauses.
private QueryComponent mapClauses(QueryComponent filter, BiFunction<ComponentWithComparison, List<FieldKeyExpression>, QueryComponent> mapper, @Nullable List<FieldKeyExpression> fields) {
if (filter instanceof ComponentWithComparison) {
final ComponentWithComparison withComparison = (ComponentWithComparison) filter;
return mapper.apply(withComparison, fields);
} else if (filter instanceof ComponentWithChildren) {
ComponentWithChildren componentWithChildren = (ComponentWithChildren) filter;
return componentWithChildren.withOtherChildren(componentWithChildren.getChildren().stream().map(component -> mapClauses(component, mapper, fields)).collect(Collectors.toList()));
} else if (filter instanceof ComponentWithSingleChild) {
ComponentWithSingleChild componentWithSingleChild = (ComponentWithSingleChild) filter;
List<FieldKeyExpression> nestedFields = null;
if (fields != null && (componentWithSingleChild instanceof NestedField || componentWithSingleChild instanceof OneOfThemWithComponent)) {
nestedFields = new ArrayList<>(fields);
nestedFields.add(Key.Expressions.field(((BaseField) componentWithSingleChild).getFieldName(), componentWithSingleChild instanceof NestedField ? KeyExpression.FanType.None : KeyExpression.FanType.FanOut));
}
return componentWithSingleChild.withOtherChild(mapClauses(componentWithSingleChild.getChild(), mapper, nestedFields));
} else if (filter instanceof ComponentWithNoChildren) {
return filter;
} else {
throw new Query.InvalidExpressionException("Unsupported query type " + filter.getClass());
}
}
use of com.apple.foundationdb.record.query.expressions.BaseField in project fdb-record-layer by FoundationDB.
the class IndexAggregateFunctionCall method extractFieldPaths.
/**
* Helper method to extract a set of key expressions that are bound through some comparison in the
* query component passed in.
* @param queryComponent the query component
* @param predicate a predicate used for filtering each encountered {@link FieldWithComparison}
* @return a set of {@link KeyExpression}s where each element is a key expression of a field (i.e. a
* {@link com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression}) or a simple nesting field
* (i.e. a {@link com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression}) that is bound
* through some comparison
*/
@Nonnull
public static Set<KeyExpression> extractFieldPaths(@Nonnull QueryComponent queryComponent, @Nonnull final Predicate<FieldWithComparison> predicate) {
if (queryComponent instanceof BaseField) {
final BaseField baseField = (BaseField) queryComponent;
if (baseField instanceof NestedField) {
final NestedField nestedField = (NestedField) baseField;
final Set<KeyExpression> nestedExpressions = extractFieldPaths(nestedField.getChild(), predicate);
return nestedExpressions.stream().map(nestedExpression -> Key.Expressions.field(nestedField.getFieldName()).nest(nestedExpression)).collect(ImmutableSet.toImmutableSet());
}
if (baseField instanceof FieldWithComparison) {
final FieldWithComparison fieldWithComparison = (FieldWithComparison) baseField;
if (predicate.test(fieldWithComparison)) {
return ImmutableSet.of(Key.Expressions.field(fieldWithComparison.getFieldName()));
}
}
return ImmutableSet.of();
} else if (queryComponent instanceof AndComponent) {
final Set<KeyExpression> boundFields = Sets.newHashSet();
final AndComponent andComponent = (AndComponent) queryComponent;
andComponent.getChildren().forEach(child -> boundFields.addAll(extractEqualityBoundFields(child)));
return boundFields;
}
return ImmutableSet.of();
}
use of com.apple.foundationdb.record.query.expressions.BaseField in project fdb-record-layer by FoundationDB.
the class GroupingValidator method matchNestingField.
private static boolean matchNestingField(@Nonnull QueryComponent filter, @Nonnull ComponentWithSingleChild nestingComponent, @Nonnull NestingKeyExpression nesting, @Nonnull List<QueryComponent> groupFilters, @Nonnull List<Comparisons.Comparison> groupComparisons) {
if (nesting.getChild() instanceof NestingKeyExpression) {
NestingKeyExpression childNesting = (NestingKeyExpression) nesting.getChild();
QueryComponent childComponent = nestingComponent.getChild();
if (childComponent instanceof NestedField || childComponent instanceof OneOfThemWithComponent) {
if (childNesting.getParent().getFieldName().equals(((BaseField) childComponent).getFieldName()) && matchNestingField(filter, (ComponentWithSingleChild) childComponent, childNesting, groupFilters, groupComparisons)) {
return true;
}
}
} else if (nesting.getChild() instanceof FieldKeyExpression) {
FieldKeyExpression childField = (FieldKeyExpression) nesting.getChild();
if (nestingComponent.getChild() instanceof FieldWithComparison) {
FieldWithComparison comparisonFilter = (FieldWithComparison) nestingComponent.getChild();
if (comparisonFilter.getFieldName().equals(childField.getFieldName()) && (comparisonFilter.getComparison().getType() == Comparisons.Type.EQUALS || comparisonFilter.getComparison().getType() == Comparisons.Type.IS_NULL)) {
groupFilters.add(filter);
groupComparisons.add(comparisonFilter.getComparison());
return true;
}
}
}
return false;
}
Aggregations