Search in sources :

Example 56 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class InformationContentServiceTest method redistributedNGramScore.

@Test
public void redistributedNGramScore() {
    String ontologyIri = "http://www.molgenis.org";
    Entity ontologyEntity = ontologyFactory.create();
    ontologyEntity.set(OntologyMetaData.ONTOLOGY_IRI, ontologyIri);
    when(dataService.findOne(ONTOLOGY, new QueryImpl<>().eq(OntologyMetaData.ONTOLOGY_IRI, ontologyIri))).thenReturn(ontologyEntity);
    when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>().eq(OntologyTermMetaData.ONTOLOGY, ontologyEntity))).thenReturn((long) 100);
    QueryRule queryRule = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, Operator.FUZZY_MATCH, "hear")));
    queryRule.setOperator(Operator.DIS_MAX);
    QueryRule finalQuery = new QueryRule(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, Operator.EQUALS, ontologyEntity), new QueryRule(Operator.AND), queryRule));
    when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>(finalQuery))).thenReturn((long) 30);
    QueryRule queryRule2 = new QueryRule(singletonList(new QueryRule(OntologyTermMetaData.ONTOLOGY_TERM_SYNONYM, Operator.FUZZY_MATCH, "impair")));
    queryRule2.setOperator(Operator.DIS_MAX);
    QueryRule finalQuery2 = new QueryRule(asList(new QueryRule(OntologyTermMetaData.ONTOLOGY, Operator.EQUALS, ontologyEntity), new QueryRule(Operator.AND), queryRule2));
    when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>(finalQuery2))).thenReturn((long) 10);
    Map<String, Double> redistributedNGramScore = informationContentService.redistributedNGramScore("hearing impairment", ontologyIri);
    Assert.assertEquals(redistributedNGramScore.get("hear").intValue(), -7);
    Assert.assertEquals(redistributedNGramScore.get("impair").intValue(), 7);
}
Also used : Entity(org.molgenis.data.Entity) QueryImpl(org.molgenis.data.support.QueryImpl) QueryRule(org.molgenis.data.QueryRule) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 57 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class DataServiceIT method testAggregateOneDimensional.

@WithMockUser(username = USERNAME_READ)
@Test(groups = "readtest")
public void testAggregateOneDimensional() {
    AggregateQuery aggregateQuery = new AggregateQueryImpl().query(new QueryImpl<>()).attrX(entityType.getAttribute(ATTR_BOOL));
    AggregateResult result = dataService.aggregate(entityType.getId(), aggregateQuery);
    AggregateResult expectedResult = new AggregateResult(asList(singletonList(1L), singletonList(2L)), asList(0L, 1L), emptyList());
    assertEquals(result, expectedResult);
}
Also used : AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) QueryImpl(org.molgenis.data.support.QueryImpl) AggregateResult(org.molgenis.data.aggregation.AggregateResult) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 58 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class PostgreSqlQueryGenerator method getSqlWhere.

