Skip to content

Commit

Permalink
refactor: moved some logic of index mode selection to query steps
Browse files Browse the repository at this point in the history
  • Loading branch information
tglman committed Aug 12, 2024
1 parent 5fd3bc5 commit 4ff85d6
Show file tree
Hide file tree
Showing 26 changed files with 432 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import com.orientechnologies.orient.core.sql.parser.OBinaryCondition;
import com.orientechnologies.orient.core.sql.parser.OBooleanExpression;
import com.orientechnologies.orient.core.sql.parser.OCollection;
import com.orientechnologies.orient.core.sql.parser.OContainsAnyCondition;
import com.orientechnologies.orient.core.sql.parser.OContainsKeyOperator;
import com.orientechnologies.orient.core.sql.parser.OContainsTextCondition;
import com.orientechnologies.orient.core.sql.parser.OContainsValueCondition;
import com.orientechnologies.orient.core.sql.parser.OContainsValueOperator;
import com.orientechnologies.orient.core.sql.parser.OEqualsCompareOperator;
Expand Down Expand Up @@ -149,7 +147,7 @@ private void updateIndexStats() {
OBooleanExpression lastOp = andBlock.getSubBlocks().get(andBlock.getSubBlocks().size() - 1);
if (lastOp instanceof OBinaryCondition) {
OBinaryCompareOperator op = ((OBinaryCondition) lastOp).getOperator();
range = op.isRangeOperator();
range = op.isRange();
}
} else if (condition instanceof OInCondition) {
size = 1;
Expand Down Expand Up @@ -212,14 +210,12 @@ private static List<OIndexStream> processInCondition(
}
}

OIndexStream localCursor =
createCursor(index, equals, definition, item, ctx, orderAsc, condition);
OIndexStream localCursor = createCursor(index, equals, item, ctx, orderAsc, condition);

acquiredStreams.add(localCursor);
}
} else {
OIndexStream stream =
createCursor(index, equals, definition, rightValue, ctx, orderAsc, condition);
OIndexStream stream = createCursor(index, equals, rightValue, ctx, orderAsc, condition);
acquiredStreams.add(stream);
}
return acquiredStreams;
Expand Down Expand Up @@ -315,66 +311,73 @@ private static List<OIndexStream> multipleRange(
item -> {
Object itemVal =
convertToIndexDefinitionTypes(condition, item, indexDef.getTypes());
if (index.supportsOrderedIterations()) {

Object from = toBetweenIndexKey(indexDef, itemVal);
Object to = toBetweenIndexKey(indexDef, itemVal);
if (from == null && to == null) {
// manage null value explicitly, as the index API does not seem to work
// correctly in this
// case
if (!index.getDefinition().isNullValuesIgnored()) {
acquiredStreams.add(new ONullIndexStream(index));
}
} else {
acquiredStreams.add(
new OBetweenIndexStream(
index, from, fromKeyIncluded, to, toKeyIncluded, isOrderAsc));
}

} else if (additionalRangeCondition == null
&& allEqualities((OAndBlock) condition)) {
acquiredStreams.add(
new OExactIndexStream(index, toIndexKey(indexDef, itemVal), isOrderAsc));
} else if (isFullTextIndex(index)) {
acquiredStreams.add(
new OExactIndexStream(index, toIndexKey(indexDef, itemVal), isOrderAsc));
} else {
throw new UnsupportedOperationException(
"Cannot evaluate " + condition + " on index " + index);
}
rangeIndexOps(
index,
fromKeyIncluded,
toKeyIncluded,
condition,
isOrderAsc,
additionalRangeCondition,
acquiredStreams,
indexDef,
itemVal,
itemVal);
});
}

// some problems in key conversion, so the params do not match the key types
continue;
}
if (index.supportsOrderedIterations()) {

Object from = toBetweenIndexKey(indexDef, secondValue);
Object to = toBetweenIndexKey(indexDef, thirdValue);
rangeIndexOps(
index,
fromKeyIncluded,
toKeyIncluded,
condition,
isOrderAsc,
additionalRangeCondition,
acquiredStreams,
indexDef,
secondValue,
thirdValue);
}
return acquiredStreams;
}

