use of com.mysql.cj.xdevapi.Row in project aws-mysql-jdbc by awslabs.
the class CollectionFindTest method testCollectionFindInUpdate.
@Test
public void testCollectionFindInUpdate() throws Exception {
assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
int i = 0, maxrec = 10;
DbDoc doc = null;
DocResult docs = null;
/* add(DbDoc[] docs) */
DbDoc[] jsonlist = new DbDocImpl[maxrec];
for (i = 0; i < maxrec; i++) {
DbDoc newDoc2 = new DbDocImpl();
newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1000)));
newDoc2.add("F1", new JsonString().setValue("Field-1-Data-" + i));
newDoc2.add("F2", new JsonNumber().setValue(String.valueOf(10 * (i + 1) + 0.1234)));
newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(i + 1)));
newDoc2.add("F4", new JsonNumber().setValue(String.valueOf(10000 - i)));
jsonlist[i] = newDoc2;
newDoc2 = null;
}
this.collection.add(jsonlist).execute();
assertEquals((maxrec), this.collection.count());
/* find without Condition */
docs = this.collection.find().fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3,$.F2/10 as tmp1,1/2 as tmp2").orderBy("$.F3").execute();
i = 0;
while (docs.hasNext()) {
doc = docs.next();
i++;
}
assertEquals((maxrec), i);
/* modify with single IN */
Result res = this.collection.modify("'1001' in $._id").set("$.F1", "Data_New").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("'1001' in $._id").execute();
doc = docs.next();
assertEquals("Data_New", (((JsonString) doc.get("F1")).getString()));
assertFalse(docs.hasNext());
/* find with = Condition and fetchAll() keyword */
String findCond = "";
for (i = 1; i < maxrec; i++) {
findCond = findCond + "'";
findCond = findCond + String.valueOf(i + 1000) + "' not in $._id";
if (i != maxrec - 1) {
findCond = findCond + " and ";
}
}
/* modify with multiple IN */
res = this.collection.modify(findCond).set("$.F1", "Data_New_1").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find(findCond).execute();
doc = docs.next();
assertEquals("Data_New_1", (((JsonString) doc.get("F1")).getString()));
assertFalse(docs.hasNext());
/* modify with single IN and sort */
res = this.collection.modify("10000 in $.F4").set("$.F1", "Data_New_2").sort("CAST($.F4 as SIGNED)").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("10000 in $.F4").orderBy("CAST($.F4 as SIGNED)").execute();
doc = docs.next();
assertEquals("Data_New_2", (((JsonString) doc.get("F1")).getString()));
assertFalse(docs.hasNext());
/* modify with single IN and sort */
res = this.collection.modify("20.1234 in $.F2").set("$.F1", "Data_New_3").sort("CAST($.F4 as SIGNED)").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("20.1234 in $.F2").orderBy("CAST($.F4 as SIGNED)").execute();
doc = docs.next();
assertEquals("Data_New_3", (((JsonString) doc.get("F1")).getString()));
assertFalse(docs.hasNext());
Table table = this.schema.getCollectionAsTable(this.collectionName);
/* update with single IN */
String toUpdate = String.format("JSON_REPLACE(doc, \"$.F1\", \"Data_New_4\")");
res = table.update().set("doc", expr(toUpdate)).where("'1001' in doc->$._id").execute();
assertEquals(res.getAffectedItemsCount(), 1);
RowResult rows = table.select("doc->$.F1 as F1").where("'1001' in doc->$._id").execute();
Row r = rows.next();
assertEquals(r.getString("F1"), "\"Data_New_4\"");
assertFalse(rows.hasNext());
findCond = "";
for (i = 1; i < maxrec; i++) {
findCond = findCond + "'";
findCond = findCond + String.valueOf(i + 1000) + "' not in doc->$._id";
if (i != maxrec - 1) {
findCond = findCond + " and ";
}
}
/* update with multiple IN */
toUpdate = String.format("JSON_REPLACE(doc, \"$.F1\", \"Data_New_5\")");
res = table.update().set("doc", expr(toUpdate)).where(findCond).execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as F1").where(findCond).execute();
r = rows.next();
assertEquals(r.getString("F1"), "\"Data_New_5\"");
assertFalse(rows.hasNext());
/* update with single IN for float */
toUpdate = String.format("JSON_REPLACE(doc, \"$.F1\", \"Data_New_6\")");
res = table.update().set("doc", expr(toUpdate)).where("20.1234 in doc->$.F2").execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as F1").where("20.1234 in doc->$.F2").execute();
r = rows.next();
assertEquals(r.getString("F1"), "\"Data_New_6\"");
assertFalse(rows.hasNext());
/* update with single IN for int */
toUpdate = String.format("JSON_REPLACE(doc, \"$.F1\", \"Data_New_7\")");
res = table.update().set("doc", expr(toUpdate)).where("10000 in doc->$.F4").execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as F1").where("10000 in doc->$.F4").execute();
r = rows.next();
assertEquals(r.getString("F1"), "\"Data_New_7\"");
assertFalse(rows.hasNext());
}
use of com.mysql.cj.xdevapi.Row in project aws-mysql-jdbc by awslabs.
the class CollectionFindTest method testCollModifyTabUpdateWithOverlaps.
@Test
public void testCollModifyTabUpdateWithOverlaps() throws Exception {
assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
int i = 0, maxrec = 10;
DbDoc doc = null;
DocResult docs = null;
Result res = null;
/* add(DbDoc[] docs) */
DbDoc[] jsonlist = new DbDocImpl[maxrec];
for (i = 0; i < maxrec; i++) {
DbDoc newDoc2 = new DbDocImpl();
newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1000)));
newDoc2.add("F1", new JsonString().setValue("Field-1-Data-" + i));
newDoc2.add("F2", new JsonNumber().setValue(String.valueOf(10 * (i + 1) + 0.1234)));
newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(i + 1)));
newDoc2.add("F4", new JsonNumber().setValue(String.valueOf(10000 - i)));
jsonlist[i] = newDoc2;
newDoc2 = null;
}
this.collection.add(jsonlist).execute();
assertEquals((maxrec), this.collection.count());
/* find without Condition */
docs = this.collection.find().fields("$._id as _id, $.F1 as f1, $.F2 as f2, $.F3 as f3,$.F2/10 as tmp1,1/2 as tmp2").orderBy("$.F3").execute();
i = 0;
while (docs.hasNext()) {
doc = docs.next();
i++;
}
assertEquals((maxrec), i);
/* modify with single OVERLAPS */
res = this.collection.modify("'1001' overlaps $._id").set("$.F1", "Data_New").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("'1001' overlaps $._id").execute();
doc = docs.next();
assertEquals("Data_New", (((JsonString) doc.get("F1")).getString()));
assertFalse(docs.hasNext());
/* find with = Condition and fetchAll() keyword */
String findCond = "";
for (i = 1; i < maxrec; i++) {
findCond = findCond + "'";
findCond = findCond + String.valueOf(i + 1000) + "' not overlaps $._id";
if (i != maxrec - 1) {
findCond = findCond + " and ";
}
}
/* modify with multiple overlaps */
res = this.collection.modify(findCond).set("$.F1", "Data_New_1").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find(findCond).execute();
doc = docs.next();
assertEquals("Data_New_1", (((JsonString) doc.get("F1")).getString()));
assertFalse(docs.hasNext());
/* modify with single overlaps and sort */
res = this.collection.modify("10000 overlaps $.F4").set("$.F1", "Data_New_2").sort("CAST($.F4 as SIGNED)").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("10000 overlaps $.F4").orderBy("CAST($.F4 as SIGNED)").execute();
doc = docs.next();
assertEquals("Data_New_2", (((JsonString) doc.get("F1")).getString()));
assertFalse(docs.hasNext());
/* modify with single overlaps and sort */
res = this.collection.modify("20.1234 overlaps $.F2").set("$.F1", "Data_New_3").sort("CAST($.F4 as SIGNED)").execute();
assertEquals(1, res.getAffectedItemsCount());
docs = this.collection.find("20.1234 overlaps $.F2").orderBy("CAST($.F4 as SIGNED)").execute();
doc = docs.next();
assertEquals("Data_New_3", (((JsonString) doc.get("F1")).getString()));
assertFalse(docs.hasNext());
Table table = this.schema.getCollectionAsTable(this.collectionName);
/* update with single OVERLAPS */
String toUpdate = String.format("JSON_REPLACE(doc, \"$.F1\", \"Data_New_4\")");
res = table.update().set("doc", expr(toUpdate)).where("'1001' overlaps doc->$._id").execute();
assertEquals(res.getAffectedItemsCount(), 1);
RowResult rows = table.select("doc->$.F1 as F1").where("'1001' overlaps doc->$._id").execute();
Row r = rows.next();
assertEquals(r.getString("F1"), "\"Data_New_4\"");
assertFalse(rows.hasNext());
findCond = "";
for (i = 1; i < maxrec; i++) {
findCond = findCond + "'";
findCond = findCond + String.valueOf(i + 1000) + "' not overlaps doc->$._id";
if (i != maxrec - 1) {
findCond = findCond + " and ";
}
}
/* update with multiple OVERLAPS */
toUpdate = String.format("JSON_REPLACE(doc, \"$.F1\", \"Data_New_5\")");
res = table.update().set("doc", expr(toUpdate)).where(findCond).execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as F1").where(findCond).execute();
r = rows.next();
assertEquals(r.getString("F1"), "\"Data_New_5\"");
assertFalse(rows.hasNext());
/* update with single OVERLAPS for float */
toUpdate = String.format("JSON_REPLACE(doc, \"$.F1\", \"Data_New_6\")");
res = table.update().set("doc", expr(toUpdate)).where("20.1234 overlaps doc->$.F2").execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as F1").where("20.1234 overlaps doc->$.F2").execute();
r = rows.next();
assertEquals(r.getString("F1"), "\"Data_New_6\"");
assertFalse(rows.hasNext());
/* update with single OVERLAPS for int */
toUpdate = String.format("JSON_REPLACE(doc, \"$.F1\", \"Data_New_7\")");
res = table.update().set("doc", expr(toUpdate)).where("10000 overlaps doc->$.F4").execute();
assertEquals(res.getAffectedItemsCount(), 1);
rows = table.select("doc->$.F1 as F1").where("10000 overlaps doc->$.F4").execute();
r = rows.next();
assertEquals(r.getString("F1"), "\"Data_New_7\"");
assertFalse(rows.hasNext());
}
use of com.mysql.cj.xdevapi.Row in project aws-mysql-jdbc by awslabs.
the class CollectionFindTest method testCollectionFindStress_003.
/* Large Data */
@Test
public // @Ignore("Wait for 1M Data issue Fix in Plugin")
void testCollectionFindStress_003() throws Exception {
int i = 0, maxrec = 5;
int maxLen = 1024 * 1024 + 4;
SqlResult res1 = null;
Session tmpSess = null;
Row r = null;
int defPackLen = 0;
int defXPackLen = 0;
try {
tmpSess = new SessionFactory().getSession(this.baseUrl);
res1 = tmpSess.sql("show variables like 'mysqlx_max_allowed_packet'").execute();
r = res1.next();
defXPackLen = Integer.parseInt(r.getString("Value"));
res1 = tmpSess.sql("show variables like 'max_allowed_packet'").execute();
r = res1.next();
defPackLen = Integer.parseInt(r.getString("Value"));
tmpSess.sql("set Global mysqlx_max_allowed_packet=128*1024*1024 ").execute();
tmpSess.sql("set Global max_allowed_packet=128*1024*1024 ").execute();
((SessionImpl) this.session).getSession().getProtocol().setMaxAllowedPacket(128 * 1024 * 1024);
String s1 = "";
/* maxLen Data length */
s1 = buildString(maxLen + 1, 'q');
for (i = 0; i < maxrec; i++) {
DbDoc newDoc2 = new DbDocImpl();
newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1 + 1000)));
newDoc2.add("F1", new JsonString().setValue(s1 + i));
newDoc2.add("F2", new JsonString().setValue(s1 + i));
this.collection.add(newDoc2).execute();
newDoc2 = null;
}
DocResult docs0 = this.collection.find("$._id= '1001'").fields("$._id as _id, $.F1 as f1, $.F2 as f2").execute();
DbDoc doc0 = docs0.next();
assertEquals(String.valueOf(1 + 1000), (((JsonString) doc0.get("_id")).getString()));
} finally {
if (tmpSess != null) {
tmpSess.sql("set Global mysqlx_max_allowed_packet=" + defXPackLen).execute();
tmpSess.sql("set Global max_allowed_packet=" + defPackLen).execute();
tmpSess.close();
}
}
}
use of com.mysql.cj.xdevapi.Row in project aws-mysql-jdbc by awslabs.
the class SessionTest method testSessionAttributes_checkSession.
private void testSessionAttributes_checkSession(Session s, Map<String, String> userAttributes) {
Map<String, Integer> matchedCounts = new HashMap<>();
Map<String, String> matchValues = new HashMap<>();
matchValues.put("_platform", Constants.OS_ARCH);
matchValues.put("_os", Constants.OS_NAME + "-" + Constants.OS_VERSION);
matchValues.put("_client_name", Constants.CJ_NAME);
matchValues.put("_client_version", Constants.CJ_VERSION);
matchValues.put("_client_license", Constants.CJ_LICENSE);
matchValues.put("_runtime_version", Constants.JVM_VERSION);
matchValues.put("_runtime_vendor", Constants.JVM_VENDOR);
matchValues.putAll(userAttributes);
SqlResult res = s.sql("SELECT * FROM performance_schema.session_connect_attrs WHERE processlist_id = CONNECTION_ID()").execute();
while (res.hasNext()) {
Row r = res.next();
String key = r.getString(1);
String val = r.getString(2);
assertTrue(matchValues.containsKey(key), "Unexpected connection attribute key: " + key);
Integer cnt = matchedCounts.get(key);
matchedCounts.put(key, cnt == null ? 1 : cnt++);
// when client sends an empty string as an attribute value the NULL value is stored to performance_schema.session_connect_attrs
String expected = matchValues.get(key);
if (expected.equals("")) {
expected = null;
}
assertEquals(expected, val);
}
for (String key : matchValues.keySet()) {
assertTrue(matchedCounts.containsKey(key), "Incorrect number of entries for key \"" + key + "\": 0");
assertTrue(matchedCounts.get(key) == 1, "Incorrect number of entries for key \"" + key + "\": " + matchedCounts.get(key));
}
}
use of com.mysql.cj.xdevapi.Row in project aws-mysql-jdbc by awslabs.
the class SessionTest method testExecAsyncNegative.
/**
* Few Negative Scenarios
*
* @throws Exception
*/
@Test
public void testExecAsyncNegative() throws Exception {
int i = 0;
SqlResult sqlRes = null;
Row r = null;
try {
assertThrows(ExecutionException.class, ".*Unknown table '" + this.schema.getName() + ".non_existing'.*", () -> {
CompletableFuture<SqlResult> res = this.session.sql("drop table non_existing").executeAsync();
res.get();
return null;
});
assertThrows(ExecutionException.class, ".* BIGINT value is out of range .*", () -> {
CompletableFuture<SqlResult> res = this.session.sql("select 123456*123456722323289").executeAsync();
res.get();
return null;
});
sqlUpdate("drop table if exists testExecAsyncNegative");
sqlUpdate("create table testExecAsyncNegative(a int,b bigint ,c bigint GENERATED ALWAYS AS (b*1000) VIRTUAL COMMENT '1',d bigint GENERATED ALWAYS AS (c*100000) STOred COMMENT '2')");
sqlUpdate("Insert into testExecAsyncNegative (a,b) values(1,100)");
sqlUpdate("create index id on testExecAsyncNegative(d)");
sqlUpdate("create unique index id2 on testExecAsyncNegative(a)");
int NUMBER_OF_QUERIES = 5000;
List<CompletableFuture<SqlResult>> futures = new ArrayList<>();
for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
if (i % 6 == 0) {
futures.add(this.session.sql("replace into testExecAsyncNegative (a,b) values(?,?)").bind(1).bind(1555666000000L).executeAsync());
} else if (i % 6 == 1) {
futures.add(this.session.sql("insert into testExecAsyncNegative (a,b) values (?,?) ON DUPLICATE KEY UPDATE b= ?").bind(1).bind(2).bind(1555666009990L).executeAsync());
} else if (i % 6 == 2) {
futures.add(this.session.sql("alter table testExecAsyncNegative add d point").executeAsync());
} else if (i % 6 == 3) {
futures.add(this.session.sql("insert into testExecAsyncNegative (a,b) values (?,?) ON DUPLICATE KEY UPDATE b=b/?").bind(1).bind(2).bind(0).executeAsync());
} else if (i % 6 == 4) {
futures.add(this.session.sql("SELECT /*+ max_execution_time (100) bad_hint */ SLEEP(0.5)").executeAsync());
} else {
futures.add(this.session.sql("select /*+*/ * from testExecAsyncNegative").executeAsync());
}
}
for (i = 0; i < NUMBER_OF_QUERIES; ++i) {
int i1 = i;
if (i % 6 == 0) {
assertThrows(ExecutionException.class, ".* BIGINT value is out of range .*", () -> futures.get(i1).get());
} else if (i % 6 == 1) {
assertThrows(ExecutionException.class, ".* BIGINT value is out of range .*", () -> futures.get(i1).get());
} else if (i % 6 == 2) {
assertThrows(ExecutionException.class, ".*Duplicate column name 'd'.*", () -> futures.get(i1).get());
} else if (i % 6 == 3) {
assertThrows(ExecutionException.class, ".*Division by 0.*", () -> futures.get(i1).get());
} else {
sqlRes = futures.get(i).get();
r = sqlRes.next();
assertEquals(1, r.getInt(0));
assertFalse(sqlRes.hasNext());
Iterator<Warning> w = sqlRes.getWarnings();
while (w.hasNext()) {
Warning element = w.next();
assertTrue(element.getMessage().contains("Optimizer hint syntax error"));
}
}
}
} finally {
sqlUpdate("drop table if exists testExecAsyncNegative");
}
}
Aggregations