Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support mysql update join sql #4914

Merged

Conversation

renliangyu857
Copy link
Contributor

  • I have registered the PR changes.

Ⅰ. Describe what this PR did

support mysql update join sql

Ⅱ. Does this pull request fix one issue?

fixes #4118
fixes #4890

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

MysqlUpdateJoinTest有测试用例

Ⅴ. Special notes for reviews

@renliangyu857
Copy link
Contributor Author

支持语句:update employees inner join merit on employees.performance = merit.performance set salary = salary + salary * count
实现思路 拆分为多张表,分别构造这些表的前后置镜像查询语句,主要考虑查哪些表、查哪些字段、如何查
1)查哪些表 解析join表子句,递归解析获得表(SQLUpdateRecognizer#getTableName)
2)查哪些字段 解析update join中set子句获得update join的所有需要更新的字段(UpdateRecognizer#getUpdateColumns),这些字段可能涉及到多张表,需要根据特定的表进行过滤(UpdateExecutor#getItemUpdateColumns)
3)如何查 以join表子句作为union 表(eg: employees inner join merit on employees.performance = merit.performance),分别查询不同表所需的数据

!!!暂不支持Oracle和PG数据库update join语法

@slievrly slievrly added mode: AT AT transaction mode module/rm-datasource rm-datasource module labels Sep 7, 2022
@slievrly slievrly added this to the 1.6.0 milestone Sep 15, 2022
@codecov-commenter
Copy link

codecov-commenter commented Sep 22, 2022

Codecov Report

Merging #4914 (0af1292) into develop (caeb231) will increase coverage by 0.17%.
The diff coverage is 77.51%.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #4914      +/-   ##
=============================================
+ Coverage      48.62%   48.80%   +0.17%     
- Complexity      4052     4077      +25     
=============================================
  Files            732      733       +1     
  Lines          25773    25942     +169     
  Branches        3173     3197      +24     
=============================================
+ Hits           12532    12660     +128     
- Misses         11909    11933      +24     
- Partials        1332     1349      +17     
Impacted Files Coverage Δ
...seata/discovery/registry/MultiRegistryFactory.java 0.00% <ø> (ø)
...a/io/seata/rm/datasource/exec/ExecuteTemplate.java 4.87% <0.00%> (-0.68%) ⬇️
.../seata/rm/datasource/undo/UndoExecutorFactory.java 75.00% <ø> (ø)
...core/src/main/java/io/seata/sqlparser/SQLType.java 0.00% <ø> (ø)
.../java/io/seata/sqlparser/druid/BaseRecognizer.java 76.19% <ø> (-0.56%) ⬇️
.../rm/datasource/exec/BaseTransactionalExecutor.java 56.47% <57.89%> (+0.54%) ⬆️
...a/sqlparser/druid/mysql/MySQLUpdateRecognizer.java 71.76% <71.05%> (+2.27%) ⬆️
...va/io/seata/rm/datasource/exec/UpdateExecutor.java 80.95% <83.33%> (+9.12%) ⬆️
...datasource/exec/mysql/MySQLUpdateJoinExecutor.java 83.33% <83.33%> (ø)
...ser/druid/postgresql/BasePostgresqlRecognizer.java 23.21% <84.61%> (+18.56%) ⬆️
... and 10 more

@lgtm-com
Copy link

lgtm-com bot commented Sep 29, 2022

This pull request introduces 2 alerts when merging 2c73223 into d0f324c - view on LGTM.com

new alerts:

  • 2 for Useless null check

@lgtm-com
Copy link

lgtm-com bot commented Sep 29, 2022

This pull request introduces 2 alerts when merging f9314c6 into d0f324c - view on LGTM.com

new alerts:

  • 2 for Useless null check

@@ -134,28 +118,36 @@ private String buildAfterImageSQL(TableMeta tableMeta, TableRecords beforeImage)
String whereSql = SqlGenerateUtils.buildWhereConditionByPKs(tableMeta.getPrimaryKeyOnlyName(), beforeImage.pkRows().size(), getDbType());
String suffix = " FROM " + getFromTableInSQL() + " WHERE " + whereSql;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FROM WHERE 是否可以考虑定义成静态常量来使用?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FROM WHERE 是否可以考虑定义成静态常量来使用?

这个pr先不要定义了,刘洋有个pr专门搞这个的 @doubleDimple

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块已有的常量比如where可以直接使用,没有的就先不要专门弄了

druidDataSource.setDriverClassName(mysql_driverClassName);
druidDataSource.init();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本地执行下./mvnw clean install -DskipTests=true -Dcheckstyle.skip=false -Dlicense.skip=false
命令,再执行下代码提交,不然checks不通过

Copy link
Contributor

@funky-eyes funky-eyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UpdateJoinExecutorTest.testUpdateJoinUndoLog:64 » NullPointer

@lgtm-com
Copy link

lgtm-com bot commented Oct 3, 2022

This pull request introduces 2 alerts when merging c27ee3e into dac8aa8 - view on LGTM.com

new alerts:

  • 2 for Useless null check

@lgtm-com
Copy link

lgtm-com bot commented Oct 3, 2022

This pull request introduces 2 alerts when merging 02cb4fa into dac8aa8 - view on LGTM.com

new alerts:

  • 2 for Useless null check

Copy link
Contributor

@funky-eyes funky-eyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Please register pr and author information

SQLUpdateRecognizer recognizer = (SQLUpdateRecognizer) sqlRecognizer;
String tableNames = recognizer.getTableName();
if (StringUtils.isEmpty(tableNames)) {
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw Exceptions with an explicit meaning, otherwise it will result in NPE with an unclear meaning


@Override
public boolean visit(SQLSubqueryTableSource x) {
//just like: select * from (select * from t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

select SQL statement that seata does not intercept.

* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not reformat the code that have nothing to do with this pr.

@jsbxyyx
Copy link
Member

jsbxyyx commented Oct 15, 2022

update ignore join is sub update join
If not support update ignore, please judge the sql, execute plain sql executor.

Copy link
Member

@slievrly slievrly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jsbxyyx
Copy link
Member

jsbxyyx commented Oct 18, 2022

seata mysql8,test库中的test1,test2两张表执行如下语句无法回滚。

update test1, test2 set test1.name = '1', test2.name2 = '2xxxxx' where test2.name = '2xx'

CREATE TABLE `test1` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `name2` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
insert into test1 values(1,'1',NULL), (2,'1',NULL);


CREATE TABLE `test2` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `name2` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
insert into test2 values (1,'2xx','2xxxxx'),(2,'2xx', '2xxxxx');

@funky-eyes funky-eyes added the module/sqlparser sql-parser module label Oct 18, 2022
@funky-eyes funky-eyes merged commit 7f55c26 into apache:develop Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mode: AT AT transaction mode module/rm-datasource rm-datasource module module/sqlparser sql-parser module multilingual
Projects
None yet
Development

Successfully merging this pull request may close these issues.

不支持连表更新? 在@GlobalTransactional 中使用update join 报错
7 participants