Search in sources :

Example 16 with CDateSet

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

the class RelativeFormQueryPlan 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
    Optional<? extends EntityResult> preResult = query.execute(ctx, entity);
    if (preResult.isEmpty()) {
        return Optional.empty();
    }
    int size = calculateCompleteLength();
    EntityResult contained = preResult.get();
    // Gather all validity dates from prerequisite
    CDateSet dateSet = query.getValidityDateAggregator().map(Aggregator::createAggregationResult).orElseGet(CDateSet::create);
    final OptionalInt sampled = indexSelector.sample(dateSet);
    // dateset is empty or sampling failed.
    if (sampled.isEmpty()) {
        log.warn("Sampled empty result for Entity[{}]: `{}({})`", contained.getEntityId(), indexSelector, dateSet);
        List<Object[]> results = new ArrayList<>();
        results.add(new Object[size]);
        return Optional.of(ResultModifier.modify(new MultilineEntityResult(entity.getId(), results), ResultModifier.existAggValuesSetterFor(getAggregators(), OptionalInt.of(getFirstAggregatorPosition()))));
    }
    int sample = sampled.getAsInt();
    List<DateContext> contexts = DateContext.generateRelativeContexts(sample, indexPlacement, timeCountBefore, timeCountAfter, timeUnit, resolutionsAndAlignmentMap);
    // create feature and outcome plans
    featureSubquery = createSubQuery(featurePlan, contexts, FeatureGroup.FEATURE);
    outcomeSubquery = createSubQuery(outcomePlan, contexts, FeatureGroup.OUTCOME);
    // determine result length and check against aggregators in query
    int featureLength = featureSubquery.columnCount();
    int outcomeLength = outcomeSubquery.columnCount();
    featureSubquery.init(ctx, entity);
    outcomeSubquery.init(ctx, entity);
    Optional<MultilineEntityResult> featureResult = featureSubquery.execute(ctx, entity);
    Optional<MultilineEntityResult> outcomeResult = outcomeSubquery.execute(ctx, entity);
    // determine result length and check against aggregators in query
    assertResultWidth(featureResult.get(), featureLength);
    assertResultWidth(outcomeResult.get(), outcomeLength);
    List<Object[]> featureResultValues = featureResult.get().getValues();
    List<Object[]> outcomeResultValues = outcomeResult.get().getValues();
    int resultStartIndex = 0;
    List<Object[]> values = new ArrayList<>();
    if (hasCompleteDateContexts(contexts)) {
        // merge a line for the complete daterange, when two dateContext were generated
        // that don't target the same feature group,
        // which would be a mistake by the generation
        // Since the DateContexts are primarily ordered by their coarseness and COMPLETE
        // is the most coarse resolution it must be at the first
        // to indexes of the list.
        Object[] mergedFull = new Object[size];
        if (featurePlan.getAggregatorSize() > 0) {
            setFeatureValues(mergedFull, featureResultValues.get(resultStartIndex));
        }
        if (outcomePlan.getAggregatorSize() > 0) {
            setOutcomeValues(mergedFull, outcomeResultValues.get(resultStartIndex));
        }
        values.add(mergedFull);
        resultStartIndex++;
    }
    // append all other lines directly
    for (int i = resultStartIndex; i < featureResultValues.size(); i++) {
        Object[] result = new Object[size];
        setFeatureValues(result, featureResultValues.get(i));
        values.add(result);
    }
    for (int i = resultStartIndex; i < outcomeResultValues.size(); i++) {
        Object[] result = new Object[size];
        setOutcomeValues(result, outcomeResultValues.get(i));
        values.add(result);
    }
    return Optional.of(new MultilineEntityResult(entity.getId(), values));
}
Also used : ArrayList(java.util.ArrayList) OptionalInt(java.util.OptionalInt) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) MultilineEntityResult(com.bakdata.conquery.models.query.results.MultilineEntityResult) DateContext(com.bakdata.conquery.models.forms.util.DateContext) MultilineEntityResult(com.bakdata.conquery.models.query.results.MultilineEntityResult) CDateSet(com.bakdata.conquery.models.common.CDateSet)

Example 17 with CDateSet

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

the class CDateSetTest method testMaskedAddAtMostMaskClosed.

@Test
public void testMaskedAddAtMostMaskClosed() {
    CDateSet set = CDateSet.create();
    CDateSet mask = CDateSet.create(CDateRange.of(-10, 10));
    set.maskedAdd(CDateRange.atMost(5), mask);
    assertThat(set.asRanges()).containsExactly(CDateRange.of(-10, 5));
}
Also used : CDateSet(com.bakdata.conquery.models.common.CDateSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 18 with CDateSet

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

the class CDateSetTest method testMaskedTEST.

@Test
public void testMaskedTEST() {
    CDateSet set = CDateSet.create();
    CDateSet mask = CDateSet.create(CDateRange.of(-10, 10));
    set.maskedAdd(CDateRange.of(-5, 5), mask);
    assertThat(set.asRanges()).containsExactly(CDateRange.of(-5, 5));
}
Also used : CDateSet(com.bakdata.conquery.models.common.CDateSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with CDateSet

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

the class CDateSetTest method testAddMerging.

@ParameterizedTest(name = "{0}")
@MethodSource("arguments")
public void testAddMerging(String expected, CDateRange[] ranges) {
    CDateSet set = CDateSet.create();
    for (CDateRange range : ranges) {
        set.add(range);
    }
    assertThat(set).hasToString(expected);
}
Also used : CDateRange(com.bakdata.conquery.models.common.daterange.CDateRange) CDateSet(com.bakdata.conquery.models.common.CDateSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 20 with CDateSet

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

the class CDateSetTest method testRetain.

@Test
public void testRetain() {
    CDateSet set = CDateSet.create();
    set.add(CDateRange.of(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 31)));
    CDateSet retain = CDateSet.create();
    retain.add(CDateRange.of(LocalDate.of(2000, 6, 1), LocalDate.of(2000, 6, 20)));
    retain.add(CDateRange.atLeast(LocalDate.of(2000, 12, 1)));
    set.retainAll(retain);
    assertThat(set).hasToString("{2000-06-01/2000-06-20, 2000-12-01/2000-12-31}");
}
Also used : CDateSet(com.bakdata.conquery.models.common.CDateSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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