use of com.alibaba.cobar.route.util.PermutationUtil.PermutationGenerator in project cobar by alibaba.
the class ServerRouteTest method testDuplicatePartitionKey.
public void testDuplicatePartitionKey() throws Exception {
String sql = "select * from offer.wp_image where member_id in ('pavarotti17', 'qaa') or offer.wp_image.member_id='1qq' or member_id='1qq'";
SchemaConfig schema = schemaMap.get("cndb");
RouteResultset rrs = ServerRouter.route(schema, sql, null, null);
Assert.assertEquals(-1l, rrs.getLimitSize());
Map<String, RouteResultsetNode> nodeMap = getNodeMap(rrs, 3);
NodeNameAsserter nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]", "offer_dn[66]");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
SimpleSQLAsserter sqlAsserter = new SimpleSQLAsserter();
sqlAsserter.addExpectSQL(0, "SELECT * FROM wp_image WHERE member_id IN ('pavarotti17') OR FALSE OR FALSE").addExpectSQL(1, "SELECT * FROM wp_image WHERE member_id IN ('qaa') OR FALSE OR FALSE").addExpectSQL(2, "SELECT * FROM wp_image WHERE FALSE OR wp_image.member_id = '1qq' OR member_id = '1qq'");
RouteNodeAsserter asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
for (RouteResultsetNode node : nodeMap.values()) {
asserter.assertNode(node);
}
sql = "insert into wp_image (id, member_id, gmt) values (1,'pavarotti17',now()),(2,'pavarotti17',now()),(3,'qaa',now())";
schema = schemaMap.get("cndb");
rrs = ServerRouter.route(schema, sql, null, null);
Assert.assertEquals(-1l, rrs.getLimitSize());
nodeMap = getNodeMap(rrs, 2);
nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
sqlAsserter = new SimpleSQLAsserter();
sqlAsserter.addExpectSQL(0, "INSERT INTO wp_image (id, member_id, gmt) VALUES (2, 'pavarotti17', NOW()), (1, 'pavarotti17', NOW())", "INSERT INTO wp_image (id, member_id, gmt) VALUES (1, 'pavarotti17', NOW()), (2, 'pavarotti17', NOW())").addExpectSQL(1, "INSERT INTO wp_image (id, member_id, gmt) VALUES (3, 'qaa', NOW())");
asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
for (RouteResultsetNode node : nodeMap.values()) {
asserter.assertNode(node);
}
sql = "select * from offer.wp_image where member_id in ('pavarotti17','pavarotti17', 'qaa') or offer.wp_image.member_id='pavarotti17'";
schema = schemaMap.get("cndb");
rrs = ServerRouter.route(schema, sql, null, null);
Assert.assertEquals(-1l, rrs.getLimitSize());
Assert.assertEquals(2, rrs.getNodes().length);
nodeMap = getNodeMap(rrs, 2);
nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
sqlAsserter = new SimpleSQLAsserter();
sqlAsserter.addExpectSQL(0, "SELECT * FROM wp_image WHERE member_id IN ('pavarotti17', 'pavarotti17') OR wp_image.member_id = 'pavarotti17'").addExpectSQL(1, "SELECT * FROM wp_image WHERE member_id IN ('qaa') OR FALSE");
asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
for (RouteResultsetNode node : nodeMap.values()) {
asserter.assertNode(node);
}
sql = "select * from offer.`wp_image` where `member_id` in ('pavarotti17','pavarotti17', 'qaa') or member_id in ('pavarotti17','1qq','pavarotti17') or offer.wp_image.member_id='pavarotti17'";
schema = schemaMap.get("cndb");
rrs = ServerRouter.route(schema, sql, null, null);
Assert.assertEquals(-1l, rrs.getLimitSize());
Assert.assertEquals(3, rrs.getNodes().length);
nodeMap = getNodeMap(rrs, 3);
nameAsserter = new NodeNameAsserter("offer_dn[123]", "offer_dn[10]", "offer_dn[66]");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
sqlAsserter = new SimpleSQLAsserter();
sqlAsserter.addExpectSQL(0, "SELECT * FROM `wp_image` WHERE `member_id` IN ('pavarotti17', 'pavarotti17') OR member_id IN ('pavarotti17', 'pavarotti17') OR wp_image.member_id = 'pavarotti17'").addExpectSQL(1, "SELECT * FROM `wp_image` WHERE `member_id` IN ('qaa') OR FALSE OR FALSE").addExpectSQL(2, "SELECT * FROM `wp_image` WHERE FALSE OR member_id IN ('1qq') OR FALSE");
asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
for (RouteResultsetNode node : nodeMap.values()) {
asserter.assertNode(node);
}
sql = "insert into offer_detail (offer_id, gmt) values (123,now()),(123,now()+1),(234,now()),(123,now()),(345,now()),(122+1,now()),(456,now())";
schema = schemaMap.get("cndb");
rrs = ServerRouter.route(schema, sql, null, null);
Assert.assertEquals(-1l, rrs.getLimitSize());
nodeMap = getNodeMap(rrs, 4);
nameAsserter = new NodeNameAsserter("detail_dn[29]", "detail_dn[43]", "detail_dn[57]", "detail_dn[15]");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
sqlAsserter = new SimpleSQLAsserter();
sqlAsserter.addExpectSQL(0, "INSERT INTO offer_detail (offer_id, gmt) VALUES (234, NOW())").addExpectSQL(1, "INSERT INTO offer_detail (offer_id, gmt) VALUES (345, NOW())").addExpectSQL(2, "INSERT INTO offer_detail (offer_id, gmt) VALUES (456, NOW())").addExpectSQL(3, "INSERT INTO offer_detail (offer_id, gmt) VALUES ", new PermutationGenerator("(123, NOW())", "(123, NOW() + 1)", "(122 + 1, NOW())", "(123, NOW())").setDelimiter(", "), "");
asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
for (RouteResultsetNode node : nodeMap.values()) {
asserter.assertNode(node);
}
sql = "insert into offer (offer_id, group_id, gmt) values " + "(123, 123, now()),(123, 234, now()),(123, 345, now()),(123, 456, now())" + ",(234, 123, now()),(234, 234, now()),(234, 345, now()),(234, 456, now())" + ",(345, 123, now()),(345, 234, now()),(345, 345, now()),(345, 456, now())" + ",(456, 123, now()),(456, 234, now()),(456, 345, now()),(456, 456, now())";
schema = schemaMap.get("cndb");
rrs = ServerRouter.route(schema, sql, null, null);
Assert.assertEquals(-1l, rrs.getLimitSize());
nodeMap = getNodeMap(rrs, 7);
nameAsserter = new NodeNameAsserter("offer_dn[58]", "offer_dn[100]", "offer_dn[86]", "offer_dn[72]", "offer_dn[114]", "offer_dn[44]", "offer_dn[30]");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
sqlAsserter = new SimpleSQLAsserter();
sqlAsserter.addExpectSQL(0, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(345, 123, NOW())", "(123, 345, NOW())", "(234, 234, NOW())").setDelimiter(", "), "").addExpectSQL(1, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(345, 456, NOW())", "(456, 345, NOW())").setDelimiter(", "), "").addExpectSQL(2, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(456, 234, NOW())", "(234, 456, NOW())", "(345, 345, NOW())").setDelimiter(", "), "").addExpectSQL(3, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(123, 456, NOW())", "(345, 234, NOW())", "(234, 345, NOW())", "(456, 123, NOW())").setDelimiter(", "), "").addExpectSQL(4, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(456, 456, NOW())").setDelimiter(", "), "").addExpectSQL(5, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(234, 123, NOW())", "(123, 234, NOW())").setDelimiter(", "), "").addExpectSQL(6, "INSERT INTO offer (offer_id, group_id, gmt) VALUES ", new PermutationGenerator("(123, 123, NOW())").setDelimiter(", "), "");
asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
for (RouteResultsetNode node : nodeMap.values()) {
asserter.assertNode(node);
}
sql = "select * from offer where (offer_id, group_id ) = (123,234)";
schema = schemaMap.get("cndb");
rrs = ServerRouter.route(schema, sql, null, null);
Assert.assertEquals(-1l, rrs.getLimitSize());
Assert.assertEquals(128, rrs.getNodes().length);
for (int i = 0; i < 128; i++) {
Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[i].getReplicaIndex());
Assert.assertEquals("offer_dn[" + i + "]", rrs.getNodes()[i].getName());
Assert.assertEquals("select * from offer where (offer_id, group_id ) = (123,234)", rrs.getNodes()[i].getStatement());
}
sql = "select * from offer where offer_id=123 and group_id=234";
schema = schemaMap.get("cndb");
rrs = ServerRouter.route(schema, sql, null, null);
Assert.assertEquals(-1l, rrs.getLimitSize());
Assert.assertEquals(1, rrs.getNodes().length);
Assert.assertEquals((int) RouteResultsetNode.DEFAULT_REPLICA_INDEX, rrs.getNodes()[0].getReplicaIndex());
Assert.assertEquals("offer_dn[44]", rrs.getNodes()[0].getName());
Assert.assertEquals("select * from offer where offer_id=123 and group_id=234", rrs.getNodes()[0].getStatement());
// WITHOUT SQL CHANGE unless schema is appeared
sql = "select * from cndb.offer where false" + " or offer_id=123 and group_id=123 or offer_id=123 and group_id=234 or offer_id=123 and group_id=345 or offer_id=123 and group_id=456 " + " or offer_id=234 and group_id=123 or offer_id=234 and group_id=234 or offer_id=234 and group_id=345 or offer_id=234 and group_id=456 " + " or offer_id=345 and group_id=123 or offer_id=345 and group_id=234 or offer_id=345 and group_id=345 or offer_id=345 and group_id=456 " + " or offer_id=456 and group_id=123 or offer_id=456 and group_id=234 or offer_id=456 and group_id=345 or offer_id=456 and group_id=456 ";
schema = schemaMap.get("cndb");
rrs = ServerRouter.route(schema, sql, null, null);
Assert.assertEquals(-1l, rrs.getLimitSize());
String sqlTemp = "SELECT * FROM offer WHERE FALSE OR offer_id = 123 AND group_id = 123 OR offer_id = 123 AND group_id = 234 OR offer_id = 123 AND group_id = 345 OR offer_id = 123 AND group_id = 456 OR offer_id = 234 AND group_id = 123 OR offer_id = 234 AND group_id = 234 OR offer_id = 234 AND group_id = 345 OR offer_id = 234 AND group_id = 456 OR offer_id = 345 AND group_id = 123 OR offer_id = 345 AND group_id = 234 OR offer_id = 345 AND group_id = 345 OR offer_id = 345 AND group_id = 456 OR offer_id = 456 AND group_id = 123 OR offer_id = 456 AND group_id = 234 OR offer_id = 456 AND group_id = 345 OR offer_id = 456 AND group_id = 456";
nodeMap = getNodeMap(rrs, 7);
nameAsserter = new NodeNameAsserter("offer_dn[58]", "offer_dn[100]", "offer_dn[86]", "offer_dn[72]", "offer_dn[114]", "offer_dn[44]", "offer_dn[30]");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
sqlAsserter = new SimpleSQLAsserter();
sqlAsserter.addExpectSQL(0, sqlTemp).addExpectSQL(1, sqlTemp).addExpectSQL(2, sqlTemp).addExpectSQL(3, sqlTemp).addExpectSQL(4, sqlTemp).addExpectSQL(5, sqlTemp).addExpectSQL(6, sqlTemp);
asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
for (RouteResultsetNode node : nodeMap.values()) {
asserter.assertNode(node);
}
sql = "select * from offer where false" + " or offer_id=123 and group_id=123" + " or group_id=123 and offer_id=234" + " or offer_id=123 and group_id=345" + " or offer_id=123 and group_id=456 ";
schema = schemaMap.get("cndb");
rrs = ServerRouter.route(schema, sql, null, null);
Assert.assertEquals(-1l, rrs.getLimitSize());
sqlTemp = "select * from offer where false or offer_id=123 and group_id=123 or group_id=123 and offer_id=234 or offer_id=123 and group_id=345 or offer_id=123 and group_id=456 ";
nodeMap = getNodeMap(rrs, 4);
nameAsserter = new NodeNameAsserter("offer_dn[72]", "offer_dn[58]", "offer_dn[44]", "offer_dn[30]");
nameAsserter.assertRouteNodeNames(nodeMap.keySet());
sqlAsserter = new SimpleSQLAsserter();
sqlAsserter.addExpectSQL(0, sqlTemp).addExpectSQL(1, sqlTemp).addExpectSQL(2, sqlTemp).addExpectSQL(3, sqlTemp);
asserter = new RouteNodeAsserter(nameAsserter, sqlAsserter);
for (RouteResultsetNode node : nodeMap.values()) {
asserter.assertNode(node);
}
}
Aggregations