use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class TextIndexTest method invalidIndexes.
@Test
public void invalidIndexes() {
final String testIndex = "test_index";
List<KeyExpression> expressions = Arrays.asList(// Version but not where text is
concat(field("text"), VersionKeyExpression.VERSION), // not on text field
field("group"), field("group").groupBy(field("text")), concatenateFields("group", "text"), field("text", FanType.FanOut));
for (KeyExpression expression : expressions) {
LOGGER.info(KeyValueLogMessage.of("testing index expression", LogMessageKeys.KEY_EXPRESSION, expression));
invalidateIndex(KeyExpression.InvalidExpressionException.class, new Index(testIndex, expression, IndexTypes.TEXT));
}
// Verify doesn't work with repeated fields but in a case where a repeated field is possible.
invalidateIndex(KeyExpression.InvalidExpressionException.class, "MultiDocument", new Index(testIndex, field("text", FanType.FanOut), IndexTypes.TEXT));
// Verify that unique indexes are invalid
invalidateIndex(MetaDataException.class, new Index(testIndex, field("text"), EmptyKeyExpression.EMPTY, IndexTypes.TEXT, IndexOptions.UNIQUE_OPTIONS));
// Verify that it fails when there is a value expression
invalidateIndex(KeyExpression.InvalidExpressionException.class, new Index(testIndex, field("text"), field("group"), IndexTypes.TEXT, IndexOptions.EMPTY_OPTIONS));
invalidateIndex(KeyExpression.InvalidExpressionException.class, new Index(testIndex, field("text").groupBy(field("group")), field("group"), IndexTypes.TEXT, IndexOptions.EMPTY_OPTIONS));
// Tokenizer does not exist
invalidateIndex(MetaDataException.class, new Index(testIndex, field("text"), IndexTypes.TEXT, ImmutableMap.of(IndexOptions.TEXT_TOKENIZER_NAME_OPTION, "not_a_real_tokenizer")));
// Invalid tokenizer versions
invalidateIndex(MetaDataException.class, new Index(testIndex, field("text"), IndexTypes.TEXT, ImmutableMap.of(IndexOptions.TEXT_TOKENIZER_NAME_OPTION, PrefixTextTokenizer.NAME, IndexOptions.TEXT_TOKENIZER_VERSION_OPTION, "-1")));
invalidateIndex(MetaDataException.class, new Index(testIndex, field("text"), IndexTypes.TEXT, ImmutableMap.of(IndexOptions.TEXT_TOKENIZER_NAME_OPTION, PrefixTextTokenizer.NAME, IndexOptions.TEXT_TOKENIZER_VERSION_OPTION, "1000")));
invalidateIndex(MetaDataException.class, new Index(testIndex, field("text"), IndexTypes.TEXT, ImmutableMap.of(IndexOptions.TEXT_TOKENIZER_NAME_OPTION, PrefixTextTokenizer.NAME, IndexOptions.TEXT_TOKENIZER_VERSION_OPTION, "one")));
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class VersionIndexTest method invalidIndexes.
@Test
public void invalidIndexes() {
List<KeyExpression> expressions = Arrays.asList(field("num_value_2"), field("num_value_2").groupBy(field("num_value_3_indexed")), field("num_value_2").groupBy(VersionKeyExpression.VERSION), concat(field("num_value_2"), VersionKeyExpression.VERSION).groupBy(field("num_value_3_indexed")), concat(VersionKeyExpression.VERSION, field("num_value_2")).groupBy(field("num_value_3_indexed")), concat(VersionKeyExpression.VERSION, VersionKeyExpression.VERSION));
for (KeyExpression expression : expressions) {
assertThrows(KeyExpression.InvalidExpressionException.class, () -> {
Index index = new Index("test_index", expression, IndexTypes.VERSION);
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
metaDataBuilder.addIndex("MySimpleRecord", index);
metaDataBuilder.getRecordMetaData();
});
}
assertThrows(MetaDataException.class, () -> {
Index index = new Index("test_index", VersionKeyExpression.VERSION, EmptyKeyExpression.EMPTY, IndexTypes.VERSION, IndexOptions.UNIQUE_OPTIONS);
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords1Proto.getDescriptor());
metaDataBuilder.addIndex("MySimpleRecord", index);
metaDataBuilder.getRecordMetaData();
});
assertThrows(MetaDataException.class, () -> {
Index index = new Index("global_version", VersionKeyExpression.VERSION, IndexTypes.VERSION);
RecordMetaDataBuilder metaDataBuilder = RecordMetaData.newBuilder().setRecords(TestRecords2Proto.getDescriptor());
metaDataBuilder.addUniversalIndex(index);
metaDataBuilder.getRecordMetaData();
});
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression 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())));
}
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBCollateQueryTest method rangeScan.
@Test
public void rangeScan() throws Exception {
final KeyExpression key = function(collateFunctionName, concat(NAME_FIELD, value("da_DK")));
final RecordMetaDataHook hook = md -> {
md.removeIndex("MySimpleRecord$str_value_indexed");
md.addIndex("MySimpleRecord", "collated_name", key);
};
loadNames(hook);
final RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.keyExpression(key).greaterThan("Z")).setRequiredResults(Arrays.asList(NAME_FIELD)).build();
final List<String> actual = queryNames(query, hook);
final List<String> expected = Arrays.asList("Ørsted", "Ångström");
assertEquals(expected, actual);
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, indexScan("collated_name"));
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FDBCollateQueryTest method sortOnly.
protected void sortOnly(String locale, String... expected) throws Exception {
final KeyExpression key;
final RecordMetaDataHook hook;
if (locale == null) {
key = NAME_FIELD;
hook = NO_HOOK;
} else {
key = function(collateFunctionName, concat(NAME_FIELD, value(locale)));
hook = md -> {
md.removeIndex("MySimpleRecord$str_value_indexed");
md.addIndex("MySimpleRecord", "collated_name", key);
};
}
loadNames(hook);
final RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setSort(key).setRequiredResults(Arrays.asList(NAME_FIELD)).build();
final List<String> actual = queryNames(query, hook);
assertEquals(Arrays.asList(expected), actual);
}
Aggregations