Search in sources :

Example 1 with RecordTypeKeyComparison

use of com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison in project fdb-record-layer by FoundationDB.

the class RecordQueryPlanner method planNoFilterNoSort.

@Nullable
private ScoredPlan planNoFilterNoSort(PlanContext planContext, @Nullable Index index) {
    if (index != null && (!indexTypes.getValueTypes().contains(index.getType()) || index.getRootExpression().createsDuplicates())) {
        return null;
    }
    ScanComparisons scanComparisons = null;
    if (index == null && planContext.query.getRecordTypes().size() == 1 && planContext.commonPrimaryKey != null && Key.Expressions.hasRecordTypePrefix(planContext.commonPrimaryKey)) {
        // Can scan just the one requested record type.
        final RecordTypeKeyComparison recordTypeKeyComparison = new RecordTypeKeyComparison(planContext.query.getRecordTypes().iterator().next());
        scanComparisons = new ScanComparisons.Builder().addEqualityComparison(recordTypeKeyComparison.getComparison()).build();
    }
    return new ScoredPlan(0, valueScan(new CandidateScan(planContext, index, false), scanComparisons, false));
}
Also used : TimeWindowScanComparisons(com.apple.foundationdb.record.provider.foundationdb.leaderboard.TimeWindowScanComparisons) IndexScanComparisons(com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons) RecordTypeKeyComparison(com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison) Nullable(javax.annotation.Nullable)

Example 2 with RecordTypeKeyComparison

use of com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison in project fdb-record-layer by FoundationDB.

the class FDBRecordStore method mergeRecordTypeAndComponent.

@Nonnull
protected static QueryComponent mergeRecordTypeAndComponent(@Nonnull String recordType, @Nullable QueryComponent component) {
    if (component == null) {
        return new RecordTypeKeyComparison(recordType);
    }
    List<QueryComponent> components = new ArrayList<>();
    components.add(new RecordTypeKeyComparison(recordType));
    if (component instanceof AndComponent) {
        components.addAll(((AndComponent) component).getChildren());
    } else {
        components.add(component);
    }
    return Query.and(components);
}
Also used : QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) AndComponent(com.apple.foundationdb.record.query.expressions.AndComponent) ArrayList(java.util.ArrayList) RecordTypeKeyComparison(com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison) Nonnull(javax.annotation.Nonnull)

Example 3 with RecordTypeKeyComparison

use of com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison in project fdb-record-layer by FoundationDB.

the class RecordTypeKeyTest method testIndexScanOnSecondColumn.

