use of com.apple.foundationdb.record.metadata.IndexAggregateFunctionCall in project fdb-record-layer by FoundationDB.
the class IndexFunctionHelperTest method filterIndexForBindAggregateFunctionCall.
@Test
void filterIndexForBindAggregateFunctionCall() {
final GroupingKeyExpression group = concat(field("str_value_indexed"), field("num_value_2")).group(1);
RecordMetaDataHook hook = metaData -> {
metaData.addIndex("MySimpleRecord", new Index("filtered_sum_value2", group, Index.EMPTY_VALUE, IndexTypes.SUM, Map.of()));
};
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
recordStore.deleteAllRecords();
final IndexAggregateFunctionCall indexAggregateFunctionCall = new IndexAggregateFunctionCall("sum", group);
final Optional<IndexAggregateFunction> expected = Optional.of(new IndexAggregateFunction("sum", group, "filtered_sum_value2"));
assertEquals(expected, IndexFunctionHelper.bindAggregateFunctionCall(recordStore, indexAggregateFunctionCall, List.of("MySimpleRecord"), IndexQueryabilityFilter.TRUE));
assertEquals(Optional.empty(), IndexFunctionHelper.bindAggregateFunctionCall(recordStore, indexAggregateFunctionCall, List.of("MySimpleRecord"), IndexQueryabilityFilter.FALSE));
commit(context);
}
}
use of com.apple.foundationdb.record.metadata.IndexAggregateFunctionCall in project fdb-record-layer by FoundationDB.
the class BitmapValueIndexTest method nestedAndQuery.
@Test
void nestedAndQuery() {
final KeyExpression num_by_str = field("nested").nest(field("entry", FanOut).nest(concatenateFields("str_value", "num_value")));
final GroupingKeyExpression nested_num_by_str = concat(field("num_value_1"), num_by_str).group(1);
final KeyExpression nested_num_by_str_num2 = concat(field("num_value_1"), field("num_value_2"), num_by_str).group(1);
final KeyExpression nested_num_by_str_num3 = concat(field("num_value_1"), field("num_value_3"), num_by_str).group(1);
final RecordMetaDataHook nested_rec_no_by_str_nums_hook = metadata -> {
final RecordTypeBuilder recordType = metadata.getRecordType("MyNestedRecord");
metadata.addIndex(recordType, new Index("nested_num_by_str_num2", nested_num_by_str_num2, IndexTypes.BITMAP_VALUE, SMALL_BITMAP_OPTIONS));
metadata.addIndex(recordType, new Index("nested_num_by_str_num3", nested_num_by_str_num3, IndexTypes.BITMAP_VALUE, SMALL_BITMAP_OPTIONS));
};
final IndexAggregateFunctionCall bitmap_value_nested_num_by_str = new IndexAggregateFunctionCall(FunctionNames.BITMAP_VALUE, nested_num_by_str);
try (FDBRecordContext context = openContext()) {
createOrOpenRecordStore(context, metaData(nested_rec_no_by_str_nums_hook));
for (int recNo = 100; recNo < 200; recNo++) {
recordStore.saveRecord(TestRecordsBitmapProto.MyNestedRecord.newBuilder().setRecNo(recNo).setNumValue1(1).setNested(TestRecordsBitmapProto.MyNestedRecord.Nested.newBuilder().addEntry(TestRecordsBitmapProto.MyNestedRecord.Nested.Entry.newBuilder().setStrValue((recNo & 1) == 1 ? "odd" : "even").setNumValue(recNo + 1000))).setNumValue2(recNo % 7).setNumValue3(recNo % 5).build());
}
commit(context);
}
try (FDBRecordContext context = openContext()) {
createOrOpenRecordStore(context, metaData(nested_rec_no_by_str_nums_hook));
setupPlanner(null);
final RecordQuery recordQuery = RecordQuery.newBuilder().setRecordType("MyNestedRecord").setFilter(Query.and(Query.field("num_value_1").equalsValue(1), Query.field("nested").matches(Query.field("entry").oneOfThem().matches(Query.field("str_value").equalsValue("odd"))), Query.field("num_value_2").equalsValue(3), Query.field("num_value_3").equalsValue(4))).setRequiredResults(Collections.singletonList(field("nested").nest(field("entry", FanOut).nest("num_value")))).build();
final RecordQueryPlan queryPlan = ComposedBitmapIndexAggregate.tryPlan((RecordQueryPlanner) planner, recordQuery, bitmap_value_nested_num_by_str, IndexQueryabilityFilter.DEFAULT).orElseGet(() -> fail("Cannot plan query"));
assertThat(queryPlan, compositeBitmap(hasToString("[0] BITAND [1]"), Arrays.asList(coveringIndexScan(indexScan(allOf(indexName("nested_num_by_str_num2"), indexScanType(IndexScanType.BY_GROUP), bounds(hasTupleString("[[1, 3, odd],[1, 3, odd]]"))))), coveringIndexScan(indexScan(allOf(indexName("nested_num_by_str_num3"), indexScanType(IndexScanType.BY_GROUP), bounds(hasTupleString("[[1, 4, odd],[1, 4, odd]]"))))))));
assertEquals(1000204717, queryPlan.planHash());
assertThat(collectOnBits(queryPlan.execute(recordStore).map(FDBQueriedRecord::getIndexEntry)), equalTo(IntStream.range(100, 200).boxed().filter(i -> (i & 1) == 1).filter(i -> (i % 7) == 3 && (i % 5) == 4).map(i -> i + 1000).collect(Collectors.toList())));
}
}
Aggregations