Skip to content

Commit

Permalink
Pass AttrSwapFields to nested translators (#1126)
Browse files Browse the repository at this point in the history
  • Loading branch information
dexamundsen authored Jan 14, 2025
1 parent df4fc84 commit 23050e1
Show file tree
Hide file tree
Showing 18 changed files with 239 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import bio.terra.tanagra.query.sql.translator.ApiFieldTranslator;
import bio.terra.tanagra.query.sql.translator.ApiFilterTranslator;
import bio.terra.tanagra.query.sql.translator.ApiTranslator;
import bio.terra.tanagra.underlay.entitymodel.Attribute;
import jakarta.annotation.*;
import java.util.*;

Expand Down Expand Up @@ -72,61 +73,83 @@ public ApiFieldTranslator translator(RelatedEntityIdCountField relatedEntityIdCo
}

@Override
public ApiFilterTranslator translator(AttributeFilter attributeFilter) {
return new BQAttributeFilterTranslator(this, attributeFilter);
public ApiFilterTranslator translator(
AttributeFilter attributeFilter, Map<Attribute, SqlField> attributeSwapFields) {
return new BQAttributeFilterTranslator(this, attributeFilter, attributeSwapFields);
}

@Override
public Optional<ApiFilterTranslator> mergedTranslator(
List<AttributeFilter> attributeFilters, LogicalOperator logicalOperator) {
List<AttributeFilter> attributeFilters,
LogicalOperator logicalOperator,
Map<Attribute, SqlField> attributeSwapFields) {
return BQAttributeFilterTranslator.canMergeTranslation(attributeFilters)
? Optional.of(new BQAttributeFilterTranslator(this, attributeFilters, logicalOperator))
? Optional.of(
new BQAttributeFilterTranslator(
this, attributeFilters, logicalOperator, attributeSwapFields))
: Optional.empty();
}

@Override
public ApiFilterTranslator translator(HierarchyHasAncestorFilter hierarchyHasAncestorFilter) {
return new BQHierarchyHasAncestorFilterTranslator(this, hierarchyHasAncestorFilter);
public ApiFilterTranslator translator(
HierarchyHasAncestorFilter hierarchyHasAncestorFilter,
Map<Attribute, SqlField> attributeSwapFields) {
return new BQHierarchyHasAncestorFilterTranslator(
this, hierarchyHasAncestorFilter, attributeSwapFields);
}

@Override
public ApiFilterTranslator translator(HierarchyHasParentFilter hierarchyHasParentFilter) {
return new BQHierarchyHasParentFilterTranslator(this, hierarchyHasParentFilter);
public ApiFilterTranslator translator(
HierarchyHasParentFilter hierarchyHasParentFilter,
Map<Attribute, SqlField> attributeSwapFields) {
return new BQHierarchyHasParentFilterTranslator(
this, hierarchyHasParentFilter, attributeSwapFields);
}

@Override
public ApiFilterTranslator translator(HierarchyIsLeafFilter hierarchyIsLeafFilter) {
return new BQHierarchyIsLeafFilterTranslator(this, hierarchyIsLeafFilter);
public ApiFilterTranslator translator(
HierarchyIsLeafFilter hierarchyIsLeafFilter, Map<Attribute, SqlField> attributeSwapFields) {
return new BQHierarchyIsLeafFilterTranslator(this, hierarchyIsLeafFilter, attributeSwapFields);
}

@Override
public ApiFilterTranslator translator(HierarchyIsMemberFilter hierarchyIsMemberFilter) {
return new BQHierarchyIsMemberFilterTranslator(this, hierarchyIsMemberFilter);
public ApiFilterTranslator translator(
HierarchyIsMemberFilter hierarchyIsMemberFilter,
Map<Attribute, SqlField> attributeSwapFields) {
return new BQHierarchyIsMemberFilterTranslator(
this, hierarchyIsMemberFilter, attributeSwapFields);
}

@Override
public ApiFilterTranslator translator(HierarchyIsRootFilter hierarchyIsRootFilter) {
return new BQHierarchyIsRootFilterTranslator(this, hierarchyIsRootFilter);
public ApiFilterTranslator translator(
HierarchyIsRootFilter hierarchyIsRootFilter, Map<Attribute, SqlField> attributeSwapFields) {
return new BQHierarchyIsRootFilterTranslator(this, hierarchyIsRootFilter, attributeSwapFields);
}

@Override
public ApiFilterTranslator translator(PrimaryWithCriteriaFilter primaryWithCriteriaFilter) {
return new BQPrimaryWithCriteriaFilterTranslator(this, primaryWithCriteriaFilter);
public ApiFilterTranslator translator(
PrimaryWithCriteriaFilter primaryWithCriteriaFilter,
Map<Attribute, SqlField> attributeSwapFields) {
return new BQPrimaryWithCriteriaFilterTranslator(
this, primaryWithCriteriaFilter, attributeSwapFields);
}

@Override
public ApiFilterTranslator translator(RelationshipFilter relationshipFilter) {
return new BQRelationshipFilterTranslator(this, relationshipFilter);
public ApiFilterTranslator translator(
RelationshipFilter relationshipFilter, Map<Attribute, SqlField> attributeSwapFields) {
return new BQRelationshipFilterTranslator(this, relationshipFilter, attributeSwapFields);
}

@Override
public ApiFilterTranslator translator(TextSearchFilter textSearchFilter) {
return new BQTextSearchFilterTranslator(this, textSearchFilter);
public ApiFilterTranslator translator(
TextSearchFilter textSearchFilter, Map<Attribute, SqlField> attributeSwapFields) {
return new BQTextSearchFilterTranslator(this, textSearchFilter, attributeSwapFields);
}

@Override
public ApiFilterTranslator translator(TemporalPrimaryFilter temporalPrimaryFilter) {
return new BQTemporalPrimaryFilterTranslator(this, temporalPrimaryFilter);
public ApiFilterTranslator translator(
TemporalPrimaryFilter temporalPrimaryFilter, Map<Attribute, SqlField> attributeSwapFields) {
return new BQTemporalPrimaryFilterTranslator(this, temporalPrimaryFilter, attributeSwapFields);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
import bio.terra.tanagra.underlay.indextable.ITEntityMain;
import bio.terra.tanagra.underlay.indextable.ITEntitySearchByAttribute;
import java.util.List;
import java.util.Map;

public class BQAttributeFilterTranslator extends ApiFilterTranslator {
private final AttributeFilter attributeFilter;
private final List<AttributeFilter> attributeFilterList;
private final LogicalOperator logicalOperatorForList;

public BQAttributeFilterTranslator(
ApiTranslator apiTranslator, AttributeFilter singleAttributeFilter) {
super(apiTranslator);
ApiTranslator apiTranslator,
AttributeFilter singleAttributeFilter,
Map<Attribute, SqlField> attributeSwapFields) {
super(apiTranslator, attributeSwapFields);
this.attributeFilter = singleAttributeFilter;
this.attributeFilterList = null;
this.logicalOperatorForList = null;
Expand All @@ -31,8 +34,9 @@ public BQAttributeFilterTranslator(
public BQAttributeFilterTranslator(
ApiTranslator apiTranslator,
List<AttributeFilter> attributeFilters,
LogicalOperator logicalOperator) {
super(apiTranslator);
LogicalOperator logicalOperator,
Map<Attribute, SqlField> attributeSwapFields) {
super(apiTranslator, attributeSwapFields);
this.attributeFilter = null;
this.attributeFilterList = attributeFilters;
this.logicalOperatorForList = logicalOperator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
import bio.terra.tanagra.query.sql.translator.ApiTranslator;
import bio.terra.tanagra.underlay.entitymodel.Attribute;
import bio.terra.tanagra.underlay.indextable.ITHierarchyAncestorDescendant;
import java.util.Map;

public class BQHierarchyHasAncestorFilterTranslator extends ApiFilterTranslator {
private final HierarchyHasAncestorFilter hierarchyHasAncestorFilter;

public BQHierarchyHasAncestorFilterTranslator(
ApiTranslator apiTranslator, HierarchyHasAncestorFilter hierarchyHasAncestorFilter) {
super(apiTranslator);
ApiTranslator apiTranslator,
HierarchyHasAncestorFilter hierarchyHasAncestorFilter,
Map<Attribute, SqlField> attributeSwapFields) {
super(apiTranslator, attributeSwapFields);
this.hierarchyHasAncestorFilter = hierarchyHasAncestorFilter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import bio.terra.tanagra.query.sql.translator.ApiTranslator;
import bio.terra.tanagra.underlay.entitymodel.Attribute;
import bio.terra.tanagra.underlay.indextable.ITHierarchyChildParent;
import java.util.Map;

public class BQHierarchyHasParentFilterTranslator extends ApiFilterTranslator {
private final HierarchyHasParentFilter hierarchyHasParentFilter;

public BQHierarchyHasParentFilterTranslator(
ApiTranslator apiTranslator, HierarchyHasParentFilter hierarchyHasParentFilter) {
super(apiTranslator);
ApiTranslator apiTranslator,
HierarchyHasParentFilter hierarchyHasParentFilter,
Map<Attribute, SqlField> attributeSwapFields) {
super(apiTranslator, attributeSwapFields);
this.hierarchyHasParentFilter = hierarchyHasParentFilter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import bio.terra.tanagra.query.sql.translator.ApiTranslator;
import bio.terra.tanagra.underlay.entitymodel.Attribute;
import bio.terra.tanagra.underlay.indextable.ITEntityMain;
import java.util.Map;

public class BQHierarchyIsLeafFilterTranslator extends ApiFilterTranslator {
private final HierarchyIsLeafFilter hierarchyIsLeafFilter;

public BQHierarchyIsLeafFilterTranslator(
ApiTranslator apiTranslator, HierarchyIsLeafFilter hierarchyIsLeafFilter) {
super(apiTranslator);
ApiTranslator apiTranslator,
HierarchyIsLeafFilter hierarchyIsLeafFilter,
Map<Attribute, SqlField> attributeSwapFields) {
super(apiTranslator, attributeSwapFields);
this.hierarchyIsLeafFilter = hierarchyIsLeafFilter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import bio.terra.tanagra.query.sql.translator.ApiTranslator;
import bio.terra.tanagra.underlay.entitymodel.Attribute;
import bio.terra.tanagra.underlay.indextable.ITEntityMain;
import java.util.Map;

public class BQHierarchyIsMemberFilterTranslator extends ApiFilterTranslator {
private final HierarchyIsMemberFilter hierarchyIsMemberFilter;

public BQHierarchyIsMemberFilterTranslator(
ApiTranslator apiTranslator, HierarchyIsMemberFilter hierarchyIsMemberFilter) {
super(apiTranslator);
ApiTranslator apiTranslator,
HierarchyIsMemberFilter hierarchyIsMemberFilter,
Map<Attribute, SqlField> attributeSwapFields) {
super(apiTranslator, attributeSwapFields);
this.hierarchyIsMemberFilter = hierarchyIsMemberFilter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import bio.terra.tanagra.query.sql.translator.ApiTranslator;
import bio.terra.tanagra.underlay.entitymodel.Attribute;
import bio.terra.tanagra.underlay.indextable.ITEntityMain;
import java.util.Map;

public class BQHierarchyIsRootFilterTranslator extends ApiFilterTranslator {
private final HierarchyIsRootFilter hierarchyIsRootFilter;

public BQHierarchyIsRootFilterTranslator(
ApiTranslator apiTranslator, HierarchyIsRootFilter hierarchyIsRootFilter) {
super(apiTranslator);
ApiTranslator apiTranslator,
HierarchyIsRootFilter hierarchyIsRootFilter,
Map<Attribute, SqlField> attributeSwapFields) {
super(apiTranslator, attributeSwapFields);
this.hierarchyIsRootFilter = hierarchyIsRootFilter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class BQPrimaryWithCriteriaFilterTranslator extends ApiFilterTranslator {
private final PrimaryWithCriteriaFilter primaryWithCriteriaFilter;

public BQPrimaryWithCriteriaFilterTranslator(
ApiTranslator apiTranslator, PrimaryWithCriteriaFilter primaryWithCriteriaFilter) {
super(apiTranslator);
ApiTranslator apiTranslator,
PrimaryWithCriteriaFilter primaryWithCriteriaFilter,
Map<Attribute, SqlField> attributeSwapFields) {
super(apiTranslator, attributeSwapFields);
this.primaryWithCriteriaFilter = primaryWithCriteriaFilter;
}

Expand Down Expand Up @@ -152,7 +155,9 @@ public String buildSql(SqlParams sqlParams, String tableAlias) {
+ " FROM "
+ occurrenceEntityTable.getTablePointer().render()
+ " WHERE "
+ apiTranslator.translator(allOccurrenceFilters).buildSql(sqlParams, null));
+ apiTranslator
.translator(allOccurrenceFilters, attributeSwapFields)
.buildSql(sqlParams, null));
});

ITEntityMain selectEntityTable =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import bio.terra.tanagra.underlay.indextable.ITRelationshipIdPairs;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -25,8 +26,10 @@ public class BQRelationshipFilterTranslator extends ApiFilterTranslator {
private final RelationshipFilter relationshipFilter;

public BQRelationshipFilterTranslator(
ApiTranslator apiTranslator, RelationshipFilter relationshipFilter) {
super(apiTranslator);
ApiTranslator apiTranslator,
RelationshipFilter relationshipFilter,
Map<Attribute, SqlField> attributeSwapFields) {
super(apiTranslator, attributeSwapFields);
this.relationshipFilter = relationshipFilter;
}

Expand Down Expand Up @@ -62,12 +65,12 @@ private String foreignKeyOnSelectEntity(SqlParams sqlParams, String tableAlias)
return apiTranslator.unaryFilterSql(
foreignKeyField, UnaryOperator.IS_NOT_NULL, null, sqlParams);
} else if (apiTranslator
.translator(relationshipFilter.getSubFilter())
.translator(relationshipFilter.getSubFilter(), attributeSwapFields)
.isFilterOnAttribute(relationshipFilter.getFilterEntity().getIdAttribute())
&& !relationshipFilter.hasGroupByFilter()) {
// subFilter(filterId=>foreignKey)
return apiTranslator
.translator(relationshipFilter.getSubFilter())
.translator(relationshipFilter.getSubFilter(), attributeSwapFields)
.swapAttributeField(
relationshipFilter.getFilterEntity().getIdAttribute(), foreignKeyField)
.buildSql(sqlParams, tableAlias);
Expand All @@ -84,7 +87,7 @@ private String foreignKeyOnSelectEntity(SqlParams sqlParams, String tableAlias)
String inSelectFilterSql =
relationshipFilter.hasSubFilter()
? apiTranslator
.translator(relationshipFilter.getSubFilter())
.translator(relationshipFilter.getSubFilter(), attributeSwapFields)
.buildSql(sqlParams, null)
: null;
LOGGER.debug(
Expand Down Expand Up @@ -150,20 +153,22 @@ private String foreignKeyOnFilterEntity(SqlParams sqlParams, String tableAlias)
selectRollupField, BinaryOperator.GREATER_THAN, Literal.forInt64(0L), null, sqlParams);
} else if (relationshipFilter.hasSubFilter()
&& apiTranslator
.translator(relationshipFilter.getSubFilter())
.translator(relationshipFilter.getSubFilter(), attributeSwapFields)
.isFilterOnAttribute(foreignKeyAttribute)
&& !relationshipFilter.hasGroupByFilter()) {
// subFilter(foreignKey=selectId)
return apiTranslator
.translator(relationshipFilter.getSubFilter())
.translator(relationshipFilter.getSubFilter(), attributeSwapFields)
.swapAttributeField(foreignKeyAttribute, selectIdField)
.buildSql(sqlParams, tableAlias);
} else {
// id IN (SELECT foreignKey FROM filterEntity [WHERE subFilter])
String inSelectFilterSql = null;
if (relationshipFilter.hasSubFilter()) {
inSelectFilterSql =
apiTranslator.translator(relationshipFilter.getSubFilter()).buildSql(sqlParams, null);
apiTranslator
.translator(relationshipFilter.getSubFilter(), attributeSwapFields)
.buildSql(sqlParams, null);
if (inSelectFilterSql.isEmpty()) {
inSelectFilterSql = null;
}
Expand Down Expand Up @@ -276,7 +281,7 @@ private String intermediateTable(SqlParams sqlParams, String tableAlias) {
selectRollupField, BinaryOperator.GREATER_THAN, Literal.forInt64(0L), null, sqlParams);
} else if (relationshipFilter.hasSubFilter()
&& apiTranslator
.translator(relationshipFilter.getSubFilter())
.translator(relationshipFilter.getSubFilter(), attributeSwapFields)
.isFilterOnAttribute(relationshipFilter.getFilterEntity().getIdAttribute())
&& (!relationshipFilter.hasGroupByFilter()
|| !relationshipFilter.hasGroupByCountAttributes())) {
Expand All @@ -286,7 +291,7 @@ private String intermediateTable(SqlParams sqlParams, String tableAlias) {
if (relationshipFilter.hasSubFilter()) {
subFilterSql =
apiTranslator
.translator(relationshipFilter.getSubFilter())
.translator(relationshipFilter.getSubFilter(), attributeSwapFields)
.swapAttributeField(
relationshipFilter.getFilterEntity().getIdAttribute(), filterIdIntTable)
.buildSql(sqlParams, null);
Expand Down Expand Up @@ -326,7 +331,9 @@ private String intermediateTable(SqlParams sqlParams, String tableAlias) {
filterEntityTable.getAttributeValueField(
relationshipFilter.getFilterEntity().getIdAttribute().getName());
String subFilterSql =
apiTranslator.translator(relationshipFilter.getSubFilter()).buildSql(sqlParams, null);
apiTranslator
.translator(relationshipFilter.getSubFilter(), attributeSwapFields)
.buildSql(sqlParams, null);
filterIdInSelectSql =
apiTranslator.inSelectFilterSql(
filterIdIntTable,
Expand Down Expand Up @@ -415,7 +422,7 @@ private String intermediateTable(SqlParams sqlParams, String tableAlias) {
+ (relationshipFilter.hasSubFilter()
? " WHERE "
+ apiTranslator
.translator(relationshipFilter.getSubFilter())
.translator(relationshipFilter.getSubFilter(), attributeSwapFields)
.buildSql(sqlParams, filterTableAlias)
: "")
+ " GROUP BY "
Expand Down
Loading

0 comments on commit 23050e1

Please sign in to comment.