Search in sources :

Example 1 with Query

use of org.molgenis.emx2.Query in project entity-service by hypertrace.

the class EntityDataServiceTest method testEntityQueryOrderBy.

@Test
public void testEntityQueryOrderBy() {
    Entity entity1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("Some Service 1").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), generateRandomUUIDAttrValue()).putAttributes("foo", AttributeValue.newBuilder().setValue(Value.newBuilder().setInt(5).build()).build()).build();
    Entity createdEntity1 = entityDataServiceClient.upsert(entity1);
    assertNotNull(createdEntity1);
    assertNotNull(createdEntity1.getEntityId().trim());
    Entity entity2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("Some Service 2").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), generateRandomUUIDAttrValue()).putAttributes("foo", AttributeValue.newBuilder().setValue(Value.newBuilder().setInt(10).build()).build()).build();
    Entity createdEntity2 = entityDataServiceClient.upsert(entity2);
    assertNotNull(createdEntity2);
    assertNotNull(createdEntity2.getEntityId().trim());
    // Query by field name
    Query entityNameQuery = Query.newBuilder().setEntityType(EntityType.K8S_POD.name()).addOrderBy(OrderByExpression.newBuilder().setOrder(SortOrder.DESC).setName("entityName").build()).build();
    List<Entity> entitiesList = entityDataServiceClient.query(TENANT_ID, entityNameQuery);
    assertTrue(entitiesList.size() > 1);
    assertTrue(entitiesList.contains(createdEntity1) && entitiesList.contains(createdEntity2));
    // ordered such that entity with "larger" entity name value is listed earlier
    assertTrue(entitiesList.indexOf(createdEntity2) < entitiesList.indexOf(createdEntity1));
    // Query by attribute
    Query attributeQuery = Query.newBuilder().setEntityType(EntityType.K8S_POD.name()).addOrderBy(OrderByExpression.newBuilder().setOrder(SortOrder.DESC).setName("attributes.foo").build()).build();
    entitiesList = entityDataServiceClient.query(TENANT_ID, attributeQuery);
    assertTrue(entitiesList.size() > 1);
    assertTrue(entitiesList.contains(createdEntity1) && entitiesList.contains(createdEntity2));
    // ordered such that entity with "larger" attributes value is listed earlier
    assertTrue(entitiesList.indexOf(createdEntity2) < entitiesList.indexOf(createdEntity1));
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) Query(org.hypertrace.entity.data.service.v1.Query) Test(org.junit.jupiter.api.Test)

Example 2 with Query

use of org.molgenis.emx2.Query in project entity-service by hypertrace.

the class EntityDataServiceTest method testEntityNonAttributeQuery.

@Test
public void testEntityNonAttributeQuery() {
    Entity entity1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("Some Service 1").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), generateRandomUUIDAttrValue()).build();
    Entity createdEntity1 = entityDataServiceClient.upsert(entity1);
    assertNotNull(createdEntity1);
    assertFalse(createdEntity1.getEntityId().trim().isEmpty());
    Entity entity2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.K8S_POD.name()).setEntityName("Some Service 2").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_EXTERNAL_ID), generateRandomUUIDAttrValue()).build();
    Entity createdEntity2 = entityDataServiceClient.upsert(entity2);
    assertNotNull(createdEntity2);
    assertFalse(createdEntity2.getEntityId().trim().isEmpty());
    long afterCreatedTime = Instant.now().toEpochMilli();
    Query createTimeQuery = Query.newBuilder().setEntityType(EntityType.K8S_POD.name()).setFilter(AttributeFilter.newBuilder().setOperator(Operator.LT).setName("createdTime").setAttributeValue(AttributeValue.newBuilder().setValue(Value.newBuilder().setLong(afterCreatedTime).build())).build()).build();
    // Query all entities that created time is less than now
    List<Entity> entitiesList = entityDataServiceClient.query(TENANT_ID, createTimeQuery);
    assertTrue(entitiesList.size() > 1);
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) Query(org.hypertrace.entity.data.service.v1.Query) Test(org.junit.jupiter.api.Test)

Example 3 with Query

use of org.molgenis.emx2.Query in project entity-service by hypertrace.

the class DocStoreConverterTest method testStringArrayValueTypeColumnOrChain.

