Search in sources :

Example 6 with ScanComparisons

use of com.apple.foundationdb.record.query.plan.ScanComparisons in project fdb-record-layer by FoundationDB.

the class FDBRecordStoreTest method testOverlappingPrimaryKey.

@Test
public void testOverlappingPrimaryKey() {
    try (FDBRecordContext context = openContext()) {
        RecordMetaDataBuilder builder = RecordMetaData.newBuilder().setRecords(TestRecordsWithHeaderProto.getDescriptor());
        builder.getRecordType("MyRecord").setPrimaryKey(field("header").nest(concatenateFields("path", "rec_no")));
        builder.addIndex("MyRecord", "MyRecord$path_str", concat(field("header").nest("path"), field("str_value")));
        RecordMetaData metaData = builder.getRecordMetaData();
        createOrOpenRecordStore(context, metaData);
        TestRecordsWithHeaderProto.MyRecord.Builder recBuilder = TestRecordsWithHeaderProto.MyRecord.newBuilder();
        TestRecordsWithHeaderProto.HeaderRecord.Builder headerBuilder = recBuilder.getHeaderBuilder();
        headerBuilder.setPath("aaa");
        headerBuilder.setRecNo(1);
        recBuilder.setStrValue("hello");
        recordStore.saveRecord(recBuilder.build());
        headerBuilder.setPath("aaa");
        headerBuilder.setRecNo(2);
        recBuilder.setStrValue("goodbye");
        recordStore.saveRecord(recBuilder.build());
        headerBuilder.setPath("zzz");
        headerBuilder.setRecNo(3);
        recBuilder.setStrValue("end");
        recordStore.saveRecord(recBuilder.build());
        List<List<Object>> rows = new ArrayList<>();
        Index index = metaData.getIndex("MyRecord$path_str");
        ScanComparisons comparisons = ScanComparisons.from(new Comparisons.SimpleComparison(Comparisons.Type.EQUALS, "aaa"));
        TupleRange range = comparisons.toTupleRange();
        try (RecordCursor<IndexEntry> cursor = recordStore.scanIndex(index, IndexScanType.BY_VALUE, range, null, ScanProperties.FORWARD_SCAN)) {
            cursor.forEach(row -> rows.add(row.getKey().getItems())).join();
        }
        assertEquals(Arrays.asList(Arrays.asList("aaa", "goodbye", 2L), Arrays.asList("aaa", "hello", 1L)), rows);
    }
}
Also used : IndexEntry(com.apple.foundationdb.record.IndexEntry) DescriptorProtos(com.google.protobuf.DescriptorProtos) DELETE_INDEX_ENTRY(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer.Events.DELETE_INDEX_ENTRY) Arrays(java.util.Arrays) PlanMatchers.bounds(com.apple.foundationdb.record.query.plan.match.PlanMatchers.bounds) RecordCoreStorageException(com.apple.foundationdb.record.RecordCoreStorageException) ByteBuffer(java.nio.ByteBuffer) RecordSerializer(com.apple.foundationdb.record.provider.common.RecordSerializer) RecordSerializationException(com.apple.foundationdb.record.provider.common.RecordSerializationException) IndexScanType(com.apple.foundationdb.record.IndexScanType) Tuple(com.apple.foundationdb.tuple.Tuple) Range(com.apple.foundationdb.Range) FDBError(com.apple.foundationdb.FDBError) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) RecordMetaDataProto(com.apple.foundationdb.record.RecordMetaDataProto) RecordMetaDataOptionsProto(com.apple.foundationdb.record.RecordMetaDataOptionsProto) Is.is(org.hamcrest.core.Is.is) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) Tag(org.junit.jupiter.api.Tag) MethodSource(org.junit.jupiter.params.provider.MethodSource) TestRecords7Proto(com.apple.foundationdb.record.TestRecords7Proto) ByteArrayUtil(com.apple.foundationdb.tuple.ByteArrayUtil) 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) Set(java.util.Set) Collectors(java.util.stream.Collectors) TupleRange(com.apple.foundationdb.record.TupleRange) ByteString(com.google.protobuf.ByteString) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) KeySpacePath(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpacePath) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) List(java.util.List) PlanMatchers.indexName(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexName) RecordMetaDataProvider(com.apple.foundationdb.record.RecordMetaDataProvider) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) IndexTypes(com.apple.foundationdb.record.metadata.IndexTypes) TestRecordsWithHeaderProto(com.apple.foundationdb.record.TestRecordsWithHeaderProto) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.containsString(org.hamcrest.Matchers.containsString) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) TestNoUnionProto(com.apple.foundationdb.record.TestNoUnionProto) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) TestRecordsDuplicateUnionFields(com.apple.foundationdb.record.TestRecordsDuplicateUnionFields) DynamicMessage(com.google.protobuf.DynamicMessage) Descriptors(com.google.protobuf.Descriptors) PlanMatchers.indexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexScan) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) CompletableFuture(java.util.concurrent.CompletableFuture) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) ScanProperties(com.apple.foundationdb.record.ScanProperties) RecordCursorIterator(com.apple.foundationdb.record.RecordCursorIterator) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) 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) Nullable(javax.annotation.Nullable) Charsets(com.google.common.base.Charsets) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Tags(com.apple.test.Tags) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) TestRecordsImportProto(com.apple.foundationdb.record.TestRecordsImportProto) Index(com.apple.foundationdb.record.metadata.Index) FDBException(com.apple.foundationdb.FDBException) Message(com.google.protobuf.Message) RecordCursor(com.apple.foundationdb.record.RecordCursor) Collections(java.util.Collections) SAVE_INDEX_ENTRY(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer.Events.SAVE_INDEX_ENTRY) RecordMetaData(com.apple.foundationdb.record.RecordMetaData) ArrayList(java.util.ArrayList) IndexEntry(com.apple.foundationdb.record.IndexEntry) Index(com.apple.foundationdb.record.metadata.Index) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) List(java.util.List) ArrayList(java.util.ArrayList) TupleRange(com.apple.foundationdb.record.TupleRange) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with ScanComparisons

