Skip to content

Commit

Permalink
In the scenario of intercepting a specified shard destination, an upd…
Browse files Browse the repository at this point in the history
…ate statement with a sharding key SQL
  • Loading branch information
wangweicugw committed Sep 22, 2023
1 parent 260a98a commit eaf6ef8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
9 changes: 8 additions & 1 deletion src/main/java/com/jd/jdbc/planbuilder/PlanBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,17 @@ private static PrimitiveEngine createInstructionFor(final SQLStatement stmt, fin
}

private static PrimitiveEngine buildPlanForBypass(final SQLStatement stmt, final VSchemaManager vm, final String defaultKeyspace, final Destination destination)
throws SQLFeatureNotSupportedException {
throws SQLException {
if (stmt instanceof MySqlInsertReplaceStatement) {
throw new SQLFeatureNotSupportedException("insert statement does not support execute by destination");
}
if (stmt instanceof SQLUpdateStatement) {
SQLTableSource tableSource = ((SQLUpdateStatement) stmt).getTableSource();
if (!(tableSource instanceof SQLExprTableSource)) {
throw new SQLFeatureNotSupportedException("unsupported: multi-table update");
}
UpdatePlan.buildChangedVindexesValues((SQLUpdateStatement) stmt, vm.getTable(defaultKeyspace, TableNameUtils.getTableSimpleName((SQLExprTableSource) tableSource)), null);
}

Vschema.Keyspace ks = vm.getKeyspace(defaultKeyspace);
VKeyspace keyspace = new VKeyspace(defaultKeyspace, ks.getSharded());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jd/jdbc/planbuilder/UpdatePlan.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private static void buildChangedTindexesVlaues(SQLUpdateStatement update, LogicT
* <p>
* planbuilder/update.go --> buildChangedVindexesValues
*/
private static void buildChangedVindexesValues(SQLUpdateStatement update, Vschema.Table table, String ksidCol) throws SQLException {
public static void buildChangedVindexesValues(SQLUpdateStatement update, Vschema.Table table, String ksidCol) throws SQLException {
/*changedVindexes := make(map[string]*engine.VindexValues)
buf, offset := initialQuery(ksidCol, table)*/
for (Vschema.ColumnVindex vindex : table.getColumnVindexesList()) {
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/jd/jdbc/engine/destination/DestinationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ public void insert() throws SQLException {
}
}

@Test
public void updateWithVindex() throws SQLException {
thrown.expect(SQLFeatureNotSupportedException.class);
thrown.expectMessage("unsupported: You can't update primary vindex columns.");
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate("/*shard=80-, set for specific shard*/update engine_test set f_key = '1' where f_key = '11'");
}
}

@Test
public void updateWithjoin() throws SQLException {
thrown.expect(SQLFeatureNotSupportedException.class);
thrown.expectMessage("unsupported: multi-table update");
try (Statement stmt = conn.createStatement()) {
stmt.executeUpdate("/*shard=80-*/update t_users t1, t_users2 t2 set t2.age=t1.age+1 where t1.id=t2.id");
}
}

@Test
public void select() throws IOException, SQLException {
this.execute(conn, "src/test/resources/engine/destination/select_cases.json");
Expand Down
27 changes: 10 additions & 17 deletions src/test/resources/plan/destination_case.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,7 @@

# update
"/*shard=80-, set for specific shard*/update music set user_id = 11 where id = 11 order by id asc limit 1"
{
"QueryType": "UPDATE",
"Original": "/*shard=80-, set for specific shard*/update music set user_id = 11 where id = 11 order by id asc limit 1",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"TargetDestination": "80-",
"SingleShardOnly": true,
"ShardNameNeeded": true,
"MultiShardAutocommit": true,
"IsDML": true,
"Query": "update music set user_id = 11 where id = 11 order by id asc limit 1"
}
}
"unsupported: You can't update primary vindex columns. Invalid update on vindex: user_id

"/*shard=-80, set for specific shard*/DELETE FROM USER WHERE ID = 42"
{
Expand Down Expand Up @@ -226,5 +210,14 @@
}
}

# update
"/*shard=80-*/update user t1, user_metadata t2 set t2.email = t1.costly where t1.id = t2.user_id"
"unsupported: multi-table update"

# update
"/*shard=80-*/update user t1 join user_metadata t2 on t1.id = t2.user_id set t2.email = t1.costly"
"unsupported: multi-table update"

# update
"/*shard=80-*/update user t1 left join user_metadata t2 on t1.id = t2.user_id set t2.email = t1.costly"
"unsupported: multi-table update"

0 comments on commit eaf6ef8

Please sign in to comment.