Skip to content

Commit

Permalink
fix: scopes are not passed to nested queries
Browse files Browse the repository at this point in the history
  • Loading branch information
novoj committed Dec 18, 2024
1 parent cb4ad4e commit e4a0119
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,46 +80,46 @@ public class EvitaRequest {
private final String entityType;
private final Locale implicitLocale;
@Getter private final Class<?> expectedType;
private int[] primaryKeys;
@Nullable private int[] primaryKeys;
private boolean localeExamined;
private Locale locale;
private Boolean requiredLocales;
private Set<Locale> requiredLocaleSet;
@Nullable private Locale locale;
@Nullable private Boolean requiredLocales;
@Nullable private Set<Locale> requiredLocaleSet;
private QueryPriceMode queryPriceMode;
private Boolean priceValidInTimeSet;
private OffsetDateTime priceValidInTime;
@Nullable private OffsetDateTime priceValidInTime;
private Boolean requiresEntity;
private Boolean requiresParent;
private HierarchyContent parentContent;
private EntityFetch entityRequirement;
private Boolean entityAttributes;
private Set<String> entityAttributeSet;
private Boolean entityAssociatedData;
private Set<String> entityAssociatedDataSet;
private Boolean entityReference;
private PriceContentMode entityPrices;
@Nullable private Boolean requiresParent;
@Nullable private HierarchyContent parentContent;
@Nullable private EntityFetch entityRequirement;
@Nullable private Boolean entityAttributes;
@Nullable private Set<String> entityAttributeSet;
@Nullable private Boolean entityAssociatedData;
@Nullable private Set<String> entityAssociatedDataSet;
@Nullable private Boolean entityReference;
@Nullable private PriceContentMode entityPrices;
private Boolean currencySet;
private Currency currency;
@Nullable private Currency currency;
private Boolean requiresPriceLists;
private String[] priceLists;
private String[] additionalPriceLists;
private Integer start;
private ConditionalGap[] conditionalGaps;
private Map<String, HierarchyFilterConstraint> hierarchyWithin;
private Boolean requiredWithinHierarchy;
private Boolean requiresHierarchyStatistics;
private Boolean requiresHierarchyParents;
private Integer limit;
private EvitaRequest.ResultForm resultForm;
private Map<String, FacetFilterBy> facetGroupConjunction;
private Map<String, FacetFilterBy> facetGroupDisjunction;
private Map<String, FacetFilterBy> facetGroupNegation;
@Nullable private Integer start;
@Nullable private ConditionalGap[] conditionalGaps;
@Nullable private Map<String, HierarchyFilterConstraint> hierarchyWithin;
@Nullable private Boolean requiredWithinHierarchy;
@Nullable private Boolean requiresHierarchyStatistics;
@Nullable private Boolean requiresHierarchyParents;
@Nullable private Integer limit;
@Nullable private EvitaRequest.ResultForm resultForm;
@Nullable private Map<String, FacetFilterBy> facetGroupConjunction;
@Nullable private Map<String, FacetFilterBy> facetGroupDisjunction;
@Nullable private Map<String, FacetFilterBy> facetGroupNegation;
private Boolean queryTelemetryRequested;
private EnumSet<DebugMode> debugModes;
@Nullable private EnumSet<DebugMode> debugModes;
private Scope[] scopesAsArray;
private Set<Scope> scopes;
private Map<String, RequirementContext> entityFetchRequirements;
private RequirementContext defaultReferenceRequirement;
@Nullable private Set<Scope> scopes;
@Nullable private Map<String, RequirementContext> entityFetchRequirements;
@Nullable private RequirementContext defaultReferenceRequirement;

/**
* Parses the requirement context from the passed {@link ReferenceContent} and {@link AttributeContent}.
Expand Down Expand Up @@ -269,9 +269,10 @@ public EvitaRequest(
public EvitaRequest(
@Nonnull EvitaRequest evitaRequest,
@Nonnull String entityType,
@Nonnull FilterBy filterBy,
@Nullable FilterBy filterBy,
@Nullable OrderBy orderBy,
@Nullable Locale locale
@Nullable Locale locale,
@Nullable Set<Scope> scopes
) {

this.requiresEntity = true;
Expand Down Expand Up @@ -877,20 +878,20 @@ public Map<String, RequirementContext> getReferenceEntityFetch() {
*/
@Nullable
public HierarchyFilterConstraint getHierarchyWithin(@Nullable String referenceName) {
if (requiredWithinHierarchy == null) {
if (query.getFilterBy() == null) {
hierarchyWithin = Collections.emptyMap();
if (this.requiredWithinHierarchy == null) {
if (this.query.getFilterBy() == null) {
this.hierarchyWithin = Collections.emptyMap();
} else {
hierarchyWithin = new HashMap<>();
this.hierarchyWithin = new HashMap<>();
QueryUtils.findConstraints(
query.getFilterBy(),
HierarchyFilterConstraint.class
)
.forEach(it -> hierarchyWithin.put(it.getReferenceName().orElse(null), it));
.forEach(it -> this.hierarchyWithin.put(it.getReferenceName().orElse(null), it));
}
requiredWithinHierarchy = true;
this.requiredWithinHierarchy = true;
}
return hierarchyWithin.get(referenceName);
return this.hierarchyWithin == null ? null : this.hierarchyWithin.get(referenceName);
}

/**
Expand All @@ -916,11 +917,12 @@ public EvitaRequest deriveCopyWith(
@Nonnull String entityType,
@Nullable FilterBy filterConstraint,
@Nullable OrderBy orderConstraint,
@Nullable Locale locale
@Nullable Locale locale,
@Nonnull Set<Scope> scopes
) {
return new EvitaRequest(
this,
entityType, filterConstraint, orderConstraint, locale
entityType, filterConstraint, orderConstraint, locale, scopes
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,11 @@ private static void initializeComparatorFromGlobalIndex(
final OrderBy orderBy = new OrderBy(entityOrderBy.getChildren());
final QueryPlanningContext nestedQueryContext = targetEntityCollection.createQueryContext(
evitaRequest.deriveCopyWith(
targetEntityCollection.getEntityType(), null, orderBy,
entityNestedQueryComparator.getLocale()
targetEntityCollection.getEntityType(),
null,
orderBy,
entityNestedQueryComparator.getLocale(),
evitaRequest.getScopes()
),
evitaSession
);
Expand All @@ -718,7 +721,13 @@ private static void initializeComparatorFromGlobalIndex(

final OrderBy orderBy = new OrderBy(entityGroupOrderBy.getChildren());
final QueryPlanningContext nestedQueryContext = targetEntityGroupCollection.createQueryContext(
evitaRequest.deriveCopyWith(targetEntityGroupCollection.getEntityType(), null, orderBy, entityNestedQueryComparator.getLocale()),
evitaRequest.deriveCopyWith(
targetEntityGroupCollection.getEntityType(),
null,
orderBy,
entityNestedQueryComparator.getLocale(),
evitaRequest.getScopes()
),
evitaSession
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ public NestedContextSorter createSorter(
QueryPhase.PLANNING_SORT,
stepDescriptionSupplier
);
final Set<Scope> scopes = getProcessingScope().getScopes();
// we have to create and trap the nested query context here to carry it along with the sorter
// otherwise the sorter will target and use the incorrectly originally queried (prefetched) entities
final QueryPlanningContext nestedQueryContext = entityCollection.createQueryContext(
Expand All @@ -405,12 +406,12 @@ public NestedContextSorter createSorter(
entityType,
null,
new OrderBy(orderBy.getChildren()),
this.queryContext.getLocale()
this.queryContext.getLocale(),
scopes
),
this.queryContext.getEvitaSession()
);

final Set<Scope> scopes = getProcessingScope().getScopes();
final GlobalEntityIndex[] entityIndexes = scopes.stream()
.map(scope -> entityCollection.getIndexByKeyIfExists(new EntityIndexKey(EntityIndexType.GLOBAL, scope)))
.map(GlobalEntityIndex.class::cast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
Expand Down Expand Up @@ -130,7 +131,8 @@ private static List<GlobalIndexAndFormula> planNestedQuery(
.orElse(null),
ofNullable(entityNestedQueryComparator)
.map(EntityNestedQueryComparator::getLocale)
.orElse(null)
.orElse(null),
EnumSet.of(globalIndex.getIndexKey().scope())
),
filterByVisitor.getEvitaSession()
);
Expand Down

0 comments on commit e4a0119

Please sign in to comment.