use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class QueryHashTest method compareParameter.
@Test
void compareParameter() {
final KeyExpression key1 = function("collate_jre", concat(field("str_value_indexed"), value("de_DE")));
final KeyExpression key2 = function("collate_jre", concat(field("str_value_indexed"), value("en_EN")));
final RecordMetaDataHook hook = md -> {
md.removeIndex("MySimpleRecord$str_value_indexed");
md.addIndex("MySimpleRecord", "collated_name1", key1);
md.addIndex("MySimpleRecord", "collated_name2", key2);
};
runHook(hook);
RecordQuery query1 = createQuery("MySimpleRecord", Query.keyExpression(key1).equalsParameter("name"), null, Collections.singletonList(field("str_value_indexed")));
RecordQuery query2 = createQuery("MySimpleRecord", Query.keyExpression(key2).equalsParameter("no-name"), null, Collections.singletonList(field("str_value_indexed")));
assertHash(query1, 334321807, -113593969);
assertHash(query2, -1234725093, -278982629);
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class PushInterestingOrderingThroughSortRule method onMatch.
@Override
public void onMatch(@Nonnull PlannerRuleCall call) {
final PlannerBindings bindings = call.getBindings();
final LogicalSortExpression logicalSortExpression = bindings.get(root);
final ExpressionRef<? extends RelationalExpression> lowerRef = bindings.get(lowerRefMatcher);
final KeyExpression sortKeyExpression = logicalSortExpression.getSort();
if (sortKeyExpression == null) {
call.pushRequirement(lowerRef, OrderingAttribute.ORDERING, ImmutableSet.of(RequestedOrdering.preserve()));
} else {
final List<KeyExpression> normalizedSortKeys = sortKeyExpression.normalizeKeyForPositions();
final ImmutableList.Builder<KeyPart> keyPartBuilder = ImmutableList.builder();
for (final KeyExpression keyExpression : normalizedSortKeys) {
keyPartBuilder.add(KeyPart.of(keyExpression, logicalSortExpression.isReverse()));
}
final var orderings = ImmutableSet.of(new RequestedOrdering(keyPartBuilder.build(), RequestedOrdering.Distinctness.PRESERVE_DISTINCTNESS));
call.pushRequirement(lowerRef, OrderingAttribute.ORDERING, orderings);
}
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class FilterVisitor method partitionFilters.
public static void partitionFilters(@Nonnull final List<QueryComponent> filters, @Nonnull final AvailableFields availableFields, @Nonnull final List<QueryComponent> indexFilters, @Nonnull final List<QueryComponent> residualFilters, @Nullable final Set<KeyExpression> allReferencedFields) {
for (final QueryComponent filter : filters) {
final Set<KeyExpression> referencedFields = new HashSet<>();
if (findFilterReferencedFields(filter, referencedFields)) {
if (availableFields.containsAll(referencedFields)) {
indexFilters.add(filter);
if (allReferencedFields != null) {
allReferencedFields.addAll(referencedFields);
}
continue;
}
}
residualFilters.add(filter);
}
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class RecordQueryPlannerSubstitutionVisitor method removeIndexFetch.
@Nullable
public static RecordQueryPlan removeIndexFetch(@Nonnull RecordMetaData recordMetaData, @Nonnull PlannableIndexTypes indexTypes, @Nullable KeyExpression commonPrimaryKey, @Nonnull RecordQueryPlan plan, @Nonnull Set<KeyExpression> requiredFields) {
if (plan instanceof RecordQueryPlanWithIndex) {
RecordQueryPlanWithIndex indexPlan = (RecordQueryPlanWithIndex) plan;
Index index = recordMetaData.getIndex(indexPlan.getIndexName());
final Collection<RecordType> recordTypes = recordMetaData.recordTypesForIndex(index);
if (recordTypes.size() != 1) {
return null;
}
final RecordType recordType = Iterables.getOnlyElement(recordTypes);
AvailableFields fieldsFromIndex = AvailableFields.fromIndex(recordType, index, indexTypes, commonPrimaryKey);
Set<KeyExpression> fields = new HashSet<>(requiredFields);
if (commonPrimaryKey != null) {
// Need the primary key, even if it wasn't one of the explicit result fields.
fields.addAll(commonPrimaryKey.normalizeKeyForPositions());
}
if (fieldsFromIndex.containsAll(fields)) {
final IndexKeyValueToPartialRecord keyValueToPartialRecord = fieldsFromIndex.buildIndexKeyValueToPartialRecord(recordType).build();
if (keyValueToPartialRecord != null) {
return new RecordQueryCoveringIndexPlan(indexPlan, recordType.getName(), fieldsFromIndex, keyValueToPartialRecord);
}
}
} else if (plan instanceof RecordQueryFetchFromPartialRecordPlan) {
RecordQueryFetchFromPartialRecordPlan fetchPlan = (RecordQueryFetchFromPartialRecordPlan) plan;
if (fetchPlan.getChild().getAvailableFields().containsAll(requiredFields)) {
return ((RecordQueryFetchFromPartialRecordPlan) plan).getChild();
}
}
return null;
}
use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.
the class KeyExpressionTest method testNestWithParentField2.
@Test
public void testNestWithParentField2() throws Exception {
final KeyExpression expression = field("repeated_nesty", FanType.FanOut).nest(field("regular_old_field"), field("repeated_field", FanType.FanOut));
expression.validate(NestedField.getDescriptor());
assertTrue(expression.createsDuplicates());
assertEquals(Arrays.asList(concatenate("Daughter", "daffodil"), concatenate("Sister", "lady slipper"), concatenate("Sister", "orchid"), concatenate("Sister", "morning glory")), evaluate(expression, matryoshkaDolls));
assertEquals(Collections.emptyList(), evaluate(expression, emptyNested));
assertEquals(Collections.emptyList(), evaluate(expression, lonelyDoll));
assertEquals(Collections.emptyList(), evaluate(expression, null));
}
Aggregations