use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class OrderingProperty method fromIndexScanOrCoveringIndexScan.
@Nonnull
private static Optional<Ordering> fromIndexScanOrCoveringIndexScan(@Nonnull final PlanContext context, @Nonnull final RecordQueryPlan plan) {
final RecordQueryIndexPlan recordQueryIndexPlan;
if (plan instanceof RecordQueryIndexPlan) {
recordQueryIndexPlan = (RecordQueryIndexPlan) plan;
} else if (plan instanceof RecordQueryCoveringIndexPlan) {
final RecordQueryPlanWithIndex planWithIndex = ((RecordQueryCoveringIndexPlan) plan).getIndexPlan();
if (planWithIndex instanceof RecordQueryIndexPlan) {
recordQueryIndexPlan = (RecordQueryIndexPlan) planWithIndex;
} else {
return Optional.empty();
}
} else {
return Optional.empty();
}
final String indexName = recordQueryIndexPlan.getIndexName();
final RecordMetaData metaData = context.getMetaData();
final Index index = metaData.getIndex(indexName);
final Collection<RecordType> recordTypesForIndex = metaData.recordTypesForIndex(index);
final KeyExpression commonPrimaryKeyForIndex = RecordMetaData.commonPrimaryKey(recordTypesForIndex);
final KeyExpression keyExpression = ValueIndexExpansionVisitor.fullKey(index, commonPrimaryKeyForIndex);
final ScanComparisons scanComparisons = recordQueryIndexPlan.getComparisons();
return fromKeyAndScanComparisons(keyExpression, scanComparisons, plan.isReverse(), index.isUnique());
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class OrderingProperty method deriveForIntersectionFromOrderings.
public static Optional<Ordering> deriveForIntersectionFromOrderings(@Nonnull final List<Optional<Ordering>> orderingOptionals, @Nonnull final RequestedOrdering requestedOrdering, @Nonnull final BinaryOperator<SetMultimap<KeyExpression, Comparisons.Comparison>> combineFn) {
final Optional<SetMultimap<KeyExpression, Comparisons.Comparison>> commonEqualityBoundKeysMapOptional = Ordering.combineEqualityBoundKeys(orderingOptionals, combineFn);
if (commonEqualityBoundKeysMapOptional.isEmpty()) {
return Optional.empty();
}
final var commonEqualityBoundKeysMap = commonEqualityBoundKeysMapOptional.get();
final Optional<List<KeyPart>> commonOrderingKeysOptional = Ordering.commonOrderingKeys(orderingOptionals, requestedOrdering);
if (commonOrderingKeysOptional.isEmpty()) {
return Optional.empty();
}
final var commonOrderingKeys = commonOrderingKeysOptional.get().stream().filter(keyPart -> !commonEqualityBoundKeysMap.containsKey(keyPart.getNormalizedKeyExpression())).collect(ImmutableList.toImmutableList());
final boolean allAreDistinct = orderingOptionals.stream().anyMatch(orderingOptional -> orderingOptional.map(Ordering::isDistinct).orElse(false));
return Optional.of(new Ordering(commonEqualityBoundKeysMap, commonOrderingKeys, allAreDistinct));
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class KeyExpressionTest method testScalarFieldAccess.
@Test
public void testScalarFieldAccess() throws Exception {
final KeyExpression expression = field("field");
expression.validate(TestScalarFieldAccess.getDescriptor());
assertFalse(expression.createsDuplicates());
assertEquals(Collections.singletonList(scalar("Plants")), evaluate(expression, plantsBoxesAndBowls));
assertEquals(Collections.singletonList(Key.Evaluated.NULL), evaluate(expression, emptyScalar));
assertEquals(Collections.singletonList(Key.Evaluated.NULL), evaluate(expression, null));
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class KeyExpressionTest method testThenFlattens.
@Test
public void testThenFlattens() throws Exception {
final KeyExpression concat = concat(field("f1"), concat(field("f2"), field("f3")), field("f4"));
ThenKeyExpression then = (ThenKeyExpression) concat;
assertFalse(then.createsDuplicates());
assertEquals(4, then.getChildren().size());
for (KeyExpression child : then.getChildren()) {
if (child instanceof ThenKeyExpression) {
fail("Expected no instances of Then, got one " + child.getClass());
}
}
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class KeyExpressionTest method testScalarThenFanned.
@Test
public void testScalarThenFanned() throws Exception {
final KeyExpression expression = concat(field("field"), field("repeat_me", FanType.FanOut));
expression.validate(TestScalarFieldAccess.getDescriptor());
assertTrue(expression.createsDuplicates());
assertEquals(Arrays.asList(concatenate("Plants", "Boxes"), concatenate("Plants", "Bowls")), evaluate(expression, plantsBoxesAndBowls));
assertEquals(Collections.emptyList(), evaluate(expression, emptyScalar));
assertEquals(Collections.emptyList(), evaluate(expression, null));
}
Aggregations