Skip to content

Commit

Permalink
Merge pull request #594 from FgForrest/593-query-targetting-localized…
Browse files Browse the repository at this point in the history
…-attribute-returns-no-result

fix(#593): Query targeting localized attribute returns no result
  • Loading branch information
novoj authored Jun 2, 2024
2 parents 4f890a4 + ad78962 commit c8a9e22
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* | __/\ V /| | || (_| | |_| | |_) |
* \___| \_/ |_|\__\__,_|____/|____/
*
* Copyright (c) 2023
* Copyright (c) 2023-2024
*
* Licensed under the Business Source License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -263,9 +263,9 @@ private static <T extends Index<?>> List<QueryPlanBuilder> createFilterFormula(
indexSelectionResult.targetIndexQueriedByOtherConstraints()
);

final PrefetchFormulaVisitor prefetchFormulaVisitor = createPrefetchFormulaVisitor(targetIndex, queryContext);
final PrefetchFormulaVisitor prefetchFormulaVisitor = createPrefetchFormulaVisitor(targetIndex);
ofNullable(prefetchFormulaVisitor)
.ifPresent(it -> filterByVisitor.registerFormulaPostProcessorIfNotPresent(PrefetchFormulaVisitor.class, () -> it));
.ifPresent(it -> filterByVisitor.registerFormulaPostProcessor(PrefetchFormulaVisitor.class, () -> it));
ofNullable(queryContext.getFilterBy()).ifPresent(filterByVisitor::visit);
// we need the original trees to contain only non-cached forms of formula if debug mode is enabled
if (debugCachedVariantTrees) {
Expand Down Expand Up @@ -409,8 +409,7 @@ private static Sorter replaceSorterWithCachedVariant(
*/
@Nullable
private static PrefetchFormulaVisitor createPrefetchFormulaVisitor(
@Nonnull TargetIndexes<?> targetIndex,
@Nonnull QueryContext queryContext
@Nonnull TargetIndexes<?> targetIndex
) {
if (targetIndex.isGlobalIndex() || targetIndex.isCatalogIndex()) {
return new PrefetchFormulaVisitor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,16 @@ public void visit(@Nonnull Constraint<?> constraint) {
* Registers new {@link FormulaPostProcessor} to the list of processors that will be called just before
* IndexFilterByVisitor hands the result of its work to the calling logic.
*/
public <T extends FormulaPostProcessor> T registerFormulaPostProcessorIfNotPresent(
public <T extends FormulaPostProcessor> T registerFormulaPostProcessor(
@Nonnull Class<T> postProcessorType,
@Nonnull Supplier<T> formulaPostProcessorSupplier
) {
//noinspection DataFlowIssue,unchecked
return (T) this.postProcessors.computeIfAbsent(
final T value = formulaPostProcessorSupplier.get();
this.postProcessors.put(
postProcessorType,
aClass -> formulaPostProcessorSupplier.get()
value
);
return value;
}

/**
Expand Down Expand Up @@ -973,9 +974,9 @@ private void addFormula(@Nonnull Formula formula) {
private Formula constructFinalFormula(@Nonnull Formula constraintFormula) {
Formula finalFormula = constraintFormula;
final CalculationContext calculationContext = new CalculationContext();
if (!postProcessors.isEmpty()) {
if (!this.postProcessors.isEmpty()) {
final Set<FormulaPostProcessor> executedProcessors = CollectionUtils.createHashSet(postProcessors.size());
for (FormulaPostProcessor postProcessor : postProcessors.values()) {
for (FormulaPostProcessor postProcessor : this.postProcessors.values()) {
if (!executedProcessors.contains(postProcessor)) {
postProcessor.visit(finalFormula);
finalFormula = postProcessor.getPostProcessedFormula();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* | __/\ V /| | || (_| | |_| | |_) |
* \___| \_/ |_|\__\__,_|____/|____/
*
* Copyright (c) 2023
* Copyright (c) 2023-2024
*
* Licensed under the Business Source License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -28,6 +28,7 @@
import io.evitadb.core.query.algebra.Formula;
import io.evitadb.core.query.algebra.FormulaPostProcessor;
import io.evitadb.core.query.algebra.attribute.AttributeFormula;
import io.evitadb.core.query.algebra.base.OrFormula;
import io.evitadb.core.query.algebra.infra.SkipFormula;
import io.evitadb.core.query.algebra.locale.LocaleFormula;
import io.evitadb.core.query.algebra.prefetch.EntityFilteringFormula;
Expand All @@ -39,6 +40,7 @@
import io.evitadb.index.ReferencedTypeEntityIndex;

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Locale;

import static java.util.Optional.ofNullable;
Expand All @@ -59,7 +61,7 @@ public Formula translate(@Nonnull EntityLocaleEquals entityLocaleEquals, @Nonnul
.ifPresent(it -> it.setLocale(locale));

if (filterByVisitor.isEntityTypeKnown()) {
filterByVisitor.registerFormulaPostProcessorIfNotPresent(
filterByVisitor.registerFormulaPostProcessor(
LocaleOptimizingPostProcessor.class, LocaleOptimizingPostProcessor::new
);

Expand Down Expand Up @@ -122,10 +124,14 @@ public LocaleOptimizingPostProcessor() {
if (formula instanceof final AttributeFormula attributeFormula) {
clonerInstance.localizedAttributeFormulaFound = clonerInstance.localizedAttributeFormulaFound ||
(attributeFormula.isLocalized() && clonerInstance.conjunctiveScope);
} else if (formula instanceof SelectionFormula selectionFormula && selectionFormula.getDelegate() instanceof LocaleFormula) {
} else if (formula instanceof SelectionFormula selectionFormula &&
(selectionFormula.getDelegate() instanceof LocaleFormula ||
selectionFormula.getDelegate() instanceof OrFormula orFormula &&
Arrays.stream(orFormula.getInnerFormulas()).allMatch(it -> it instanceof LocaleFormula))
) {
// skip this formula
return null;
} else if (formula instanceof LocaleFormula) {
} else if (formula instanceof LocaleFormula && clonerInstance.conjunctiveScope) {
// skip this formula
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* | __/\ V /| | || (_| | |_| | |_) |
* \___| \_/ |_|\__\__,_|____/|____/
*
* Copyright (c) 2023
* Copyright (c) 2023-2024
*
* Licensed under the Business Source License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,7 +52,7 @@ public class EntityPrimaryKeyInSetTranslator implements FilteringConstraintTrans
@Override
public Formula translate(@Nonnull EntityPrimaryKeyInSet entityPrimaryKeyInSet, @Nonnull FilterByVisitor filterByVisitor) {
Assert.notNull(filterByVisitor.getSchema(), "Schema must be known!");
final SuperSetMatchingPostProcessor superSetMatchingPostProcessor = filterByVisitor.registerFormulaPostProcessorIfNotPresent(
final SuperSetMatchingPostProcessor superSetMatchingPostProcessor = filterByVisitor.registerFormulaPostProcessor(
SuperSetMatchingPostProcessor.class,
() -> new SuperSetMatchingPostProcessor(filterByVisitor)
);
Expand Down

0 comments on commit c8a9e22

Please sign in to comment.