if (from == null && to == null) {
if (!index.getDefinition().isNullValuesIgnored()) {
acquiredStreams.add(new ONullIndexStream(index));
}
} else {
acquiredStreams.add(
new OBetweenIndexStream(index, from, fromKeyIncluded, to, toKeyIncluded, isOrderAsc));
protected static void rangeIndexOps(
OIndexInternal index,
boolean fromKeyIncluded,
boolean toKeyIncluded,
OBooleanExpression condition,
boolean isOrderAsc,
OBinaryCondition additionalRangeCondition,
List<OIndexStream> acquiredStreams,
OIndexDefinition indexDef,
Object fromVal,
Object toVal) {
if (index.supportsOrderedIterations()) {

Object from = toBetweenIndexKey(indexDef, fromVal);
Object to = toBetweenIndexKey(indexDef, toVal);
if (from == null && to == null) {
// manage null value explicitly, as the index API does not seem to work
// correctly in this
// case
if (!index.getDefinition().isNullValuesIgnored()) {
acquiredStreams.add(new ONullIndexStream(index));
}

} else if (additionalRangeCondition == null && allEqualities((OAndBlock) condition)) {
acquiredStreams.add(
new OExactIndexStream(index, toIndexKey(indexDef, secondValue), isOrderAsc));
} else if (isFullTextIndex(index)) {
acquiredStreams.add(
new OExactIndexStream(index, toIndexKey(indexDef, secondValue), isOrderAsc));
} else {
throw new UnsupportedOperationException(
"Cannot evaluate " + condition + " on index " + index);
acquiredStreams.add(
new OBetweenIndexStream(index, from, fromKeyIncluded, to, toKeyIncluded, isOrderAsc));
}

} else if (additionalRangeCondition == null && allEqualities((OAndBlock) condition)) {
acquiredStreams.add(new OExactIndexStream(index, toIndexKey(indexDef, fromVal), isOrderAsc));
} else if (isFullTextIndex(index)) {
acquiredStreams.add(new OExactIndexStream(index, toIndexKey(indexDef, fromVal), isOrderAsc));
} else {
throw new UnsupportedOperationException(
"Cannot evaluate " + condition + " on index " + index);
}
return acquiredStreams;
}

private static boolean isFullTextIndex(OIndex index) {
Expand Down Expand Up @@ -543,16 +546,14 @@ private static List<OIndexStream> processBinaryCondition(
OBooleanExpression condition = desc.getKeyCondition();

List<OIndexStream> acquiredStreams = new ArrayList<>();
OIndexDefinition definition = index.getDefinition();
OBinaryCompareOperator operator = ((OBinaryCondition) condition).getOperator();
OExpression left = ((OBinaryCondition) condition).getLeft();
if (!left.toString().equalsIgnoreCase("key")) {
throw new OCommandExecutionException(
"search for index for " + condition + " is not supported yet");
}
Object rightValue = ((OBinaryCondition) condition).getRight().execute((OResult) null, ctx);
OIndexStream stream =
createCursor(index, operator, definition, rightValue, ctx, isOrderAsc, condition);
OIndexStream stream = createCursor(index, operator, rightValue, ctx, isOrderAsc, condition);
acquiredStreams.add(stream);
return acquiredStreams;
}
Expand Down Expand Up @@ -593,15 +594,14 @@ private static Object toBetweenIndexKey(OIndexDefinition definition, Object righ
private static OIndexStream createCursor(
OIndexInternal index,
OBinaryCompareOperator operator,
OIndexDefinition definition,
Object value,
OCommandContext ctx,
boolean orderAsc,
OBooleanExpression condition) {
if (operator instanceof OEqualsCompareOperator
|| operator instanceof OContainsKeyOperator
|| operator instanceof OContainsValueOperator) {
return new OExactIndexStream(index, toIndexKey(definition, value), orderAsc);
return new OExactIndexStream(index, toIndexKey(index.getDefinition(), value), orderAsc);
} else if (operator instanceof OGeOperator) {
return new OMajorIndexStream(index, value, true, orderAsc);
} else if (operator instanceof OGtOperator) {
Expand Down Expand Up @@ -645,84 +645,13 @@ private static OCollection indexKeyTo(OAndBlock keyCondition, OBinaryCondition a
private static boolean indexKeyFromIncluded(OAndBlock keyCondition, OBinaryCondition additional) {
OBooleanExpression exp =
keyCondition.getSubBlocks().get(keyCondition.getSubBlocks().size() - 1);
OBinaryCompareOperator additionalOperator =
Optional.ofNullable(additional).map(OBinaryCondition::getOperator).orElse(null);
if (exp instanceof OBinaryCondition) {
OBinaryCompareOperator operator = ((OBinaryCondition) exp).getOperator();
if (isGreaterOperator(operator)) {
return isIncludeOperator(operator);
} else
return additionalOperator == null
|| (isIncludeOperator(additionalOperator) && isGreaterOperator(additionalOperator));
} else if (exp instanceof OInCondition || exp instanceof OContainsAnyCondition) {
return additional == null
|| (isIncludeOperator(additionalOperator) && isGreaterOperator(additionalOperator));
} else if (exp instanceof OContainsTextCondition) {
return true;
} else if (exp instanceof OContainsValueCondition) {
OBinaryCompareOperator operator = ((OContainsValueCondition) exp).getOperator();
if (isGreaterOperator(operator)) {
return isIncludeOperator(operator);
} else
return additionalOperator == null
|| (isIncludeOperator(additionalOperator) && isGreaterOperator(additionalOperator));
} else if (exp instanceof OBetweenCondition) {
return true;
} else {
throw new UnsupportedOperationException("Cannot execute index query with " + exp);
}
}

private static boolean isGreaterOperator(OBinaryCompareOperator operator) {
if (operator == null) {
return false;
}
return operator instanceof OGeOperator || operator instanceof OGtOperator;
}

private static boolean isLessOperator(OBinaryCompareOperator operator) {
if (operator == null) {
return false;
}
return operator instanceof OLeOperator || operator instanceof OLtOperator;
}

private static boolean isIncludeOperator(OBinaryCompareOperator operator) {
if (operator == null) {
return false;
}
return operator instanceof OGeOperator || operator instanceof OLeOperator;
return exp.isKeyFromIncluded(additional);
}

private static boolean indexKeyToIncluded(OAndBlock keyCondition, OBinaryCondition additional) {
OBooleanExpression exp =
keyCondition.getSubBlocks().get(keyCondition.getSubBlocks().size() - 1);
OBinaryCompareOperator additionalOperator =
Optional.ofNullable(additional).map(OBinaryCondition::getOperator).orElse(null);
if (exp instanceof OBinaryCondition) {
OBinaryCompareOperator operator = ((OBinaryCondition) exp).getOperator();
if (isLessOperator(operator)) {
return isIncludeOperator(operator);
} else
return additionalOperator == null
|| (isIncludeOperator(additionalOperator) && isLessOperator(additionalOperator));
} else if (exp instanceof OInCondition || exp instanceof OContainsAnyCondition) {
return additionalOperator == null
|| (isIncludeOperator(additionalOperator) && isLessOperator(additionalOperator));
} else if (exp instanceof OContainsTextCondition) {
return true;
} else if (exp instanceof OContainsValueCondition) {
OBinaryCompareOperator operator = ((OContainsValueCondition) exp).getOperator();
if (isLessOperator(operator)) {
return isIncludeOperator(operator);
} else
return additionalOperator == null
|| (isIncludeOperator(additionalOperator) && isLessOperator(additionalOperator));
} else if (exp instanceof OBetweenCondition) {
return true;
} else {
throw new UnsupportedOperationException("Cannot execute index query with " + exp);
}
return exp.isKeyToIncluded(additional);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int cost(OCommandContext ctx) {
OBooleanExpression lastOp = getSubBlocks().get(getSubBlocks().size() - 1);
if (lastOp instanceof OBinaryCondition) {
OBinaryCompareOperator op = ((OBinaryCondition) lastOp).getOperator();
range = op.isRangeOperator();
range = op.isRange();
}

long val =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@ private boolean isRidRange(OBooleanExpression booleanExpression, OCommandContext
if (booleanExpression instanceof OBinaryCondition) {
OBinaryCondition cond = ((OBinaryCondition) booleanExpression);
OBinaryCompareOperator operator = cond.getOperator();
if (operator.isRangeOperator() && cond.getLeft().toString().equalsIgnoreCase("@rid")) {
if (operator.isRange() && cond.getLeft().toString().equalsIgnoreCase("@rid")) {
Object obj;
if (cond.getRight().getRid() != null) {
obj = cond.getRight().getRid().toRecordId((OResult) null, ctx);
Expand Down Expand Up @@ -2772,7 +2772,7 @@ private IndexSearchDescriptor buildIndexSearchDescriptor(
blockIterator.remove();
if (singleExp instanceof OBinaryCondition
&& info.allowsRange()
&& ((OBinaryCondition) singleExp).getOperator().isRangeOperator()) {
&& ((OBinaryCondition) singleExp).getOperator().isRange()) {
// look for the opposite condition, on the same field, for range queries (the other
// side of the range)
while (blockIterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,15 @@ public boolean isCacheable() {
}
return true;
}

@Override
public boolean isKeyFromIncluded(OBinaryCondition additional) {
return true;
}

@Override
public boolean isKeyToIncluded(OBinaryCondition additional) {
return true;
}
}
/* JavaCC - OriginalChecksum=f94f4779c4a6c6d09539446045ceca89 (do not edit this line) */
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@ public interface OBinaryCompareOperator {

OBinaryCompareOperator copy();

default boolean isRangeOperator() {
default boolean isRange() {
return false;
}

public OIndexFinder.Operation getOperation();

public boolean isInclude();

public boolean isLess();

public boolean isGreater();

public default boolean isGreaterInclude() {
return isGreater() && isInclude();
}

public default boolean isLessInclude() {
return isLess() && isInclude();
}
}
Loading

0 comments on commit 4ff85d6

Please sign in to comment.