Search in sources :

Example 46 with KeyExpression

use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.

the class KeyExpressionTest method testConcatenateSingleRepeatedField.

@Test
public void testConcatenateSingleRepeatedField() throws Exception {
    final KeyExpression expression = field("repeat_me", FanType.Concatenate);
    expression.validate(TestScalarFieldAccess.getDescriptor());
    assertFalse(expression.createsDuplicates());
    assertEquals(Collections.singletonList(scalar(Arrays.asList("Boxes", "Bowls"))), evaluate(expression, plantsBoxesAndBowls));
    assertEquals(Collections.singletonList(scalar(Collections.emptyList())), evaluate(expression, emptyScalar));
    assertEquals(Collections.singletonList(scalar(Collections.emptyList())), evaluate(expression, null));
}
Also used : ListKeyExpression(com.apple.foundationdb.record.metadata.expressions.ListKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) QueryableKeyExpression(com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression) FunctionKeyExpression(com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) SplitKeyExpression(com.apple.foundationdb.record.metadata.expressions.SplitKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 47 with KeyExpression

use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.

the class KeyExpressionTest method testFannedThenScalar.

@Test
public void testFannedThenScalar() throws Exception {
    final KeyExpression expression = concat(field("repeat_me", FanType.FanOut), field("field"));
    expression.validate(TestScalarFieldAccess.getDescriptor());
    assertTrue(expression.createsDuplicates());
    assertEquals(Arrays.asList(concatenate("Boxes", "Plants"), concatenate("Bowls", "Plants")), evaluate(expression, plantsBoxesAndBowls));
    assertEquals(Collections.emptyList(), evaluate(expression, emptyScalar));
    assertEquals(Collections.emptyList(), evaluate(expression, null));
}
Also used : ListKeyExpression(com.apple.foundationdb.record.metadata.expressions.ListKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) QueryableKeyExpression(com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression) FunctionKeyExpression(com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) SplitKeyExpression(com.apple.foundationdb.record.metadata.expressions.SplitKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 48 with KeyExpression

use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.

the class KeyExpressionTest method testNestWithParentField.

@Test
public void testNestWithParentField() throws Exception {
    final KeyExpression expression = concat(field("regular_old_field"), field("repeated_nesty", FanType.FanOut).nest("regular_old_field"));
    expression.validate(NestedField.getDescriptor());
    assertTrue(expression.createsDuplicates());
    assertEquals(Arrays.asList(concatenate("Grandmother", "Daughter"), concatenate("Grandmother", "Sister")), evaluate(expression, matryoshkaDolls));
    assertEquals(Collections.emptyList(), evaluate(expression, emptyNested));
    assertEquals(Arrays.asList(Key.Evaluated.concatenate("Lonely", NULL), Key.Evaluated.concatenate("Lonely", NULL)), evaluate(expression, lonelyDoll));
    assertEquals(Collections.emptyList(), evaluate(expression, null));
}
Also used : ListKeyExpression(com.apple.foundationdb.record.metadata.expressions.ListKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) QueryableKeyExpression(com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression) FunctionKeyExpression(com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) SplitKeyExpression(com.apple.foundationdb.record.metadata.expressions.SplitKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 49 with KeyExpression

use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.

the class UnionVisitor method postVisit.

@Nonnull
@Override
public RecordQueryPlan postVisit(@Nonnull final RecordQueryPlan recordQueryPlan) {
    if (recordQueryPlan instanceof RecordQueryUnionPlanBase) {
        RecordQueryUnionPlanBase unionPlan = (RecordQueryUnionPlanBase) recordQueryPlan;
        final Set<KeyExpression> requiredFields = unionPlan.getRequiredFields();
        boolean shouldPullOutFilter = false;
        QueryComponent filter = null;
        if (unionPlan.getChildren().stream().allMatch(child -> child instanceof RecordQueryFilterPlan)) {
            filter = ((RecordQueryFilterPlan) unionPlan.getChildren().get(0)).getConjunctedFilter();
            // needed for lambda expression
            final QueryComponent finalFilter = filter;
            shouldPullOutFilter = unionPlan.getChildren().stream().allMatch(plan -> ((RecordQueryFilterPlan) plan).getConjunctedFilter().equals(finalFilter));
        }
        List<ExpressionRef<RecordQueryPlan>> newChildren = new ArrayList<>(unionPlan.getChildren().size());
        for (RecordQueryPlan plan : unionPlan.getChildren()) {
            if (shouldPullOutFilter) {
                // Check if the plan under the filter can have its index fetch removed.
                if (!(plan instanceof RecordQueryFilterPlan)) {
                    throw new RecordCoreException("serious logic error: thought this was a filter plan but it wasn't");
                }
                plan = ((RecordQueryFilterPlan) plan).getChild();
            }
            @Nullable RecordQueryPlan newPlan = removeIndexFetch(plan, requiredFields);
            if (newPlan == null) {
                // can't remove index fetch, so give up
                return recordQueryPlan;
            }
            newChildren.add(GroupExpressionRef.of(newPlan));
        }
        RecordQueryPlan newUnionPlan = new RecordQueryFetchFromPartialRecordPlan(unionPlan.withChildrenReferences(newChildren), TranslateValueFunction.unableToTranslate());
        if (shouldPullOutFilter) {
            return new RecordQueryFilterPlan(newUnionPlan, filter);
        } else {
            return newUnionPlan;
        }
    }
    return recordQueryPlan;
}
Also used : TranslateValueFunction(com.apple.foundationdb.record.query.plan.plans.TranslateValueFunction) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) RecordQueryFilterPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryFilterPlan) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) RecordQueryFetchFromPartialRecordPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan) Set(java.util.Set) PlannableIndexTypes(com.apple.foundationdb.record.query.plan.PlannableIndexTypes) GroupExpressionRef(com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ArrayList(java.util.ArrayList) List(java.util.List) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) ExpressionRef(com.apple.foundationdb.record.query.plan.temp.ExpressionRef) Nonnull(javax.annotation.Nonnull) RecordQueryUnionPlanBase(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnionPlanBase) Nullable(javax.annotation.Nullable) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) RecordQueryUnionPlanBase(com.apple.foundationdb.record.query.plan.plans.RecordQueryUnionPlanBase) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) ArrayList(java.util.ArrayList) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) RecordQueryFilterPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryFilterPlan) RecordQueryFetchFromPartialRecordPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan) GroupExpressionRef(com.apple.foundationdb.record.query.plan.temp.GroupExpressionRef) ExpressionRef(com.apple.foundationdb.record.query.plan.temp.ExpressionRef) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull)