@Test
public void testStringArrayValueTypeColumnOrChain() throws JsonProcessingException {
    Query query = Query.newBuilder().addEntityId("some id").setFilter(AttributeFilter.newBuilder().setOperator(Operator.AND).addChildFilter(AttributeFilter.newBuilder().setName("attributes.some_col").setOperator(Operator.EQ).setAttributeValue(AttributeValue.newBuilder().setValue(Value.newBuilder().setString("some_val")))).addChildFilter(AttributeFilter.newBuilder().setName(ATTRIBUTES_LABELS_FIELD_NAME).setIsMultiValued(true).setOperator(Operator.IN).setAttributeValue(AttributeValue.newBuilder().setValueList(AttributeValueList.newBuilder().addValues(AttributeValue.newBuilder().setValue(Value.newBuilder().setString("l1"))).addValues(AttributeValue.newBuilder().setValue(Value.newBuilder().setString("l2"))).addValues(AttributeValue.newBuilder().setValue(Value.newBuilder().setString("l3"))))))).build();
    org.hypertrace.core.documentstore.Query transformedQuery = DocStoreConverter.transform(TENANT_ID, query, Collections.emptyList());
    Filter transformedFilter = transformedQuery.getFilter();
    Assertions.assertEquals(Filter.Op.AND, transformedFilter.getOp());
    Assertions.assertEquals(3, transformedFilter.getChildFilters().length);
    Assertions.assertEquals(EntityServiceConstants.ENTITY_ID, transformedFilter.getChildFilters()[1].getFieldName());
    Assertions.assertEquals(Collections.singletonList("some id"), transformedFilter.getChildFilters()[1].getValue());
    Assertions.assertEquals(Op.AND, transformedFilter.getChildFilters()[2].getOp());
    Assertions.assertEquals("attributes.some_col.value.string", transformedFilter.getChildFilters()[2].getChildFilters()[0].getFieldName());
    Assertions.assertEquals(Op.EQ, transformedFilter.getChildFilters()[2].getChildFilters()[0].getOp());
    Assertions.assertEquals("some_val", transformedFilter.getChildFilters()[2].getChildFilters()[0].getValue());
    Assertions.assertEquals(Op.OR, transformedFilter.getChildFilters()[2].getChildFilters()[1].getOp());
    Assertions.assertEquals(ATTRIBUTES_LABELS_FIELD_NAME + ".valueList.values", transformedFilter.getChildFilters()[2].getChildFilters()[1].getChildFilters()[0].getFieldName());
    Assertions.assertEquals(Op.EQ, transformedFilter.getChildFilters()[2].getChildFilters()[1].getChildFilters()[0].getOp());
    Assertions.assertEquals(OBJECT_MAPPER.convertValue(OBJECT_MAPPER.readTree("{\"value\": {\"string\":\"l1\"}}"), Map.class), transformedFilter.getChildFilters()[2].getChildFilters()[1].getChildFilters()[0].getValue());
    Assertions.assertEquals(ATTRIBUTES_LABELS_FIELD_NAME + ".valueList.values", transformedFilter.getChildFilters()[2].getChildFilters()[1].getChildFilters()[1].getFieldName());
    Assertions.assertEquals(Op.EQ, transformedFilter.getChildFilters()[2].getChildFilters()[1].getChildFilters()[1].getOp());
    Assertions.assertEquals(OBJECT_MAPPER.convertValue(OBJECT_MAPPER.readTree("{\"value\": {\"string\":\"l2\"}}"), Map.class), transformedFilter.getChildFilters()[2].getChildFilters()[1].getChildFilters()[1].getValue());
    Assertions.assertEquals(ATTRIBUTES_LABELS_FIELD_NAME + ".valueList.values", transformedFilter.getChildFilters()[2].getChildFilters()[1].getChildFilters()[2].getFieldName());
    Assertions.assertEquals(Op.EQ, transformedFilter.getChildFilters()[2].getChildFilters()[1].getChildFilters()[2].getOp());
    Assertions.assertEquals(OBJECT_MAPPER.convertValue(OBJECT_MAPPER.readTree("{\"value\": {\"string\":\"l3\"}}"), Map.class), transformedFilter.getChildFilters()[2].getChildFilters()[1].getChildFilters()[2].getValue());
}
Also used : Query(org.hypertrace.entity.data.service.v1.Query) AttributeFilter(org.hypertrace.entity.data.service.v1.AttributeFilter) Filter(org.hypertrace.core.documentstore.Filter) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 4 with Query

