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

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 @@ -306,7 +307,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 @@ -320,15 +321,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 @@ -405,4 +397,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;
}

}