diff --git a/src/test/java/ru/rt/restream/reindexer/connector/SubQueryTest.java b/src/test/java/ru/rt/restream/reindexer/connector/SubQueryTest.java index 796e6a4..ac97b61 100644 --- a/src/test/java/ru/rt/restream/reindexer/connector/SubQueryTest.java +++ b/src/test/java/ru/rt/restream/reindexer/connector/SubQueryTest.java @@ -32,7 +32,6 @@ import static ru.rt.restream.reindexer.Query.Condition.GE; import static ru.rt.restream.reindexer.Query.Condition.GT; import static ru.rt.restream.reindexer.Query.Condition.LT; -import static ru.rt.restream.reindexer.Query.Condition.SET; /** * Base SubQuery test. @@ -97,6 +96,40 @@ public void testWhereWithArgsIndexConditionQueryAndTwoNs() { assertThat(actualRetireesPurchases.size(), is(3)); } + @Test + public void testWhereWithArgsIndexConditionQueryAndTwoNsAndNoAgg() { + Namespace personsNs = db.openNamespace("persons", NamespaceOptions.defaultOptions(), Person.class); + Namespace purchasesNs = db.openNamespace("purchases", NamespaceOptions.defaultOptions(), Purchase.class); + int purchaseId = 0; + // 24 persons, everyone has from 0 to 3 purchases, for a total of 36 purchases. + for (int i = 0; i < 24; i++) { + int age = 9 + (i % 8) * 10; + String name = "Person" + i + "Age" + age; + personsNs.insert(new Person(i, name, age)); + for (int j = 0; j < i % 4; j++) { + int price = (j + 1) * 10; + purchasesNs.insert(new Purchase(purchaseId++, i, price, "Asset" + j)); + } + } + + // select * from purchases p + // where p.person_id in (select id from persons where age > 60) + Query retireeSubQuery = personsNs.query() + .select("id") + .where("age", GT, 60); + Query purchasesQuery = purchasesNs.query() + .where("person_id", EQ, retireeSubQuery); + ResultIterator iterator = purchasesQuery.execute(); + + List actualRetireesPurchases = new ArrayList<>(); + while (iterator.hasNext()) { + actualRetireesPurchases.add(iterator.next()); + } + + // 6 persons with age > 60 have 15 purchases ((2 + 3) * 3). + assertThat(actualRetireesPurchases.size(), is(15)); + } + @Test public void testWhereWithArgsQueryConditionValues() { Namespace bannersNs = db.openNamespace("banners", NamespaceOptions.defaultOptions(), Banner.class); @@ -143,6 +176,52 @@ public void testWhereWithArgsQueryConditionValues() { assertThat(bannerNotExistsIterator.hasNext(), is(false)); } + @Test + public void testWhereWithArgsQueryConditionValuesAndNoAgg() { + Namespace bannersNs = db.openNamespace("banners", NamespaceOptions.defaultOptions(), Banner.class); + Namespace purchasesNs = db.openNamespace("purchases", NamespaceOptions.defaultOptions(), Purchase.class); + bannersNs.insert(new Banner(1, "Banner")); + int purchaseId = 0; + // 24 persons, everyone has from 0 to 3 purchases, for a total of 36 purchases. + for (int i = 0; i < 24; i++) { + for (int j = 0; j < i % 4; j++) { + int price = (j + 1) * 10; + purchasesNs.insert(new Purchase(purchaseId++, i, price, "Asset" + j)); + } + } + + int personId = 14; + int personPurchasesCnt = 2; + // prices of person14 purchases: 10, 20 + + // select * from banners b + // where b.id = 1 and (select p.price from purchases p where p.person_id = 14) = 20 + Query subQuery = purchasesNs.query() + .select("price") + .where("person_id", EQ, personId); + Query bannerExistsOnEqQuery = bannersNs.query() + .where("id", EQ, 1) + .where(subQuery, EQ, 20); + ResultIterator bannerExistsOnEqIterator = bannerExistsOnEqQuery.execute(); + assertThat(bannerExistsOnEqIterator.hasNext(), is(true)); + + // select * from banners b + // where b.id = 1 and (select p.price from purchases p where p.person_id = 14) >= 20 + Query bannerExistsOnGeQuery = bannersNs.query() + .where("id", EQ, 1) + .where(subQuery, GE, 20); + ResultIterator bannerExistsOnGeIterator = bannerExistsOnGeQuery.execute(); + assertThat(bannerExistsOnGeIterator.hasNext(), is(true)); + + // select * from banners b + // where b.id = 1 and (select p.price from purchases p where p.person_id = 14) > 20 + Query bannerNotExistsQuery = bannersNs.query() + .where("id", EQ, 1) + .where(subQuery, GT, 20); + ResultIterator bannerNotExistsIterator = bannerNotExistsQuery.execute(); + assertThat(bannerNotExistsIterator.hasNext(), is(false)); + } + public static class Person { @Reindex(name = "id", isPrimaryKey = true) private int id;