Search in sources :

Example 31 with KeyExpression

use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.

the class KeyExpressionTest method testFunctionEvalToWrongType.

@Test
public void testFunctionEvalToWrongType() throws Exception {
    assertThrows(KeyExpression.InvalidResultException.class, () -> {
        final KeyExpression expression = function("substr", concat(field("field"), field("field"), value(2)));
        expression.validate(TestScalarFieldAccess.getDescriptor());
        evaluate(expression, plantsBoxesAndBowls);
    });
}
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)

Example 32 with KeyExpression

use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.

the class KeyExpressionTest method testSubstrFunctionStaticFanout.

@Test
public void testSubstrFunctionStaticFanout() throws Exception {
    final KeyExpression expression = function("substr", concat(field("repeat_me", FanType.FanOut), value(0), value(3)));
    expression.validate(TestScalarFieldAccess.getDescriptor());
    List<Key.Evaluated> results = evaluate(expression, plantsBoxesAndBowls);
    assertEquals(2, results.size(), "Wrong number of results");
    assertEquals(ImmutableList.of(Key.Evaluated.scalar("Box"), Key.Evaluated.scalar("Bow")), results);
}
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)

Example 33 with KeyExpression

use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.

the class KeyExpressionTest method testFieldThenConcatenateRepeated.

@Test
public void testFieldThenConcatenateRepeated() throws Exception {
    final KeyExpression expression = Key.Expressions.concat(field("field"), field("repeat_me", FanType.Concatenate));
    expression.validate(TestScalarFieldAccess.getDescriptor());
    assertFalse(expression.createsDuplicates());
    assertEquals(Collections.singletonList(Key.Evaluated.concatenate("Plants", Arrays.asList("Boxes", "Bowls"))), evaluate(expression, plantsBoxesAndBowls));
    assertEquals(Collections.singletonList(Key.Evaluated.concatenate(NULL, Collections.emptyList())), evaluate(expression, emptyScalar));
    assertEquals(Collections.singletonList(Key.Evaluated.concatenate(NULL, 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)

Example 34 with KeyExpression

use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.

the class KeyExpressionTest method testDoubleNested.

@Test
public void testDoubleNested() throws Exception {
    final KeyExpression expression = concat(field("id"), field("order", FanType.FanOut).nest(field("id"), field("item", FanType.FanOut).nest(field("id"), field("name"))), field("first_name"), field("last_name"));
    expression.validate(Customer.getDescriptor());
    assertTrue(expression.createsDuplicates());
    // note Same as testDoubleNestedWithExtraConcats
    assertEquals(Arrays.asList(concatenate("customer1", "order1", "i1", "a1", "1 first name", "1 last name"), concatenate("customer1", "order1", "j1", "a2", "1 first name", "1 last name"), concatenate("customer1", "order2", "i2", "b1", "1 first name", "1 last name"), concatenate("customer1", "order2", "j2", "b2", "1 first name", "1 last name"), concatenate("customer1", "order3", "i3", "c1", "1 first name", "1 last name"), concatenate("customer1", "order3", "j3", "c2", "1 first name", "1 last name"), concatenate("customer1", "order3", "k3", "c3", "1 first name", "1 last name"), concatenate("customer1", "order3", "l3", "c4", "1 first name", "1 last name")), evaluate(expression, customer));
    assertEquals(Collections.emptyList(), evaluate(expression, emptyCustomer));
    assertEquals(Collections.emptyList(), evaluate(expression, aleph));
    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)

Example 35 with KeyExpression

use of com.apple.foundationdb.record.metadata.expressions.KeyExpression in project fdb-record-layer by FoundationDB.

the class FDBCollateQueryTest method coveringIndex.

@Test
public void coveringIndex() throws Exception {
    // Note how the name field needs to be repeated in the value because it can't be recovered from an index
    // entry after transformation to a collation key.
    final KeyExpression collateKey = function(collateFunctionName, concat(NAME_FIELD, value("da_DK")));
    final KeyExpression indexKey = keyWithValue(concat(collateKey, NAME_FIELD), 1);
    final RecordMetaDataHook hook = md -> {
        md.removeIndex("MySimpleRecord$str_value_indexed");
        md.addIndex("MySimpleRecord", "collated_name", indexKey);
    };
    loadNames(hook);
    final RecordQuery query = RecordQuery.newBuilder().setRecordType("MySimpleRecord").setFilter(Query.keyExpression(collateKey).lessThan("B")).setRequiredResults(Arrays.asList(NAME_FIELD)).build();
    final List<String> actual = queryNames(query, hook);
    final List<String> expected = Arrays.asList("Ampère");
    assertEquals(expected, actual);
    RecordQueryPlan plan = planner.plan(query);
    assertThat(plan, coveringIndexScan(indexScan("collated_name")));
}
Also used : Arrays(java.util.Arrays) PlanMatchers.indexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexScan) Bindings(com.apple.foundationdb.record.Bindings) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) PlanMatchers.bounds(com.apple.foundationdb.record.query.plan.match.PlanMatchers.bounds) Expressions.concat(com.apple.foundationdb.record.metadata.Key.Expressions.concat) CollateFunctionKeyExpressionFactoryJRE(com.apple.foundationdb.record.metadata.expressions.CollateFunctionKeyExpressionFactoryJRE) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Nonnull(javax.annotation.Nonnull) Expressions.field(com.apple.foundationdb.record.metadata.Key.Expressions.field) PlanMatchers.coveringIndexScan(com.apple.foundationdb.record.query.plan.match.PlanMatchers.coveringIndexScan) Query(com.apple.foundationdb.record.query.expressions.Query) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) TestRecords1Proto(com.apple.foundationdb.record.TestRecords1Proto) Tags(com.apple.test.Tags) Matchers.allOf(org.hamcrest.Matchers.allOf) Expressions.keyWithValue(com.apple.foundationdb.record.metadata.Key.Expressions.keyWithValue) Test(org.junit.jupiter.api.Test) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) List(java.util.List) PlanMatchers.indexName(com.apple.foundationdb.record.query.plan.match.PlanMatchers.indexName) Expressions.value(com.apple.foundationdb.record.metadata.Key.Expressions.value) EvaluationContext(com.apple.foundationdb.record.EvaluationContext) Expressions.function(com.apple.foundationdb.record.metadata.Key.Expressions.function) RecordQueryPlan(com.apple.foundationdb.record.query.plan.plans.RecordQueryPlan) KeyExpression(com.apple.foundationdb.record.metadata.expressions.KeyExpression) PlanMatchers.hasTupleString(com.apple.foundationdb.record.query.plan.match.PlanMatchers.hasTupleString) RecordQuery(com.apple.foundationdb.record.query.RecordQuery) Test(org.junit.jupiter.api.Test)

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