Skip to content

Commit

Permalink
Rework select and filter conversion (#3453)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsrnhld authored Jun 12, 2024
1 parent f16f91d commit 94657e1
Show file tree
Hide file tree
Showing 72 changed files with 1,344 additions and 1,331 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.bakdata.conquery.models.query.QueryResolveContext;
import com.bakdata.conquery.models.query.queryplan.filter.FilterNode;
import com.bakdata.conquery.sql.conversion.cqelement.ConversionContext;
import com.bakdata.conquery.sql.conversion.cqelement.concept.ConceptConversionTables;
import com.bakdata.conquery.sql.conversion.cqelement.concept.ConnectorSqlTables;
import com.bakdata.conquery.sql.conversion.cqelement.concept.FilterContext;
import com.bakdata.conquery.sql.conversion.model.SqlIdColumns;
import com.bakdata.conquery.sql.conversion.model.filter.SqlFilters;
Expand Down Expand Up @@ -61,15 +61,16 @@ public abstract class FilterValue<VALUE> {
private VALUE value;


public void resolve(QueryResolveContext context) {};
public void resolve(QueryResolveContext context) {
}

public FilterNode<?> createNode() {
return getFilter().createFilterNode(getValue());
}

public SqlFilters convertToSqlFilter(SqlIdColumns ids, ConversionContext context, ConceptConversionTables tables) {
public SqlFilters convertToSqlFilter(SqlIdColumns ids, ConversionContext context, ConnectorSqlTables tables) {
FilterContext<VALUE> filterContext = FilterContext.forConceptConversion(ids, value, context, tables);
SqlFilters sqlFilters = filter.convertToSqlFilter(filterContext);
SqlFilters sqlFilters = filter.createConverter().convertToSqlFilter(filter, filterContext);
if (context.isNegation()) {
return new SqlFilters(sqlFilters.getSelects(), sqlFilters.getWhereClauses().negated());
}
Expand All @@ -78,7 +79,7 @@ public SqlFilters convertToSqlFilter(SqlIdColumns ids, ConversionContext context

public Condition convertForTableExport(SqlIdColumns ids, ConversionContext context) {
FilterContext<VALUE> filterContext = FilterContext.forTableExport(ids, value, context);
return filter.convertForTableExport(filterContext);
return filter.createConverter().convertForTableExport(filter, filterContext);
}

@NoArgsConstructor
Expand Down Expand Up @@ -217,7 +218,11 @@ public GroupFilterValue deserialize(JsonParser p, DeserializationContext ctxt) t
final Filter<?> filter = nsIdDeserializer.deserialize(filterTraverse, ctxt);

if (!(filter instanceof GroupFilter)) {
throw InvalidTypeIdException.from(filterNode.traverse(), GroupFilter.class, String.format("Expected filter of type %s but was: %s", GroupFilter.class, filter != null ? filter.getClass() : null));
throw InvalidTypeIdException.from(filterNode.traverse(), GroupFilter.class, String.format("Expected filter of type %s but was: %s", GroupFilter.class,
filter != null
? filter.getClass()
: null
));
}
GroupFilter groupFilter = (GroupFilter) filter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import com.bakdata.conquery.models.identifiable.ids.NamespacedIdentifiable;
import com.bakdata.conquery.models.identifiable.ids.specific.FilterId;
import com.bakdata.conquery.models.query.queryplan.filter.FilterNode;
import com.bakdata.conquery.sql.conversion.cqelement.concept.FilterContext;
import com.bakdata.conquery.sql.conversion.model.filter.SqlFilters;
import com.bakdata.conquery.sql.conversion.model.filter.FilterConverter;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand All @@ -25,7 +24,6 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jooq.Condition;

/**
* This class is the abstract superclass for all filters.
Expand Down Expand Up @@ -92,7 +90,8 @@ public boolean isForConnectorsTable() {
continue;
}

log.error("Filter[{}] of Table[{}] is not of Connector[{}]#Table[{}]", getId(), column.getTable().getId(), connector.getId(), connector.getTable().getId());
log.error("Filter[{}] of Table[{}] is not of Connector[{}]#Table[{}]", getId(), column.getTable().getId(), connector.getId(), connector.getTable()
.getId());

valid = false;
}
Expand All @@ -101,13 +100,7 @@ public boolean isForConnectorsTable() {
}

@JsonIgnore
public SqlFilters convertToSqlFilter(FilterContext<FILTER_VALUE> filterContext) {
throw new UnsupportedOperationException("SQL conversion of filter %s not implemented yet.".formatted(getClass()));
public <F extends Filter<FILTER_VALUE>> FilterConverter<F, FILTER_VALUE> createConverter() {
throw new UnsupportedOperationException("No converter implemented for Filter %s".formatted(getClass()));
}

@JsonIgnore
public Condition convertForTableExport(FilterContext<FILTER_VALUE> filterContext) {
throw new UnsupportedOperationException("SQL conversion of filter %s not implemented yet".formatted(getClass()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
import com.bakdata.conquery.models.datasets.concepts.filters.Filter;
import com.bakdata.conquery.models.query.filter.event.MultiSelectFilterNode;
import com.bakdata.conquery.models.query.queryplan.filter.FilterNode;
import com.bakdata.conquery.sql.conversion.cqelement.concept.FilterContext;
import com.bakdata.conquery.sql.conversion.cqelement.concept.SelectFilterUtil;
import com.bakdata.conquery.sql.conversion.model.filter.MultiSelectCondition;
import com.bakdata.conquery.sql.conversion.model.filter.SqlFilters;
import com.bakdata.conquery.sql.conversion.model.filter.FilterConverter;
import com.bakdata.conquery.sql.conversion.model.filter.MultiSelectFilterConverter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;
import org.jooq.Condition;

/**
* This filter represents a select in the front end. This means that the user can select one or more values from a list of values.
*
* <p>
* This Filter can use optional labels or a template for displaying, same as {@link MultiSelectFilter}.
* However, the frontend will fetch and display data beyond the defined values for {@link BigMultiSelectFilter}/BIG_MULTI_SELECT.
*/
Expand All @@ -37,12 +34,7 @@ public FilterNode createFilterNode(String[] value) {
}

@Override
public SqlFilters convertToSqlFilter(FilterContext<String[]> filterContext) {
return SelectFilterUtil.convert(this, filterContext, filterContext.getValue());
}

@Override
public Condition convertForTableExport(FilterContext<String[]> filterContext) {
return MultiSelectCondition.onColumn(getColumn(), filterContext.getValue(), filterContext.getSqlDialect().getFunctionProvider()).condition();
public FilterConverter<MultiSelectFilter, String[]> createConverter() {
return new MultiSelectFilterConverter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@
import com.bakdata.conquery.models.query.queryplan.aggregators.DistinctValuesWrapperAggregator;
import com.bakdata.conquery.models.query.queryplan.aggregators.specific.CountAggregator;
import com.bakdata.conquery.models.query.queryplan.filter.FilterNode;
import com.bakdata.conquery.sql.conversion.cqelement.concept.FilterContext;
import com.bakdata.conquery.sql.conversion.model.aggregator.CountSqlAggregator;
import com.bakdata.conquery.sql.conversion.model.filter.CountCondition;
import com.bakdata.conquery.sql.conversion.model.filter.SqlFilters;
import com.bakdata.conquery.sql.conversion.model.filter.FilterConverter;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jooq.Condition;

@CPSType(id = "COUNT", base = Filter.class)
@NoArgsConstructor
Expand Down Expand Up @@ -74,12 +71,7 @@ public List<Column> getRequiredColumns() {
}

@Override
public SqlFilters convertToSqlFilter(FilterContext<Range.LongRange> filterContext) {
return CountSqlAggregator.create(this, filterContext).getSqlFilters();
}

@Override
public Condition convertForTableExport(FilterContext<Range.LongRange> filterContext) {
return CountCondition.onColumn(column, filterContext.getValue()).condition();
public FilterConverter<CountFilter, Range.LongRange> createConverter() {
return new CountSqlAggregator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
import com.bakdata.conquery.models.query.queryplan.aggregators.specific.CountQuartersOfDateRangeAggregator;
import com.bakdata.conquery.models.query.queryplan.aggregators.specific.CountQuartersOfDatesAggregator;
import com.bakdata.conquery.models.query.queryplan.filter.FilterNode;
import com.bakdata.conquery.sql.conversion.cqelement.concept.FilterContext;
import com.bakdata.conquery.sql.conversion.model.filter.SqlFilters;
import com.bakdata.conquery.sql.conversion.model.select.CountQuartersSqlAggregator;
import com.bakdata.conquery.sql.conversion.model.aggregator.CountQuartersSqlAggregator;
import com.bakdata.conquery.sql.conversion.model.filter.FilterConverter;
import lombok.Getter;
import lombok.Setter;

@Setter @Getter
@CPSType(id="COUNT_QUARTERS", base=Filter.class)
@Setter
@Getter
@CPSType(id = "COUNT_QUARTERS", base = Filter.class)
public class CountQuartersFilter extends SingleColumnFilter<Range.LongRange> {

@Override
public EnumSet<MajorTypeId> getAcceptedColumnTypes() {
return EnumSet.of(MajorTypeId.DATE, MajorTypeId.DATE_RANGE);
Expand All @@ -44,11 +44,7 @@ public FilterNode createFilterNode(Range.LongRange value) {
}

@Override
public SqlFilters convertToSqlFilter(FilterContext<Range.LongRange> filterContext) {
if (getColumn().getType() == MajorTypeId.DATE_RANGE) {
throw new UnsupportedOperationException("COUNT_QUARTERS conversion on columns of type DATE_RANGE not implemented yet.");
}
return CountQuartersSqlAggregator.create(this, filterContext).getSqlFilters();
public FilterConverter<CountQuartersFilter, Range.LongRange> createConverter() {
return new CountQuartersSqlAggregator();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,25 @@
import com.bakdata.conquery.models.exceptions.ConceptConfigurationException;
import com.bakdata.conquery.models.query.filter.event.DateDistanceFilterNode;
import com.bakdata.conquery.models.query.queryplan.filter.FilterNode;
import com.bakdata.conquery.sql.conversion.cqelement.concept.FilterContext;
import com.bakdata.conquery.sql.conversion.model.aggregator.DateDistanceSqlAggregator;
import com.bakdata.conquery.sql.conversion.model.filter.DateDistanceCondition;
import com.bakdata.conquery.sql.conversion.model.filter.SqlFilters;
import com.bakdata.conquery.sql.conversion.model.filter.FilterConverter;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jooq.Condition;

/**
* This filter represents a select in the front end. This means that the user can select one or more values from a list of values.
*/
@Getter @Setter @Slf4j
@CPSType(id="DATE_DISTANCE", base=Filter.class)
@Getter
@Setter
@Slf4j
@CPSType(id = "DATE_DISTANCE", base = Filter.class)
public class DateDistanceFilter extends SingleColumnFilter<Range.LongRange> {

@NotNull
private ChronoUnit timeUnit = ChronoUnit.YEARS;

@Override
public EnumSet<MajorTypeId> getAcceptedColumnTypes() {
return EnumSet.of(MajorTypeId.DATE);
Expand All @@ -47,19 +46,14 @@ public void configureFrontend(FrontendFilterConfiguration.Top f, ConqueryConfig

f.setType(FrontendFilterType.Fields.INTEGER_RANGE);
}

@Override
public FilterNode createFilterNode(Range.LongRange value) {
return new DateDistanceFilterNode(getColumn(), timeUnit, value);
}

@Override
public SqlFilters convertToSqlFilter(FilterContext<Range.LongRange> filterContext) {
return DateDistanceSqlAggregator.create(this, filterContext).getSqlFilters();
}

@Override
public Condition convertForTableExport(FilterContext<Range.LongRange> filterContext) {
return DateDistanceCondition.onColumn(getColumn(), getTimeUnit(), filterContext).condition();
public FilterConverter<DateDistanceFilter, Range.LongRange> createConverter() {
return new DateDistanceSqlAggregator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,19 @@
import com.bakdata.conquery.models.exceptions.ConceptConfigurationException;
import com.bakdata.conquery.models.query.filter.event.FlagColumnsFilterNode;
import com.bakdata.conquery.models.query.queryplan.filter.FilterNode;
import com.bakdata.conquery.sql.conversion.cqelement.concept.FilterContext;
import com.bakdata.conquery.sql.conversion.model.aggregator.FlagSqlAggregator;
import com.bakdata.conquery.sql.conversion.model.filter.FlagCondition;
import com.bakdata.conquery.sql.conversion.model.filter.SqlFilters;
import com.bakdata.conquery.sql.conversion.model.filter.FilterConverter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.dropwizard.validation.ValidationMethod;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.jooq.Condition;

/**
* Implements a MultiSelect type filter, where an event can meet multiple criteria (as opposed to {@link MultiSelectFilter} which is restricted to one value per event).
* This is achieved by using multiple {@link com.bakdata.conquery.models.types.ResultType.BooleanT} columns, each defining if one property is met or not.
*
* <p>
* The selected flags are logically or-ed.
*/
@Getter
Expand Down Expand Up @@ -76,7 +73,7 @@ public FilterNode<?> createFilterNode(String[] labels) {
columns[index] = column;
}

if(!missing.isEmpty()){
if (!missing.isEmpty()) {
throw new ConqueryError.ExecutionCreationPlanMissingFlagsError(missing);
}

Expand All @@ -96,12 +93,7 @@ public boolean isAllColumnsBoolean() {
}

@Override
public SqlFilters convertToSqlFilter(FilterContext<String[]> filterContext) {
return FlagSqlAggregator.create(this, filterContext).getSqlFilters();
}

@Override
public Condition convertForTableExport(FilterContext<String[]> filterContext) {
return FlagCondition.onColumn(getFlags(), filterContext.getValue()).condition();
public FilterConverter<FlagFilter, String[]> createConverter() {
return new FlagSqlAggregator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import com.bakdata.conquery.models.datasets.concepts.filters.Filter;
import com.bakdata.conquery.models.query.filter.event.MultiSelectFilterNode;
import com.bakdata.conquery.models.query.queryplan.filter.FilterNode;
import com.bakdata.conquery.sql.conversion.cqelement.concept.FilterContext;
import com.bakdata.conquery.sql.conversion.cqelement.concept.SelectFilterUtil;
import com.bakdata.conquery.sql.conversion.model.filter.MultiSelectCondition;
import com.bakdata.conquery.sql.conversion.model.filter.SqlFilters;
import com.bakdata.conquery.sql.conversion.model.filter.FilterConverter;
import com.bakdata.conquery.sql.conversion.model.filter.MultiSelectFilterConverter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.jooq.Condition;

/**
* This filter represents a select in the front end. This means that the user can select one or more values from a list of values.
Expand All @@ -36,12 +33,7 @@ public FilterNode<?> createFilterNode(String[] value) {
}

@Override
public SqlFilters convertToSqlFilter(FilterContext<String[]> filterContext) {
return SelectFilterUtil.convert(this, filterContext, filterContext.getValue());
}

@Override
public Condition convertForTableExport(FilterContext<String[]> filterContext) {
return MultiSelectCondition.onColumn(getColumn(), filterContext.getValue(), filterContext.getSqlDialect().getFunctionProvider()).condition();
public FilterConverter<MultiSelectFilter, String[]> createConverter() {
return new MultiSelectFilterConverter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
import com.bakdata.conquery.models.query.filter.event.number.MoneyFilterNode;
import com.bakdata.conquery.models.query.filter.event.number.RealFilterNode;
import com.bakdata.conquery.models.query.queryplan.filter.FilterNode;
import com.bakdata.conquery.sql.conversion.cqelement.concept.FilterContext;
import com.bakdata.conquery.sql.conversion.model.aggregator.NumberSqlAggregator;
import com.bakdata.conquery.sql.conversion.model.filter.NumberCondition;
import com.bakdata.conquery.sql.conversion.model.filter.SqlFilters;
import com.bakdata.conquery.sql.conversion.model.filter.FilterConverter;
import com.bakdata.conquery.sql.conversion.model.filter.NumberFilterConverter;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jooq.Condition;

/**
* This filter represents a filter on an integer columnof each event.
Expand Down Expand Up @@ -60,12 +57,7 @@ public FilterNode<?> createFilterNode(RANGE value) {
}

@Override
public SqlFilters convertToSqlFilter(FilterContext<RANGE> filterContext) {
return NumberSqlAggregator.create(this, filterContext).getSqlFilters();
}

@Override
public Condition convertForTableExport(FilterContext<RANGE> filterContext) {
return NumberCondition.onColumn(getColumn(), filterContext.getValue()).condition();
public FilterConverter<? extends NumberFilter<RANGE>, RANGE> createConverter() {
return new NumberFilterConverter<>();
}
}
Loading

0 comments on commit 94657e1

Please sign in to comment.