use of com.apple.foundationdb.record.query.plan.ScanComparisons in project fdb-record-layer by FoundationDB.

the class QueryPlanCursorTest method reverse.

@Test
public void reverse() throws Exception {
    final IndexScanParameters scan = IndexScanComparisons.byValue(new ScanComparisons(Collections.emptyList(), ImmutableSet.of(new Comparisons.SimpleComparison(Comparisons.Type.GREATER_THAN, 2), new Comparisons.SimpleComparison(Comparisons.Type.LESS_THAN, 4))));
    final RecordQueryPlan plan = new RecordQueryIndexPlan("MySimpleRecord$num_value_3_indexed", scan, true);
    compareSkipsAndCursors(plan);
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) Test(org.junit.jupiter.api.Test)

Example 8 with ScanComparisons

use of com.apple.foundationdb.record.query.plan.ScanComparisons in project fdb-record-layer by FoundationDB.

the class QueryPlanCursorTest method in.

@Test
public void in() throws Exception {
    final IndexScanParameters scan = IndexScanComparisons.byValue(new ScanComparisons(Arrays.asList(new Comparisons.ParameterComparison(Comparisons.Type.EQUALS, "in_num")), Collections.emptySet()));
    final RecordQueryPlan plan = new RecordQueryInValuesJoinPlan(new RecordQueryIndexPlan("MySimpleRecord$num_value_3_indexed", scan, false), "in_num", Bindings.Internal.IN, Arrays.asList(2, 4), false, false);
    compareSkipsAndCursors(plan);
}
Also used : RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) RecordQueryIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan) RecordQueryInValuesJoinPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryInValuesJoinPlan) Test(org.junit.jupiter.api.Test)

Example 9 with ScanComparisons

use of com.apple.foundationdb.record.query.plan.ScanComparisons 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 10 with ScanComparisons

use of com.apple.foundationdb.record.query.plan.ScanComparisons in project fdb-record-layer by FoundationDB.

the class LuceneIndexQueryPlan method merge.

public static LuceneIndexQueryPlan merge(LuceneIndexQueryPlan plan1, LuceneIndexQueryPlan plan2, String type) {
    verify(plan1.indexName.equals(plan2.indexName));
    verify(plan1.sort == null || plan2.sort == null || plan1.sort.equals(plan2.sort));
    KeyExpression newSort = plan1.sort != null ? plan1.sort : plan2.sort;
    String newQuery = String.format("(%s) %s (%s)", plan1.getLuceneQueryString(), type, plan2.getLuceneQueryString());
    Comparisons.LuceneComparison comparison = new Comparisons.LuceneComparison(newQuery);
    boolean newReverse = plan1.isReverse() ? plan1.isReverse() : plan2.isReverse();
    IndexScanType scanType = IndexScanType.BY_LUCENE;
    if (plan1.getScanType() == IndexScanType.BY_LUCENE_FULL_TEXT || plan2.getScanType() == IndexScanType.BY_LUCENE_FULL_TEXT) {
        scanType = IndexScanType.BY_LUCENE_FULL_TEXT;
    }
    ScanComparisons newGrouping = plan1.groupingComparisons == null ? ScanComparisons.EMPTY : plan1.groupingComparisons;
    if (newGrouping.isEmpty()) {
        newGrouping = plan2.groupingComparisons;
    } else if (plan2.groupingComparisons != null) {
        newGrouping = newGrouping.merge(plan2.groupingComparisons);
    }
    LuceneIndexQueryPlan plan = new LuceneIndexQueryPlan(plan1.indexName, scanType, comparison, newReverse, newSort, newGrouping);
    if (plan1.createsDuplicates() || plan2.createsDuplicates()) {
        plan.setCreatesDuplicates();
    }
    return plan;
}
Also used : IndexScanComparisons(com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) IndexScanType(com.apple.foundationdb.record.IndexScanType) IndexScanComparisons(com.apple.foundationdb.record.provider.foundationdb.IndexScanComparisons) ScanComparisons(com.apple.foundationdb.record.query.plan.ScanComparisons) Comparisons(com.apple.foundationdb.record.query.expressions.Comparisons) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression)

Aggregations

ScanComparisons (com.apple.foundationdb.record.query.plan.ScanComparisons)14 Comparisons (com.apple.foundationdb.record.query.expressions.Comparisons)10 KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)9 Test (org.junit.jupiter.api.Test)7 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)6 IndexScanType (com.apple.foundationdb.record.IndexScanType)5 Index (com.apple.foundationdb.record.metadata.Index)5 RecordQueryIndexPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryIndexPlan)5 Nonnull (javax.annotation.Nonnull)5 IndexEntry (com.apple.foundationdb.record.IndexEntry)4 ScanProperties (com.apple.foundationdb.record.ScanProperties)4 TestRecords1Proto (com.apple.foundationdb.record.TestRecords1Proto)4 TupleRange (com.apple.foundationdb.record.TupleRange)4 Tuple (com.apple.foundationdb.tuple.Tuple)4 Tags (com.apple.test.Tags)4 Message (com.google.protobuf.Message)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 List (java.util.List)4