-
Notifications
You must be signed in to change notification settings - Fork 13
Implementation of distinctBy for DurationSumFilter/-Select #3839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
awildturtok
wants to merge
2
commits into
develop
Choose a base branch
from
feature/distinct-duration-sum
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 67 additions & 10 deletions
77
...ava/com/bakdata/conquery/models/datasets/concepts/filters/specific/DurationSumFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,105 @@ | ||
| package com.bakdata.conquery.models.datasets.concepts.filters.specific; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.EnumSet; | ||
| import java.util.List; | ||
| import javax.annotation.Nullable; | ||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| import com.bakdata.conquery.apiv1.frontend.FrontendFilterConfiguration; | ||
| import com.bakdata.conquery.apiv1.frontend.FrontendFilterType; | ||
| import com.bakdata.conquery.io.cps.CPSType; | ||
| import com.bakdata.conquery.models.common.Range; | ||
| import com.bakdata.conquery.models.config.ConqueryConfig; | ||
| import com.bakdata.conquery.models.datasets.Column; | ||
| import com.bakdata.conquery.models.datasets.concepts.filters.Filter; | ||
| import com.bakdata.conquery.models.datasets.concepts.filters.SingleColumnFilter; | ||
| import com.bakdata.conquery.models.events.MajorTypeId; | ||
| import com.bakdata.conquery.models.exceptions.ConceptConfigurationException; | ||
| import com.bakdata.conquery.models.identifiable.ids.specific.ColumnId; | ||
| import com.bakdata.conquery.models.query.filter.RangeFilterNode; | ||
| import com.bakdata.conquery.models.query.queryplan.aggregators.ColumnAggregator; | ||
| import com.bakdata.conquery.models.query.queryplan.aggregators.DistinctValuesWrapperAggregator; | ||
| import com.bakdata.conquery.models.query.queryplan.aggregators.specific.DurationSumAggregator; | ||
| import com.bakdata.conquery.models.query.queryplan.filter.FilterNode; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import com.fasterxml.jackson.annotation.JsonAlias; | ||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||
| import io.dropwizard.validation.ValidationMethod; | ||
| import lombok.Data; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @Data | ||
| @Slf4j | ||
| @CPSType(id = "DURATION_SUM", base = Filter.class) | ||
| public class DurationSumFilter extends SingleColumnFilter<Range.LongRange> { | ||
| public class DurationSumFilter extends Filter<Range.LongRange> { | ||
|
|
||
| @Override | ||
| @Valid | ||
| @NotNull | ||
| @JsonAlias("column") | ||
| private final ColumnId dateRangeColumn; | ||
|
|
||
| @Valid | ||
| @Nullable | ||
| private final List<ColumnId> distinctBy; | ||
|
|
||
| @JsonIgnore | ||
| public EnumSet<MajorTypeId> getAcceptedColumnTypes() { | ||
| return EnumSet.of(MajorTypeId.DATE_RANGE); | ||
| } | ||
|
|
||
| @Override | ||
| public void configureFrontend(FrontendFilterConfiguration.Top f, ConqueryConfig conqueryConfig) throws ConceptConfigurationException { | ||
| MajorTypeId type = getColumn().resolve().getType(); | ||
| MajorTypeId type = getDateRangeColumn().resolve().getType(); | ||
| if (type != MajorTypeId.DATE_RANGE) { | ||
| throw new ConceptConfigurationException(getConnector(), "DURATION_SUM filter is incompatible with columns of type " | ||
| + type); | ||
| + type | ||
| ); | ||
| } | ||
|
|
||
| f.setType(FrontendFilterType.Fields.INTEGER_RANGE); | ||
| f.setMin(0); | ||
| } | ||
|
|
||
| @JsonIgnore | ||
| private boolean hasDistinct() { | ||
| return distinctBy != null && !distinctBy.isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| public FilterNode createFilterNode(Range.LongRange value) { | ||
| return new RangeFilterNode(value, new DurationSumAggregator(getColumn().resolve())); | ||
| ColumnAggregator<?> aggregator = new DurationSumAggregator(getDateRangeColumn().resolve()); | ||
|
|
||
| if (hasDistinct()) { | ||
| aggregator = new DistinctValuesWrapperAggregator<>(aggregator, distinctBy.stream().map(ColumnId::resolve).toList()); | ||
| } | ||
|
|
||
| return new RangeFilterNode(value, aggregator); | ||
| } | ||
|
|
||
| @Override | ||
| public List<ColumnId> getRequiredColumns() { | ||
| List<ColumnId> required = new ArrayList<>(); | ||
|
|
||
| if (hasDistinct()) { | ||
| required.addAll(distinctBy); | ||
| } | ||
|
|
||
| required.add(dateRangeColumn); | ||
| return required; | ||
|
|
||
| } | ||
|
|
||
| @JsonIgnore | ||
| @ValidationMethod(message = "Columns do not match required Type.") | ||
| public boolean isValidColumnType() { | ||
| final Column resolved = getDateRangeColumn().resolve(); | ||
| final boolean acceptable = getAcceptedColumnTypes().contains(resolved.getType()); | ||
|
|
||
| if (!acceptable) { | ||
| log.error("Column[{}] is of Type[{}]. Not one of [{}]", resolved.getId(), resolved.getType(), getAcceptedColumnTypes()); | ||
| } | ||
|
|
||
| return acceptable; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...c/test/resources/tests/aggregator/DURATION_SUM_DISTINCT_AGGREGATOR/DURATION_SUM.test.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| { | ||
| "type": "QUERY_TEST", | ||
| "label": "DURATION_SUM_AGGREGATOR Test", | ||
| "expectedCsv": "tests/aggregator/DURATION_SUM_DISTINCT_AGGREGATOR/expected.csv", | ||
| "query": { | ||
| "type": "CONCEPT_QUERY", | ||
| "root": { | ||
| "ids": [ | ||
| "concept" | ||
| ], | ||
| "type": "CONCEPT", | ||
| "tables": [ | ||
| { | ||
| "id": "concept.connector", | ||
| "selects": [ | ||
| "concept.connector.select" | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "concepts": [ | ||
| { | ||
| "name": "concept", | ||
| "type": "TREE", | ||
| "connectors": [ | ||
| { | ||
| "name": "connector", | ||
| "table": "table", | ||
| "validityDates": { | ||
| "label": "datum", | ||
| "column": "table.datum" | ||
| }, | ||
| "selects": { | ||
| "type": "DURATION_SUM", | ||
| "name": "select", | ||
| "column": "table.datum", | ||
| "distinctBy" : ["table.val"] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ], | ||
| "content": { | ||
| "tables": [ | ||
| { | ||
| "csv": "tests/aggregator/DURATION_SUM_DISTINCT_AGGREGATOR/content.csv", | ||
| "name": "table", | ||
| "primaryColumn": { | ||
| "name": "pid", | ||
| "type": "STRING" | ||
| }, | ||
| "columns": [ | ||
| { | ||
| "name": "datum", | ||
| "type": "DATE_RANGE" | ||
| }, | ||
| { | ||
| "name": "val", | ||
| "type": "STRING" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
backend/src/test/resources/tests/aggregator/DURATION_SUM_DISTINCT_AGGREGATOR/content.csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| pid,datum,val | ||
| 1,2010-01-01/2010-01-01,a | ||
| 3,2010-01-01/2010-01-01, | ||
| 4,2010-01-01/2010-01-01,a | ||
| 4,2010-01-02/2010-01-02,a | ||
| 5,2010-01-01/2010-01-01,a | ||
| 5,2010-01-01/2010-01-02,b | ||
| 6,/2010-01-01,a |
6 changes: 6 additions & 0 deletions
6
backend/src/test/resources/tests/aggregator/DURATION_SUM_DISTINCT_AGGREGATOR/expected.csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| result,dates,concept select | ||
| 1,{2010-01-01/2010-01-01},1 | ||
| 3,{2010-01-01/2010-01-01}, | ||
| 4,{2010-01-01/2010-01-02},1 | ||
| 5,{2010-01-01/2010-01-02},2 | ||
| 6,{-∞/2010-01-01}, | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow das sieht irgendwie gefährlich und unintuitiv aus. Das Ergebnis hängt von der Sortierung der Zeilen ab 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naja, es geht am Ende nicht anders. Es geht ja darum, dass bestimmte Rezepte oä nur einmal gezählt werden. Aber ist definitiv wert in der Doku zu erwähnen, guter Punkt.