Search in sources :

Example 11 with CDateSet

use of com.bakdata.conquery.models.common.CDateSet in project conquery by bakdata.

the class CQConcept method createQueryPlan.

@Override
public QPNode createQueryPlan(QueryPlanContext context, ConceptQueryPlan plan) {
    final List<Aggregator<?>> conceptAggregators = createAggregators(plan, selects);
    List<QPNode> tableNodes = new ArrayList<>();
    for (CQTable table : tables) {
        List<FilterNode<?>> filters = table.getFilters().stream().map(FilterValue::createNode).collect(Collectors.toList());
        // add filter to children
        List<Aggregator<?>> aggregators = new ArrayList<>();
        aggregators.addAll(conceptAggregators);
        final List<Aggregator<?>> connectorAggregators = createAggregators(plan, table.getSelects());
        // Exists aggregators hold a reference to their parent FiltersNode so they need to be treated separately.
        // They also don't need aggregation as they simply imitate their reference.
        List<ExistsAggregator> existsAggregators = connectorAggregators.stream().filter(ExistsAggregator.class::isInstance).map(ExistsAggregator.class::cast).collect(Collectors.toList());
        aggregators.addAll(connectorAggregators);
        aggregators.removeIf(ExistsAggregator.class::isInstance);
        List<Aggregator<CDateSet>> eventDateUnionAggregators = aggregateEventDates ? List.of(new EventDateUnionAggregator(Set.of(table.getConnector().getTable()))) : Collections.emptyList();
        aggregators.addAll(eventDateUnionAggregators);
        final QPNode filtersNode = getConcept().createConceptQuery(context, filters, aggregators, eventDateUnionAggregators);
        // Link up the ExistsAggregators to the node
        existsAggregators.forEach(agg -> agg.setReference(filtersNode));
        // Select if matching secondaryId available
        final boolean hasSelectedSecondaryId = Arrays.stream(table.getConnector().getTable().getColumns()).map(Column::getSecondaryId).filter(Objects::nonNull).anyMatch(o -> Objects.equals(context.getSelectedSecondaryId(), o));
        final Column validityDateColumn = selectValidityDateColumn(table);
        final ConceptNode node = new ConceptNode(// TODO Don't set validity node, when no validity column exists. See workaround for this and remove it: https://github.com/bakdata/conquery/pull/1362
        new ValidityDateNode(validityDateColumn, filtersNode), elements, table, // if the node is excluded, don't pass it into the Node.
        !excludeFromSecondaryId && hasSelectedSecondaryId ? context.getSelectedSecondaryId() : null);
        tableNodes.add(node);
    }
    // We always merge on concept level
    final QPNode outNode = OrNode.of(tableNodes, aggregateEventDates ? DateAggregationAction.MERGE : DateAggregationAction.BLOCK);
    // Link concept-level Exists-select to outer node.
    conceptAggregators.stream().filter(aggregator -> aggregator instanceof ExistsAggregator).forEach(aggregator -> ((ExistsAggregator) aggregator).setReference(outNode));
    return outNode;
}
Also used : ConceptNode(com.bakdata.conquery.models.query.queryplan.specific.ConceptNode) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Arrays(java.util.Arrays) ConceptElement(com.bakdata.conquery.models.datasets.concepts.ConceptElement) EventDateUnionAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.EventDateUnionAggregator) NotEmpty(javax.validation.constraints.NotEmpty) Valid(javax.validation.Valid) JsonAlias(com.fasterxml.jackson.annotation.JsonAlias) Locale(java.util.Locale) QPNode(com.bakdata.conquery.models.query.queryplan.QPNode) ToString(lombok.ToString) CQTable(com.bakdata.conquery.apiv1.query.concept.filter.CQTable) InternalOnly(com.bakdata.conquery.io.jackson.InternalOnly) ConceptQueryPlan(com.bakdata.conquery.models.query.queryplan.ConceptQueryPlan) NamespacedIdentifiableHolding(com.bakdata.conquery.models.query.NamespacedIdentifiableHolding) CPSType(com.bakdata.conquery.io.cps.CPSType) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) NsIdRefCollection(com.bakdata.conquery.io.jackson.serializer.NsIdRefCollection) Concept(com.bakdata.conquery.models.datasets.concepts.Concept) Set(java.util.Set) ConceptNode(com.bakdata.conquery.models.query.queryplan.specific.ConceptNode) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) FilterValue(com.bakdata.conquery.apiv1.query.concept.filter.FilterValue) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) JsonManagedReference(com.fasterxml.jackson.annotation.JsonManagedReference) Column(com.bakdata.conquery.models.datasets.Column) Setter(lombok.Setter) Getter(lombok.Getter) NamespacedIdentifiable(com.bakdata.conquery.models.identifiable.ids.NamespacedIdentifiable) ArrayList(java.util.ArrayList) Select(com.bakdata.conquery.models.datasets.concepts.select.Select) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) DateAggregationMode(com.bakdata.conquery.models.query.DateAggregationMode) FilterNode(com.bakdata.conquery.models.query.queryplan.filter.FilterNode) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) DateAggregationAction(com.bakdata.conquery.models.query.queryplan.DateAggregationAction) ValidityDateNode(com.bakdata.conquery.models.query.queryplan.specific.ValidityDateNode) ExportForm(com.bakdata.conquery.apiv1.forms.export_form.ExportForm) QueryPlanContext(com.bakdata.conquery.models.query.QueryPlanContext) ValidationMethod(io.dropwizard.validation.ValidationMethod) QueryResolveContext(com.bakdata.conquery.models.query.QueryResolveContext) OrNode(com.bakdata.conquery.models.query.queryplan.specific.OrNode) Aggregator(com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator) CQElement(com.bakdata.conquery.apiv1.query.CQElement) ExistsAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.ExistsAggregator) Collections(java.util.Collections) CDateSet(com.bakdata.conquery.models.common.CDateSet) QPNode(com.bakdata.conquery.models.query.queryplan.QPNode) CQTable(com.bakdata.conquery.apiv1.query.concept.filter.CQTable) FilterNode(com.bakdata.conquery.models.query.queryplan.filter.FilterNode) EventDateUnionAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.EventDateUnionAggregator) ArrayList(java.util.ArrayList) EventDateUnionAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.EventDateUnionAggregator) Aggregator(com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator) ExistsAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.ExistsAggregator) Column(com.bakdata.conquery.models.datasets.Column) ValidityDateNode(com.bakdata.conquery.models.query.queryplan.specific.ValidityDateNode) ExistsAggregator(com.bakdata.conquery.models.query.queryplan.aggregators.specific.ExistsAggregator)

