-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PREFIX aggregation select and PREFIX_TEXT event filter (#134)
* Add Prefix select implementation * Add Prefix select test * Add PREFIX_TEXT filter * Add test case * Refactor string aggregation SQL * Improve PREFIX test case * Fix field data type * Improve string agg SQL * Fix formatting Co-authored-by: Jonas Arnhold <jonas.arnhold@bakdata.com> --------- Co-authored-by: Jonas Arnhold <jonas.arnhold@bakdata.com>
- Loading branch information
Showing
12 changed files
with
318 additions
and
2 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...m/bakdata/conquery/sql/conversion/cqelement/concept/filter/PrefixTextFilterConverter.java
This file contains 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,49 @@ | ||
package com.bakdata.conquery.sql.conversion.cqelement.concept.filter; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import com.bakdata.conquery.models.datasets.concepts.filters.specific.PrefixTextFilter; | ||
import com.bakdata.conquery.sql.conversion.cqelement.concept.CteStep; | ||
import com.bakdata.conquery.sql.conversion.model.filter.ConceptFilter; | ||
import com.bakdata.conquery.sql.conversion.model.filter.Filters; | ||
import com.bakdata.conquery.sql.conversion.model.filter.PrefixTextCondition; | ||
import com.bakdata.conquery.sql.conversion.model.select.ExtractingSqlSelect; | ||
import com.bakdata.conquery.sql.conversion.model.select.SqlSelects; | ||
import org.jooq.Field; | ||
|
||
public class PrefixTextFilterConverter implements FilterConverter<String, PrefixTextFilter> { | ||
|
||
@Override | ||
public ConceptFilter convert(PrefixTextFilter prefixTextFilter, FilterContext<String> context) { | ||
|
||
ExtractingSqlSelect<Object> rootSelect = new ExtractingSqlSelect<>( | ||
context.getConceptTables().getPredecessorTableName(CteStep.PREPROCESSING), | ||
prefixTextFilter.getColumn().getName(), | ||
Object.class | ||
); | ||
|
||
Field<Object> qualifiedRootSelect = context.getConceptTables().qualifyOnPredecessorTableName(CteStep.EVENT_FILTER, rootSelect.aliased()); | ||
PrefixTextCondition prefixTextCondition = new PrefixTextCondition(qualifiedRootSelect, context.getValue()); | ||
|
||
return new ConceptFilter( | ||
SqlSelects.builder() | ||
.forPreprocessingStep(Collections.singletonList(rootSelect)) | ||
.build(), | ||
Filters.builder() | ||
.event(Collections.singletonList(prefixTextCondition)) | ||
.build() | ||
); | ||
} | ||
|
||
@Override | ||
public Set<CteStep> requiredSteps() { | ||
return CteStep.withOptionalSteps(CteStep.EVENT_FILTER); | ||
} | ||
|
||
@Override | ||
public Class<PrefixTextFilter> getConversionClass() { | ||
return PrefixTextFilter.class; | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...a/com/bakdata/conquery/sql/conversion/cqelement/concept/select/PrefixSelectConverter.java
This file contains 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,48 @@ | ||
package com.bakdata.conquery.sql.conversion.cqelement.concept.select; | ||
|
||
import java.util.Collections; | ||
|
||
import com.bakdata.conquery.models.datasets.concepts.select.connector.specific.PrefixSelect; | ||
import com.bakdata.conquery.sql.conversion.cqelement.concept.CteStep; | ||
import com.bakdata.conquery.sql.conversion.model.select.ExtractingSqlSelect; | ||
import com.bakdata.conquery.sql.conversion.model.select.PrefixSqlSelect; | ||
import com.bakdata.conquery.sql.conversion.model.select.SqlSelects; | ||
import org.jooq.Field; | ||
|
||
public class PrefixSelectConverter implements SelectConverter<PrefixSelect> { | ||
|
||
@Override | ||
public SqlSelects convert(PrefixSelect prefixSelect, SelectContext context) { | ||
ExtractingSqlSelect<String> rootSelect = new ExtractingSqlSelect<>( | ||
context.getConceptTables().getPredecessorTableName(CteStep.PREPROCESSING), | ||
prefixSelect.getColumn().getName(), | ||
String.class | ||
); | ||
|
||
Field<Object> qualifiedRootSelect = context.getConceptTables().qualifyOnPredecessorTableName(CteStep.AGGREGATION_SELECT, rootSelect.aliased()); | ||
PrefixSqlSelect prefixGroupBy = new PrefixSqlSelect( | ||
qualifiedRootSelect, | ||
prefixSelect.getPrefix(), | ||
context.getParentContext().getSqlDialect().getFunction(), | ||
prefixSelect.getName() | ||
); | ||
|
||
ExtractingSqlSelect<String> finalSelect = new ExtractingSqlSelect<>( | ||
context.getConceptTables().getPredecessorTableName(CteStep.FINAL), | ||
prefixGroupBy.aliased().getName(), | ||
String.class | ||
); | ||
|
||
return SqlSelects.builder() | ||
.forPreprocessingStep(Collections.singletonList(rootSelect)) | ||
.forAggregationSelectStep(Collections.singletonList(prefixGroupBy)) | ||
.forFinalStep(Collections.singletonList(finalSelect)) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public Class<? extends PrefixSelect> getConversionClass() { | ||
return PrefixSelect.class; | ||
} | ||
|
||
} |
This file contains 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 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
23 changes: 23 additions & 0 deletions
23
...d/src/main/java/com/bakdata/conquery/sql/conversion/model/filter/PrefixTextCondition.java
This file contains 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,23 @@ | ||
package com.bakdata.conquery.sql.conversion.model.filter; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.jooq.Condition; | ||
import org.jooq.Field; | ||
|
||
@RequiredArgsConstructor | ||
public class PrefixTextCondition implements FilterCondition { | ||
|
||
private final Field<Object> prefixTextColumn; | ||
private final String value; | ||
|
||
@Override | ||
public Condition filterCondition() { | ||
return prefixTextColumn.like(value + "%"); | ||
} | ||
|
||
@Override | ||
public FilterType type() { | ||
return FilterType.EVENT; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
backend/src/main/java/com/bakdata/conquery/sql/conversion/model/select/PrefixSqlSelect.java
This file contains 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,34 @@ | ||
package com.bakdata.conquery.sql.conversion.model.select; | ||
|
||
import com.bakdata.conquery.sql.conversion.dialect.SqlFunctionProvider; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.RequiredArgsConstructor; | ||
import org.jooq.Field; | ||
import org.jooq.impl.DSL; | ||
|
||
@RequiredArgsConstructor | ||
@EqualsAndHashCode | ||
public class PrefixSqlSelect implements SqlSelect { | ||
|
||
private final Field<Object> columnToPrefix; | ||
private final String prefix; | ||
private final SqlFunctionProvider functionProvider; | ||
private final String alias; | ||
|
||
@Override | ||
public Field<Object> select() { | ||
return functionProvider.prefixStringAggregation(columnToPrefix, prefix).as(alias); | ||
} | ||
|
||
@Override | ||
public Field<?> aliased() { | ||
return DSL.field(alias); | ||
} | ||
|
||
@Override | ||
public String columnName() { | ||
return columnToPrefix.getName(); | ||
} | ||
|
||
} | ||
|
6 changes: 6 additions & 0 deletions
6
backend/src/test/resources/tests/sql/filter/prefix_text/content.csv
This file contains 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 @@ | ||
pid,value | ||
1,"fa" | ||
2,"fb" | ||
3, | ||
4,"f" | ||
5,"m" |
4 changes: 4 additions & 0 deletions
4
backend/src/test/resources/tests/sql/filter/prefix_text/expected.csv
This file contains 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,4 @@ | ||
result,prefix | ||
1 | ||
2 | ||
4 |
67 changes: 67 additions & 0 deletions
67
backend/src/test/resources/tests/sql/filter/prefix_text/prefix_text.json
This file contains 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,67 @@ | ||
{ | ||
"label": "PREFIX_TEXT filter", | ||
"type": "SQL_TEST", | ||
"expectedCsv": "expected.csv", | ||
"query": { | ||
"type": "CONCEPT_QUERY", | ||
"root": { | ||
"type": "AND", | ||
"children": [ | ||
{ | ||
"ids": [ | ||
"prefix" | ||
], | ||
"type": "CONCEPT", | ||
"label": "prefix", | ||
"tables": [ | ||
{ | ||
"id": "prefix.prefix_connector", | ||
"filters": [ | ||
{ | ||
"filter": "prefix.prefix_connector.prefix_filter", | ||
"type": "SELECT", | ||
"value": "f" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
"concepts": [ | ||
{ | ||
"label": "prefix", | ||
"type": "TREE", | ||
"connectors": [ | ||
{ | ||
"label": "prefix_connector", | ||
"table": "table1", | ||
"filters": { | ||
"label": "prefix_filter", | ||
"column": "table1.value", | ||
"type": "PREFIX_TEXT" | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"content": { | ||
"tables": [ | ||
{ | ||
"csv": "tests/sql/filter/prefix_text/content.csv", | ||
"name": "table1", | ||
"primaryColumn": { | ||
"name": "pid", | ||
"type": "STRING" | ||
}, | ||
"columns": [ | ||
{ | ||
"name": "value", | ||
"type": "STRING" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
backend/src/test/resources/tests/sql/selects/prefix/content.csv
This file contains 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 @@ | ||
pid,prefix | ||
1,"fa" | ||
1,"fb" | ||
2, | ||
3,"f" | ||
4,"m" |
5 changes: 5 additions & 0 deletions
5
backend/src/test/resources/tests/sql/selects/prefix/expected.csv
This file contains 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,5 @@ | ||
result,prefix | ||
1,"[fa, fb]" | ||
2, | ||
3,[f] | ||
4, |
64 changes: 64 additions & 0 deletions
64
backend/src/test/resources/tests/sql/selects/prefix/prefix.json
This file contains 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,64 @@ | ||
{ | ||
"label": "PREFIX select", | ||
"type": "SQL_TEST", | ||
"expectedCsv": "expected.csv", | ||
"query": { | ||
"type": "CONCEPT_QUERY", | ||
"root": { | ||
"type": "AND", | ||
"children": [ | ||
{ | ||
"ids": [ | ||
"prefix" | ||
], | ||
"type": "CONCEPT", | ||
"label": "prefix", | ||
"tables": [ | ||
{ | ||
"id": "prefix.prefix_connector", | ||
"selects": [ | ||
"prefix.prefix_connector.prefix_select" | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
}, | ||
"concepts": [ | ||
{ | ||
"label": "prefix", | ||
"type": "TREE", | ||
"connectors": [ | ||
{ | ||
"label": "prefix_connector", | ||
"table": "table1", | ||
"selects": { | ||
"type": "PREFIX", | ||
"name": "prefix_select", | ||
"column": "table1.prefix", | ||
"prefix": "f" | ||
} | ||
} | ||
] | ||
} | ||
], | ||
"content": { | ||
"tables": [ | ||
{ | ||
"csv": "tests/sql/selects/prefix/content.csv", | ||
"name": "table1", | ||
"primaryColumn": { | ||
"name": "pid", | ||
"type": "STRING" | ||
}, | ||
"columns": [ | ||
{ | ||
"name": "prefix", | ||
"type": "STRING" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} |