pietermartin / sqlg

TinkerPop graph over sql
MIT License
243 stars 51 forks source link

User supplied id doesn't work #378

Closed Rucchao closed 3 years ago

Rucchao commented 4 years ago

I have inserted the records with supplied id to the public test table, and I can see the results after I inserted records. However, when I try to query the data again, it returns empty results. I am using sqlg v2.0.0. Would you please help me with this? Thanks!

Assume the record with uid "100" has been inserted, then, when I try the following query, it returns an empty result, I can clearly see the record in the database.

Traversal<Vertex, Vertex> traversal = graph.traversal(). V(RecordId.from(SchemaTable.of("public", "Test"), ListOrderedSet.listOrderedSet(Collections.singletonList("100")))); Vertex v = traversal.next(); System.out.println(v.id());

Rucchao commented 4 years ago

Exception in thread "main" java.lang.NullPointerException at org.umlg.sqlg.sql.dialect.PostgresDialect.propertyTypeToSqlDefinition(PostgresDialect.java:2011) at org.umlg.sqlg.structure.SqlgElement.writeColumnNames(SqlgElement.java:446) at org.umlg.sqlg.structure.SqlgVertex.internalAddVertex(SqlgVertex.java:394) at org.umlg.sqlg.structure.SqlgVertex.insertVertex(SqlgVertex.java:341) at org.umlg.sqlg.structure.SqlgVertex.(SqlgVertex.java:47) at org.umlg.sqlg.structure.SqlgGraph.addVertex(SqlgGraph.java:391) at SQLG.testIdQuery(SQLG.java:363) at SQLG.main(SQLG.java:482)

Rucchao commented 4 years ago

OK, seems like it is because the property types between varchar and string do not match, after I change the type to string, it works. Thanks!

Rucchao commented 4 years ago

Hi martin,

With user-supplied id for the data, I can traverse the data with one edge step, either GraphTraversal<Vertex, Vertex> traversal = graph.traversal().V().hasLabel("person").has("gender", "male").out("knows") or GraphTraversal<Vertex, Vertex> traversal = graph.traversal().V().hasLabel("person").has("gender", "male").out("isLocatedIn") works, but when trying with two steps, I get the errors as follows:

GraphTraversal<Vertex, Vertex> traversal = graph.traversal().V().hasLabel("person").has("gender", "male").out("knows").out("isLocatedIn"); System.out.println(traversal.hasNext());

Exception in thread "main" java.lang.NullPointerException at org.umlg.sqlg.sql.dialect.SqlDialect.maybeWrapInQoutes(SqlDialect.java:82) at org.umlg.sqlg.sql.parse.SchemaTableTree.constructSectionedJoin(SchemaTableTree.java:723) ... at SQLG.main(SQLG.java:466)

pietermartin commented 4 years ago

Hmm, that seems odd as its rather trivial gremlin. Can you post a full example duplicating the error then I'll look.

Rucchao commented 4 years ago

@pietermartin , sure, please see the followings:

// schema definition and sample data importing Schema aSchema = graph.getTopology().getPublicSchema(); aSchema.ensureVertexLabelExist("person", new HashMap<String,PropertyType>(){{ put("personid", PropertyType.STRING); }}, ListOrderedSet.listOrderedSet(Collections.singletonList("personid"))); graph.tx().commit(); aSchema.ensureVertexLabelExist("place", new HashMap<String, PropertyType>(){{ put("placeid", PropertyType.STRING); }}, ListOrderedSet.listOrderedSet(Collections.singletonList("placeid"))); graph.tx().commit(); Vertex v1 = graph.addVertex(T.label, "person", "personid", "1"); Vertex v2 = graph.addVertex(T.label, "person", "personid", "2"); Vertex v3 = graph.addVertex(T.label, "place", "placeid", "3"); v1.addEdge("knows", v2); v1.addEdge("isLocatedIn", v3); v2.addEdge("isLocatedIn", v3); graph.tx().commit();

// two-hop traversal GraphTraversal<Vertex, Vertex> traversal =graph.traversal().V().out("knows").out("isLocatedIn"); System.out.println(traversal.next());

Rucchao commented 4 years ago