static <E extends Entity> String getSqlWhere(EntityType entityType, Query<E> q, List<Object> parameters, AtomicInteger mrefFilterIndex) {
    StringBuilder result = new StringBuilder();
    for (QueryRule r : q.getRules()) {
        Attribute attr = null;
        if (r.getField() != null) {
            attr = entityType.getAttribute(r.getField());
            if (attr == null) {
                throw new MolgenisDataException(format("Unknown attribute [%s]", r.getField()));
            }
            if (isPersistedInOtherTable(attr)) {
                mrefFilterIndex.incrementAndGet();
            }
        }
        StringBuilder predicate = new StringBuilder();
        Operator operator = r.getOperator();
        switch(operator) {
            case AND:
                result.append(" AND ");
                break;
            case NESTED:
                QueryImpl<Entity> nestedQ = new QueryImpl<>(r.getNestedRules());
                result.append('(').append(getSqlWhere(entityType, nestedQ, parameters, mrefFilterIndex)).append(')');
                break;
            case OR:
                result.append(" OR ");
                break;
            case LIKE:
                requireNonNull(attr, format(UNSPECIFIED_ATTRIBUTE_MSG, LIKE));
                String columnName;
                if (isPersistedInOtherTable(attr)) {
                    columnName = getFilterColumnName(attr, mrefFilterIndex.get());
                } else {
                    columnName = "this." + getColumnName(attr);
                }
                if (isStringType(attr) || isTextType(attr)) {
                    result.append(' ').append(columnName);
                } else {
                    result.append(" CAST(").append(columnName).append(" as TEXT)");
                }
                result.append(" LIKE ?");
                parameters.add("%" + PostgreSqlUtils.getPostgreSqlQueryValue(r.getValue(), attr) + '%');
                break;
            case IN:
                {
                    requireNonNull(attr, format(UNSPECIFIED_ATTRIBUTE_MSG, IN));
                    Object inValue = r.getValue();
                    if (inValue == null) {
                        throw new MolgenisDataException("Missing value for IN query");
                    }
                    if (!(inValue instanceof Iterable<?>)) {
                        throw new MolgenisDataException(format("IN value is of type [%s] instead of [Iterable]", inValue.getClass().getSimpleName()));
                    }
                    StringBuilder in = new StringBuilder();
                    Attribute inAttr = attr;
                    Stream<Object> postgreSqlIds = stream(((Iterable<?>) inValue).spliterator(), false).map(idValue -> PostgreSqlUtils.getPostgreSqlQueryValue(idValue, inAttr));
                    for (Iterator<Object> it = postgreSqlIds.iterator(); it.hasNext(); ) {
                        Object postgreSqlId = it.next();
                        in.append('?');
                        if (it.hasNext()) {
                            in.append(',');
                        }
                        parameters.add(postgreSqlId);
                    }
                    if (isPersistedInOtherTable(attr)) {
                        result.append(getFilterColumnName(attr, mrefFilterIndex.get()));
                    } else {
                        result.append("this");
                    }
                    Attribute equalsAttr;
                    if (attr.isMappedBy()) {
                        equalsAttr = attr.getRefEntity().getIdAttribute();
                    } else {
                        equalsAttr = entityType.getAttribute(r.getField());
                    }
                    result.append('.').append(getColumnName(equalsAttr));
                    result.append(" IN (").append(in).append(')');
                    break;
                }
            case NOT:
                result.append(" NOT ");
                break;
            case RANGE:
                requireNonNull(attr, format(UNSPECIFIED_ATTRIBUTE_MSG, RANGE));
                Object range = r.getValue();
                if (range == null) {
                    throw new MolgenisDataException("Missing value for RANGE query");
                }
                if (!(range instanceof Iterable<?>)) {
                    throw new MolgenisDataException(format("RANGE value is of type [%s] instead of [Iterable]", range.getClass().getSimpleName()));
                }
                Iterator<?> rangeValues = ((Iterable<?>) range).iterator();
                // from
                parameters.add(rangeValues.next());
                // to
                parameters.add(rangeValues.next());
                StringBuilder column = new StringBuilder();
                if (isPersistedInOtherTable(attr)) {
                    column.append(getFilterColumnName(attr, mrefFilterIndex.get()));
                } else {
                    column.append("this");
                }
                column.append('.').append(getColumnName(entityType.getAttribute(r.getField())));
                predicate.append(column).append(" >= ? AND ").append(column).append(" <= ?");
                result.append(predicate);
                break;
            case EQUALS:
                if (attr == null) {
                    throw new MolgenisDataException("Missing attribute field in EQUALS query rule");
                }
                if (isPersistedInOtherTable(attr)) {
                    predicate.append(getFilterColumnName(attr, mrefFilterIndex.get()));
                } else {
                    predicate.append("this");
                }
                Attribute equalsAttr;
                if (attr.isMappedBy()) {
                    equalsAttr = attr.getRefEntity().getIdAttribute();
                } else {
                    equalsAttr = entityType.getAttribute(r.getField());
                }
                predicate.append('.').append(getColumnName(equalsAttr));
                if (r.getValue() == null) {
                    // expression = null is not valid, use IS NULL
                    predicate.append(" IS NULL ");
                } else {
                    Object postgreSqlVal = PostgreSqlUtils.getPostgreSqlQueryValue(r.getValue(), attr);
                    // It does however return those rows when queried with "... NOT abstract IS TRUE"
                    if (attr.getDataType() == BOOL) {
                        Boolean bool = (Boolean) postgreSqlVal;
                        // noinspection ConstantConditions (getPostgreSqlQueryValue() != null if r.getValue() != null)
                        if (bool)
                            predicate.append(" IS TRUE");
                        else
                            predicate.append(" IS FALSE");
                    } else {
                        predicate.append(" =");
                        predicate.append(" ? ");
                        parameters.add(postgreSqlVal);
                    }
                }
                if (result.length() > 0 && !result.toString().endsWith(" OR ") && !result.toString().endsWith(" AND ") && !result.toString().endsWith(" NOT ")) {
                    result.append(" AND ");
                }
                result.append(predicate);
                break;
            case GREATER:
            case GREATER_EQUAL:
            case LESS:
            case LESS_EQUAL:
                requireNonNull(attr, format(UNSPECIFIED_ATTRIBUTE_MSG, format("%s, %s, %s or %s", GREATER, GREATER_EQUAL, LESS, LESS_EQUAL)));
                if (isPersistedInOtherTable(attr)) {
                    predicate.append(getFilterColumnName(attr, mrefFilterIndex.get()));
                } else {
                    predicate.append("this");
                }
                predicate.append('.').append(getColumnName(entityType.getAttribute(r.getField())));
                switch(operator) {
                    case GREATER:
                        predicate.append(" >");
                        break;
                    case GREATER_EQUAL:
                        predicate.append(" >=");
                        break;
                    case LESS:
                        predicate.append(" <");
                        break;
                    case LESS_EQUAL:
                        predicate.append(" <=");
                        break;
                    // $CASES-OMITTED$
                    default:
                        throw new RuntimeException(format("Unexpected query operator [%s]", operator));
                }
                predicate.append(" ? ");
                parameters.add(PostgreSqlUtils.getPostgreSqlQueryValue(r.getValue(), attr));
                if (result.length() > 0 && !result.toString().endsWith(" OR ") && !result.toString().endsWith(" AND ") && !result.toString().endsWith(" NOT ")) {
                    result.append(" AND ");
                }
                result.append(predicate);
                break;
            case DIS_MAX:
            case FUZZY_MATCH:
            case FUZZY_MATCH_NGRAM:
            case SEARCH:
            case SHOULD:
                // PostgreSQL does not support semantic searching and sorting matching rows on relevance.
                throw new UnsupportedOperationException(format("Query operator [%s] not supported by PostgreSQL repository", operator.toString()));
            default:
                throw new UnexpectedEnumException(operator);
        }
    }
    return result.toString().trim();
}
Also used : Operator(org.molgenis.data.QueryRule.Operator) IntStream.range(java.util.stream.IntStream.range) Operator(org.molgenis.data.QueryRule.Operator) LoggerFactory(org.slf4j.LoggerFactory) QueryImpl(org.molgenis.data.support.QueryImpl) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) MessageFormat(java.text.MessageFormat) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeUtils(org.molgenis.data.support.AttributeUtils) Objects.requireNonNull(java.util.Objects.requireNonNull) PostgreSqlNameGenerator(org.molgenis.data.postgresql.PostgreSqlNameGenerator) AttributeType(org.molgenis.data.meta.AttributeType) Logger(org.slf4j.Logger) PostgreSqlQueryUtils(org.molgenis.data.postgresql.PostgreSqlQueryUtils) org.molgenis.data(org.molgenis.data) Iterator(java.util.Iterator) Collection(java.util.Collection) EntityTypeUtils(org.molgenis.data.support.EntityTypeUtils) Instant(java.time.Instant) EntityType(org.molgenis.data.meta.model.EntityType) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) INCLUDE_DEFAULT_CONSTRAINT(org.molgenis.data.postgresql.PostgreSqlQueryGenerator.ColumnMode.INCLUDE_DEFAULT_CONSTRAINT) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) StreamSupport.stream(java.util.stream.StreamSupport.stream) LocalDate(java.time.LocalDate) UTC(java.time.ZoneOffset.UTC) Attribute(org.molgenis.data.meta.model.Attribute) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) QueryImpl(org.molgenis.data.support.QueryImpl) Iterator(java.util.Iterator) Stream(java.util.stream.Stream)

