use of com.apple.foundationdb.record.metadata.IndexAggregateFunction in project fdb-record-layer by FoundationDB.
the class FDBRestrictedIndexQueryTest method queryAggregateWithWriteOnly.
/**
* Verify that write-only aggregate indexes are not used by the planner.
* Verify that re-allowing reads to those indexes allows the planner to use them.
* TODO: Abstract out common code in queryWithWriteOnly, queryWithDisabled, queryAggregateWithWriteOnly and queryAggregateWithDisabled (https://github.com/FoundationDB/fdb-record-layer/issues/4)
*/
@Test
void queryAggregateWithWriteOnly() throws Exception {
Index sumIndex = new Index("value3sum", field("num_value_3_indexed").ungrouped(), IndexTypes.SUM);
Index maxIndex = new Index("value3max", field("num_value_3_indexed").ungrouped(), IndexTypes.MAX_EVER_TUPLE);
RecordMetaDataHook hook = metaData -> {
metaData.addIndex("MySimpleRecord", sumIndex);
metaData.addIndex("MySimpleRecord", maxIndex);
};
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
recordStore.deleteAllRecords();
recordStore.clearAndMarkIndexWriteOnly("value3sum").join();
recordStore.clearAndMarkIndexWriteOnly("value3max").join();
RangeSet rangeSet = new RangeSet(recordStore.indexRangeSubspace(sumIndex));
rangeSet.insertRange(context.ensureActive(), Tuple.from(1000).pack(), Tuple.from(1500).pack(), true).get();
saveSimpleRecord(1066, 42);
saveSimpleRecord(1776, 100);
assertThrowsAggregateFunctionNotSupported(() -> recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.SUM, sumIndex.getRootExpression(), sumIndex.getName()), TupleRange.ALL, IsolationLevel.SERIALIZABLE).get(), "value3sum.sum(Field { 'num_value_3_indexed' None} group 1)");
assertThrowsAggregateFunctionNotSupported(() -> recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.MAX_EVER, maxIndex.getRootExpression(), maxIndex.getName()), TupleRange.ALL, IsolationLevel.SERIALIZABLE).get(), "value3max.max_ever(Field { 'num_value_3_indexed' None} group 1)");
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
recordStore.uncheckedMarkIndexReadable("value3sum").join();
recordStore.uncheckedMarkIndexReadable("value3max").join();
// Unsafe: made readable without building indexes, which is why sum gets wrong answer.
assertEquals(42L, recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.SUM, sumIndex.getRootExpression(), sumIndex.getName()), TupleRange.ALL, IsolationLevel.SERIALIZABLE).get().getLong(0));
assertEquals(100L, recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.MAX_EVER, maxIndex.getRootExpression(), maxIndex.getName()), TupleRange.ALL, IsolationLevel.SERIALIZABLE).get().getLong(0));
recordStore.rebuildAllIndexes().get();
assertEquals(142L, recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.SUM, sumIndex.getRootExpression(), sumIndex.getName()), TupleRange.ALL, IsolationLevel.SERIALIZABLE).get().getLong(0));
assertEquals(100L, recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.MAX_EVER, maxIndex.getRootExpression(), maxIndex.getName()), TupleRange.ALL, IsolationLevel.SERIALIZABLE).get().getLong(0));
}
}
use of com.apple.foundationdb.record.metadata.IndexAggregateFunction in project fdb-record-layer by FoundationDB.
the class FDBRestrictedIndexQueryTest method queryAggregateWithFilteredIndex.
/**
* Verify that disabled aggregate indexes are not used by the planner.
* Verify that re-enabling those indexes allows the planner to use them.
* TODO: Abstract out common code in queryWithWriteOnly, queryWithDisabled, queryAggregateWithWriteOnly and queryAggregateWithDisabled (https://github.com/FoundationDB/fdb-record-layer/issues/4)
*/
@Test
void queryAggregateWithFilteredIndex() throws Exception {
Index sumIndex = new Index("value3sum", field("num_value_3_indexed").ungrouped(), IndexTypes.SUM);
Index maxIndex = new Index("value3max", field("num_value_3_indexed").ungrouped(), IndexTypes.MAX_EVER_TUPLE);
RecordMetaDataHook hook = metaData -> {
metaData.addIndex("MySimpleRecord", sumIndex);
metaData.addIndex("MySimpleRecord", maxIndex);
};
try (FDBRecordContext context = openContext()) {
// test filtered
openSimpleRecordStore(context, hook);
recordStore.deleteAllRecords();
saveSimpleRecord(1066, 42);
saveSimpleRecord(1776, 100);
assertThrowsAggregateFunctionNotSupported(() -> recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.SUM, sumIndex.getRootExpression(), null), TupleRange.ALL, IsolationLevel.SERIALIZABLE, IndexQueryabilityFilter.FALSE).get(), "sum(Field { 'num_value_3_indexed' None} group 1)");
assertThrowsAggregateFunctionNotSupported(() -> recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.MAX_EVER, maxIndex.getRootExpression(), null), TupleRange.ALL, IsolationLevel.SERIALIZABLE, IndexQueryabilityFilter.FALSE).get(), "max_ever(Field { 'num_value_3_indexed' None} group 1)");
commit(context);
}
try (FDBRecordContext context = openContext()) {
// test unfiltered
openSimpleRecordStore(context, hook);
assertEquals(142L, recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.SUM, sumIndex.getRootExpression(), null), TupleRange.ALL, IsolationLevel.SERIALIZABLE).get().getLong(0));
assertEquals(100L, recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.MAX_EVER, maxIndex.getRootExpression(), null), TupleRange.ALL, IsolationLevel.SERIALIZABLE).get().getLong(0));
assertEquals(142L, recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.SUM, sumIndex.getRootExpression(), null), TupleRange.ALL, IsolationLevel.SERIALIZABLE, IndexQueryabilityFilter.TRUE).get().getLong(0));
assertEquals(100L, recordStore.evaluateAggregateFunction(Collections.singletonList("MySimpleRecord"), new IndexAggregateFunction(FunctionNames.MAX_EVER, maxIndex.getRootExpression(), null), TupleRange.ALL, IsolationLevel.SERIALIZABLE, IndexQueryabilityFilter.TRUE).get().getLong(0));
}
}
use of com.apple.foundationdb.record.metadata.IndexAggregateFunction in project fdb-record-layer by FoundationDB.
the class FDBRecordStore method getSnapshotRecordCountForRecordType.
@Override
public CompletableFuture<Long> getSnapshotRecordCountForRecordType(@Nonnull String recordTypeName, @Nonnull IndexQueryabilityFilter indexQueryabilityFilter) {
// A COUNT index on this record type.
IndexAggregateFunction aggregateFunction = IndexFunctionHelper.count(EmptyKeyExpression.EMPTY);
Optional<IndexMaintainer> indexMaintainer = IndexFunctionHelper.indexMaintainerForAggregateFunction(this, aggregateFunction, Collections.singletonList(recordTypeName), indexQueryabilityFilter);
if (indexMaintainer.isPresent()) {
return indexMaintainer.get().evaluateAggregateFunction(aggregateFunction, TupleRange.ALL, IsolationLevel.SNAPSHOT).thenApply(tuple -> tuple.getLong(0));
}
// A universal COUNT index by record type.
// In fact, any COUNT index by record type that applied to this record type would work, no matter what other
// types it applied to.
aggregateFunction = IndexFunctionHelper.count(Key.Expressions.recordType());
indexMaintainer = IndexFunctionHelper.indexMaintainerForAggregateFunction(this, aggregateFunction, Collections.emptyList(), indexQueryabilityFilter);
if (indexMaintainer.isPresent()) {
RecordType recordType = getRecordMetaData().getRecordType(recordTypeName);
return indexMaintainer.get().evaluateAggregateFunction(aggregateFunction, TupleRange.allOf(recordType.getRecordTypeKeyTuple()), IsolationLevel.SNAPSHOT).thenApply(tuple -> tuple.getLong(0));
}
throw recordCoreException("Require a COUNT index on " + recordTypeName);
}
use of com.apple.foundationdb.record.metadata.IndexAggregateFunction in project lionrock by panghy.
the class RemoteRecordLayerTests method testRichIndex_getNumberOfCustomersWhoHaveBooksListedAsPreferenceTags.
@Test
public void testRichIndex_getNumberOfCustomersWhoHaveBooksListedAsPreferenceTags() {
Long bookPreferenceCount = fdb.runAsync((FDBRecordContext cx) -> recordStoreBuilder.copyBuilder().setContext(cx).openAsync().thenCompose(store -> {
Index index = store.getRecordMetaData().getIndex("preference_tag_count");
IndexAggregateFunction function = new IndexAggregateFunction(FunctionNames.COUNT, index.getRootExpression(), index.getName());
return store.evaluateAggregateFunction(Collections.singletonList("Customer"), function, Key.Evaluated.scalar("books"), IsolationLevel.SERIALIZABLE).thenApply(tuple -> tuple.getLong(0));
})).join();
assertEquals(2, bookPreferenceCount);
}
Aggregations