use of org.molgenis.emx2.Query in project entity-service by hypertrace.

the class DocStoreConverterTest method testEntityFieldAndAttributeValueConversion.

@Test
public void testEntityFieldAndAttributeValueConversion() {
    Query query = Query.newBuilder().addEntityId("some id").setFilter(AttributeFilter.newBuilder().setName(EntityConstants.ATTRIBUTES_MAP_PATH).setOperator(Operator.CONTAINS).setAttributeValue(AttributeValue.newBuilder().setValue(Value.newBuilder().setString("stringValue").build()).build()).build()).build();
    org.hypertrace.core.documentstore.Query transformedQuery = DocStoreConverter.transform(TENANT_ID, query, Collections.emptyList());
    Filter transformedFilter = transformedQuery.getFilter();
    Assertions.assertEquals(Filter.Op.AND, transformedFilter.getOp());
    Assertions.assertEquals(3, transformedFilter.getChildFilters().length);
    Assertions.assertEquals(EntityServiceConstants.ENTITY_ID, transformedFilter.getChildFilters()[1].getFieldName());
    Assertions.assertEquals(Collections.singletonList("some id"), transformedFilter.getChildFilters()[1].getValue());
    Assertions.assertEquals("attributes.value.string", transformedFilter.getChildFilters()[2].getFieldName());
    Assertions.assertEquals(Filter.Op.CONTAINS, transformedFilter.getChildFilters()[2].getOp());
    Assertions.assertEquals("stringValue", transformedFilter.getChildFilters()[2].getValue());
}
Also used : Query(org.hypertrace.entity.data.service.v1.Query) AttributeFilter(org.hypertrace.entity.data.service.v1.AttributeFilter) Filter(org.hypertrace.core.documentstore.Filter) Test(org.junit.jupiter.api.Test)

Example 5 with Query

use of org.molgenis.emx2.Query in project entity-service by hypertrace.

the class DocStoreConverterTest method testInFilterConversion.