@Test
@Disabled
public void testIndexScanOnSecondColumn() throws Exception {
    final Index index = new Index("recno-type", concat(field("num_value_2"), recordType()));
    RecordMetaDataHook hook = metaData -> {
        BASIC_HOOK.apply(metaData);
        metaData.addUniversalIndex(index);
    };
    List<FDBStoredRecord<Message>> recs = saveSomeRecords(hook);
    try (FDBRecordContext context = openContext()) {
        openSimpleRecordStore(context, hook);
        RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.field("num_value_2").equalsValue(2)).build();
        // RecordQueryPlan plan = planner.plan(query);
        ScanComparisons comparison = new ScanComparisons(Arrays.asList(new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, 2), new RecordTypeKeyComparison("MySimpleRecord").getComparison()), Collections.emptySet());
        IndexScanParameters scan = IndexScanComparisons.byValue(comparison);
        RecordQueryPlan plan = new RecordQueryIndexPlan(index.getName(), scan, false);
        assertEquals(recs.subList(1, 2), recordStore.executeQuery(query).map(FDBQueriedRecord::getStoredRecord).asList().join());
        assertThat(plan, indexScan(allOf(indexName(index.getName()), bounds(hasTupleString("[EQUALS 2, IS MySimpleRecord]")))));
    }
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) Arrays(java.util.Arrays) MetaDataException(com.apple.foundationdb.record.metadata.MetaDataException) Disabled(org.junit.jupiter.api.Disabled) PlanMatchers.bounds(com.apple.foundationdb.record.query.plan.match.PlanMatchers.bounds) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) Tag(org.junit.jupiter.api.Tag) MethodSource(org.junit.jupiter.params.provider.MethodSource) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Matchers.allOf(org.hamcrest.Matchers.allOf) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) List(java.util.List) Stream(java.util.stream.Stream) PlanMatchers.indexName(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexName) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RecordTypeKeyComparison(com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) Matchers.anyOf(org.hamcrest.Matchers.anyOf) FDBRecordStoreQueryTestBase(com.apple.foundationdb.record.provider.foundationdb.query.FDBRecordStoreQueryTestBase) IntStream(java.util.stream.IntStream) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) PlanMatchers.indexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexScan) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ScanProperties(com.apple.foundationdb.record.ScanProperties) Expressions.empty(com.apple.foundationdb.record.metadata.Key.Expressions.empty) PlanMatchers.scan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.scan) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) RecordTypeBuilder(com.apple.foundationdb.record.metadata.RecordTypeBuilder) Tags(com.apple.test.Tags) Matchers(org.hamcrest.Matchers) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Expressions.recordType(com.apple.foundationdb.record.metadata.Key.Expressions.recordType) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Index(com.apple.foundationdb.record.metadata.Index) Assumptions(org.junit.jupiter.api.Assumptions) PlanMatchers.unbounded(com.apple.foundationdb.record.query.plan.match.PlanMatchers.unbounded) TestHelpers.assertThrows(com.apple.foundationdb.record.TestHelpers.assertThrows) Message(com.google.protobuf.Message) Collections(java.util.Collections) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) Index(com.apple.foundationdb.record.metadata.Index) RecordTypeKeyComparison(com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Disabled(org.junit.jupiter.api.Disabled)

Example 4 with RecordTypeKeyComparison

use of com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison in project fdb-record-layer by FoundationDB.

the class QueryToKeyMatcherTest method testUnexpected.

@Test
public void testUnexpected() {
    // Make sure the places that throw an error when given an unknown expression all do so.
    assertUnexpected(queryField("a").equalsValue(1), UnknownKeyExpression.UNKNOWN);
    assertUnexpected(queryField("a").oneOfThem().equalsValue(1), UnknownKeyExpression.UNKNOWN);
    assertUnexpected(queryField("p").matches(queryField("b").equalsValue(1)), UnknownKeyExpression.UNKNOWN);
    assertUnexpected(queryField("p").oneOfThem().matches(queryField("b").equalsValue(1)), UnknownKeyExpression.UNKNOWN);
    assertUnexpected(queryField("p").matches(queryField("b").equalsValue(1)), keyField("p").nest(UnknownKeyExpression.UNKNOWN));
    assertUnexpected(queryField("p").oneOfThem().matches(queryField("b").equalsValue(1)), keyField("p", FanType.FanOut).nest(UnknownKeyExpression.UNKNOWN));
    assertUnexpected(Query.and(Query.field("a").equalsValue(1), Query.field("b").equalsValue(2)), concat(keyField("a"), UnknownKeyExpression.UNKNOWN));
    assertUnexpected(new RecordTypeKeyComparison("DummyRecordType"), UnknownKeyExpression.UNKNOWN);
}
Also used : RecordTypeKeyComparison(com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison) Test(org.junit.jupiter.api.Test)

Example 5 with RecordTypeKeyComparison

use of com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison in project fdb-record-layer by FoundationDB.

the class QueryToKeyMatcherTest method testThen.

@Test
public void testThen() {
    assertEquality(MatchType.EQUALITY, queryField("a").equalsValue(1), concatenateFields("a", "b"));
    assertEquality(MatchType.NO_MATCH, queryField("a").equalsValue(1), concatenateFields("b", "a"));
    assertEqualityCoveringKey(MatchType.NO_MATCH, queryField("a").equalsValue(1), concatenateFields("a", "b"));
    assertEquality(MatchType.EQUALITY, queryField("a").oneOfThem().equalsValue(1), concat(keyField("a", FanType.FanOut), keyField("b")));
    assertEquality(MatchType.NO_MATCH, queryField("a").oneOfThem().equalsValue(1), concat(keyField("b"), keyField("a", FanType.FanOut)));
    assertEqualityCoveringKey(MatchType.NO_MATCH, queryField("a").oneOfThem().equalsValue(1), concat(keyField("a", FanType.FanOut), keyField("b")));
    assertEquality(MatchType.EQUALITY, new RecordTypeKeyComparison("ErsatzRecordType"), concat(recordType(), keyField("a")));
    assertEquality(MatchType.NO_MATCH, new RecordTypeKeyComparison("ErsatzRecordType"), concat(keyField("a"), recordType()));
    assertEqualityCoveringKey(MatchType.NO_MATCH, new RecordTypeKeyComparison("ErsatzRecordType"), concat(recordType(), keyField("a")));
}
Also used : RecordTypeKeyComparison(com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison) Test(org.junit.jupiter.api.Test)

Aggregations

RecordTypeKeyComparison (com.apple.foundationdb.record.query.expressions.RecordTypeKeyComparison)5 Test (org.junit.jupiter.api.Test)3 ArrayList (java.util.ArrayList)2 IndexEntry (com.apple.foundationdb.record.IndexEntry)1 IndexScanType (com.apple.foundationdb.record.IndexScanType)1 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)1 RecordMetaDataBuilder (com.apple.foundationdb.record.RecordMetaDataBuilder)1 ScanProperties (com.apple.foundationdb.record.ScanProperties)1 TestHelpers.assertThrows (com.apple.foundationdb.record.TestHelpers.assertThrows)1 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)1 TupleRange (com.apple.foundationdb.record.TupleRange)1 Index (com.apple.foundationdb.record.metadata.Index)1 IndexTypes (com.apple.foundationdb.record.metadata.IndexTypes)1 Expressions.concat (com.apple.foundationdb.record.metadata.Key.Expressions.concat)1 Expressions.concatenateFields (com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields)1 Expressions.empty (com.apple.foundationdb.record.metadata.Key.Expressions.empty)1 Expressions.field (com.apple.foundationdb.record.metadata.Key.Expressions.field)1 Expressions.recordType (com.apple.foundationdb.record.metadata.Key.Expressions.recordType)1 MetaDataException (com.apple.foundationdb.record.metadata.MetaDataException)1 RecordTypeBuilder (com.apple.foundationdb.record.metadata.RecordTypeBuilder)1