Example 50 with KeyExpression

use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.

the class LuceneDocumentFromRecordTest method mapWithSubMessageWithAmbiguity.

/**
 * When a schema leads an ambiguity of the parsed path given a concatenated Lucene field, the first path that satisfies the given field will be selected and the correct one could be ignored.
 * In this test, because both the path {entry -> sub_entry -> value} and {entry -> sub_entry -> second_value} could match with the given Lucene field "entry_k1_second_value",
 * and in the first path the key is "k1_second" and in the second one the key is "k1".
 * There is no more information to tell which is the expected one so we just pick up the first one to build the partial record.
 * TODO: Predicate the potential ambiguity when loading a schema and reject it
 */
@Test
void mapWithSubMessageWithAmbiguity() {
    TestRecordsTextProto.NestedMapDocument message = TestRecordsTextProto.NestedMapDocument.newBuilder().setDocId(5).addEntry(TestRecordsTextProto.NestedMapDocument.Entry.newBuilder().setKey("k1").setSubEntry(TestRecordsTextProto.NestedMapDocument.SubEntry.newBuilder().setSecondValue("testValue").build()).build()).build();
    KeyExpression index = field("entry", KeyExpression.FanType.FanOut).nest(function(LuceneFunctionNames.LUCENE_FIELD_NAME, concat(field("sub_entry").nest(concat(function(LuceneFunctionNames.LUCENE_TEXT, field("value")), function(LuceneFunctionNames.LUCENE_TEXT, field("second_value")))), field("key"))));
    FDBRecord<Message> record = unstoredRecord(message);
    assertEquals(ImmutableMap.of(Tuple.from(), ImmutableList.of(textField("entry_k1_second_value", "testValue"))), LuceneDocumentFromRecord.getRecordFields(index, record));
    // Build the partial record message for suggestion
    Descriptors.Descriptor recordDescriptor = message.getDescriptorForType();
    TestRecordsTextProto.NestedMapDocument.Builder builder = TestRecordsTextProto.NestedMapDocument.newBuilder();
    LuceneIndexKeyValueToPartialRecordUtils.buildPartialRecord(index, recordDescriptor, builder, "entry_k1_second_value", "suggestion");
    TestRecordsTextProto.NestedMapDocument partialMsg = builder.build();
    assertEquals(1, partialMsg.getEntryCount());
    TestRecordsTextProto.NestedMapDocument.Entry entry = partialMsg.getEntry(0);
    // The k1_second is supposed to be populated for the key field under entry
    assertEquals("k1_second", entry.getKey());
    // The suggestion is supposed to show up in value field within sub-entry, instead of second_value
    TestRecordsTextProto.NestedMapDocument.SubEntry subEntry = entry.getSubEntry();
    assertEquals("suggestion", subEntry.getValue());
    assertFalse(subEntry.hasSecondValue());
}
Also used : TestRecordsTextProto(com.apple.foundationdb.record.TestRecordsTextProto) Message(com.google.protobuf.Message) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Descriptors(com.google.protobuf.Descriptors) Test(org.junit.jupiter.api.Test)

Aggregations

KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)152 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)85 Test (org.junit.jupiter.api.Test)63 FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)60 EmptyKeyExpression (com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression)56 NestingKeyExpression (com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression)56 ThenKeyExpression (com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression)52 Nonnull (javax.annotation.Nonnull)52 Index (com.apple.foundationdb.record.metadata.Index)37 List (java.util.List)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)35 Nullable (javax.annotation.Nullable)33 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)30 FunctionKeyExpression (com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression)28 Message (com.google.protobuf.Message)26 ArrayList (java.util.ArrayList)26 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)25 QueryableKeyExpression (com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression)25 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)24 ListKeyExpression (com.apple.foundationdb.record.metadata.expressions.ListKeyExpression)23