Skip to content

Commit

Permalink
Merge pull request #2061 from bakdata/feature/reset-dont-clone
Browse files Browse the repository at this point in the history
Reset, don't clone
  • Loading branch information
awildturtok authored Sep 20, 2021
2 parents 8fd81fa + badd453 commit bc8b5fc
Show file tree
Hide file tree
Showing 88 changed files with 823 additions and 1,008 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.bakdata.conquery.models.query.QueryPlanContext;
import com.bakdata.conquery.models.query.QueryResolveContext;
import com.bakdata.conquery.models.query.Visitable;
import com.bakdata.conquery.models.query.queryplan.ConceptQueryPlan;
import com.bakdata.conquery.models.query.queryplan.SecondaryIdQueryPlan;
import com.bakdata.conquery.models.query.resultinfo.ResultInfoCollector;
import com.bakdata.conquery.models.query.resultinfo.SimpleResultInfo;
Expand All @@ -49,7 +48,7 @@ public class SecondaryIdQuery extends Query {

@NotNull
protected DateAggregationMode dateAggregationMode = DateAggregationMode.MERGE;


/**
* @apiNote not using {@link ConceptQuery} directly in the API-spec simplifies the API.
Expand All @@ -68,11 +67,7 @@ public class SecondaryIdQuery extends Query {
@Override
public SecondaryIdQueryPlan createQueryPlan(QueryPlanContext context) {

context = context.withSelectedSecondaryId(getSecondaryId());

final ConceptQueryPlan queryPlan = query.createQueryPlan(context);

return new SecondaryIdQueryPlan(queryPlan, secondaryId, withSecondaryId, withoutSecondaryId);
return new SecondaryIdQueryPlan(query, context, secondaryId, withSecondaryId, withoutSecondaryId, query.createQueryPlan(context.withSelectedSecondaryId(secondaryId)));
}

@Override
Expand All @@ -85,7 +80,7 @@ public void collectRequiredQueries(Set<ManagedExecution<?>> requiredQueries) {
public void resolve(QueryResolveContext context) {

DateAggregationMode resolvedDateAggregationMode = dateAggregationMode;
if(context.getDateAggregationMode() != null) {
if (context.getDateAggregationMode() != null) {
log.trace("Overriding date aggregation mode ({}) with mode from context ({})", dateAggregationMode, context.getDateAggregationMode());
resolvedDateAggregationMode = context.getDateAggregationMode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public QPNode createQueryPlan(QueryPlanContext ctx, ConceptQueryPlan plan) {
plan.getDateAggregator().register(dateAggregator);

return new TemporalQueryNode(
index.createQueryPlan(ctx),
preceding.createQueryPlan(ctx),
new BeforeOrSameTemporalMatcher(),
index.createQueryPlan(ctx),
preceding.createQueryPlan(ctx),
new BeforeOrSameTemporalMatcher(),
dateAggregator
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import lombok.Setter;
import lombok.ToString;

@Getter @Setter @ToString
@Getter
@Setter
@ToString
public class ThreadPoolDefinition {
@Min(0)
private int minThreads = Runtime.getRuntime().availableProcessors();
Expand All @@ -20,7 +22,7 @@ public class ThreadPoolDefinition {
private boolean allowCoreThreadTimeOut = false;
private Duration keepAliveTime = Duration.seconds(60);
private Duration shutdownTime = Duration.hours(1);

public ThreadPoolExecutor createService(String nameFormat) {
final ThreadPoolExecutor executor = new ThreadPoolExecutor(
minThreads,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
package com.bakdata.conquery.models.forms.managed;

import java.util.Optional;

import com.bakdata.conquery.models.common.CDateSet;
import com.bakdata.conquery.models.query.QueryExecutionContext;
import com.bakdata.conquery.models.query.entity.Entity;
import com.bakdata.conquery.models.query.queryplan.QueryPlan;
import com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator;
import com.bakdata.conquery.models.query.queryplan.clone.CloneContext;
import com.bakdata.conquery.models.query.results.EntityResult;
import com.bakdata.conquery.models.query.results.MultilineEntityResult;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Optional;

@Getter @RequiredArgsConstructor
public class AbsoluteFormQueryPlan implements QueryPlan<MultilineEntityResult> {

private final QueryPlan query;
private final FormQueryPlan subPlan;


@Override
public void init(QueryExecutionContext ctxt, Entity entity) {
query.init(ctxt, entity);
subPlan.init(ctxt, entity);
}

@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<EntityResult> preResult = query.execute(ctx, entity);

if (preResult.isEmpty()) {
return Optional.empty();
}
return subPlan.execute(ctx, entity);
}

@Override
public AbsoluteFormQueryPlan clone(CloneContext ctx) {
return new AbsoluteFormQueryPlan(
query.clone(ctx),
subPlan.clone(ctx)
);
}

@Override
public boolean isOfInterest(Entity entity) {
return query.isOfInterest(entity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.bakdata.conquery.models.forms.managed;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.bakdata.conquery.apiv1.forms.export_form.ExportForm;
import com.bakdata.conquery.models.common.CDateSet;
import com.bakdata.conquery.models.common.daterange.CDateRange;
Expand All @@ -10,16 +14,10 @@
import com.bakdata.conquery.models.query.queryplan.DateAggregator;
import com.bakdata.conquery.models.query.queryplan.QueryPlan;
import com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator;
import com.bakdata.conquery.models.query.queryplan.clone.CloneContext;
import com.bakdata.conquery.models.query.results.EntityResult;
import com.bakdata.conquery.models.query.results.MultilineEntityResult;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
* Implementation of the QueryPlan for an {@link EntityDateQuery}.
*/
Expand All @@ -32,9 +30,14 @@ public class EntityDateQueryPlan implements QueryPlan<MultilineEntityResult> {
private final List<ExportForm.ResolutionAndAlignment> resolutionsAndAlignments;
private final CDateRange dateRestriction;

private Function<MultilineEntityResult, CDateSet> validityDateCollector;

@Override
@Override
public void init(QueryExecutionContext ctxt, Entity entity) {
query.init(ctxt, entity);
features.init(ctxt, entity);
}

@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
Expand All @@ -50,7 +53,7 @@ public Optional<MultilineEntityResult> execute(QueryExecutionContext ctx, Entity
return Optional.empty();
}

final CDateSet aggregationResult = validityDateAggregator.get().getAggregationResult();
final CDateSet aggregationResult = validityDateAggregator.get().createAggregationResult();
aggregationResult.retainAll(dateRestriction);

// Generate DateContexts in the provided resolutions
Expand All @@ -64,17 +67,7 @@ public Optional<MultilineEntityResult> execute(QueryExecutionContext ctx, Entity
return resolutionQuery.execute(ctx, entity);
}

@Override
public EntityDateQueryPlan clone(CloneContext ctx) {
return new EntityDateQueryPlan(
query.clone(ctx),
features.clone(ctx),
resolutionsAndAlignments,
dateRestriction
);
}

@Override
@Override
public boolean isOfInterest(Entity entity) {
return query.isOfInterest(entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
import com.bakdata.conquery.models.query.QueryExecutionContext;
import com.bakdata.conquery.models.query.entity.Entity;
import com.bakdata.conquery.models.query.queryplan.ArrayConceptQueryPlan;
import com.bakdata.conquery.models.query.queryplan.DateAggregationAction;
import com.bakdata.conquery.models.query.queryplan.DateAggregator;
import com.bakdata.conquery.models.query.queryplan.QueryPlan;
import com.bakdata.conquery.models.query.queryplan.aggregators.Aggregator;
import com.bakdata.conquery.models.query.queryplan.clone.CloneContext;
import com.bakdata.conquery.models.query.results.MultilineEntityResult;
import com.bakdata.conquery.models.query.results.SinglelineEntityResult;
import com.bakdata.conquery.util.QueryUtils;
Expand All @@ -26,111 +23,115 @@ public class FormQueryPlan implements QueryPlan<MultilineEntityResult> {

private final List<DateContext> dateContexts;
private final ArrayConceptQueryPlan features;

private final int constantCount;
private final List<ArrayConceptQueryPlan> subPlans = new ArrayList<>();


public FormQueryPlan(List<DateContext> dateContexts, ArrayConceptQueryPlan features) {
this.dateContexts = dateContexts;
this.features = features;

if (dateContexts.size() <= 0) {
// There is nothing to do for this FormQueryPlan but we will return an empty result when its executed
constantCount = 3;
return;
}

// Either all date contexts have an relative event date or none has one
boolean withRelativeEventdate = dateContexts.get(0).getEventDate() != null;
for(DateContext dateContext : dateContexts) {
if((dateContext.getEventDate() == null) == withRelativeEventdate) {
throw new IllegalStateException("Queryplan has absolute AND relative date contexts. Only one kind is allowed.");
boolean withRelativeEventDate = dateContexts.get(0).getEventDate() != null;

for (DateContext dateContext : dateContexts) {
if ((dateContext.getEventDate() == null) == withRelativeEventDate) {
throw new IllegalStateException("QueryPlan has absolute AND relative date contexts. Only one kind is allowed.");
}
}
constantCount = withRelativeEventdate ? 4 : 3; // resolution indicator, index value, (event date,) date range
constantCount = withRelativeEventDate ? 4 : 3; // resolution indicator, index value, (event date,) date range
}

@Override
public Optional<MultilineEntityResult> execute(QueryExecutionContext ctx, Entity entity) {


features.init(ctx,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) {

CloneContext clCtx = new CloneContext(ctx.getStorage());

ArrayConceptQueryPlan subPlan = features.clone(clCtx);
subPlans.add(subPlan);


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
ctx = QueryUtils.determineDateAggregatorForContext(ctx, subPlan::getValidityDateAggregator);
Optional<SinglelineEntityResult> subResult = subPlan.execute(ctx.withDateRestriction(dateRestriction), entity);

if(subResult.isEmpty()) {
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(subPlan.getAggregators(), OptionalInt.of(0)).unaryAndThen(v->addConstants(v, dateContext))
).listResultLines()
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));
}

@Override
public void init(QueryExecutionContext ctx, Entity entity) {
features.init(ctx, entity);
}

private MultilineEntityResult createResultForNotContained(Entity entity, DateContext dateContext) {

List<Object[]> result = new ArrayList<>();
result.add(new Object[features.getAggregatorSize()]);
return ResultModifier.modify(new MultilineEntityResult(entity.getId(), result), ResultModifier.existAggValuesSetterFor(getAggregators(), OptionalInt.of(0)).unaryAndThen(v->addConstants(v, dateContext)));

return ResultModifier.modify(
new MultilineEntityResult(entity.getId(), result),
ResultModifier.existAggValuesSetterFor(getAggregators(), OptionalInt.of(0))
.unaryAndThen(v -> addConstants(v, dateContext))
);
}

public List<Aggregator<?>> getAggregators() {
return features.getAggregators();
}

private Object[] addConstants(Object[] values, DateContext dateContext) {
Object[] result = new Object[values.length + constantCount];
System.arraycopy(values, 0, result, constantCount, values.length);
if(dateContext == null) {

if (dateContext == null) {
return result;
}

//add resolution indicator
result[0] = dateContext.getSubdivisionMode().toString();
//add index value
result[1] = dateContext.getIndex();
// add event date
if(dateContext.getEventDate() != null) {
if (dateContext.getEventDate() != null) {
result[2] = dateContext.getEventDate().toEpochDay();
}
//add date range at [2] or [3]
result[getDateRangeResultPosition()] = dateContext.getDateRange();

return result;
}

private int getDateRangeResultPosition() {
return constantCount-1;
}

@Override
public FormQueryPlan clone(CloneContext ctx) {
return new FormQueryPlan(dateContexts, features.clone(ctx));
return constantCount - 1;
}

@Override
Expand All @@ -140,12 +141,6 @@ public boolean isOfInterest(Entity entity) {

@Override
public Optional<Aggregator<CDateSet>> getValidityDateAggregator() {
DateAggregator agg = new DateAggregator(DateAggregationAction.MERGE);

subPlans.forEach(
p -> p.getValidityDateAggregator().ifPresent(agg::register)
);

return Optional.empty();
}

Expand Down
Loading

0 comments on commit bc8b5fc

Please sign in to comment.