Example 59 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class PostgreSqlQueryGeneratorTest method getSqlSelectMref.

@Test
public void getSqlSelectMref() {
    Package package_ = when(mock(Package.class).getId()).thenReturn("org_molgenis").getMock();
    Attribute ref1IdAttr = when(mock(Attribute.class).getName()).thenReturn("ref1Id").getMock();
    when(ref1IdAttr.getIdentifier()).thenReturn("ref1IdAttrId");
    EntityType ref1Meta = when(mock(EntityType.class).getId()).thenReturn("Ref1").getMock();
    when(ref1Meta.getId()).thenReturn("ref1Id");
    when(ref1Meta.getIdAttribute()).thenReturn(ref1IdAttr);
    Attribute ref2IdAttr = when(mock(Attribute.class).getName()).thenReturn("ref2Id").getMock();
    when(ref2IdAttr.getIdentifier()).thenReturn("ref2IdAttrId");
    EntityType ref2Meta = when(mock(EntityType.class).getId()).thenReturn("Ref2").getMock();
    when(ref2Meta.getId()).thenReturn("ref2Id");
    when(ref2Meta.getIdAttribute()).thenReturn(ref2IdAttr);
    Attribute masterIdAttr = when(mock(Attribute.class).getName()).thenReturn("masterId").getMock();
    when(masterIdAttr.getIdentifier()).thenReturn("masterIdAttrId");
    when(masterIdAttr.getDataType()).thenReturn(STRING);
    Attribute mref1Attr = when(mock(Attribute.class).getName()).thenReturn("mref1").getMock();
    when(mref1Attr.getIdentifier()).thenReturn("mref1AttrId");
    when(mref1Attr.getDataType()).thenReturn(MREF);
    when(mref1Attr.getRefEntity()).thenReturn(ref1Meta);
    Attribute mref2Attr = when(mock(Attribute.class).getName()).thenReturn("mref2").getMock();
    when(mref2Attr.getIdentifier()).thenReturn("mref2AttrId");
    when(mref2Attr.getDataType()).thenReturn(MREF);
    when(mref2Attr.getRefEntity()).thenReturn(ref2Meta);
    EntityType entityType = when(mock(EntityType.class).getId()).thenReturn("org_molgenis_MasterEntity").getMock();
    when(entityType.getId()).thenReturn("entityTypeId");
    when(entityType.getPackage()).thenReturn(package_);
    when(entityType.getIdAttribute()).thenReturn(masterIdAttr);
    when(entityType.getAttribute("masterId")).thenReturn(masterIdAttr);
    when(entityType.getAttribute("mref1")).thenReturn(mref1Attr);
    when(entityType.getAttribute("mref2")).thenReturn(mref2Attr);
    when(entityType.getAtomicAttributes()).thenReturn(asList(masterIdAttr, mref1Attr, mref2Attr));
    QueryImpl<Entity> q = new QueryImpl<>();
    List<Object> parameters = Lists.newArrayList();
    String sqlSelect = PostgreSqlQueryGenerator.getSqlSelect(entityType, q, parameters, true);
    assertEquals(sqlSelect, "SELECT this.\"masterId\", (SELECT array_agg(DISTINCT ARRAY[\"mref1\".\"order\"::TEXT,\"mref1\".\"mref1\"::TEXT]) FROM \"entityTypeId#c34894ba_mref1\" AS \"mref1\" WHERE this.\"masterId\" = \"mref1\".\"masterId\") AS \"mref1\", (SELECT array_agg(DISTINCT ARRAY[\"mref2\".\"order\"::TEXT,\"mref2\".\"mref2\"::TEXT]) FROM \"entityTypeId#c34894ba_mref2\" AS \"mref2\" WHERE this.\"masterId\" = \"mref2\".\"masterId\") AS \"mref2\" FROM \"entityTypeId#c34894ba\" AS this ORDER BY \"masterId\" ASC");
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) Attribute(org.molgenis.data.meta.model.Attribute) Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test)

