Skip to content

Commit

Permalink
Fix full outer joining when converting forms (#3510)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsrnhld authored Aug 1, 2024
1 parent b1ad47a commit c766d18
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ public ColumnDateRange as(String alias) {
);
}

public ColumnDateRange coalesce(ColumnDateRange right) {
if (this.isSingleColumnRange() != right.isSingleColumnRange()) {
throw new UnsupportedOperationException("Can only join ColumnDateRanges of same type");
}
if (isSingleColumnRange()) {
return ColumnDateRange.of(DSL.coalesce(this.range, right.getRange())).as(this.alias);
}
return ColumnDateRange.of(
DSL.coalesce(this.start, right.getStart()),
DSL.coalesce(this.end, right.getEnd())
).as(this.alias);
}

public Condition join(ColumnDateRange right) {
if (this.isSingleColumnRange() != right.isSingleColumnRange()) {
throw new UnsupportedOperationException("Can only join ColumnDateRanges of same type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public static QueryStep joinSteps(

DateAggregationDates dateAggregationDates = DateAggregationDates.forSteps(queriesToJoin);
if (dateAggregationAction == DateAggregationAction.BLOCK || dateAggregationDates.dateAggregationImpossible()) {
// for forms, date aggregation is allways blocked // TODO check if this is really correct
Optional<ColumnDateRange> stratificationDate = queriesToJoin.get(0).getQualifiedSelects().getStratificationDate();
// for forms, date aggregation is allways blocked, but dates need to be coalesced in case we do a fulll outer join
Optional<ColumnDateRange> stratificationDate = coalesceStratificationDates(queriesToJoin);
joinedStep = buildJoinedStep(ids, mergedSelects, Optional.empty(), stratificationDate, joinedStepBuilder);
}
// if there is only 1 child node containing a validity date, we just keep it as overall validity date for the joined node
Expand Down Expand Up @@ -169,6 +169,15 @@ private static QueryStep buildStepAndAggregateDates(
);
}

private static Optional<ColumnDateRange> coalesceStratificationDates(List<QueryStep> queriesToJoin) {
return queriesToJoin.stream()
.map(QueryStep::getQualifiedSelects)
.map(Selects::getStratificationDate)
.filter(Optional::isPresent)
.map(Optional::get)
.reduce(ColumnDateRange::coalesce);
}

@FunctionalInterface
private interface JoinType {
TableOnConditionStep<Record> join(Table<?> leftPart, Table<?> rightPart, List<Condition> joinConditions);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,67 @@
"type": "AND",
"children": [
{
"ids": [
"alter"
],
"type": "CONCEPT",
"tables": [
{
"id": "alter.alter",
"filters": []
}
]
"ids": "abc-concept",
"tables": {
"id": "abc-concept.connector2",
"filters": [
{
"filter": "abc-concept.connector2.value",
"type": "MULTI_SELECT",
"value": [
"A",
"B"
]
}
]
}
},
{
"ids": [
"geschlecht"
],
"type": "CONCEPT",
"tables": [
{
"id": "geschlecht.geschlecht",
"filters": []
}
]
"ids": "geschlecht",
"tables": {
"id": "geschlecht.geschlecht"
}
}
]
}
],
"timeMode": {
"value": "ABSOLUTE",
"dateRange": {
"min": "2012-01-16",
"min": "2012-01-01",
"max": "2012-12-17"
}
}
},
"concepts": [
"/shared/alter.concept.json",
"/tests/form/shared/abc.concept.json",
"/shared/geschlecht.concept.json"
],
"content": {
"tables": [
"/shared/vers_stamm.table.json"
"/shared/vers_stamm.table.json",
"/tests/form/shared/abc.table.json"
],
"previousQueryResults": [
"tests/form/EXPORT_FORM/ABSOLUT/SIMPLE/query_results_1.csv"
"secondaryIds": [
{
"name": "secondary"
}
],
"previousQueries": [
{
"type": "CONCEPT_QUERY",
"root": {
"ids": [
"abc-concept"
],
"tables": {
"id": "abc-concept.connector"
},
"type": "CONCEPT"
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"type": "FORM_TEST",
"label": "ABS-EXPORT-FORM with multiple features and OR connector",
"expectedCsv": {
"results": "tests/sql/form/ABSOLUT/MULTIPLE_FEATURES/expected_or_connector.csv"
},
"form": {
"type": "EXPORT_FORM",
"queryGroup": "00000000-0000-0000-0000-000000000001",
"resolution": "QUARTERS",
"alsoCreateCoarserSubdivisions": true,
"features": [
{
"type": "OR",
"children": [
{
"type": "CONCEPT",
"ids": "abc-concept",
"tables": {
"id": "abc-concept.connector2",
"filters": [
{
"filter": "abc-concept.connector2.value",
"type": "MULTI_SELECT",
"value": [
"A"
]
}
]
}
},
{
"type": "CONCEPT",
"ids": "geschlecht",
"tables": {
"id": "geschlecht.geschlecht"
}
}
]
}
],
"timeMode": {
"value": "ABSOLUTE",
"dateRange": {
"min": "2012-01-01",
"max": "2012-12-17"
}
}
},
"concepts": [
"/tests/form/shared/abc.concept.json",
"/shared/geschlecht.concept.json"
],
"content": {
"tables": [
"/shared/vers_stamm.table.json",
"/tests/form/shared/abc.table.json"
],
"secondaryIds": [
{
"name": "secondary"
}
],
"previousQueries": [
{
"type": "CONCEPT_QUERY",
"root": {
"ids": [
"abc-concept"
],
"tables": {
"id": "abc-concept.connector"
},
"type": "CONCEPT"
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
result,resolution,index,date_range,Alter and Geschlecht
1,complete,,2012-01-16/2012-12-17,1
1,year,1,2012-01-16/2012-12-17,1
1,quarter,1,2012-01-16/2012-03-31,1
1,quarter,2,2012-04-01/2012-06-30,1
1,quarter,3,2012-07-01/2012-09-30,1
1,quarter,4,2012-10-01/2012-12-17,1
result,resolution,index,date_range,abc-concept and Geschlecht
1,complete,,2012-01-01/2012-12-17,1
1,year,1,2012-01-01/2012-12-17,1
1,quarter,1,2012-01-01/2012-03-31,1
1,quarter,2,2012-04-01/2012-06-30,
1,quarter,3,2012-07-01/2012-09-30,
1,quarter,4,2012-10-01/2012-12-17,
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
result,resolution,index,date_range,abc-concept or Geschlecht
1,complete,,2012-01-01/2012-12-17,1
1,quarter,1,2012-01-01/2012-03-31,1
1,quarter,2,2012-04-01/2012-06-30,1
1,quarter,3,2012-07-01/2012-09-30,1
1,quarter,4,2012-10-01/2012-12-17,1
1,year,1,2012-01-01/2012-12-17,1

0 comments on commit c766d18

Please sign in to comment.