Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contains Clause with Parameter in Map Fails to Filter Results #10303

Open
mikhalov opened this issue Sep 12, 2024 · 1 comment
Open

contains Clause with Parameter in Map Fails to Filter Results #10303

mikhalov opened this issue Sep 12, 2024 · 1 comment
Labels
Milestone

Comments

@mikhalov
Copy link

mikhalov commented Sep 12, 2024

OrientDB Version: 3.2.33
Java Version: 21
OS: Windows

I encountered an issue in OrientDB when using the contains clause with a parameter passed through a map. The filtering fails and returns no results, even though it works correctly when the same parameter is passed in other formats, such as an object array or formatted string.

Tests to reproduce:

import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.OrientDBConfig;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.record.OEdge;
import com.orientechnologies.orient.core.record.OVertex;
import com.orientechnologies.orient.core.sql.executor.OResult;

import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class OrientDBContainsTest {

    private ODatabaseSession db;

    private OVertex relatedEntity2;

    private OVertex entity1;

    private OrientDB orientDB;

    @BeforeEach
    public void setup() {
        orientDB = new OrientDB("remote:localhost", OrientDBConfig.defaultConfig());
        db = orientDB.open("test", "root", "root");
        OSchema schema = db.getMetadata().getSchema();
        if (!schema.existsClass("Entity")) {
            db.command("CREATE CLASS Entity EXTENDS V");
        }
        if (!schema.existsClass("RelatedEntity")) {
            db.command("CREATE CLASS RelatedEntity EXTENDS V");
        }
        if (!schema.existsClass("ConnectorEdge")) {
            db.command("CREATE CLASS ConnectorEdge EXTENDS E");
        }

        entity1 = db.newVertex("Entity");
        entity1.setProperty("name", "Entity1");
        db.save(entity1);

        OVertex entity2 = db.newVertex("Entity");
        entity2.setProperty("name", "Entity2");
        db.save(entity2);

        OVertex relatedEntity1 = db.newVertex("RelatedEntity");
        relatedEntity1.setProperty("name", "RelatedEntity1");
        db.save(relatedEntity1);

        relatedEntity2 = db.newVertex("RelatedEntity");
        relatedEntity2.setProperty("name", "RelatedEntity2");
        db.save(relatedEntity2);

        OVertex relatedEntity3 = db.newVertex("RelatedEntity");
        relatedEntity3.setProperty("name", "RelatedEntity3");
        db.save(relatedEntity3);

        final OEdge connectorEdge11 = entity1.addEdge(relatedEntity1, "ConnectorEdge");
        final OEdge connectorEdge12 = entity1.addEdge(relatedEntity2, "ConnectorEdge");
        final OEdge connectorEdge21 = entity2.addEdge(relatedEntity1, "ConnectorEdge");
        final OEdge connectorEdge23 = entity2.addEdge(relatedEntity3, "ConnectorEdge");
        db.save(connectorEdge11);
        db.save(connectorEdge12);
        db.save(connectorEdge21);
        db.save(connectorEdge23);
    }

    @Test
    void testContainsParamFormatted() {
        String query = """
                SELECT * as result FROM Entity
                WHERE out('ConnectorEdge') contains %s"""
                .formatted(relatedEntity2.getIdentity());
        try (var rs = db.query(query).stream()) {
            final List<OResult> results = rs.toList();
            assertEquals(1, results.size());
            final ORID resultId = results.getFirst().getIdentity().orElseThrow();
            assertEquals(entity1.getIdentity(), resultId);
        }
    }

    @Test
    void testContainsParamInMap() {
        String query = """
                SELECT * as result FROM Entity
                WHERE out('ConnectorEdge') contains :relatedEntity2
                """;
        try (var rs = db.query(query, Map.of("relatedEntity2", relatedEntity2.getIdentity())).stream()) {
            final List<OResult> results = rs.toList();
            assertEquals(1, results.size());
            final ORID resultId = results.getFirst().getIdentity().orElseThrow();
            assertEquals(entity1.getIdentity(), resultId);
        }
    }

    @Test
    void testContainsParamInObjectArray() {
        String query = """
                SELECT * as result FROM Entity
                WHERE out('ConnectorEdge') contains :relatedEntity2
                """;
        try (var rs = db.query(query, relatedEntity2.getIdentity()).stream()) {
            final List<OResult> results = rs.toList();
            assertEquals(1, results.size());
            final ORID resultId = results.getFirst().getIdentity().orElseThrow();
            assertEquals(entity1.getIdentity(), resultId);
        }
    }

    @AfterEach
    public void tearDown() {
        db.command("DELETE VERTEX Entity");
        db.command("DELETE VERTEX RelatedEntity");
        db.command("DELETE EDGE ConnectorEdge");

        db.close();
        orientDB.close();
    }
}

This issue occurs only when connected to a remote OrientDB database. If the database is set up as in-memory using the following configuration, all tests pass successfully:

var builder = OrientDBConfig.builder()
                .addConfig(OGlobalConfiguration.CREATE_DEFAULT_USERS, true);
orientDB = new OrientDB("memory:",
        OrientBaseGraph.ADMIN,
        OrientBaseGraph.ADMIN,
        builder.build());
orientDB.create("test", ODatabaseType.MEMORY);
db = orientDB.open("test", "admin", "admin");
@tglman tglman added the bug label Sep 16, 2024
@tglman tglman added this to the 3.2.x milestone Sep 16, 2024
@mikhalov
Copy link
Author

Found another approach and it works well, Is this the correct approach that should be used?

    @Test
    void testContainsParamInMapInBrackets() {
        String query = """
                SELECT * as result FROM Entity
                WHERE out('ConnectorEdge') contains [:relatedEntity2]
                """;
        try (var rs = db.query(query, Map.of("relatedEntity2", relatedEntity2.getIdentity())).stream()) {
            final List<OResult> results = rs.toList();
            assertEquals(1, results.size());
            final ORID resultId = results.getFirst().getIdentity().orElseThrow();
            assertEquals(entity1.getIdentity(), resultId);
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants