Skip to content

Commit

Permalink
Fix DELETE statement on subquery with WHERE condition
Browse files Browse the repository at this point in the history
the WHERE condition was ignored

Resolves: #6174
  • Loading branch information
luigidellaquila committed May 23, 2016
1 parent 81da0a8 commit 266ceca
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,24 @@ else if (word.equalsIgnoreCase(KEYWORD_WHERE))
} else if (subjectName.startsWith("(")) {
subjectName = subjectName.trim();
query = database.command(new OSQLAsynchQuery<ODocument>(subjectName.substring(1, subjectName.length() - 1), this));
parserNextWord(true);
if (!parserIsEnded()) {
while (!parserIsEnded()) {
final String word = parserGetLastWord();

if (word.equals(KEYWORD_LOCK))
lockStrategy = parseLock();
else if (word.equals(KEYWORD_RETURN))
returning = parseReturn();
else if (word.equals(KEYWORD_UNSAFE))
unsafe = true;
else if (word.equalsIgnoreCase(KEYWORD_WHERE))
compiledFilter = OSQLEngine.getInstance()
.parseCondition(parserText.substring(parserGetCurrentPosition()), getContext(), KEYWORD_WHERE);

parserNextWord(true);
}
}
} else {
parserNextWord(true);

Expand Down Expand Up @@ -278,6 +295,9 @@ public long getDistributedTimeout() {
public boolean result(final Object iRecord) {
final ORecordAbstract record = ((OIdentifiable) iRecord).getRecord();

if(record instanceof ODocument && compiledFilter!=null && !Boolean.TRUE.equals(this.compiledFilter.evaluate(record, (ODocument)record, getContext()))){
return true;
}
try {
if (record.getIdentity().isValid()) {
if (returning.equalsIgnoreCase("BEFORE"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static org.testng.Assert.fail;

@Test
public class ODeleteStatementTest {
@Test public class ODeleteStatementTest {

protected SimpleNode checkRightSyntax(String query) {
return checkSyntax(query, true);
Expand Down Expand Up @@ -47,15 +49,12 @@ protected SimpleNode checkSyntax(String query, boolean isCorrect) {

public void testDeleteFromIndexBinary() {

ODatabaseDocument database = new ODatabaseDocumentTx("memory:tryMe");
ODatabaseDocument database = new ODatabaseDocumentTx("memory:ODeleteStatementTestDeleteFromIndexBinary");
database.create();

OIndexFactory factory = OIndexes.getFactory("NOTUNIQUE", null);
database
.getMetadata()
.getIndexManager()
.createIndex("byte-array-manualIndex-notunique", "NOTUNIQUE",
new OSimpleKeyIndexDefinition(factory.getLastVersion(), OType.BINARY), null, null, null);
database.getMetadata().getIndexManager().createIndex("byte-array-manualIndex-notunique", "NOTUNIQUE",
new OSimpleKeyIndexDefinition(factory.getLastVersion(), OType.BINARY), null, null, null);

OIndex<?> index = database.getMetadata().getIndexManager().getIndex("byte-array-manualIndex-notunique");

Expand All @@ -78,14 +77,50 @@ public void testDeleteFromIndexBinary() {
index.put(key2, doc4);

Assert.assertTrue(index.remove(key1, doc2));
database.command(new OCommandSQL("delete from index:byte-array-manualIndex-notunique where key = ? and rid = ?")).execute(key1,
doc1);
database.command(new OCommandSQL("delete from index:byte-array-manualIndex-notunique where key = ? and rid = ?"))
.execute(key1, doc1);

// Assert.assertEquals(((Collection<?>) index.get(key1)).size(), 1);
// Assert.assertEquals(((Collection<?>) index.get(key2)).size(), 2);
database.close();
}

public void deleteFromSubqueryWithWhereTest() {

ODatabaseDocument database = new ODatabaseDocumentTx("memory:ODeleteStatementTestFromSubqueryWithWhereTest");
database.create();

try {
database.command(new OCommandSQL("create class Foo")).execute();
database.command(new OCommandSQL("create class Bar")).execute();
final ODocument doc1 = new ODocument("Foo").field("k", "key1");
final ODocument doc2 = new ODocument("Foo").field("k", "key2");
final ODocument doc3 = new ODocument("Foo").field("k", "key3");

doc1.save();
doc2.save();
doc3.save();

List<ODocument> list = new ArrayList<ODocument>();
list.add(doc1);
list.add(doc2);
list.add(doc3);
final ODocument bar = new ODocument("Bar").field("arr", list);
bar.save();

database.command(new OCommandSQL("delete from (select expand(arr) from Bar) where k = 'key2'")).execute();

List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>("select from Foo"));
Assert.assertNotNull(result);
Assert.assertEquals(result.size(), 2);
for (ODocument doc : result) {
Assert.assertNotEquals(doc.field("k"), "key2");
}
} finally {
database.close();
}
}

private void printTree(String s) {
OrientSql osql = getParserFor(s);
try {
Expand Down

0 comments on commit 266ceca

Please sign in to comment.