diff --git a/engine/src/test/java/com/arcadedb/query/sql/executor/AlterTypeExecutionTest.java b/engine/src/test/java/com/arcadedb/query/sql/executor/AlterTypeExecutionTest.java index dbb4cf2f9..bac76d7a9 100644 --- a/engine/src/test/java/com/arcadedb/query/sql/executor/AlterTypeExecutionTest.java +++ b/engine/src/test/java/com/arcadedb/query/sql/executor/AlterTypeExecutionTest.java @@ -21,6 +21,7 @@ import com.arcadedb.TestHelper; import com.arcadedb.database.bucketselectionstrategy.BucketSelectionStrategy; import com.arcadedb.database.bucketselectionstrategy.PartitionedBucketSelectionStrategy; +import com.arcadedb.schema.DocumentType; import com.arcadedb.serializer.json.JSONObject; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -43,10 +44,10 @@ public void sqlAlterTypeInheritanceUsing() { database.command("sql", "ALTER TYPE Car SUPERTYPE +Vehicle"); assertThat(database.getSchema().getType("Car").getSuperTypes().size()).isEqualTo(1); - assertThat(database.getSchema().getType("Car").getSuperTypes().stream().map(x -> x.getName()).collect(Collectors.toSet()) + assertThat(database.getSchema().getType("Car").getSuperTypes().stream().map(DocumentType::getName).collect(Collectors.toSet()) .contains("Vehicle")).isTrue(); assertThat(database.getSchema().getType("Vehicle").getSubTypes().size()).isEqualTo(1); - assertThat(database.getSchema().getType("Vehicle").getSubTypes().stream().map(x -> x.getName()).collect(Collectors.toSet()) + assertThat(database.getSchema().getType("Vehicle").getSubTypes().stream().map(DocumentType::getName).toList() .contains("Car")).isTrue(); assertThat(database.getSchema().getType("Vehicle").isSuperTypeOf("Car")).isTrue(); @@ -57,7 +58,7 @@ public void sqlAlterTypeInheritanceUsing() { assertThat(database.getSchema().getType("Suv").getSuperTypes().size()).isEqualTo(1); assertThat(database.getSchema().getType("Car").isSuperTypeOf("Suv")).isTrue(); assertThat(database.getSchema().getType("Car").getSubTypes().size()).isEqualTo(1); - assertThat(database.getSchema().getType("Car").getSubTypes().stream().map(x -> x.getName()).collect(Collectors.toSet()) + assertThat(database.getSchema().getType("Car").getSubTypes().stream().map(DocumentType::getName).collect(Collectors.toSet()) .contains("Suv")).isTrue(); assertThat(database.getSchema().getType("Car").isSuperTypeOf("Suv")).isTrue(); @@ -71,7 +72,7 @@ public void sqlAlterTypeInheritanceUsing() { assertThat(database.getSchema().getType("Vehicle").isSuperTypeOf("Suv")).isTrue(); assertThat(database.getSchema().getType("Car").getSubTypes().size()).isEqualTo(1); assertThat(database.getSchema().getType("Vehicle").getSubTypes().size()).isEqualTo(1); - assertThat(database.getSchema().getType("Car").getSubTypes().stream().map(x -> x.getName()).collect(Collectors.toSet()) + assertThat(database.getSchema().getType("Car").getSubTypes().stream().map(DocumentType::getName).collect(Collectors.toSet()) .contains("Suv")).isTrue(); assertThat(database.getSchema().getType("Car").isSuperTypeOf("Suv")).isTrue(); assertThat(database.getSchema().getType("Vehicle").isSuperTypeOf("Suv")).isTrue(); diff --git a/engine/src/test/java/com/arcadedb/query/sql/executor/CreateVertexStatementExecutionTest.java b/engine/src/test/java/com/arcadedb/query/sql/executor/CreateVertexStatementExecutionTest.java index e0af3143b..975e3a071 100644 --- a/engine/src/test/java/com/arcadedb/query/sql/executor/CreateVertexStatementExecutionTest.java +++ b/engine/src/test/java/com/arcadedb/query/sql/executor/CreateVertexStatementExecutionTest.java @@ -25,6 +25,8 @@ import org.junit.jupiter.api.Test; import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; @@ -40,24 +42,19 @@ public CreateVertexStatementExecutionTest() { @Test public void testVerticesContentJsonArray() { final String className = "testVertexContentArray"; - database.getSchema().createVertexType(className, 1); - - String array = "["; - for (int i = 0; i < 1000; i++) { - if (i > 0) - array += ","; - array += "{'name':'name" + i + "', 'surname':'surname" + i + "'}"; - } - array += "]"; + database.getSchema().buildVertexType().withName(className).withTotalBuckets(1).create(); + String array = IntStream.range(0, 1000) + .mapToObj(i -> String.format("{'name':'name%d', 'surname':'surname%d'}", i, i)) + .collect(Collectors.joining(",", "[", "]")); ResultSet result = database.command("sql", "create vertex " + className + " content " + array); for (int i = 0; i < 1000; i++) { assertThat(result.hasNext()).isTrue(); final Result item = result.next(); assertThat(item).isNotNull(); - assertThat(item.getProperty("name").toString()).isEqualTo("name" + i); - assertThat(item.getProperty("surname").toString()).isEqualTo("surname" + i); + assertThat(item.getProperty("name")).isEqualTo("name" + i); + assertThat(item.getProperty("surname")).isEqualTo("surname" + i); } assertThat(result.hasNext()).isFalse(); @@ -67,8 +64,8 @@ public void testVerticesContentJsonArray() { assertThat(result.hasNext()).isTrue(); Result item = result.next(); assertThat(item).isNotNull(); - assertThat(item.getProperty("name").toString()).isEqualTo("name" + i); - assertThat(item.getProperty("surname").toString()).isEqualTo("surname" + i); + assertThat(item.getProperty("name")).isEqualTo("name" + i); + assertThat(item.getProperty("surname")).isEqualTo("surname" + i); } assertThat(result.hasNext()).isFalse(); diff --git a/engine/src/test/java/com/arcadedb/query/sql/executor/GroupByExecutionTest.java b/engine/src/test/java/com/arcadedb/query/sql/executor/GroupByExecutionTest.java index 3479019da..4d683e89f 100644 --- a/engine/src/test/java/com/arcadedb/query/sql/executor/GroupByExecutionTest.java +++ b/engine/src/test/java/com/arcadedb/query/sql/executor/GroupByExecutionTest.java @@ -48,7 +48,12 @@ public void testGroupByCount() { } } - final ResultSet result = database.query("sql", "select address, count(*) as occurrences from InputTx where address is not null group by address limit 10"); + final ResultSet result = database.query("sql", """ + select address, count(*) as occurrences + from InputTx where address is not null + group by address + limit 10 + """); while (result.hasNext()) { final Result row = result.next(); assertThat(row.getProperty("address")).isNotNull(); diff --git a/engine/src/test/java/com/arcadedb/query/sql/executor/MatchInheritanceTest.java b/engine/src/test/java/com/arcadedb/query/sql/executor/MatchInheritanceTest.java index 5fb7d0805..5e2dc0439 100644 --- a/engine/src/test/java/com/arcadedb/query/sql/executor/MatchInheritanceTest.java +++ b/engine/src/test/java/com/arcadedb/query/sql/executor/MatchInheritanceTest.java @@ -11,9 +11,7 @@ public class MatchInheritanceTest extends TestHelper { public void testInheritance() { ResultSet result = null; - String sql = "SELECT FROM Services"; - - result = database.command("SQL", sql); + result = database.command("SQL", "SELECT FROM Services"); int selectFromServices = 0; while (result.hasNext()) { Result record = result.next(); @@ -21,8 +19,7 @@ public void testInheritance() { } assertThat(selectFromServices).isEqualTo(4); - sql = "SELECT FROM Attractions"; - result = database.command("SQL", sql); + result = database.command("SQL", "SELECT FROM Attractions"); int selectFromAttractions = 0; while (result.hasNext()) { Result record = result.next(); @@ -30,9 +27,7 @@ public void testInheritance() { } assertThat(selectFromAttractions).isEqualTo(4); - sql = "SELECT FROM Locations"; - - result = database.command("SQL", sql); + result = database.command("SQL", "SELECT FROM Locations"); int selectFromLocations = 0; while (result.hasNext()) { Result record = result.next(); @@ -40,32 +35,29 @@ public void testInheritance() { } assertThat(selectFromLocations).isEqualTo(8); - sql = "MATCH {type: Customers, as: customer, where: (OrderedId=1)}--{type: Monuments} " + "RETURN $pathelements"; - result = database.query("SQL", sql); + result = database.query("SQL", + "MATCH {type: Customers, as: customer, where: (OrderedId=1)}--{type: Monuments} RETURN $pathelements"); assertThat(result.stream().count()).isEqualTo(2); - sql = "MATCH {type: Customers, as: customer, where: (OrderedId=1)}--{type: Services} " + "RETURN $pathelements"; - result = database.query("SQL", sql); + result = database.query("SQL", + "MATCH {type: Customers, as: customer, where: (OrderedId=1)}--{type: Services} RETURN $pathelements"); assertThat(result.stream().count()).isEqualTo(8); - sql = "MATCH {type: Customers, as: customer, where: (OrderedId=1)}--{type: Attractions} " + "RETURN $pathelements"; - result = database.query("SQL", sql); + result = database.query("SQL", + "MATCH {type: Customers, as: customer, where: (OrderedId=1)}--{type: Attractions} RETURN $pathelements"); assertThat(result.stream().count()).isEqualTo(8); - sql = "MATCH {type: Customers, as: customer, where: (OrderedId=1)}--{type: Locations} " + "RETURN $pathelements"; - result = database.query("SQL", sql); + result = database.query("SQL", + "MATCH {type: Customers, as: customer, where: (OrderedId=1)}--{type: Locations} RETURN $pathelements"); assertThat(result.stream().count()).isEqualTo(16); } @Override public void beginTest() { - StringBuilder sb = new StringBuilder(); - - sb.append("BEGIN;"); /* -- Locations -- + Services @@ -77,95 +69,96 @@ public void beginTest() { -- + Theatres -- + Archaeological Sites */ - sb.append("CREATE VERTEX TYPE Locations;"); - sb.append("CREATE PROPERTY Locations.Id LONG;"); - sb.append("CREATE PROPERTY Locations.Type STRING;"); - sb.append("CREATE PROPERTY Locations.Name STRING;"); - - sb.append("CREATE INDEX ON Locations (Type) NOTUNIQUE;"); - sb.append("CREATE INDEX ON Locations (Name) FULL_TEXT;"); +String sqlScript = """ + BEGIN; + CREATE VERTEX TYPE Locations; + CREATE PROPERTY Locations.Id LONG; + CREATE PROPERTY Locations.Type STRING; + CREATE PROPERTY Locations.Name STRING; - sb.append("CREATE VERTEX TYPE Services EXTENDS Locations;"); - sb.append("CREATE VERTEX TYPE Hotels EXTENDS Services;"); - sb.append("CREATE INDEX ON Hotels (Id) UNIQUE;"); + CREATE INDEX ON Locations (Type) NOTUNIQUE; + CREATE INDEX ON Locations (Name) FULL_TEXT; - sb.append("CREATE VERTEX TYPE Restaurants EXTENDS Services;\n"); - sb.append("CREATE INDEX ON Restaurants(Id) UNIQUE;\n"); + CREATE VERTEX TYPE Services EXTENDS Locations; + CREATE VERTEX TYPE Hotels EXTENDS Services; + CREATE INDEX ON Hotels (Id) UNIQUE; - sb.append("CREATE VERTEX TYPE Attractions EXTENDS Locations;\n"); - sb.append("CREATE VERTEX TYPE Monuments EXTENDS Attractions;\n"); - sb.append("CREATE INDEX ON Monuments (Id) UNIQUE;\n"); + CREATE VERTEX TYPE Restaurants EXTENDS Services; + CREATE INDEX ON Restaurants(Id) UNIQUE; - sb.append("CREATE VERTEX TYPE Castles EXTENDS Attractions;\n"); - sb.append("CREATE INDEX ON Castles(Id) UNIQUE;\n"); + CREATE VERTEX TYPE Attractions EXTENDS Locations; + CREATE VERTEX TYPE Monuments EXTENDS Attractions; + CREATE INDEX ON Monuments (Id) UNIQUE; - sb.append("CREATE VERTEX TYPE Theatres EXTENDS Attractions;\n"); - sb.append("CREATE INDEX ON Theatres(Id) UNIQUE;\n"); + CREATE VERTEX TYPE Castles EXTENDS Attractions; + CREATE INDEX ON Castles(Id) UNIQUE; - sb.append("CREATE VERTEX TYPE ArchaeologicalSites EXTENDS Attractions;\n"); - sb.append("CREATE INDEX ON ArchaeologicalSites(Id) UNIQUE;\n"); + CREATE VERTEX TYPE Theatres EXTENDS Attractions; + CREATE INDEX ON Theatres(Id) UNIQUE; - sb.append("CREATE VERTEX TYPE Customers;"); - sb.append("CREATE PROPERTY Customers.OrderedId LONG;"); + CREATE VERTEX TYPE ArchaeologicalSites EXTENDS Attractions; + CREATE INDEX ON ArchaeologicalSites(Id) UNIQUE; - sb.append("CREATE VERTEX TYPE Orders;"); - sb.append("CREATE PROPERTY Orders.Id LONG;"); - sb.append("CREATE PROPERTY Orders.Amount LONG;"); - sb.append("CREATE PROPERTY Orders.OrderDate DATE;"); + CREATE VERTEX TYPE Customers; + CREATE PROPERTY Customers.OrderedId LONG; - sb.append("CREATE INDEX ON Customers(OrderedId) UNIQUE;"); + CREATE VERTEX TYPE Orders; + CREATE PROPERTY Orders.Id LONG; + CREATE PROPERTY Orders.Amount LONG; + CREATE PROPERTY Orders.OrderDate DATE; - sb.append("CREATE INDEX ON Orders(Id) UNIQUE;"); + CREATE INDEX ON Customers(OrderedId) UNIQUE; - sb.append("CREATE EDGE TYPE HasUsedService;"); - sb.append("CREATE PROPERTY HasUsedService.out LINK OF Customers;"); + CREATE INDEX ON Orders(Id) UNIQUE; - sb.append("CREATE EDGE TYPE HasStayed EXTENDS HasUsedService;"); - sb.append("CREATE PROPERTY HasStayed.in LINK OF Hotels;"); + CREATE EDGE TYPE HasUsedService; + CREATE PROPERTY HasUsedService.out LINK OF Customers; - sb.append("CREATE EDGE TYPE HasEaten EXTENDS HasUsedService;"); - sb.append("CREATE PROPERTY HasEaten.in LINK OF Restaurants;"); + CREATE EDGE TYPE HasStayed EXTENDS HasUsedService; + CREATE PROPERTY HasStayed.in LINK OF Hotels; - sb.append("CREATE EDGE TYPE HasVisited;"); - sb.append("CREATE PROPERTY HasVisited.out LINK OF Customers;"); - sb.append("CREATE PROPERTY HasVisited.in LINK;"); - sb.append("CREATE INDEX ON HasVisited (`in`, `out`) UNIQUE;"); + CREATE EDGE TYPE HasEaten EXTENDS HasUsedService; + CREATE PROPERTY HasEaten.in LINK OF Restaurants; - sb.append("CREATE EDGE TYPE HasCustomer;"); - sb.append("CREATE PROPERTY HasCustomer.in LINK OF Customers;"); - sb.append("CREATE PROPERTY HasCustomer.out LINK OF Orders ;"); + CREATE EDGE TYPE HasVisited; + CREATE PROPERTY HasVisited.out LINK OF Customers; + CREATE PROPERTY HasVisited.in LINK; + CREATE INDEX ON HasVisited (`in`, `out`) UNIQUE; - sb.append("INSERT INTO Customers SET OrderedId = 1, Phone = '+1400844724';"); - sb.append("INSERT INTO Orders SET Id = 1, Amount = 536, OrderDate = '2013-05-23';"); + CREATE EDGE TYPE HasCustomer; + CREATE PROPERTY HasCustomer.in LINK OF Customers; + CREATE PROPERTY HasCustomer.out LINK OF Orders; - sb.append("INSERT INTO Hotels SET Id = 730, Name = 'Toules', Type = 'alpine_hut';"); + INSERT INTO Customers SET OrderedId = 1, Phone = '+1400844724'; + INSERT INTO Orders SET Id = 1, Amount = 536, OrderDate = '2013-05-23'; - sb.append("INSERT INTO Restaurants SET Id = 1834, Name = 'Uliassi', Type = 'restaurant';"); - sb.append("INSERT INTO Restaurants SET Id = 1099, Name = 'L\\'Angelo d\\'Oro', Type = 'restaurant';"); + INSERT INTO Hotels SET Id = 730, Name = 'Toules', Type = 'alpine_hut'; - sb.append("INSERT INTO Restaurants SET Id = 1738, Name = 'Johnny Paranza', Type = 'fast_food';"); + INSERT INTO Restaurants SET Id = 1834, Name = 'Uliassi', Type = 'restaurant'; + INSERT INTO Restaurants SET Id = 1099, Name = 'L\\'Angelo d\\'Oro', Type = 'restaurant'; - sb.append("INSERT INTO Castles SET Id = 127, Name = 'Haselburg', Type = 'castle';"); - sb.append("INSERT INTO ArchaeologicalSites SET Id = 47, Name = 'Villa Romana', Type = 'archaeological_site';"); - sb.append("INSERT INTO Monuments SET Id = 62, Name = 'Giuseppe Garibaldi', Type = 'monument';"); - sb.append("INSERT INTO Theatres SET Id = 65, Name = 'Teatro Civico', Type = 'theatre';"); + INSERT INTO Restaurants SET Id = 1738, Name = 'Johnny Paranza', Type = 'fast_food'; - sb.append("CREATE EDGE HasStayed FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Hotels WHERE Id=730);"); + INSERT INTO Castles SET Id = 127, Name = 'Haselburg', Type = 'castle'; + INSERT INTO ArchaeologicalSites SET Id = 47, Name = 'Villa Romana', Type = 'archaeological_site'; + INSERT INTO Monuments SET Id = 62, Name = 'Giuseppe Garibaldi', Type = 'monument'; + INSERT INTO Theatres SET Id = 65, Name = 'Teatro Civico', Type = 'theatre'; - sb.append("CREATE EDGE HasEaten FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Restaurants WHERE Id=1834);"); - sb.append("CREATE EDGE HasEaten FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Restaurants WHERE Id=1099);"); - sb.append("CREATE EDGE HasEaten FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Restaurants WHERE Id=1738);"); + CREATE EDGE HasStayed FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Hotels WHERE Id=730); - sb.append("CREATE EDGE HasCustomer FROM (SELECT FROM Orders WHERE Id=1) TO (SELECT FROM Customers WHERE OrderedId=1);"); + CREATE EDGE HasEaten FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Restaurants WHERE Id=1834); + CREATE EDGE HasEaten FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Restaurants WHERE Id=1099); + CREATE EDGE HasEaten FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Restaurants WHERE Id=1738); - sb.append("CREATE EDGE HasVisited FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Castles WHERE Id=127);"); - sb.append( - "CREATE EDGE HasVisited FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM ArchaeologicalSites WHERE Id=47);"); - sb.append("CREATE EDGE HasVisited FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Monuments WHERE Id=62);"); - sb.append("CREATE EDGE HasVisited FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Theatres WHERE Id=65);"); + CREATE EDGE HasCustomer FROM (SELECT FROM Orders WHERE Id=1) TO (SELECT FROM Customers WHERE OrderedId=1); - sb.append("COMMIT;"); + CREATE EDGE HasVisited FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Castles WHERE Id=127); + CREATE EDGE HasVisited FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM ArchaeologicalSites WHERE Id=47); + CREATE EDGE HasVisited FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Monuments WHERE Id=62); + CREATE EDGE HasVisited FROM (SELECT FROM Customers WHERE OrderedId=1) TO (SELECT FROM Theatres WHERE Id=65); - database.command("SQLScript", sb.toString()); + COMMIT; + """; + database.command("SQLScript", sqlScript); } } diff --git a/engine/src/test/java/com/arcadedb/query/sql/executor/MatchResultTest.java b/engine/src/test/java/com/arcadedb/query/sql/executor/MatchResultTest.java index fa0059400..41d378181 100644 --- a/engine/src/test/java/com/arcadedb/query/sql/executor/MatchResultTest.java +++ b/engine/src/test/java/com/arcadedb/query/sql/executor/MatchResultTest.java @@ -17,24 +17,23 @@ public class MatchResultTest extends TestHelper { */ @Test public void testIssue1689() { - database.transaction(() -> database.command("sqlscript", "CREATE VERTEX TYPE Person IF NOT EXISTS;\n" - + "CREATE PROPERTY Person.role IF NOT EXISTS STRING;\n" - + "CREATE VERTEX TYPE House IF NOT EXISTS;\n" - + "CREATE EDGE TYPE LivesIn IF NOT EXISTS;\n" -// + "CREATE EDGE TYPE DummyEdge IF NOT EXISTS;\n" -// + "DELETE FROM LivesIn;\n" -// + "DELETE FROM Person;\n" -// + "DELETE FROM House;\n" - + "CREATE VERTEX House;\n" - + "CREATE VERTEX Person SET role='mom';\n" - + "CREATE VERTEX Person SET role='dad';\n" - + "CREATE VERTEX Person SET role='child';\n" - + "CREATE EDGE LivesIn FROM (SELECT FROM Person) TO (SELECT FROM House);")); - - final ResultSet resultSet = database.query("sql", "MATCH {TYPE: Person, AS: personVertex} -LivesIn-> {TYPE: House}\n" - + ", NOT {AS: personVertex} -DummyEdge-> {TYPE: House}\n" - + "RETURN personVertex"); - +database.transaction(() -> database.command("sqlscript", """ + CREATE VERTEX TYPE Person IF NOT EXISTS; + CREATE PROPERTY Person.role IF NOT EXISTS STRING; + CREATE VERTEX TYPE House IF NOT EXISTS; + CREATE EDGE TYPE LivesIn IF NOT EXISTS; + CREATE VERTEX House; + CREATE VERTEX Person SET role='mom'; + CREATE VERTEX Person SET role='dad'; + CREATE VERTEX Person SET role='child'; + CREATE EDGE LivesIn FROM (SELECT FROM Person) TO (SELECT FROM House); + """)); + +final ResultSet resultSet = database.query("sql", """ + MATCH {TYPE: Person, AS: personVertex} -LivesIn-> {TYPE: House} + , NOT {AS: personVertex} -DummyEdge-> {TYPE: House} + RETURN personVertex + """); Set set = new HashSet<>(); while (resultSet.hasNext()) { final Vertex next = resultSet.nextIfAvailable().getProperty("personVertex"); diff --git a/engine/src/test/java/com/arcadedb/query/sql/executor/MatchStatementExecutionTest.java b/engine/src/test/java/com/arcadedb/query/sql/executor/MatchStatementExecutionTest.java index d02de5fe6..13a1af4e1 100644 --- a/engine/src/test/java/com/arcadedb/query/sql/executor/MatchStatementExecutionTest.java +++ b/engine/src/test/java/com/arcadedb/query/sql/executor/MatchStatementExecutionTest.java @@ -22,6 +22,7 @@ import com.arcadedb.database.*; import com.arcadedb.graph.MutableVertex; import com.arcadedb.graph.Vertex; +import com.arcadedb.index.Index; import com.arcadedb.index.TypeIndex; import org.junit.jupiter.api.Test; @@ -926,17 +927,18 @@ public void testManagedArrows() { } private ResultSet getManagedByArrows(final String managerName) { - final StringBuilder query = new StringBuilder(); - query.append("select expand(managed) from ("); - query.append(" match {type:Employee, where: (name = '" + managerName + "')}"); - query.append(" -ManagerOf->{}<-ParentDepartment-{"); - query.append(" while: ($depth = 0 or in('ManagerOf').size() = 0),"); - query.append(" where: ($depth = 0 or in('ManagerOf').size() = 0)"); - query.append(" }<-WorksAt-{as: managed}"); - query.append(" return managed"); - query.append(")"); + final String query = """ + select expand(managed) from ( + match {type:Employee, where: (name = '%s')} + -ManagerOf->{}<-ParentDepartment-{ + while: ($depth = 0 or in('ManagerOf').size() = 0), + where: ($depth = 0 or in('ManagerOf').size() = 0) + }<-WorksAt-{as: managed} + return managed + ) + """.formatted(managerName); - return database.query("sql", query.toString()); + return database.query("sql", query); } @Test @@ -969,19 +971,20 @@ public void testManaged2() { } private ResultSet getManagedBy2(final String managerName) { - final StringBuilder query = new StringBuilder(); - query.append("select expand(managed) from ("); - query.append(" match {type:Employee, where: (name = '" + managerName + "')}"); - query.append(" .out('ManagerOf')"); - query.append(" .(inE('ParentDepartment').outV()){"); - query.append(" while: ($depth = 0 or in('ManagerOf').size() = 0),"); - query.append(" where: ($depth = 0 or in('ManagerOf').size() = 0)"); - query.append(" }"); - query.append(" .in('WorksAt'){as: managed}"); - query.append(" return managed"); - query.append(")"); + final String query = """ + select expand(managed) from ( + match {type:Employee, where: (name = '%s')} + .out('ManagerOf') + .(inE('ParentDepartment').outV()){ + while: ($depth = 0 or in('ManagerOf').size() = 0), + where: ($depth = 0 or in('ManagerOf').size() = 0) + } + .in('WorksAt'){as: managed} + return managed + ) + """.formatted(managerName); - return database.query("sql", query.toString()); + return database.query("sql", query); } @Test @@ -1014,33 +1017,33 @@ public void testManaged2Arrows() { } private ResultSet getManagedBy2Arrows(final String managerName) { - final StringBuilder query = new StringBuilder(); - query.append("select expand(managed) from ("); - query.append(" match {type:Employee, where: (name = '" + managerName + "')}"); - query.append(" -ManagerOf->{}"); - query.append(" .(inE('ParentDepartment').outV()){"); - query.append(" while: ($depth = 0 or in('ManagerOf').size() = 0),"); - query.append(" where: ($depth = 0 or in('ManagerOf').size() = 0)"); - query.append(" }<-WorksAt-{as: managed}"); - query.append(" return managed"); - query.append(")"); + final String query = """ + select expand(managed) from ( + match {type:Employee, where: (name = '%s')} + -ManagerOf->{} + .(inE('ParentDepartment').outV()){ + while: ($depth = 0 or in('ManagerOf').size() = 0), + where: ($depth = 0 or in('ManagerOf').size() = 0) + }<-WorksAt-{as: managed} + return managed + ) + """.formatted(managerName); - return database.query("sql", query.toString()); + return database.query("sql", query); } @Test public void testTriangle1() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1, where: (uid = 0)}"); - query.append(" .out('TriangleE'){as: friend2}"); - query.append(" .out('TriangleE'){as: friend3},"); - query.append("{type:TriangleV, as: friend1}"); - query.append(" .out('TriangleE'){as: friend3}"); - query.append("return $matches"); - - final ResultSet result = database.query("sql", query.toString()); + final String query = """ + match {type:TriangleV, as: friend1, where: (uid = 0)} + .out('TriangleE'){as: friend2} + .out('TriangleE'){as: friend3}, + {type:TriangleV, as: friend1} + .out('TriangleE'){as: friend3} + return $matches + """; + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); result.next(); assertThat(result.hasNext()).isFalse(); @@ -1049,13 +1052,14 @@ public void testTriangle1() { @Test public void testTriangle1Arrows() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1, where: (uid = 0)} -TriangleE-> {as: friend2} -TriangleE-> {as: friend3},"); - query.append("{type:TriangleV, as: friend1} -TriangleE-> {as: friend3}"); - query.append("return $matches"); + final String query = """ + match {type:TriangleV, as: friend1, where: (uid = 0)} -TriangleE-> {as: friend2} -TriangleE-> {as: friend3}, + {type:TriangleV, as: friend1} -TriangleE-> {as: friend3} + return $matches + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); + assertThat(result.hasNext()).isTrue(); assertThat(result.hasNext()).isTrue(); result.next(); assertThat(result.hasNext()).isFalse(); @@ -1064,17 +1068,16 @@ public void testTriangle1Arrows() { @Test public void testTriangle2Old() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1}"); - query.append(" .out('TriangleE'){type:TriangleV, as: friend2, where: (uid = 1)}"); - query.append(" .out('TriangleE'){as: friend3},"); - query.append("{type:TriangleV, as: friend1}"); - query.append(" .out('TriangleE'){as: friend3}"); - query.append("return $matches"); - - final ResultSet result = database.query("sql", query.toString()); + final String query = """ + match {type:TriangleV, as: friend1} + .out('TriangleE'){type:TriangleV, as: friend2, where: (uid = 1)} + .out('TriangleE'){as: friend3}, + {type:TriangleV, as: friend1} + .out('TriangleE'){as: friend3} + return $matches + """; + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); final Document friend1 = doc.getProperty("friend1"); @@ -1088,16 +1091,17 @@ public void testTriangle2Old() { @Test public void testTriangle2() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1}"); - query.append(" .out('TriangleE'){type:TriangleV, as: friend2, where: (uid = 1)}"); - query.append(" .out('TriangleE'){as: friend3},"); - query.append("{type:TriangleV, as: friend1}"); - query.append(" .out('TriangleE'){as: friend3}"); - query.append("return $patterns"); + final String query = """ + match {type:TriangleV, as: friend1} + .out('TriangleE'){type:TriangleV, as: friend2, where: (uid = 1)} + .out('TriangleE'){as: friend3}, + {type:TriangleV, as: friend1} + .out('TriangleE'){as: friend3} + return $patterns + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); + assertThat(result.hasNext()).isTrue(); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1112,16 +1116,17 @@ public void testTriangle2() { @Test public void testTriangle2Arrows() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1}"); - query.append(" -TriangleE->{type:TriangleV, as: friend2, where: (uid = 1)}"); - query.append(" -TriangleE->{as: friend3},"); - query.append("{type:TriangleV, as: friend1}"); - query.append(" -TriangleE->{as: friend3}"); - query.append("return $matches"); + final String query = """ + match {type:TriangleV, as: friend1} + -TriangleE->{type:TriangleV, as: friend2, where: (uid = 1)} + -TriangleE->{as: friend3}, + {type:TriangleV, as: friend1} + -TriangleE->{as: friend3} + return $matches + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); + assertThat(result.hasNext()).isTrue(); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1136,16 +1141,16 @@ public void testTriangle2Arrows() { @Test public void testTriangle3() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1}"); - query.append(" -TriangleE->{as: friend2}"); - query.append(" -TriangleE->{as: friend3, where: (uid = 2)},"); - query.append("{type:TriangleV, as: friend1}"); - query.append(" -TriangleE->{as: friend3}"); - query.append("return $matches"); + final String query = """ + match {type:TriangleV, as: friend1} + -TriangleE->{as: friend2} + -TriangleE->{as: friend3, where: (uid = 2)}, + {type:TriangleV, as: friend1} + -TriangleE->{as: friend3} + return $matches + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1154,16 +1159,17 @@ public void testTriangle3() { @Test public void testTriangle4() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1}"); - query.append(" .out('TriangleE'){as: friend2, where: (uid = 1)}"); - query.append(" .out('TriangleE'){as: friend3},"); - query.append("{type:TriangleV, as: friend1}"); - query.append(" .out('TriangleE'){as: friend3}"); - query.append("return $matches"); + final String query = """ + match {type:TriangleV, as: friend1} + .out('TriangleE'){as: friend2, where: (uid = 1)} + .out('TriangleE'){as: friend3}, + {type:TriangleV, as: friend1} + .out('TriangleE'){as: friend3} + return $matches + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); + assertThat(result.hasNext()).isTrue(); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1172,16 +1178,17 @@ public void testTriangle4() { @Test public void testTriangle4Arrows() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1}"); - query.append(" -TriangleE->{as: friend2, where: (uid = 1)}"); - query.append(" -TriangleE->{as: friend3},"); - query.append("{type:TriangleV, as: friend1}"); - query.append(" -TriangleE->{as: friend3}"); - query.append("return $matches"); + final String query = """ + match {type:TriangleV, as: friend1} + -TriangleE->{as: friend2, where: (uid = 1)} + -TriangleE->{as: friend3}, + {type:TriangleV, as: friend1} + -TriangleE->{as: friend3} + return $matches + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); + assertThat(result.hasNext()).isTrue(); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1190,17 +1197,16 @@ public void testTriangle4Arrows() { @Test public void testTriangleWithEdges4() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1}"); - query.append(" .outE('TriangleE').inV(){as: friend2, where: (uid = 1)}"); - query.append(" .outE('TriangleE').inV(){as: friend3},"); - query.append("{type:TriangleV, as: friend1}"); - query.append(" .outE('TriangleE').inV(){as: friend3}"); - query.append("return $matches"); - - final ResultSet result = database.query("sql", query.toString()); + final String query = """ + match {type:TriangleV, as: friend1} + .outE('TriangleE').inV(){as: friend2, where: (uid = 1)} + .outE('TriangleE').inV(){as: friend3}, + {type:TriangleV, as: friend1} + .outE('TriangleE').inV(){as: friend3} + return $matches + """; + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1209,14 +1215,13 @@ public void testTriangleWithEdges4() { @Test public void testCartesianProduct() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1, where:(uid = 1)},"); - query.append("{type:TriangleV, as: friend2, where:(uid = 2 or uid = 3)}"); - query.append("return $matches"); - - final ResultSet result = database.query("sql", query.toString()); + final String query = """ + match {type:TriangleV, as: friend1, where:(uid = 1)}, + {type:TriangleV, as: friend2, where:(uid = 2 or uid = 3)} + return $matches + """; + final ResultSet result = database.query("sql", query); for (int i = 0; i < 2; i++) { assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); @@ -1229,13 +1234,12 @@ public void testCartesianProduct() { @Test public void testNoPrefetch() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:IndexedVertex, as: one}"); - query.append("return $patterns"); - - final ResultSet result = database.query("sql", query.toString()); + final String query = """ + match {type:IndexedVertex, as: one} + return $patterns + """; + final ResultSet result = database.query("sql", query); result.getExecutionPlan() .ifPresent(x -> x.getSteps().stream().filter(y -> y instanceof MatchPrefetchStep).forEach(prefetchStepFound -> fail())); @@ -1249,14 +1253,13 @@ public void testNoPrefetch() { @Test public void testCartesianProductLimit() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1, where:(uid = 1)},"); - query.append("{type:TriangleV, as: friend2, where:(uid = 2 or uid = 3)}"); - query.append("return $matches LIMIT 1"); - - final ResultSet result = database.query("sql", query.toString()); + final String query = """ + match {type:TriangleV, as: friend1, where:(uid = 1)}, + {type:TriangleV, as: friend2, where:(uid = 2 or uid = 3)} + return $matches LIMIT 1 + """; + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result d = result.next(); final Document friend1 = d.getProperty("friend1"); @@ -1322,12 +1325,12 @@ public void testArrayRangeSelectors1() { @Test public void testArrayRange2() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1, where: (uid = 0)}"); - query.append("return friend1.out('TriangleE')[0..2] as foo"); + final String query = """ + match {type:TriangleV, as: friend1, where: (uid = 0)} + return friend1.out('TriangleE')[0..2] as foo + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1341,12 +1344,12 @@ public void testArrayRange2() { @Test public void testArrayRange3() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1, where: (uid = 0)}"); - query.append("return friend1.out('TriangleE')[0..3] as foo"); + final String query = """ + match {type:TriangleV, as: friend1, where: (uid = 0)} + return friend1.out('TriangleE')[0..3] as foo + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1360,12 +1363,12 @@ public void testArrayRange3() { @Test public void testConditionInSquareBrackets() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:TriangleV, as: friend1, where: (uid = 0)}"); - query.append("return friend1.out('TriangleE')[uid = 2] as foo"); + final String query = """ + match {type:TriangleV, as: friend1, where: (uid = 0)} + return friend1.out('TriangleE')[uid = 2] as foo + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1381,14 +1384,13 @@ public void testConditionInSquareBrackets() { @Test public void testIndexedEdge() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:IndexedVertex, as: one, where: (uid = 0)}"); - query.append(".out('IndexedEdge'){type:IndexedVertex, as: two, where: (uid = 1)}"); - query.append("return one, two"); - - final ResultSet result = database.query("sql", query.toString()); + final String query = """ + match {type:IndexedVertex, as: one, where: (uid = 0)} + .out('IndexedEdge'){type:IndexedVertex, as: two, where: (uid = 1)} + return one, two + """; + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1397,13 +1399,13 @@ public void testIndexedEdge() { @Test public void testIndexedEdgeArrows() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:IndexedVertex, as: one, where: (uid = 0)}"); - query.append("-IndexedEdge->{type:IndexedVertex, as: two, where: (uid = 1)}"); - query.append("return one, two"); + final String query = """ + match {type:IndexedVertex, as: one, where: (uid = 0)} + -IndexedEdge->{type:IndexedVertex, as: two, where: (uid = 1)} + return one, two + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1412,12 +1414,11 @@ public void testIndexedEdgeArrows() { @Test public void testJson() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:IndexedVertex, as: one, where: (uid = 0)} "); - query.append("return {'name':'foo', 'uuid':one.uid}"); + final String query = """ + match {type:IndexedVertex, as: one, where: (uid = 0)} + return {'name':'foo', 'uuid':one.uid}"""; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1430,12 +1431,12 @@ public void testJson() { @Test public void testJson2() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:IndexedVertex, as: one, where: (uid = 0)} "); - query.append("return {'name':'foo', 'sub': {'uuid':one.uid}}"); + final String query = """ + match {type:IndexedVertex, as: one, where: (uid = 0)} + return {'name':'foo', 'sub': {'uuid':one.uid}} + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1447,12 +1448,12 @@ public void testJson2() { @Test public void testJson3() { - final StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:IndexedVertex, as: one, where: (uid = 0)} "); - query.append("return {'name':'foo', 'sub': [{'uuid':one.uid}]}"); + final String query = """ + match {type:IndexedVertex, as: one, where: (uid = 0)} + return {'name':'foo', 'sub': [{'uuid':one.uid}]} + """; - final ResultSet result = database.query("sql", query.toString()); + final ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); final Result doc = result.next(); assertThat(result.hasNext()).isFalse(); @@ -1465,42 +1466,41 @@ public void testJson3() { @Test public void testUnique() { - StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:DiamondV, as: one, where: (uid = 0)}.out('DiamondE').out('DiamondE'){as: two} "); - query.append("return DISTINCT one, two"); + String query = """ + match {type:DiamondV, as: one, where: (uid = 0)} + .out('DiamondE').out('DiamondE'){as: two} + return DISTINCT one, two + """; - ResultSet result = database.query("sql", query.toString()); + ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); Result doc = result.next(); assertThat(result.hasNext()).isFalse(); - query = new StringBuilder(); - query.append("match "); - query.append("{type:DiamondV, as: one, where: (uid = 0)}.out('DiamondE').out('DiamondE'){as: two} "); - query.append("return DISTINCT one.uid, two.uid"); + query = """ + match {type:DiamondV, as: one, where: (uid = 0)} + .out('DiamondE').out('DiamondE'){as: two} + return DISTINCT one.uid, two.uid + """; result.close(); - result = database.query("sql", query.toString()); + result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); doc = result.next(); assertThat(result.hasNext()).isFalse(); result.close(); - // Document doc = result.get(0); - // assertEquals("foo", doc.set("name"); - // assertEquals(0, doc.set("sub[0].uuid"); } @Test public void testNotUnique() { - StringBuilder query = new StringBuilder(); - query.append("match "); - query.append("{type:DiamondV, as: one, where: (uid = 0)}.out('DiamondE').out('DiamondE'){as: two} "); - query.append("return one, two"); + String query = """ + match {type:DiamondV, as: one, where: (uid = 0)}.out('DiamondE').out('DiamondE'){as: two} + return one, two + """; - ResultSet result = database.query("sql", query.toString()); + ResultSet result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); Result doc = result.next(); @@ -1509,21 +1509,18 @@ public void testNotUnique() { assertThat(result.hasNext()).isFalse(); result.close(); - query = new StringBuilder(); - query.append("match "); - query.append("{type:DiamondV, as: one, where: (uid = 0)}.out('DiamondE').out('DiamondE'){as: two} "); - query.append("return one.uid, two.uid"); + query = """ + match {type:DiamondV, as: one, where: (uid = 0)}.out('DiamondE').out('DiamondE'){as: two} + return one.uid, two.uid + """; - result = database.query("sql", query.toString()); + result = database.query("sql", query); assertThat(result.hasNext()).isTrue(); doc = result.next(); assertThat(result.hasNext()).isTrue(); doc = result.next(); assertThat(result.hasNext()).isFalse(); result.close(); - // Document doc = result.get(0); - // assertEquals("foo", doc.set("name"); - // assertEquals(0, doc.set("sub[0].uuid"); } @Test @@ -1550,15 +1547,16 @@ public void testManagedElements() { } private ResultSet getManagedElements(final String managerName) { - final StringBuilder query = new StringBuilder(); - query.append(" match {type:Employee, as:boss, where: (name = '" + managerName + "')}"); - query.append(" -ManagerOf->{}<-ParentDepartment-{"); - query.append(" while: ($depth = 0 or in('ManagerOf').size() = 0),"); - query.append(" where: ($depth = 0 or in('ManagerOf').size() = 0)"); - query.append(" }<-WorksAt-{as: managed}"); - query.append(" return distinct $elements"); + final String query = """ + match {type:Employee, as:boss, where: (name = '%s')} + -ManagerOf->{}<-ParentDepartment-{ + while: ($depth = 0 or in('ManagerOf').size() = 0), + where: ($depth = 0 or in('ManagerOf').size() = 0) + }<-WorksAt-{as: managed} + return distinct $elements + """.formatted(managerName); - return database.query("sql", query.toString()); + return database.query("sql", query); } @Test @@ -1622,11 +1620,13 @@ public void testOptional2() { @Test public void testOptional3() { - final ResultSet qResult = database.query("sql", "select friend.name as name, b from (" - + "match {type:Person, as:a, where:(name = 'n1' and 1 + 1 = 2)}.out('Friend'){as:friend, where:(name = 'n2' and 1 + 1 = 2)}," - + "{as:a}.out(){as:b, where:(nonExisting = 12), optional:true}," + "{as:friend}.out(){as:b, optional:true}" - + " return friend, b)"); - + final ResultSet qResult = database.query("sql", """ + select friend.name as name, b from ( + match {type:Person, as:a, where:(name = 'n1' and 1 + 1 = 2)}.out('Friend'){as:friend, where:(name = 'n2' and 1 + 1 = 2)}, + {as:a}.out(){as:b, where:(nonExisting = 12), optional:true}, + {as:friend}.out(){as:b, optional:true} + return friend, b) + """); assertThat(qResult.hasNext()).isTrue(); final Result doc = qResult.next(); assertThat(doc.getProperty("name")).isEqualTo("n2"); @@ -1841,7 +1841,7 @@ public void testUnwind() { for (int i = 0; i < 4; i++) { assertThat(result.hasNext()).isTrue(); final Result item = result.next(); - sum += item.getProperty("num"); + sum += item.getProperty("num"); } assertThat(result.hasNext()).isFalse(); @@ -1960,20 +1960,23 @@ public void testPathAlias() { final List thePath = (List) path; final String bname = item.getProperty("bname"); - if (bname.equals("aaa")) { - assertThat(thePath.size()).isEqualTo(0); - } else if (bname.equals("aaa")) { + switch (bname) { + case "aaa" -> assertThat(thePath.size()).isEqualTo(0); + case "bbb" -> { assertThat(thePath.size()).isEqualTo(1); - assertThat(((Document) thePath.get(0).getRecord()).getString("name")).isEqualTo("bbb"); - } else if (bname.equals("ccc")) { + assertThat( thePath.get(0).getRecord().asDocument().getString("name")).isEqualTo("bbb"); + } + case "ccc" -> { assertThat(thePath.size()).isEqualTo(2); - assertThat(((Document) thePath.get(0).getRecord()).getString("name")).isEqualTo("bbb"); - assertThat(((Document) thePath.get(1).getRecord()).getString("name")).isEqualTo("ccc"); - } else if (bname.equals("ddd")) { + assertThat(thePath.get(0).getRecord().asDocument().getString("name")).isEqualTo("bbb"); + assertThat(thePath.get(1).getRecord().asDocument().getString("name")).isEqualTo("ccc"); + } + case "ddd" -> { assertThat(thePath.size()).isEqualTo(3); - assertThat(((Document) thePath.get(0).getRecord()).getString("name")).isEqualTo("bbb"); - assertThat(((Document) thePath.get(1).getRecord()).getString("name")).isEqualTo("ccc"); - assertThat(((Document) thePath.get(2).getRecord()).getString("name")).isEqualTo("ddd"); + assertThat(thePath.get(0).getRecord().asDocument().getString("name")).isEqualTo("bbb"); + assertThat(thePath.get(1).getRecord().asDocument().getString("name")).isEqualTo("ccc"); + assertThat(thePath.get(2).getRecord().asDocument().getString("name")).isEqualTo("ddd"); + } } } assertThat(result.hasNext()).isFalse(); @@ -2024,49 +2027,49 @@ public void testBucketTarget() { assertThat(result.hasNext()).isFalse(); - assertThat(database.getSchema().getIndexByName(clazz + "[name]").get(new String[]{"one"}).hasNext()).isTrue(); - assertThat(database.getSchema().getIndexByName(clazz + "[name]").get(new String[]{"onex"}).hasNext()).isTrue(); - assertThat(database.getSchema().getIndexByName(clazz + "[name]").get(new String[]{"two"}).hasNext()).isTrue(); - assertThat(database.getSchema().getIndexByName(clazz + "[name]").get(new String[]{"three"}).hasNext()).isTrue(); + assertThat(database.getSchema().getIndexByName(clazz + "[name]").get(new String[] { "one" }).hasNext()).isTrue(); + assertThat(database.getSchema().getIndexByName(clazz + "[name]").get(new String[] { "onex" }).hasNext()).isTrue(); + assertThat(database.getSchema().getIndexByName(clazz + "[name]").get(new String[] { "two" }).hasNext()).isTrue(); + assertThat(database.getSchema().getIndexByName(clazz + "[name]").get(new String[] { "three" }).hasNext()).isTrue(); //-------------------------------------------------------------------------------------------------------- // CHECK THE SUB-INDEX EXISTS assertThat(Set.of(((TypeIndex) database.getSchema().getIndexByName(clazz + "[name]")).getIndexesOnBuckets()).stream() - .map((r) -> r.getAssociatedBucketId()).collect(Collectors.toSet()) - .contains(database.getSchema().getBucketByName(clazz + "_one").getFileId())).isTrue(); + .map(Index::getAssociatedBucketId).collect(Collectors.toSet()) + .contains(database.getSchema().getBucketByName(clazz + "_one").getFileId())).isTrue(); database.command("SQL", "ALTER TYPE " + clazz + " BUCKET -" + clazz + "_one").close(); // CHECK THE SUB-INDEX HAS BEN REMOVED assertThat(Set.of(((TypeIndex) database.getSchema().getIndexByName(clazz + "[name]")).getIndexesOnBuckets()).stream() - .map((r) -> r.getAssociatedBucketId()).collect(Collectors.toSet()) - .contains(database.getSchema().getBucketByName(clazz + "_one").getFileId())).isFalse(); + .map(Index::getAssociatedBucketId).collect(Collectors.toSet()) + .contains(database.getSchema().getBucketByName(clazz + "_one").getFileId())).isFalse(); //-------------------------------------------------------------------------------------------------------- // CHECK THE SUB-INDEX EXISTS assertThat(Set.of(((TypeIndex) database.getSchema().getIndexByName(clazz + "[name]")).getIndexesOnBuckets()).stream() - .map((r) -> r.getAssociatedBucketId()).collect(Collectors.toSet()) - .contains(database.getSchema().getBucketByName(clazz + "_two").getFileId())).isTrue(); + .map(Index::getAssociatedBucketId).collect(Collectors.toSet()) + .contains(database.getSchema().getBucketByName(clazz + "_two").getFileId())).isTrue(); database.command("SQL", "ALTER TYPE " + clazz + " BUCKET -" + clazz + "_two").close(); // CHECK THE SUB-INDEX HAS BEN REMOVED assertThat(Set.of(((TypeIndex) database.getSchema().getIndexByName(clazz + "[name]")).getIndexesOnBuckets()).stream() - .map((r) -> r.getAssociatedBucketId()).collect(Collectors.toSet()) - .contains(database.getSchema().getBucketByName(clazz + "_two").getFileId())).isFalse(); + .map(Index::getAssociatedBucketId).collect(Collectors.toSet()) + .contains(database.getSchema().getBucketByName(clazz + "_two").getFileId())).isFalse(); //-------------------------------------------------------------------------------------------------------- // CHECK THE SUB-INDEX EXISTS assertThat(Set.of(((TypeIndex) database.getSchema().getIndexByName(clazz + "[name]")).getIndexesOnBuckets()).stream() - .map((r) -> r.getAssociatedBucketId()).collect(Collectors.toSet()) - .contains(database.getSchema().getBucketByName(clazz + "_three").getFileId())).isTrue(); + .map(Index::getAssociatedBucketId).collect(Collectors.toSet()) + .contains(database.getSchema().getBucketByName(clazz + "_three").getFileId())).isTrue(); database.command("SQL", "ALTER TYPE " + clazz + " BUCKET -" + clazz + "_three").close(); // CHECK THE SUB-INDEX HAS BEN REMOVED assertThat(Set.of(((TypeIndex) database.getSchema().getIndexByName(clazz + "[name]")).getIndexesOnBuckets()).stream() - .map((r) -> r.getAssociatedBucketId()).collect(Collectors.toSet()) - .contains(database.getSchema().getBucketByName(clazz + "_three").getFileId())).isFalse(); + .map(Index::getAssociatedBucketId).collect(Collectors.toSet()) + .contains(database.getSchema().getBucketByName(clazz + "_three").getFileId())).isFalse(); result.close(); } @@ -2218,15 +2221,16 @@ public void testPathTraversal() { } private ResultSet getManagedPathElements(final String managerName) { - final StringBuilder query = new StringBuilder(); - query.append(" match {type:Employee, as:boss, where: (name = '" + managerName + "')}"); - query.append(" -ManagerOf->{}<-ParentDepartment-{"); - query.append(" while: ($depth = 0 or in('ManagerOf').size() = 0),"); - query.append(" where: ($depth = 0 or in('ManagerOf').size() = 0)"); - query.append(" }<-WorksAt-{as: managed}"); - query.append(" return distinct $pathElements"); + final String query = """ + match {type:Employee, as:boss, where: (name = '%s')} + -ManagerOf->{}<-ParentDepartment-{ + while: ($depth = 0 or in('ManagerOf').size() = 0), + where: ($depth = 0 or in('ManagerOf').size() = 0) + }<-WorksAt-{as: managed} + return distinct $pathElements + """.formatted(managerName); - return database.query("sql", query.toString()); + return database.query("sql", query); } @Test @@ -2244,8 +2248,7 @@ public void testQuotedClassName() { @Test public void testMatchInSubQuery() { - try (final ResultSet rs = database.query("SQL", - "SELECT $a LET $a=(MATCH{type:Person,as:Person_0}RETURN expand(Person_0))")) { + try (final ResultSet rs = database.query("SQL", "SELECT $a LET $a=(MATCH{type:Person,as:Person_0}RETURN expand(Person_0))")) { assertThat(rs.stream().count()).isEqualTo(1L); } } diff --git a/engine/src/test/java/com/arcadedb/query/sql/executor/WhileBlockExecutionTest.java b/engine/src/test/java/com/arcadedb/query/sql/executor/WhileBlockExecutionTest.java index b9cc28c72..8c0dc54b2 100644 --- a/engine/src/test/java/com/arcadedb/query/sql/executor/WhileBlockExecutionTest.java +++ b/engine/src/test/java/com/arcadedb/query/sql/executor/WhileBlockExecutionTest.java @@ -38,21 +38,21 @@ public void testPlain() { database.getSchema().createDocumentType(className); - String script = ""; - script += "LET $i = 0;"; - script += "WHILE ($i < 3){\n"; - script += " insert into " + className + " set value = $i;\n"; - script += " LET $i = $i + 1;"; - script += "}"; - script += "SELECT FROM " + className + ";"; - + String script = """ + LET $i = 0; + WHILE ($i < 3){ + insert into %s set value = $i; + LET $i = $i + 1; + } + SELECT FROM %s; + """.formatted(className, className); final ResultSet results = database.command("sqlscript", script); int tot = 0; int sum = 0; while (results.hasNext()) { final Result item = results.next(); - sum += item.getProperty("value"); + sum += item.getProperty("value"); tot++; } assertThat(tot).isEqualTo(3); @@ -66,16 +66,16 @@ public void testReturn() { database.getSchema().createDocumentType(className); - String script = ""; - script += "LET $i = 0;"; - script += "WHILE ($i < 3){\n"; - script += " insert into " + className + " set value = $i;\n"; - script += " IF ($i = 1) {"; - script += " RETURN;"; - script += " }"; - script += " LET $i = $i + 1;"; - script += "}"; - + String script = """ + LET $i = 0; + WHILE ($i < 3){ + insert into %s set value = $i; + IF ($i = 1) { + RETURN; + } + LET $i = $i + 1; + } + """.formatted(className); ResultSet results = database.command("sqlscript", script); results.close(); results = database.query("sql", "SELECT FROM " + className); @@ -84,7 +84,7 @@ public void testReturn() { int sum = 0; while (results.hasNext()) { final Result item = results.next(); - sum += item.getProperty("value"); + sum += item.getProperty("value"); tot++; } assertThat(tot).isEqualTo(2);