diff --git a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/CountStep.java b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/CountStep.java index acf41d1cbd4..08550ffb753 100644 --- a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/CountStep.java +++ b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/CountStep.java @@ -28,6 +28,7 @@ public CountStep(OCommandContext ctx) { OResultSet prevResult = getPrev().get().syncPull(ctx, nRecords); if (!prevResult.hasNext()) { OInternalResultSet result = new OInternalResultSet(); + resultRecord.setProperty("count", count); result.add(resultRecord); return result; } diff --git a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/ODeleteExecutionPlanner.java b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/ODeleteExecutionPlanner.java index d38fe8fb41d..a98a626bb87 100644 --- a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/ODeleteExecutionPlanner.java +++ b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/ODeleteExecutionPlanner.java @@ -65,22 +65,42 @@ private boolean handleIndexAsTarget(ODeleteExecutionPlan result, OIndexIdentifie 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"); } - } else if (flattenedWhereClause.size() > 1) { + } else if (flattenedWhereClause.size() > 2) { 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) { + + whereClause = null;//The WHERE clause won't be used anymore, the index does all the filtering + 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) { + whereClause = null;//The WHERE clause won't be used anymore, the index does all the filtering + flattenedWhereClause = null; + 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); } - - condition = andBlock.getSubBlocks().get(0); } - result.chain(new DeleteFromIndexStep(index, condition, null, ctx)); + result.chain(new DeleteFromIndexStep(index, keyCondition, null, ctx)); + if (ridCondition != null) { + OWhereClause where = new OWhereClause(-1); + where.setBaseExpression(ridCondition); + result.chain(new FilterStep(where, ctx)); + } return true; case VALUES: case VALUESASC: @@ -130,4 +150,31 @@ private void handleTarget(OUpdateExecutionPlan result, OCommandContext ctx, OFro OSelectExecutionPlanner planner = new OSelectExecutionPlanner(sourceStatement); result.chain(new SubQueryStep(planner.createExecutionPlan(ctx), ctx, ctx)); } + + 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; + } + } diff --git a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OSelectExecutionPlanner.java b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OSelectExecutionPlanner.java index 32ec1ef3367..c02eaafefbe 100644 --- a/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OSelectExecutionPlanner.java +++ b/core/src/main/java/com/orientechnologies/orient/core/sql/executor/OSelectExecutionPlanner.java @@ -761,7 +761,7 @@ private void handleIndexAsTarget(OSelectExecutionPlan result, OIndexIdentifier i if (!index.supportsOrderedIterations()) { throw new OCommandExecutionException("Index " + indexName + " does not allow iteration without a condition"); } - } else if (flattenedWhereClause.size() > 1) { + } else if (flattenedWhereClause.size() > 2) { throw new OCommandExecutionException("Index queries with this kind of condition are not supported yet: " + whereClause); } else { OAndBlock andBlock = flattenedWhereClause.get(0);