I am using the postgresql 11.4 for sqlg 2.0.1, thanks!

pietermartin commented 4 years ago
    @Test
    public void testBug378() {
        Schema aSchema = this.sqlgGraph.getTopology().getPublicSchema();
        aSchema.ensureVertexLabelExist(
                "person",
                new HashMap<String, PropertyType>() {{
                    put("personid", PropertyType.STRING);
                }},
                ListOrderedSet.listOrderedSet(Collections.singletonList("personid"))
        );
        this.sqlgGraph.tx().commit();

        aSchema.ensureVertexLabelExist(
                "place",
                new HashMap<String, PropertyType>() {{
                    put("placeid", PropertyType.STRING);
                }},
                ListOrderedSet.listOrderedSet(Collections.singletonList("placeid"))
        );
        this.sqlgGraph.tx().commit();

        Vertex person1 = this.sqlgGraph.addVertex(T.label, "person", "personid", "1");
        Vertex person2 = this.sqlgGraph.addVertex(T.label, "person", "personid", "2");
        Vertex place1 = this.sqlgGraph.addVertex(T.label, "place", "placeid", "3");
        person1.addEdge("knows", person2);
        person1.addEdge("isLocatedIn", place1);
        person2.addEdge("isLocatedIn", place1);
        this.sqlgGraph.tx().commit();

// two-hop traversal
        List<Vertex> vertices = this.sqlgGraph.traversal().V().out("knows").out("isLocatedIn").toList();
        Assert.assertEquals(1, vertices.size());
        Assert.assertEquals(RecordId.from(SchemaTable.of("public", "place"), Collections.singletonList("3")), vertices.get(0).id());
    }

This passes on snapshot version 2.0.2-SHAPSHOT I am planning to release this on the weekend.

Rucchao commented 4 years ago

Great, I am looking forward to it, thanks!

pietermartin commented 4 years ago

Afraid I have not managed to complete https://github.com/pietermartin/sqlg/issues/377 Soon as I complete that, I can release. You can try the 2.0.2-SNAPSHOT in the meantime. It is in maven's snapshot repository.

Rucchao commented 4 years ago

Got it. Thanks for your reply.

Best Regards,

Chao

Rucchao commented 4 years ago

Hi pieter,

Now I manage to use sqlg with version 2.0.2-SNAPSHOT to do the 2-hop traversal, however, I find there is an issue about the match step when I use the data with user supplied id, consider the following simple match step for the knows edge:

g.V().as('a').match(__.as('a').out('knows').as('b')).select('a','b')

I got an error as follow:

Caused by: java.lang.RuntimeException: org.postgresql.util.PSQLException: ERROR: syntax error at or near "[" Position: 433

Rucchao commented 4 years ago

Please see the full example as follows:

// schema definition and sample data importing Schema aSchema = graph.getTopology().getPublicSchema(); aSchema.ensureVertexLabelExist("person", new HashMap<String,PropertyType>(){{ put("personid", PropertyType.STRING); }}, ListOrderedSet.listOrderedSet(Collections.singletonList("personid"))); graph.tx().commit();

Vertex v1 = graph.addVertex(T.label, "person", "personid", "1"); Vertex v2 = graph.addVertex(T.label, "person", "personid", "2"); v1.addEdge("knows", v2); graph.tx().commit();

// Evalute the match step using ScriptEngine String query = "g.V().match(__.as('a').out('knows').as('b')).select('a','b')";

ScriptEngine engine = new GremlinGroovyScriptEngine(); List results = new ArrayList(); String query=query+".fill(results)"; Bindings new_bindings = engine.createBindings(); new_bindings.put("g", graph.traversal()); new_bindings.put("results", results); engine.eval(query,new_bindings); System.out.println("The query results have "+results.size() +" records");

Rucchao commented 4 years ago

I also tried the match query in the console and got the same error, but this error doesn't occur when I use the default sequence id.

Could you please help me with this issue? many thanks!

Chao

pietermartin commented 3 years ago

Well better late than never, I have started looking at this. I can duplicate it. I am working on the 2020 branch which is running on java 11 and the latest TinkerPop.

pietermartin commented 3 years ago

Fixed on the 2020 branch.