Example 12 with CDateSet

use of com.bakdata.conquery.models.common.CDateSet in project conquery by bakdata.

the class FormQueryPlan method execute.

@Override
public Optional<MultilineEntityResult> execute(QueryExecutionContext ctx, Entity entity) {
    if (!isOfInterest(entity)) {
        // If the entity is not covered by the query generate a basic result line with constants but without features
        return Optional.of(createResultForNotContained(entity, null));
    }
    List<Object[]> resultValues = new ArrayList<>(dateContexts.size());
    for (DateContext dateContext : dateContexts) {
        CDateSet dateRestriction = CDateSet.create(ctx.getDateRestriction());
        dateRestriction.retainAll(dateContext.getDateRange());
        // Reference the dates per sub-query, don't accumulate dates of all sub-queries
        QueryExecutionContext innerContext = QueryUtils.determineDateAggregatorForContext(ctx, features::getValidityDateAggregator).withDateRestriction(dateRestriction);
        features.init(ctx, entity);
        Optional<SinglelineEntityResult> subResult = features.execute(innerContext, entity);
        if (subResult.isEmpty()) {
            resultValues.addAll(createResultForNotContained(entity, dateContext).listResultLines());
            continue;
        }
        resultValues.addAll(ResultModifier.modify(subResult.get(), ResultModifier.existAggValuesSetterFor(features.getAggregators(), OptionalInt.of(0)).unaryAndThen(v -> addConstants(v, dateContext))).listResultLines());
    }
    return Optional.of(new MultilineEntityResult(entity.getId(), resultValues));
}
Also used : Getter(lombok.Getter) SinglelineEntityResult(com.bakdata.conquery.models.query.results.SinglelineEntityResult) MultilineEntityResult(com.bakdata.conquery.models.query.results.MultilineEntityResult) OptionalInt(java.util.OptionalInt) ArrayConceptQueryPlan(com.bakdata.conquery.models.query.queryplan.ArrayConceptQueryPlan) ArrayList(java.util.ArrayList) Entity(com.bakdata.conquery.models.query.entity.Entity) List(java.util.List) DateContext(com.bakdata.conquery.models.forms.util.DateContext) QueryExecutionContext(com.bakdata.conquery.models.query.QueryExecutionContext) ResultModifier(com.bakdata.conquery.models.forms.util.ResultModifier) Aggregator(com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator) QueryUtils(com.bakdata.conquery.util.QueryUtils) Optional(java.util.Optional) QueryPlan(com.bakdata.conquery.models.query.queryplan.QueryPlan) CDateSet(com.bakdata.conquery.models.common.CDateSet) QueryExecutionContext(com.bakdata.conquery.models.query.QueryExecutionContext) ArrayList(java.util.ArrayList) DateContext(com.bakdata.conquery.models.forms.util.DateContext) SinglelineEntityResult(com.bakdata.conquery.models.query.results.SinglelineEntityResult) MultilineEntityResult(com.bakdata.conquery.models.query.results.MultilineEntityResult) CDateSet(com.bakdata.conquery.models.common.CDateSet)

Example 13 with CDateSet

use of com.bakdata.conquery.models.common.CDateSet in project conquery by bakdata.

the class CDateSetDeserializer method deserialize.

