Search in sources :

Example 41 with KeyExpression

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);
}
Also used : RecordMetaData(com.apple.foundationdb.record.RecordMetaData) Arrays(java.util.Arrays) QueryHashKind(com.apple.foundationdb.record.QueryHashable.QueryHashKind) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) TextSamples(com.apple.foundationdb.record.provider.common.text.TextSamples) Expressions.concatenateFields(com.apple.foundationdb.record.metadata.Key.Expressions.concatenateFields) Arrays.asList(java.util.Arrays.asList) TextIndexTestUtils(com.apple.foundationdb.record.provider.foundationdb.indexes.TextIndexTestUtils) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) Tag(org.junit.jupiter.api.Tag) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) RecordMetaDataBuilder(com.apple.foundationdb.record.RecordMetaDataBuilder) Tags(com.apple.test.Tags) Expressions.keyWithValue(com.apple.foundationdb.record.metadata.Key.Expressions.keyWithValue) Test(org.junit.jupiter.api.Test) TestRecordsTextProto(com.apple.foundationdb.record.TestRecordsTextProto) List(java.util.List) Index(com.apple.foundationdb.record.metadata.Index) Expressions.value(com.apple.foundationdb.record.metadata.Key.Expressions.value) QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) Expressions.function(com.apple.foundationdb.record.metadata.Key.Expressions.function) Collections(java.util.Collections) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

Example 42 with KeyExpression

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);
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) PlannerBindings(com.apple.foundationdb.record.query.plan.temp.matchers.PlannerBindings) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) KeyPart(com.apple.foundationdb.record.query.plan.temp.KeyPart) RequestedOrdering(com.apple.foundationdb.record.query.plan.temp.RequestedOrdering) LogicalSortExpression(com.apple.foundationdb.record.query.plan.temp.expressions.LogicalSortExpression)

Example 43 with KeyExpression

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);
    }
}
Also used : QueryComponent(com.apple.foundationdb.record.query.expressions.QueryComponent) FunctionKeyExpression(com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression) LiteralKeyExpression(com.apple.foundationdb.record.metadata.expressions.LiteralKeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) QueryableKeyExpression(com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) HashSet(java.util.HashSet)

Example 44 with KeyExpression

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;
}
Also used : AvailableFields(com.apple.foundationdb.record.query.plan.AvailableFields) RecordQueryFetchFromPartialRecordPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryFetchFromPartialRecordPlan) RecordType(com.apple.foundationdb.record.metadata.RecordType) IndexKeyValueToPartialRecord(com.apple.foundationdb.record.query.plan.IndexKeyValueToPartialRecord) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) RecordQueryPlanWithIndex(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex) RecordQueryCoveringIndexPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryCoveringIndexPlan) RecordQueryPlanWithIndex(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlanWithIndex) Index(com.apple.foundationdb.record.metadata.Index) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

Example 45 with KeyExpression

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));
}
Also used : ListKeyExpression(com.apple.foundationdb.record.metadata.expressions.ListKeyExpression) GroupingKeyExpression(com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) QueryableKeyExpression(com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression) FunctionKeyExpression(com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression) FieldKeyExpression(com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression) NestingKeyExpression(com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression) SplitKeyExpression(com.apple.foundationdb.record.metadata.expressions.SplitKeyExpression) EmptyKeyExpression(com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression) ThenKeyExpression(com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

KeyExpression (com.apple.foundationdb.record.metadata.expressions.KeyExpression)152 GroupingKeyExpression (com.apple.foundationdb.record.metadata.expressions.GroupingKeyExpression)85 Test (org.junit.jupiter.api.Test)63 FieldKeyExpression (com.apple.foundationdb.record.metadata.expressions.FieldKeyExpression)60 EmptyKeyExpression (com.apple.foundationdb.record.metadata.expressions.EmptyKeyExpression)56 NestingKeyExpression (com.apple.foundationdb.record.metadata.expressions.NestingKeyExpression)56 ThenKeyExpression (com.apple.foundationdb.record.metadata.expressions.ThenKeyExpression)52 Nonnull (javax.annotation.Nonnull)52 Index (com.apple.foundationdb.record.metadata.Index)37 List (java.util.List)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)35 Nullable (javax.annotation.Nullable)33 RecordQueryPlan (com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan)30 FunctionKeyExpression (com.apple.foundationdb.record.metadata.expressions.FunctionKeyExpression)28 Message (com.google.protobuf.Message)26 ArrayList (java.util.ArrayList)26 RecordMetaData (com.apple.foundationdb.record.RecordMetaData)25 QueryableKeyExpression (com.apple.foundationdb.record.metadata.expressions.QueryableKeyExpression)25 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)24 ListKeyExpression (com.apple.foundationdb.record.metadata.expressions.ListKeyExpression)23