Fix SELECT FROM INDEX: with key and rid
@@ -755,7 +755,8 @@ public class OSelectExecutionPlanner {
switch (indexIdentifier.getType()) {
case INDEX:
- OBooleanExpression condition = null;
+ OBooleanExpression keyCondition = null;
+ OBooleanExpression ridCondition = null;
if (flattenedWhereClause == null || flattenedWhereClause.size() == 0) {
if (!index.supportsOrderedIterations()) {
throw new OCommandExecutionException("Index " + indexName + " does not allow iteration without a condition");
@@ -764,14 +765,32 @@ public class OSelectExecutionPlanner {
throw new OCommandExecutionException("Index queries with this kind of condition are not supported yet: " + whereClause);
} else {
OAndBlock andBlock = flattenedWhereClause.get(0);
- if (andBlock.getSubBlocks().size() != 1) {
+ if (andBlock.getSubBlocks().size() == 1) {
+
+ this.whereClause = null;//The WHERE clause won't be used anymore, the index does all the filtering
+ this.flattenedWhereClause = null;
+ keyCondition = getKeyCondition(andBlock);
+ if (keyCondition == null) {
throw new OCommandExecutionException("Index queries with this kind of condition are not supported yet: " + whereClause);
}
+ } else if (andBlock.getSubBlocks().size() == 2) {
this.whereClause = null;//The WHERE clause won't be used anymore, the index does all the filtering
this.flattenedWhereClause = null;
- condition = andBlock.getSubBlocks().get(0);
+ keyCondition = getKeyCondition(andBlock);
+ ridCondition = getRidCondition(andBlock);
+ if (keyCondition == null || ridCondition == null) {
+ throw new OCommandExecutionException("Index queries with this kind of condition are not supported yet: " + whereClause);
+ }
+ } else {
+ throw new OCommandExecutionException("Index queries with this kind of condition are not supported yet: " + whereClause);
+ }
+ }
+ result.chain(new FetchFromIndexStep(index, keyCondition, null, ctx));
+ if (ridCondition != null) {
+ OWhereClause where = new OWhereClause(-1);
+ where.setBaseExpression(ridCondition);
+ result.chain(new FilterStep(where, ctx));
}
- result.chain(new FetchFromIndexStep(index, condition, null, ctx));
break;
case VALUES:
case VALUESASC:
@@ -791,6 +810,32 @@ public class OSelectExecutionPlanner {
}
}
+ private OBooleanExpression getKeyCondition(OAndBlock andBlock) {
+ for (OBooleanExpression exp : andBlock.getSubBlocks()) {
+ String str = exp.toString();
+ if (str.length() < 5) {
+ continue;
+ }
+ if (str.substring(0, 4).equalsIgnoreCase("key ")) {
+ return exp;
+ }
+ }
+ return null;
+ }
+
+ private OBooleanExpression getRidCondition(OAndBlock andBlock) {
+ for (OBooleanExpression exp : andBlock.getSubBlocks()) {
+ String str = exp.toString();
+ if (str.length() < 5) {
+ continue;
+ }
+ if (str.substring(0, 4).equalsIgnoreCase("rid ")) {
+ return exp;
+ }
+ }
+ return null;
+ }
+
private void handleMetadataAsTarget(OSelectExecutionPlan plan, OMetadataIdentifier metadata, OCommandContext ctx) {
ODatabaseInternal db = (ODatabaseInternal) ctx.getDatabase();
String schemaRecordIdAsString = null;
