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

bugfix: error image when use null value as image query condition in insert on duplicate #5031

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/en-us/develop.md
Expand Up @@ -33,6 +33,7 @@ Add changes here for all PR submitted to the develop branch.
- [[#5033](https://github.com/seata/seata/pull/5023)] fix mysql InsertOnDuplicateUpdate insert value type recognize error
- [[#5038](https://github.com/seata/seata/pull/5038)] remove @EnableConfigurationProperties({SagaAsyncThreadPoolProperties.class})
- [[#5050](https://github.com/seata/seata/pull/5050)] fix global session is not change to Committed in saga mode
- [[#5031](https://github.com/seata/seata/pull/5031)] fix mysql InsertOnDuplicateUpdate should not use null index value as image sql query condition



Expand Down
2 changes: 1 addition & 1 deletion changes/zh-cn/develop.md
Expand Up @@ -35,7 +35,7 @@
- [[#5033](https://github.com/seata/seata/pull/5023)] 修复InsertOnDuplicateUpdate中插入值解析为String类型导致的类型识别错误
- [[#5038](https://github.com/seata/seata/pull/5038)] 修复SagaAsyncThreadPoolProperties冲突问题
- [[#5050](https://github.com/seata/seata/pull/5050)] 修复Saga模式下全局状态未正确更改成Committed

- [[#5031](https://github.com/seata/seata/pull/5031)] 修复InsertOnDuplicateUpdate中不应该使用null值索引作为查询条件

### optimize:
- [[#4774](https://github.com/seata/seata/pull/4774)] 优化 seataio/seata-server 镜像中的 mysql8 依赖
Expand Down
Expand Up @@ -46,6 +46,7 @@
import io.seata.rm.datasource.sql.struct.Row;
import io.seata.rm.datasource.sql.struct.TableMeta;
import io.seata.rm.datasource.sql.struct.TableRecords;
import io.seata.rm.datasource.sql.struct.IndexMeta;
import io.seata.rm.datasource.undo.SQLUndoLog;
import io.seata.sqlparser.SQLInsertRecognizer;
import io.seata.sqlparser.SQLRecognizer;
Expand Down Expand Up @@ -237,7 +238,9 @@ protected TableRecords afterImage(TableRecords beforeImage) throws SQLException
primaryValues.add(v);
}
});
afterImageSql.append(" OR (").append(Joiner.on(" and ").join(wherePrimaryList)).append(") ");
if (wherePrimaryList.size() > 0) {
afterImageSql.append(" OR (").append(Joiner.on(" and ").join(wherePrimaryList)).append(") ");
}
}

return buildTableRecords2(tableMeta, afterImageSql.toString(), paramAppenderList, primaryValues);
Expand Down Expand Up @@ -308,7 +311,7 @@ public String buildImageSQL(TableMeta tableMeta) {
int finalI = i;
List<Object> paramAppenderTempList = new ArrayList<>();
tableMeta.getAllIndexes().forEach((k, v) -> {
if (!v.isNonUnique()) {
if (!v.isNonUnique() && isIndexValueNotNull(v,imageParameterMap,finalI)) {
boolean columnIsNull = true;
List<String> uniqueList = new ArrayList<>();
for (ColumnMeta m : v.getValues()) {
Expand All @@ -322,15 +325,6 @@ public String buildImageSQL(TableMeta tableMeta) {
columnIsNull = false;
continue;
}
if ((imageParameters == null && m.getColumnDef() == null) || imageParameters.get(finalI) == null || imageParameters.get(finalI) instanceof Null) {
if (!"PRIMARY".equalsIgnoreCase(k)) {
columnIsNull = false;
uniqueList.add(columnName + " is ? ");
paramAppenderTempList.add("NULL");
continue;
}
break;
}
if ("PRIMARY".equalsIgnoreCase(k)) {
primaryKeysInBeforeImageSql.add(columnName);
}
Expand Down Expand Up @@ -404,4 +398,17 @@ public Map<String, ArrayList<Object>> buildImageParameters(SQLInsertRecognizer r
return imageParameterMap;
}

private boolean isIndexValueNotNull(IndexMeta indexMeta, Map<String, ArrayList<Object>> imageParameterMap,int rowIndex) {
for (ColumnMeta columnMeta : indexMeta.getValues()) {
String columnName = columnMeta.getColumnName();
List<Object> imageParameters = imageParameterMap.get(columnName);
if (imageParameters == null && columnMeta.getColumnDef() == null) {
return false;
} else if (imageParameters != null && (imageParameters.get(rowIndex) == null || imageParameters.get(rowIndex) instanceof Null)) {
return false;
}
}
return true;
}

}