>From Vijay Sarathy <vijay.sara...@couchbase.com>: Vijay Sarathy has uploaded this change for review. ( https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18177 )
Change subject: [ASTERIXDB-3358][COMP] Indexnl hint with index names not working correctly ...................................................................... [ASTERIXDB-3358][COMP] Indexnl hint with index names not working correctly Change-Id: I603ae267efd137d4e9f3491be2a6bdcb1179eeac --- M asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java 1 file changed, 57 insertions(+), 7 deletions(-) git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/77/18177/1 diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java index 894fd93..6a6b11d 100644 --- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java +++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java @@ -73,6 +73,8 @@ import org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities; import org.apache.hyracks.algebricks.core.algebra.typing.ITypingContext; import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule; +import org.apache.hyracks.api.exceptions.IWarningCollector; +import org.apache.hyracks.api.exceptions.Warning; import com.google.common.base.Strings; @@ -367,6 +369,7 @@ Iterator<Map.Entry<Index, List<Pair<Integer, Integer>>>> indexExprAndVarIt = analysisCtx.getIteratorForIndexExprsAndVars(); boolean hasIndexPreferences = false; + Set<String> allIndexNames = null; ArrayList<Integer> matchedExpressions = new ArrayList<>(); while (indexExprAndVarIt.hasNext()) { Map.Entry<Index, List<Pair<Integer, Integer>>> indexExprAndVarEntry = indexExprAndVarIt.next(); @@ -543,13 +546,33 @@ } } analysisCtx.putNumberOfMatchedKeys(index, numMatchedKeys); + if (allIndexNames == null) { + allIndexNames = new HashSet<>(); + } + allIndexNames.add(index.getIndexName()); } if (hasIndexPreferences) { - Collection<Index> preferredSecondaryIndexes = fetchSecondaryIndexPreferences(accessMethod, analysisCtx); - if (preferredSecondaryIndexes != null) { - // if we have preferred indexes then remove all non-preferred indexes - removeNonPreferredSecondaryIndexes(analysisCtx, preferredSecondaryIndexes); + Pair<Set<Index>, Set<String>> preferredSecondaryIndexesAndNames = + fetchSecondaryIndexPreferences(accessMethod, analysisCtx); + + Collection<String> preferredSecondaryIndexNames = preferredSecondaryIndexesAndNames.getSecond(); + if (!allIndexNames.containsAll(preferredSecondaryIndexNames)) { + if (!context.getPhysicalOptimizationConfig().getCBOMode()) { + IWarningCollector warningCollector = context.getWarningCollector(); + if (warningCollector.shouldWarn()) { + warningCollector + .warn(Warning.of(null, org.apache.hyracks.api.exceptions.ErrorCode.INAPPLICABLE_HINT, + "index nested loop join", "ignored")); + } + } + removeAllSecondaryIndexes(analysisCtx); + } else { + Collection<Index> preferredSecondaryIndexes = preferredSecondaryIndexesAndNames.getFirst(); + if (preferredSecondaryIndexes != null) { + // if we have preferred indexes then remove all non-preferred indexes + removeNonPreferredSecondaryIndexes(analysisCtx, preferredSecondaryIndexes); + } } } } @@ -567,8 +590,9 @@ Index.getNonNullableType(type2).first.getTypeTag()); } - private Set<Index> fetchSecondaryIndexPreferences(IAccessMethod accessMethod, + private Pair<Set<Index>, Set<String>> fetchSecondaryIndexPreferences(IAccessMethod accessMethod, AccessMethodAnalysisContext analysisCtx) { + Set<String> preferredIndexNames = null; Set<Index> preferredSecondaryIndexes = null; for (Iterator<Map.Entry<Index, List<Pair<Integer, Integer>>>> indexExprAndVarIt = analysisCtx.getIteratorForIndexExprsAndVars(); indexExprAndVarIt.hasNext();) { @@ -577,7 +601,13 @@ if (index.isSecondaryIndex()) { for (Pair<Integer, Integer> exprVarPair : indexExprAndVarEntry.getValue()) { IOptimizableFuncExpr optFuncExpr = analysisCtx.getMatchedFuncExpr(exprVarPair.first); - Collection<String> preferredIndexNames = accessMethod.getSecondaryIndexPreferences(optFuncExpr); + Collection<String> hintedIndexNames = accessMethod.getSecondaryIndexPreferences(optFuncExpr); + if (hintedIndexNames != null) { + if (preferredIndexNames == null) { + preferredIndexNames = new HashSet<>(); + } + preferredIndexNames.addAll(hintedIndexNames); + } if (preferredIndexNames != null && preferredIndexNames.contains(index.getIndexName())) { if (preferredSecondaryIndexes == null) { preferredSecondaryIndexes = new HashSet<>(); @@ -588,7 +618,7 @@ } } } - return preferredSecondaryIndexes; + return new Pair<>(preferredSecondaryIndexes, preferredIndexNames); } private void removeNonPreferredSecondaryIndexes(AccessMethodAnalysisContext analysisCtx, @@ -603,6 +633,17 @@ } } + private void removeAllSecondaryIndexes(AccessMethodAnalysisContext analysisCtx) { + for (Iterator<Map.Entry<Index, List<Pair<Integer, Integer>>>> indexExprAndVarIt = + analysisCtx.getIteratorForIndexExprsAndVars(); indexExprAndVarIt.hasNext();) { + Map.Entry<Index, List<Pair<Integer, Integer>>> indexExprAndVarEntry = indexExprAndVarIt.next(); + Index index = indexExprAndVarEntry.getKey(); + if (index.isSecondaryIndex()) { + indexExprAndVarIt.remove(); + } + } + } + /** * Analyzes the given selection condition, filling analyzedAMs with * applicable access method types. At this point we are not yet consulting -- To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18177 To unsubscribe, or for help writing mail filters, visit https://asterix-gerrit.ics.uci.edu/settings Gerrit-Project: asterixdb Gerrit-Branch: master Gerrit-Change-Id: I603ae267efd137d4e9f3491be2a6bdcb1179eeac Gerrit-Change-Number: 18177 Gerrit-PatchSet: 1 Gerrit-Owner: Vijay Sarathy <vijay.sara...@couchbase.com> Gerrit-MessageType: newchange