use of com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreIndexTest method minMaxTupleUngrouped.
@Test
public void minMaxTupleUngrouped() throws Exception {
final FieldKeyExpression fieldKey = field("num_value_3_indexed");
final GroupingKeyExpression indexKey = fieldKey.ungrouped();
final RecordMetaDataHook hook = md -> {
RecordTypeBuilder type = md.getRecordType("MySimpleRecord");
md.addIndex(type, new Index("min", indexKey, IndexTypes.MIN_EVER_TUPLE));
md.addIndex(type, new Index("max", indexKey, IndexTypes.MAX_EVER_TUPLE));
};
final IndexAggregateFunction min = new IndexAggregateFunction(FunctionNames.MIN_EVER, fieldKey, null);
final IndexAggregateFunction max = new IndexAggregateFunction(FunctionNames.MAX_EVER, fieldKey, null);
List<String> types = Collections.singletonList("MySimpleRecord");
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertNull(recordStore.evaluateAggregateFunction(types, min, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
assertNull(recordStore.evaluateAggregateFunction(types, max, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
for (int i = 0; i < 100; i++) {
TestRecords1Proto.MySimpleRecord.Builder recBuilder = TestRecords1Proto.MySimpleRecord.newBuilder();
recBuilder.setRecNo(i);
recBuilder.setNumValue3Indexed(i * 10);
recordStore.saveRecord(recBuilder.build());
}
commit(context);
}
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
assertEquals(Tuple.from(0), recordStore.evaluateAggregateFunction(types, min, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
assertEquals(Tuple.from(990), recordStore.evaluateAggregateFunction(types, max, Key.Evaluated.EMPTY, IsolationLevel.SNAPSHOT).join());
commit(context);
}
}
use of com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression 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.expressions.GroupingKeyExpression in project fdb-record-layer by FoundationDB.
the class MetaDataProtoTest method indexGroupingCompatibility.
@Test
// Older (deprecated) types had special grouping behavior
@SuppressWarnings("deprecation")
public void indexGroupingCompatibility() throws Exception {
RecordMetaDataProto.MetaData.Builder protoBuilder = RecordMetaDataProto.MetaData.newBuilder();
protoBuilder.setRecords(TestRecordsIndexCompatProto.getDescriptor().toProto());
protoBuilder.addIndexesBuilder().setName("RecordCount").setType(IndexTypes.COUNT).addRecordType("MyModernRecord").setRootExpression(RecordMetaDataProto.KeyExpression.newBuilder().setEmpty(RecordMetaDataProto.Empty.getDefaultInstance()));
protoBuilder.addIndexesBuilder().setName("MaxRecNo").setType(IndexTypes.MAX_EVER).addRecordType("MyModernRecord").setRootExpression(RecordMetaDataProto.KeyExpression.newBuilder().setField(scalarField("rec_no")));
protoBuilder.addIndexesBuilder().setName("MaxRecNoGrouped").setType(IndexTypes.MAX_EVER).addRecordType("MyModernRecord").setRootExpression(RecordMetaDataProto.KeyExpression.newBuilder().setThen(RecordMetaDataProto.Then.newBuilder().addChild(RecordMetaDataProto.KeyExpression.newBuilder().setField(scalarField("index"))).addChild(RecordMetaDataProto.KeyExpression.newBuilder().setField(scalarField("rec_no")))));
RecordMetaData metaData = RecordMetaData.newBuilder().addDependencies(BASE_DEPENDENCIES).setRecords(protoBuilder.build(), true).getRecordMetaData();
Index regularIndex = metaData.getIndex("MyCompatRecord$index");
assertFalse(regularIndex.getRootExpression() instanceof GroupingKeyExpression, "should not have Grouping");
Index compatRank = metaData.getIndex("MyCompatRecord$rank");
assertTrue(compatRank.getRootExpression() instanceof GroupingKeyExpression, "should have Grouping");
assertEquals(1, ((GroupingKeyExpression) compatRank.getRootExpression()).getGroupedCount());
Index modernRank = metaData.getIndex("MyModernRecord$rank");
assertTrue(modernRank.getRootExpression() instanceof GroupingKeyExpression, "should have Grouping");
assertEquals(1, ((GroupingKeyExpression) modernRank.getRootExpression()).getGroupedCount());
Index recordCount = metaData.getIndex("RecordCount");
assertTrue(recordCount.getRootExpression() instanceof GroupingKeyExpression, "should have Grouping");
assertEquals(0, ((GroupingKeyExpression) recordCount.getRootExpression()).getGroupedCount());
Index maxRecNo = metaData.getIndex("MaxRecNo");
assertTrue(maxRecNo.getRootExpression() instanceof GroupingKeyExpression, "should have Grouping");
assertEquals(1, ((GroupingKeyExpression) maxRecNo.getRootExpression()).getGroupedCount());
Index maxRecNoGrouped = metaData.getIndex("MaxRecNoGrouped");
assertTrue(maxRecNoGrouped.getRootExpression() instanceof GroupingKeyExpression, "should have Grouping");
assertEquals(1, ((GroupingKeyExpression) maxRecNoGrouped.getRootExpression()).getGroupedCount());
}
use of com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreDeleteWhereTest method testDeleteWhereGroupedCount.
@Test
public void testDeleteWhereGroupedCount() throws Exception {
KeyExpression groupExpr = concat(field("header").nest(field("rec_no")), field("header").nest(field("path")));
RecordMetaDataHook hook = metaData -> {
metaData.getRecordType("MyRecord").setPrimaryKey(concat(field("header").nest(field("rec_no")), field("header").nest(field("path")), field("header").nest(field("num"))));
metaData.addUniversalIndex(new Index("MyRecord$groupedCount", new GroupingKeyExpression(groupExpr, 0), IndexTypes.COUNT));
metaData.addUniversalIndex(new Index("MyRecord$groupedUpdateCount", new GroupingKeyExpression(groupExpr, 0), IndexTypes.COUNT_UPDATES));
};
try (FDBRecordContext context = openContext()) {
openRecordWithHeader(context, hook);
saveHeaderRecord(1, "a", 0, "lynx");
saveHeaderRecord(1, "a", 1, "bobcat");
saveHeaderRecord(1, "b", 2, "panther");
saveHeaderRecord(2, "a", 3, "jaguar");
saveHeaderRecord(2, "b", 4, "leopard");
saveHeaderRecord(2, "c", 5, "lion");
saveHeaderRecord(2, "d", 6, "tiger");
context.commit();
}
try (FDBRecordContext context = openContext()) {
openRecordWithHeader(context, hook);
// Number of records where first component of primary key is 1
assertEquals(3, recordStore.getSnapshotRecordCount(groupExpr, Key.Evaluated.scalar(1)).join().longValue());
// Number of updates to such records
assertEquals(3, recordStore.getSnapshotRecordUpdateCount(groupExpr, Key.Evaluated.scalar(1)).join().longValue());
recordStore.deleteRecordsWhere(Query.and(Query.field("header").matches(Query.field("rec_no").equalsValue(1)), Query.field("header").matches(Query.field("path").equalsValue("a"))));
assertEquals(1, recordStore.getSnapshotRecordCount(groupExpr, Key.Evaluated.scalar(1)).join().longValue());
// Deleting by group prefix resets the update counter for the group(s)
assertEquals(1, recordStore.getSnapshotRecordUpdateCount(groupExpr, Key.Evaluated.scalar(1)).join().longValue());
context.commit();
}
}
use of com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression in project fdb-record-layer by FoundationDB.
the class SyntheticRecordPlannerTest method rankJoinIndex.
@Test
public void rankJoinIndex() throws Exception {
final JoinedRecordTypeBuilder joined = metaDataBuilder.addJoinedRecordType("JoinedForRank");
joined.addConstituent("simple", "MySimpleRecord");
joined.addConstituent("other", "MyOtherRecord");
joined.addJoin("simple", "other_rec_no", "other", "rec_no");
final GroupingKeyExpression group = field("simple").nest("num_value_2").groupBy(field("other").nest("num_value"));
metaDataBuilder.addIndex(joined, new Index("simple.num_value_2_by_other.num_value", group, IndexTypes.RANK));
try (FDBRecordContext context = openContext()) {
final FDBRecordStore recordStore = recordStoreBuilder.setContext(context).create();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < i; j++) {
TestRecordsJoinIndexProto.MySimpleRecord.Builder simple = TestRecordsJoinIndexProto.MySimpleRecord.newBuilder();
simple.setRecNo(100 * i + j).setOtherRecNo(1000 + i);
simple.setNumValue2(i + j);
recordStore.saveRecord(simple.build());
}
TestRecordsJoinIndexProto.MyOtherRecord.Builder other = TestRecordsJoinIndexProto.MyOtherRecord.newBuilder();
other.setRecNo(1000 + i);
other.setNumValue(i % 2);
recordStore.saveRecord(other.build());
}
context.commit();
}
try (FDBRecordContext context = openContext()) {
final FDBRecordStore recordStore = recordStoreBuilder.setContext(context).open();
Index index = recordStore.getRecordMetaData().getIndex("simple.num_value_2_by_other.num_value");
RecordCursor<IndexEntry> cursor = recordStore.scanIndex(index, IndexScanType.BY_RANK, TupleRange.allOf(Tuple.from(0, 1)), null, ScanProperties.FORWARD_SCAN);
Tuple pkey = cursor.first().get().map(IndexEntry::getPrimaryKey).orElse(null);
assertFalse(cursor.getNext().hasNext());
// 201, 1002 and 200, 1003 both have score 3, but in different groups.
assertEquals(Tuple.from(-1, Tuple.from(201), Tuple.from(1002)), pkey);
FDBSyntheticRecord record = recordStore.loadSyntheticRecord(pkey).join();
IndexRecordFunction<Long> rankFunction = ((IndexRecordFunction<Long>) Query.rank(group).getFunction()).cloneWithIndex(index.getName());
assertEquals(1, recordStore.evaluateRecordFunction(rankFunction, record).join().longValue());
}
}
Aggregations