Search in sources :

Example 61 with StringJoiner

use of java.util.StringJoiner in project requery by requery.

the class EntityMetaGenerator method generateAttribute.

private FieldSpec generateAttribute(AttributeDescriptor attribute, AttributeDescriptor parent, TypeName targetName, String fieldName, TypeMirror mirror, boolean expression) {
    TypeMirror typeMirror = mirror;
    TypeName typeName;
    if (attribute.isIterable()) {
        typeMirror = tryFirstTypeArgument(typeMirror);
        typeName = parameterizedCollectionName(attribute.typeMirror());
    } else if (attribute.isOptional()) {
        typeMirror = tryFirstTypeArgument(typeMirror);
        typeName = TypeName.get(typeMirror);
    } else {
        typeName = nameResolver.generatedTypeNameOf(typeMirror).orElse(null);
    }
    if (typeName == null) {
        typeName = boxedTypeName(typeMirror);
    }
    ParameterizedTypeName type;
    ClassName attributeType = null;
    boolean useKotlinDelegate = false;
    if (expression) {
        type = parameterizedTypeName(QueryExpression.class, typeName);
    } else {
        // if it's an association don't make it available as a query attribute
        boolean isQueryable = attribute.cardinality() == null || attribute.isForeignKey();
        Class<?> attributeClass = isQueryable ? QueryAttribute.class : Attribute.class;
        attributeType = ClassName.get(attributeClass);
        if (isQueryable) {
            TypeElement delegateType = elements.getTypeElement(KOTLIN_ATTRIBUTE_DELEGATE);
            if (delegateType != null) {
                attributeType = ClassName.get(delegateType);
                useKotlinDelegate = true;
            }
        }
        type = ParameterizedTypeName.get(attributeType, targetName, typeName);
    }
    CodeBlock.Builder builder = CodeBlock.builder();
    String attributeName = attribute.name();
    if (parent != null && parent.isEmbedded()) {
        attributeName = embeddedAttributeName(parent, attribute);
    }
    if (attribute.isIterable()) {
        typeMirror = tryFirstTypeArgument(typeMirror);
        TypeName name = nameResolver.tryGeneratedTypeName(typeMirror);
        TypeElement collection = (TypeElement) types.asElement(attribute.typeMirror());
        ParameterizedTypeName builderName = parameterizedTypeName(attribute.builderClass(), targetName, typeName, name);
        builder.add("\nnew $T($S, $T.class, $T.class)\n", builderName, attributeName, ClassName.get(collection), name);
    } else if (attribute.isMap() && attribute.cardinality() != null) {
        List<TypeMirror> parameters = Mirrors.listGenericTypeArguments(typeMirror);
        // key type
        TypeName keyName = TypeName.get(parameters.get(0));
        // value type
        typeMirror = parameters.get(1);
        TypeName valueName = nameResolver.tryGeneratedTypeName(typeMirror);
        TypeElement valueElement = (TypeElement) types.asElement(attribute.typeMirror());
        ParameterizedTypeName builderName = parameterizedTypeName(attribute.builderClass(), targetName, typeName, keyName, valueName);
        builder.add("\nnew $T($S, $T.class, $T.class, $T.class)\n", builderName, attributeName, ClassName.get(valueElement), keyName, valueName);
    } else {
        ParameterizedTypeName builderName = parameterizedTypeName(attribute.builderClass(), targetName, typeName);
        TypeName classType = typeName;
        if (typeMirror.getKind().isPrimitive()) {
            // if primitive just use the primitive class not the boxed version
            classType = TypeName.get(typeMirror);
        }
        String statement;
        if (Mirrors.listGenericTypeArguments(typeMirror).size() > 0) {
            // use the erased type and cast to class
            classType = TypeName.get(types.erasure(typeMirror));
            statement = "\nnew $T($S, (Class)$T.class)\n";
        } else {
            statement = "\nnew $T($S, $T.class)\n";
        }
        builder.add(statement, builderName, attributeName, classType);
    }
    if (!expression) {
        generateProperties(attribute, parent, typeMirror, targetName, typeName, builder);
    }
    // attribute builder properties
    if (attribute.isKey()) {
        builder.add(".setKey(true)\n");
    }
    builder.add(".setGenerated($L)\n", attribute.isGenerated());
    builder.add(".setLazy($L)\n", attribute.isLazy());
    builder.add(".setNullable($L)\n", attribute.isNullable());
    builder.add(".setUnique($L)\n", attribute.isUnique());
    if (!Names.isEmpty(attribute.defaultValue())) {
        builder.add(".setDefaultValue($S)\n", attribute.defaultValue());
    }
    if (!Names.isEmpty(attribute.collate())) {
        builder.add(".setCollate($S)\n", attribute.collate());
    }
    if (attribute.columnLength() != null) {
        builder.add(".setLength($L)\n", attribute.columnLength());
    }
    if (!Names.isEmpty(attribute.definition())) {
        builder.add(".setDefinition($S)\n", attribute.definition());
    }
    if (attribute.isVersion()) {
        builder.add(".setVersion($L)\n", attribute.isVersion());
    }
    if (attribute.converterName() != null) {
        builder.add(".setConverter(new $L())\n", attribute.converterName());
    }
    if (attribute.isForeignKey()) {
        builder.add(".setForeignKey($L)\n", attribute.isForeignKey());
        Optional<EntityDescriptor> referencedType = graph.referencingEntity(attribute);
        referencedType.ifPresent(referenced -> {
            builder.add(".setReferencedClass($T.class)\n", referenced.isImmutable() ? TypeName.get(referenced.element().asType()) : nameResolver.typeNameOf(referenced));
            graph.referencingAttribute(attribute, referenced).ifPresent(referencedAttribute -> {
                String name = upperCaseUnderscoreRemovePrefixes(referencedAttribute.fieldName());
                TypeSpec provider = CodeGeneration.createAnonymousSupplier(ClassName.get(Attribute.class), CodeBlock.builder().addStatement("return $T.$L", nameResolver.typeNameOf(referenced), name).build());
                builder.add(".setReferencedAttribute($L)\n", provider);
            });
        });
    }
    if (attribute.isIndexed()) {
        builder.add(".setIndexed($L)\n", attribute.isIndexed());
        if (!attribute.indexNames().isEmpty()) {
            StringJoiner joiner = new StringJoiner(",");
            attribute.indexNames().forEach(name -> joiner.add("$S"));
            builder.add(".setIndexNames(" + joiner + ")\n", attribute.indexNames().toArray());
        }
    }
    if (attribute.deleteAction() != null) {
        builder.add(".setDeleteAction($T.$L)\n", ClassName.get(ReferentialAction.class), attribute.deleteAction());
    }
    if (attribute.updateAction() != null) {
        builder.add(".setUpdateAction($T.$L)\n", ClassName.get(ReferentialAction.class), attribute.updateAction());
    }
    if (!attribute.cascadeActions().isEmpty()) {
        StringJoiner joiner = new StringJoiner(",");
        attribute.cascadeActions().forEach(action -> joiner.add("$T.$L"));
        int index = 0;
        ClassName cascadeClass = ClassName.get(CascadeAction.class);
        Object[] args = new Object[attribute.cascadeActions().size() * 2];
        for (CascadeAction action : attribute.cascadeActions()) {
            args[index++] = cascadeClass;
            args[index++] = action;
        }
        builder.add(".setCascadeAction(" + joiner + ")\n", args);
    }
    if (attribute.cardinality() != null) {
        if (!expression) {
            builder.add(".setCardinality($T.$L)\n", ClassName.get(Cardinality.class), attribute.cardinality());
        }
        graph.referencingEntity(attribute).ifPresent(referenced -> {
            Set<AttributeDescriptor> mappings = graph.mappedAttributes(entity, attribute, referenced);
            if (attribute.cardinality() == Cardinality.MANY_TO_MANY) {
                generateJunctionType(attribute, referenced, mappings).ifPresent(name -> builder.add(".setReferencedClass($T.class)\n", name));
            }
            if (mappings.size() == 1) {
                AttributeDescriptor mapped = mappings.iterator().next();
                String staticMemberName = upperCaseUnderscoreRemovePrefixes(mapped.fieldName());
                TypeSpec provider = CodeGeneration.createAnonymousSupplier(ClassName.get(Attribute.class), CodeBlock.builder().addStatement("return $T.$L", nameResolver.typeNameOf(referenced), staticMemberName).build());
                builder.add(".setMappedAttribute($L)\n", provider);
            }
            if (attribute.orderBy() != null) {
                referenced.attributes().values().stream().filter(entry -> entry.name().equals(attribute.orderBy())).findFirst().ifPresent(orderBy -> {
                    String staticMemberName = upperCaseUnderscoreRemovePrefixes(orderBy.fieldName());
                    TypeSpec provider = CodeGeneration.createAnonymousSupplier(ClassName.get(Attribute.class), CodeBlock.builder().addStatement("return $T.$L", nameResolver.typeNameOf(referenced), staticMemberName).build());
                    builder.add(".setOrderByAttribute($L)\n", provider);
                    builder.add(".setOrderByDirection($T.$L)\n", ClassName.get(Order.class), attribute.orderByDirection());
                });
            }
        });
    }
    builder.add(".build()");
    FieldSpec.Builder field = FieldSpec.builder(type, fieldName, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
    if (useKotlinDelegate) {
        return field.initializer("new $T($L)", attributeType, builder.build()).build();
    } else {
        return field.initializer("$L", builder.build()).build();
    }
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute) ReferentialAction(io.requery.ReferentialAction) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(com.squareup.javapoet.ClassName) LinkedList(java.util.LinkedList) List(java.util.List) QueryExpression(io.requery.meta.QueryExpression) StringJoiner(java.util.StringJoiner) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) CascadeAction(io.requery.CascadeAction) Order(io.requery.query.Order) Cardinality(io.requery.meta.Cardinality) TypeElement(javax.lang.model.element.TypeElement) CodeBlock(com.squareup.javapoet.CodeBlock) FieldSpec(com.squareup.javapoet.FieldSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 62 with StringJoiner

use of java.util.StringJoiner in project presto by prestodb.

the class ShardPredicate method create.

public static ShardPredicate create(TupleDomain<RaptorColumnHandle> tupleDomain, boolean bucketed) {
    StringJoiner predicate = new StringJoiner(" AND ").setEmptyValue("true");
    ImmutableList.Builder<JDBCType> types = ImmutableList.builder();
    ImmutableList.Builder<Object> values = ImmutableList.builder();
    for (Entry<RaptorColumnHandle, Domain> entry : tupleDomain.getDomains().get().entrySet()) {
        Domain domain = entry.getValue();
        if (domain.isNullAllowed() || domain.isAll()) {
            continue;
        }
        RaptorColumnHandle handle = entry.getKey();
        Type type = handle.getColumnType();
        JDBCType jdbcType = jdbcType(type);
        if (jdbcType == null) {
            continue;
        }
        if (handle.isShardUuid()) {
            predicate.add(createShardPredicate(types, values, domain, jdbcType));
            continue;
        }
        if (!domain.getType().isOrderable()) {
            continue;
        }
        Ranges ranges = domain.getValues().getRanges();
        // TODO: support multiple ranges
        if (ranges.getRangeCount() != 1) {
            continue;
        }
        Range range = getOnlyElement(ranges.getOrderedRanges());
        Object minValue = null;
        Object maxValue = null;
        if (range.isSingleValue()) {
            minValue = range.getSingleValue();
            maxValue = range.getSingleValue();
        } else {
            if (!range.getLow().isLowerUnbounded()) {
                minValue = range.getLow().getValue();
            }
            if (!range.getHigh().isUpperUnbounded()) {
                maxValue = range.getHigh().getValue();
            }
        }
        String min;
        String max;
        if (handle.isBucketNumber()) {
            if (!bucketed) {
                predicate.add("false");
                continue;
            }
            min = "bucket_number";
            max = "bucket_number";
        } else {
            min = minColumn(handle.getColumnId());
            max = maxColumn(handle.getColumnId());
        }
        if (minValue != null) {
            predicate.add(format("(%s >= ? OR %s IS NULL)", max, max));
            types.add(jdbcType);
            values.add(minValue);
        }
        if (maxValue != null) {
            predicate.add(format("(%s <= ? OR %s IS NULL)", min, min));
            types.add(jdbcType);
            values.add(maxValue);
        }
    }
    return new ShardPredicate(predicate.toString(), types.build(), values.build());
}
Also used : Ranges(com.facebook.presto.spi.predicate.Ranges) JDBCType(java.sql.JDBCType) RaptorColumnHandle(com.facebook.presto.raptor.RaptorColumnHandle) ImmutableList(com.google.common.collect.ImmutableList) Range(com.facebook.presto.spi.predicate.Range) JDBCType(java.sql.JDBCType) Type(com.facebook.presto.spi.type.Type) ColumnIndexStatsUtils.jdbcType(com.facebook.presto.raptor.storage.ColumnIndexStatsUtils.jdbcType) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Domain(com.facebook.presto.spi.predicate.Domain) StringJoiner(java.util.StringJoiner)

Example 63 with StringJoiner

use of java.util.StringJoiner in project presto by prestodb.

the class TestRaptorIntegrationSmokeTest method testShardingByTemporalTimestampColumnBucketed.

@Test
public void testShardingByTemporalTimestampColumnBucketed() throws Exception {
    // Make sure we have at least 2 different orderdate.
    assertEquals(computeActual("SELECT count(DISTINCT orderdate) >= 2 FROM orders WHERE orderdate < date '1992-02-08'").getOnlyValue(), true);
    assertUpdate("" + "CREATE TABLE test_shard_temporal_timestamp_bucketed(col1 BIGINT, col2 TIMESTAMP) " + "WITH (temporal_column = 'col2', bucket_count = 3, bucketed_on = ARRAY ['col1'])");
    int rows = 100;
    StringJoiner joiner = new StringJoiner(", ", "INSERT INTO test_shard_temporal_timestamp_bucketed VALUES ", "");
    for (int i = 0; i < rows; i++) {
        joiner.add(format("(%s, TIMESTAMP '2016-08-08 01:00' + interval '%s' hour)", i, i));
    }
    assertUpdate(joiner.toString(), format("VALUES(%s)", rows));
    MaterializedResult results = computeActual("" + "SELECT format_datetime(col2, 'yyyyMMdd'), \"$shard_uuid\" " + "FROM test_shard_temporal_timestamp_bucketed");
    assertEquals(results.getRowCount(), rows);
    // Each shard will only contain data of one date.
    SetMultimap<String, String> shardDateMap = HashMultimap.create();
    for (MaterializedRow row : results.getMaterializedRows()) {
        shardDateMap.put((String) row.getField(1), (String) row.getField(0));
    }
    for (Collection<String> dates : shardDateMap.asMap().values()) {
        assertEquals(dates.size(), 1);
    }
    // Ensure one shard can contain different timestamps from the same day
    assertLessThan(shardDateMap.size(), rows);
}
Also used : MaterializedResult(com.facebook.presto.testing.MaterializedResult) StringJoiner(java.util.StringJoiner) MaterializedRow(com.facebook.presto.testing.MaterializedRow) Test(org.testng.annotations.Test) AbstractTestIntegrationSmokeTest(com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)

Example 64 with StringJoiner

use of java.util.StringJoiner in project goci by EBISPOT.

the class AssociationRowProcessor method createAssociationFromUploadRow.

public Association createAssociationFromUploadRow(AssociationUploadRow row) {
    Association newAssociation = new Association();
    // Set EFO traits
    if (row.getEfoTrait() != null) {
        String[] uris = row.getEfoTrait().split(",");
        Collection<String> efoUris = new ArrayList<>();
        for (String uri : uris) {
            String trimmedUri = uri.trim();
            efoUris.add(trimmedUri);
        }
        Collection<EfoTrait> efoTraits = associationAttributeService.getEfoTraitsFromRepository(efoUris);
        newAssociation.setEfoTraits(efoTraits);
    }
    /// Set OR
    newAssociation.setOrPerCopyRecip(row.getOrPerCopyRecip());
    newAssociation.setOrPerCopyRecipRange(row.getOrPerCopyRecipRange());
    // Set beta
    newAssociation.setBetaNum(row.getBetaNum());
    newAssociation.setBetaUnit(row.getBetaUnit());
    newAssociation.setBetaDirection(row.getBetaDirection());
    // Calculate OR num if OR recip is present , otherwise set to whatever is in upload
    boolean recipReverse = false;
    if ((row.getOrPerCopyRecip() != null) && (row.getOrPerCopyNum() == null)) {
        getLog().info("Calculating OR from OR recip value");
        newAssociation.setOrPerCopyNum(((100 / row.getOrPerCopyRecip()) / 100));
        recipReverse = true;
    } else {
        newAssociation.setOrPerCopyNum(row.getOrPerCopyNum());
    }
    // Calculate range , this logic is retained from Dani's original code
    if ((row.getOrPerCopyRecipRange() != null) && recipReverse) {
        newAssociation.setRange(associationCalculationService.reverseCI(row.getOrPerCopyRecipRange()));
    } else if ((row.getRange() == null) && (row.getStandardError() != null)) {
        if (row.getOrPerCopyNum() != null) {
            newAssociation.setRange(associationCalculationService.setRange(row.getStandardError(), row.getOrPerCopyNum()));
        } else {
            if (row.getBetaNum() != null) {
                newAssociation.setRange(associationCalculationService.setRange(row.getStandardError(), row.getBetaNum()));
            }
        }
    } else {
        newAssociation.setRange(row.getRange());
    }
    // Add brackets to the p-value description
    if (row.getPvalueDescription() != null && !row.getPvalueDescription().isEmpty()) {
        if (!row.getPvalueDescription().startsWith("(") && !row.getPvalueDescription().endsWith(")")) {
            StringJoiner newPvalueDescription = new StringJoiner("", "(", ")");
            newPvalueDescription.add(row.getPvalueDescription());
            newAssociation.setPvalueDescription(newPvalueDescription.toString());
        } else {
            newAssociation.setPvalueDescription(row.getPvalueDescription());
        }
    } else {
        newAssociation.setPvalueDescription(row.getPvalueDescription());
    }
    // Set values common to all association types
    newAssociation.setRiskFrequency(row.getAssociationRiskFrequency());
    newAssociation.setPvalueMantissa(row.getPvalueMantissa());
    newAssociation.setPvalueExponent(row.getPvalueExponent());
    newAssociation.setSnpType(row.getSnpType());
    newAssociation.setStandardError(row.getStandardError());
    newAssociation.setDescription(row.getDescription());
    if (row.getMultiSnpHaplotype() != null) {
        if (row.getMultiSnpHaplotype().equalsIgnoreCase("Y")) {
            newAssociation.setMultiSnpHaplotype(true);
        }
    } else {
        newAssociation.setMultiSnpHaplotype(false);
    }
    if (row.getSnpInteraction() != null) {
        if (row.getSnpInteraction().equalsIgnoreCase("Y")) {
            newAssociation.setSnpInteraction(true);
        }
    } else {
        newAssociation.setSnpInteraction(false);
    }
    // If there is a risk allele proceed to create loci
    if (row.getStrongestAllele() != null) {
        newAssociation.setLoci(createLoci(row, newAssociation.getSnpInteraction(), newAssociation.getMultiSnpHaplotype()));
    }
    return newAssociation;
}
Also used : Association(uk.ac.ebi.spot.goci.model.Association) ArrayList(java.util.ArrayList) EfoTrait(uk.ac.ebi.spot.goci.model.EfoTrait) StringJoiner(java.util.StringJoiner)

Example 65 with StringJoiner

use of java.util.StringJoiner in project goci by EBISPOT.

the class AncestryEventsViewService method createAncestrySummary.

private String createAncestrySummary(Ancestry ancestry) {
    String ancestrySummary = null;
    StringJoiner joiner = new StringJoiner("; ");
    joiner.add("Type: ".concat(ancestry.getType()));
    joiner.add("Ancestry: ".concat(ancestry.getAncestralGroups().stream().map(i -> i.getAncestralGroup()).collect(Collectors.joining(", "))));
    joiner.add("Country of recruitment: ".concat(ancestry.getCountryOfOrigin().stream().map(i -> i.getCountryName()).collect(Collectors.joining(", "))));
    joiner.add("Country of origin: ".concat(ancestry.getCountryOfRecruitment().stream().map(i -> i.getCountryName()).collect(Collectors.joining(", "))));
    ancestrySummary = joiner.toString();
    return ancestrySummary;
}
Also used : DeletedAncestry(uk.ac.ebi.spot.goci.model.DeletedAncestry) AncestryRepository(uk.ac.ebi.spot.goci.repository.AncestryRepository) DeletedAncestryRepository(uk.ac.ebi.spot.goci.repository.DeletedAncestryRepository) Collection(java.util.Collection) AncestryEventView(uk.ac.ebi.spot.goci.curation.model.AncestryEventView) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Service(org.springframework.stereotype.Service) EventTypeService(uk.ac.ebi.spot.goci.service.EventTypeService) StringJoiner(java.util.StringJoiner) EventView(uk.ac.ebi.spot.goci.curation.model.EventView) Ancestry(uk.ac.ebi.spot.goci.model.Ancestry) StringJoiner(java.util.StringJoiner)

Aggregations

StringJoiner (java.util.StringJoiner)98 ArrayList (java.util.ArrayList)22 List (java.util.List)11 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 Map (java.util.Map)6 HashMap (java.util.HashMap)4 Collectors (java.util.stream.Collectors)4 ClassName (com.squareup.javapoet.ClassName)3 FieldSpec (com.squareup.javapoet.FieldSpec)3 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)3 TypeName (com.squareup.javapoet.TypeName)3 TypeSpec (com.squareup.javapoet.TypeSpec)3 Expression (com.sri.ai.expresso.api.Expression)3 Attribute (io.requery.meta.Attribute)3 Field (java.lang.reflect.Field)3 Scanner (java.util.Scanner)3 RaptorColumnHandle (com.facebook.presto.raptor.RaptorColumnHandle)2 Range (com.facebook.presto.spi.predicate.Range)2 MaterializedResult (com.facebook.presto.testing.MaterializedResult)2