Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ default Condition isNotEmptyValidityDate(ValidityDate validityDate) {
*/
ColumnDateRange allRange();

<T> Field<T> anyValue(Field<T> field);

/**
* Creates a {@link ColumnDateRange} for a tables {@link CQTable}s validity date. The validity dates bounds will be restricted by the given date
* restriction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public ColumnDateRange allRange() {
return ColumnDateRange.of(getMinDateExpression().as("all_range_start"), getMaxDateExpression().as("all_range_end"));
}

@Override
public <T> Field<T> anyValue(Field<T> field) {
return DSL.anyValue(field);
}

private ColumnDateRange toColumnDateRange(ValidityDate validityDate) {

String tableName = validityDate.getConnector().resolveTableId().getTable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public ColumnDateRange allRange() {
return ColumnDateRange.of(getMinDateExpression(), getMaxDateExpression());
}

@Override
public <T> Field<T> anyValue(Field<T> field) {
// Hana does not have any_value
return DSL.min(field);
}

private ColumnDateRange toColumnDateRange(ValidityDate validityDate) {

String tableName = validityDate.getConnector().resolveTableId().getTable();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.bakdata.conquery.sql.conversion.model.select;

import com.bakdata.conquery.sql.conversion.dialect.SqlFunctionProvider;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.jooq.Field;
import org.jooq.Name;
import org.jooq.impl.DSL;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -60,7 +60,14 @@ public SqlSelect toFinalRepresentation() {
return new ExistsSqlSelect(coalesceWithZero().as(alias), alias);
}

@Override
public List<Field<?>> aggregateForFinalQuery(SqlFunctionProvider functionProvider) {
// We have to coalesce at the end of the query because full-outer-joins will create null values, which we want to avoid.
Field<Integer> coalesced = coalesce(max(select()), inline(0));
return List.of(coalesced.as(alias));
}

private Field<Integer> coalesceWithZero() {
return coalesce(select(), value(0));
return coalesce(select(), inline(0));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.bakdata.conquery.sql.conversion.model.select;

import com.bakdata.conquery.models.datasets.concepts.select.concept.specific.ExistsSelect;
import java.util.List;

import com.bakdata.conquery.sql.conversion.dialect.SqlFunctionProvider;
import com.bakdata.conquery.sql.conversion.model.Qualifiable;
import org.jooq.Field;

import java.util.List;

public interface SqlSelect extends Qualifiable<SqlSelect> {

List<Field<?>> toFields();
Expand Down Expand Up @@ -37,4 +38,13 @@ default SqlSelect toFinalRepresentation() {
return this;
}

/**
* Aggregate this select to one value per ID group in the final concept query.
*/
default List<Field<?>> aggregateForFinalQuery(SqlFunctionProvider functionProvider) {
return toFinalRepresentation().toFields().stream()
.<Field<?>>map(field -> functionProvider.anyValue(field).as(field.getName()))
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
import com.bakdata.conquery.sql.conversion.cqelement.ConversionContext;
import com.bakdata.conquery.sql.conversion.dialect.SqlFunctionProvider;
import com.bakdata.conquery.sql.conversion.model.*;
import com.bakdata.conquery.sql.conversion.model.select.FieldWrapper;
import com.bakdata.conquery.sql.conversion.model.select.SqlSelect;
import lombok.RequiredArgsConstructor;
import org.jooq.Field;
import org.jooq.*;
import org.jooq.Record;
import org.jooq.Select;
import org.jooq.TableLike;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

Expand Down Expand Up @@ -82,18 +80,32 @@ public ConversionContext convert(ConceptQuery conceptQuery, ConversionContext co
}

private Selects getFinalSelects(ConceptQuery conceptQuery, Selects preFinalSelects, SqlFunctionProvider functionProvider) {
Selects finalSelects = preFinalSelects;
if (conceptQuery.getDateAggregationMode() == DateAggregationMode.NONE) {
return preFinalSelects.blockValidityDate();
finalSelects = preFinalSelects.blockValidityDate();
}
return preFinalSelects;

return Selects.builder()
.ids(finalSelects.getIds())
.validityDate(finalSelects.getValidityDate())
.stratificationDate(finalSelects.getStratificationDate())
.sqlSelects(getFinalAggregatedSelects(finalSelects, functionProvider))
.build();
}

private List<? extends FieldWrapper<?>> getFinalAggregatedSelects(Selects finalSelects, SqlFunctionProvider functionProvider) {
return finalSelects.getSqlSelects().stream()
.flatMap(sqlSelect -> sqlSelect.aggregateForFinalQuery(functionProvider).stream())
.map(this::toFieldWrapper)
.toList();
}

private FieldWrapper<?> toFieldWrapper(Field<?> field) {
return new FieldWrapper<>(field);
}

private List<Field<?>> getFinalGroupBySelects(Selects preFinalSelects) {
List<Field<?>> groupBySelects = new ArrayList<>();
groupBySelects.addAll(preFinalSelects.getIds().toFields());
// TODO instead us any_value selects
groupBySelects.addAll(preFinalSelects.explicitSelects());
return groupBySelects;
return preFinalSelects.getIds().toFields();
}

}
Loading