@Override
public CDateSet deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    if (p.currentToken() == JsonToken.START_ARRAY) {
        int[][] ints = p.readValueAs(int[][].class);
        CDateSet set = CDateSet.create();
        for (int i = 0; i < ints.length; i++) {
            set.add(CDateRange.of(ints[i][0], ints[i][1]));
        }
        return set;
    }
    if (p.currentToken() == JsonToken.VALUE_STRING) {
        return formats.parseToCDateSet(p.readValueAs(String.class));
    }
    return (CDateSet) ctxt.handleUnexpectedToken(CDateSet.class, p.currentToken(), p, "can't deserialize CDateSet");
}
Also used : CDateSet(com.bakdata.conquery.models.common.CDateSet)

Example 14 with CDateSet

use of com.bakdata.conquery.models.common.CDateSet in project conquery by bakdata.

the class CQExternal method readDates.

/**
 * For each row try and collect all dates.
 *
 * @return Row -> Dates
 */
private static CDateSet[] readDates(String[][] values, List<String> format, DateReader dateReader, FrontendConfig.UploadConfig queryUpload) {
    final CDateSet[] out = new CDateSet[values.length];
    List<DateFormat> dateFormats = format.stream().map(queryUpload::resolveDateFormat).collect(Collectors.toList());
    // If no format provided, put empty dates into output.
    if (dateFormats.stream().allMatch(Objects::isNull)) {
        // Initialize empty
        for (int row = 0; row < values.length; row++) {
            out[row] = CDateSet.create();
        }
        return out;
    }
    for (int row = 1; row < values.length; row++) {
        try {
            final CDateSet dates = CDateSet.create();
            // Collect all specified dates into a single set.
            for (int col = 0; col < dateFormats.size(); col++) {
                final DateFormat dateFormat = dateFormats.get(col);
                if (dateFormat == null) {
                    continue;
                }
                dateFormat.readDates(values[row][col], dateReader, dates);
            }
            if (dates.isEmpty()) {
                continue;
            }
            if (out[row] == null) {
                out[row] = CDateSet.create();
            }
            out[row].addAll(dates);
        } catch (Exception e) {
            log.warn("Failed to parse Date from {}", row, e);
        }
    }
    return out;
}
Also used : Objects(java.util.Objects) CDateSet(com.bakdata.conquery.models.common.CDateSet)

Example 15 with CDateSet

use of com.bakdata.conquery.models.common.CDateSet in project conquery by bakdata.

the class EntityDateQueryPlan method execute.

@Override
public Optional<MultilineEntityResult> execute(QueryExecutionContext ctx, Entity entity) {
    // Don't set the query date aggregator here because the subqueries should set their aggregator independently
    // Execute the prerequisite query
    Optional<EntityResult> preResult = query.execute(ctx, entity);
    if (preResult.isEmpty()) {
        return Optional.empty();
    }
    Optional<DateAggregator> validityDateAggregator = query.getValidityDateAggregator();
    if (validityDateAggregator.isEmpty()) {
        return Optional.empty();
    }
    final CDateSet aggregationResult = validityDateAggregator.get().createAggregationResult();
    aggregationResult.retainAll(dateRestriction);
    // Generate DateContexts in the provided resolutions
    List<DateContext> contexts = new ArrayList<>();
    for (CDateRange range : aggregationResult.asRanges()) {
        contexts.addAll(DateContext.generateAbsoluteContexts(range, resolutionsAndAlignments));
    }
    FormQueryPlan resolutionQuery = new FormQueryPlan(contexts, features);
    return resolutionQuery.execute(ctx, entity);
}
Also used : ArrayList(java.util.ArrayList) CDateRange(com.bakdata.conquery.models.common.daterange.CDateRange) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) MultilineEntityResult(com.bakdata.conquery.models.query.results.MultilineEntityResult) DateContext(com.bakdata.conquery.models.forms.util.DateContext) DateAggregator(com.bakdata.conquery.models.query.queryplan.DateAggregator) CDateSet(com.bakdata.conquery.models.common.CDateSet)

Aggregations

CDateSet (com.bakdata.conquery.models.common.CDateSet)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)4 CDateRange (com.bakdata.conquery.models.common.daterange.CDateRange)3 DateContext (com.bakdata.conquery.models.forms.util.DateContext)3 Aggregator (com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator)3 OptionalInt (java.util.OptionalInt)3 EntityResult (com.bakdata.conquery.models.query.results.EntityResult)2 MultilineEntityResult (com.bakdata.conquery.models.query.results.MultilineEntityResult)2 List (java.util.List)2 Getter (lombok.Getter)2 ExportForm (com.bakdata.conquery.apiv1.forms.export_form.ExportForm)1 CQElement (com.bakdata.conquery.apiv1.query.CQElement)1 CQTable (com.bakdata.conquery.apiv1.query.concept.filter.CQTable)1 FilterValue (com.bakdata.conquery.apiv1.query.concept.filter.FilterValue)1 CPSType (com.bakdata.conquery.io.cps.CPSType)1 InternalOnly (com.bakdata.conquery.io.jackson.InternalOnly)1 NsIdRefCollection (com.bakdata.conquery.io.jackson.serializer.NsIdRefCollection)1 LocaleConfig (com.bakdata.conquery.models.config.LocaleConfig)1