diff --git a/src/main/java/com/jd/jdbc/planbuilder/PlanBuilder.java b/src/main/java/com/jd/jdbc/planbuilder/PlanBuilder.java index fbdda38..22f7a3c 100644 --- a/src/main/java/com/jd/jdbc/planbuilder/PlanBuilder.java +++ b/src/main/java/com/jd/jdbc/planbuilder/PlanBuilder.java @@ -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()); diff --git a/src/main/java/com/jd/jdbc/planbuilder/UpdatePlan.java b/src/main/java/com/jd/jdbc/planbuilder/UpdatePlan.java index ad98156..053cf81 100644 --- a/src/main/java/com/jd/jdbc/planbuilder/UpdatePlan.java +++ b/src/main/java/com/jd/jdbc/planbuilder/UpdatePlan.java @@ -142,7 +142,7 @@ private static void buildChangedTindexesVlaues(SQLUpdateStatement update, LogicT *
* 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()) { diff --git a/src/test/java/com/jd/jdbc/engine/destination/DestinationTest.java b/src/test/java/com/jd/jdbc/engine/destination/DestinationTest.java index 25b8864..6ea7b9c 100644 --- a/src/test/java/com/jd/jdbc/engine/destination/DestinationTest.java +++ b/src/test/java/com/jd/jdbc/engine/destination/DestinationTest.java @@ -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"); diff --git a/src/test/resources/plan/destination_case.txt b/src/test/resources/plan/destination_case.txt index fcbe3f7..9cd43f0 100644 --- a/src/test/resources/plan/destination_case.txt +++ b/src/test/resources/plan/destination_case.txt @@ -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" { @@ -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"