@Test
public void testInFilterConversion() {
    Query query = Query.newBuilder().setFilter(AttributeFilter.newBuilder().setName(EntityConstants.ATTRIBUTES_MAP_PATH).setOperator(Operator.IN).setAttributeValue(AttributeValue.newBuilder().setValueList(AttributeValueList.newBuilder().addValues(AttributeValue.newBuilder().setValue(Value.newBuilder().setString("attr-v1"))).addValues(AttributeValue.newBuilder().setValue(Value.newBuilder().setString("attr-v2"))).addValues(AttributeValue.newBuilder().setValue(Value.newBuilder().setString("attr-v3")))).build()).build()).build();
    org.hypertrace.core.documentstore.Query transformedQuery = DocStoreConverter.transform(TENANT_ID, query, Collections.emptyList());
    Filter transformedFilter = transformedQuery.getFilter();
    Assertions.assertEquals(Filter.Op.AND, transformedFilter.getOp());
    Assertions.assertEquals(2, transformedFilter.getChildFilters().length);
    Assertions.assertEquals("attributes.value.string", transformedFilter.getChildFilters()[1].getFieldName());
    Assertions.assertEquals(Filter.Op.IN, transformedFilter.getChildFilters()[1].getOp());
    Assertions.assertEquals(List.of("attr-v1", "attr-v2", "attr-v3"), transformedFilter.getChildFilters()[1].getValue());
    // Empty value list. Default to string attribute value type for the field name
    Query query2 = Query.newBuilder().setFilter(AttributeFilter.newBuilder().setName(EntityConstants.ATTRIBUTES_MAP_PATH).setOperator(Operator.IN).setAttributeValue(AttributeValue.newBuilder().setValueList(AttributeValueList.newBuilder()).build()).build()).build();
    org.hypertrace.core.documentstore.Query transformedQuery2 = DocStoreConverter.transform(TENANT_ID, query2, Collections.emptyList());
    Filter transformedFilter2 = transformedQuery2.getFilter();
    Assertions.assertEquals(Filter.Op.AND, transformedFilter2.getOp());
    Assertions.assertEquals(2, transformedFilter2.getChildFilters().length);
    Assertions.assertEquals("attributes.value.string", transformedFilter2.getChildFilters()[1].getFieldName());
    Assertions.assertEquals(Filter.Op.IN, transformedFilter2.getChildFilters()[1].getOp());
    Assertions.assertEquals(List.of(), transformedFilter2.getChildFilters()[1].getValue());
    // Long list
    Query query3 = Query.newBuilder().setFilter(AttributeFilter.newBuilder().setName(EntityConstants.ATTRIBUTES_MAP_PATH).setOperator(Operator.IN).setAttributeValue(AttributeValue.newBuilder().setValueList(AttributeValueList.newBuilder().addValues(AttributeValue.newBuilder().setValue(Value.newBuilder().setLong(21L))).addValues(AttributeValue.newBuilder().setValue(Value.newBuilder().setLong(22L)))).build()).build()).build();
    org.hypertrace.core.documentstore.Query transformedQuery3 = DocStoreConverter.transform(TENANT_ID, query3, Collections.emptyList());
    Filter transformedFilter3 = transformedQuery3.getFilter();
    Assertions.assertEquals(Filter.Op.AND, transformedFilter3.getOp());
    Assertions.assertEquals(2, transformedFilter3.getChildFilters().length);
    Assertions.assertEquals("attributes.value.long", transformedFilter3.getChildFilters()[1].getFieldName());
    Assertions.assertEquals(Filter.Op.IN, transformedFilter3.getChildFilters()[1].getOp());
    Assertions.assertEquals(List.of(21L, 22L), transformedFilter3.getChildFilters()[1].getValue());
    // CreatedTime field. Will not be part of attributes and so no need to specify the type suffix
    Query query4 = Query.newBuilder().setFilter(AttributeFilter.newBuilder().setName(EntityServiceConstants.ENTITY_CREATED_TIME).setOperator(Operator.IN).setAttributeValue(AttributeValue.newBuilder().setValueList(AttributeValueList.newBuilder().addValues(AttributeValue.newBuilder().setValue(Value.newBuilder().setLong(21L))).addValues(AttributeValue.newBuilder().setValue(Value.newBuilder().setLong(22L)))).build()).build()).build();
    org.hypertrace.core.documentstore.Query transformedQuery4 = DocStoreConverter.transform(TENANT_ID, query4, Collections.emptyList());
    Filter transformedFilter4 = transformedQuery4.getFilter();
    Assertions.assertEquals(Filter.Op.AND, transformedFilter4.getOp());
    Assertions.assertEquals(2, transformedFilter4.getChildFilters().length);
    Assertions.assertEquals("createdTime", transformedFilter4.getChildFilters()[1].getFieldName());
    Assertions.assertEquals(Filter.Op.IN, transformedFilter4.getChildFilters()[1].getOp());
    Assertions.assertEquals(List.of(21L, 22L), transformedFilter4.getChildFilters()[1].getValue());
}
Also used : Query(org.hypertrace.entity.data.service.v1.Query) AttributeFilter(org.hypertrace.entity.data.service.v1.AttributeFilter) Filter(org.hypertrace.core.documentstore.Filter) Test(org.junit.jupiter.api.Test)

Aggregations

Query (org.jpl7.Query)88 Term (org.jpl7.Term)52 Variable (org.jpl7.Variable)32 Query (org.hypertrace.entity.data.service.v1.Query)31 Map (java.util.Map)28 Test (org.junit.jupiter.api.Test)23 Atom (org.jpl7.Atom)21 Compound (org.jpl7.Compound)18 Test (org.junit.Test)18 Filter (org.hypertrace.core.documentstore.Filter)16 Query (com.google.datastore.v1.Query)14 AttributeFilter (org.hypertrace.entity.data.service.v1.AttributeFilter)14 GqlQuery (com.google.datastore.v1.GqlQuery)7 ArrayList (java.util.ArrayList)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Entity (org.hypertrace.entity.data.service.v1.Entity)6 Integer (org.jpl7.Integer)6 Query (org.molgenis.emx2.Query)6 IOException (java.io.IOException)5 Collections (java.util.Collections)5