Example 60 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class PostgreSqlQueryGeneratorTest method getSqlFrom.

@Test
public void getSqlFrom() {
    Package eric = createPackage("eu_bbmri_eric");
    Attribute collectionsIdAttribute = createIdAttribute("collectionsId");
    EntityType collectionsEntity = createMockEntityWithIdAttribute("eu_bbmri_eric_collections", collectionsIdAttribute, "collectionsId");
    Attribute typeIdAttribute = createIdAttribute("typeId");
    EntityType typeEntity = createMockEntityWithIdAttribute("eu_bbmri_eric_type", typeIdAttribute, "typeId");
    Attribute categoryIdAttribute = createIdAttribute("categoryId");
    EntityType categoryEntity = createMockEntityWithIdAttribute("eu_bbmri_eric_category", categoryIdAttribute, "categoryId");
    Attribute typeAttribute = createMrefAttribute("type", typeEntity);
    Attribute categoryAttribute = createMrefAttribute("category", categoryEntity);
    when(collectionsEntity.getPackage()).thenReturn(eric);
    when(collectionsEntity.getAttribute("type")).thenReturn(typeAttribute);
    when(collectionsEntity.getAttribute("category")).thenReturn(categoryAttribute);
    when(collectionsEntity.getAtomicAttributes()).thenReturn(asList(collectionsIdAttribute, typeAttribute, categoryAttribute));
    QueryImpl<Entity> q = new QueryImpl<>();
    List<Object> parameters = Lists.newArrayList();
    String sqlSelect = PostgreSqlQueryGenerator.getSqlSelect(collectionsEntity, q, parameters, true);
    assertEquals(sqlSelect, "SELECT this.\"collectionsId\", (SELECT array_agg(DISTINCT ARRAY[\"type\".\"order\"::TEXT,\"type\".\"type\"::TEXT]) FROM \"eu_bbmri_eric_collecti#4dc023e6_type\" AS \"type\" WHERE this.\"collectionsId\" = \"type\".\"collectionsId\") AS \"type\", (SELECT array_agg(DISTINCT ARRAY[\"category\".\"order\"::TEXT,\"category\".\"category\"::TEXT]) FROM \"eu_bbmri_eric_collecti#4dc023e6_category\" AS \"category\" WHERE this.\"collectionsId\" = \"category\".\"collectionsId\") AS \"category\" FROM \"eu_bbmri_eric_collections#4dc023e6\" AS this ORDER BY \"collectionsId\" ASC");
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) Attribute(org.molgenis.data.meta.model.Attribute) Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test)

Aggregations

QueryImpl (org.molgenis.data.support.QueryImpl)98 Test (org.testng.annotations.Test)70 DynamicEntity (org.molgenis.data.support.DynamicEntity)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)36 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)36 EntityType (org.molgenis.data.meta.model.EntityType)28 Attribute (org.molgenis.data.meta.model.Attribute)25 Entity (org.molgenis.data.Entity)15 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 Stream (java.util.stream.Stream)7 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)7 AggregateQueryImpl (org.molgenis.data.support.AggregateQueryImpl)7 Objects.requireNonNull (java.util.Objects.requireNonNull)6 QueryRule (org.molgenis.data.QueryRule)6 AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)6 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 Instant (java.time.Instant)5 LocalDate (java.time.LocalDate)5 Operator (org.molgenis.data.QueryRule.Operator)5