Skip to content

Commit

Permalink
add VtPlanValue verify
Browse files Browse the repository at this point in the history
  • Loading branch information
wlx5575 committed Aug 11, 2023
1 parent 2dd4c3c commit 57b561f
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 99 deletions.
124 changes: 118 additions & 6 deletions src/test/java/com/jd/jdbc/planbuilder/PlanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.jd.jdbc.planbuilder;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Objects;
import com.jd.jdbc.VSchemaManager;
import com.jd.jdbc.common.Hex;
import com.jd.jdbc.common.util.CollectionUtils;
import com.jd.jdbc.engine.ConcatenateEngine;
import com.jd.jdbc.engine.DeleteEngine;
import com.jd.jdbc.engine.Engine;
Expand All @@ -43,12 +44,17 @@
import com.jd.jdbc.evalengine.EvalEngine;
import com.jd.jdbc.key.DestinationShard;
import com.jd.jdbc.sqlparser.SQLUtils;
import com.jd.jdbc.sqlparser.utils.StringUtils;
import com.jd.jdbc.sqltypes.VtPlanValue;
import com.jd.jdbc.sqltypes.VtValue;
import com.jd.jdbc.util.JsonUtil;
import com.jd.jdbc.vindexes.VKeyspace;
import io.netty.util.internal.StringUtil;
import static io.netty.util.internal.StringUtil.NEWLINE;
import io.vitess.proto.Query;
import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand All @@ -58,10 +64,9 @@
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Test;

import static io.netty.util.internal.StringUtil.NEWLINE;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class PlanTest extends AbstractPlanTest {

Expand Down Expand Up @@ -247,6 +252,10 @@ private Instructions formatRouteEngine(RouteEngine engine) {
}
instructions.setSysTableKeyspaceExpr(exprList);
}
List<Object> values = getValueList(engine.getVtPlanValueList());
if (values != null) {
instructions.setValueList(values);
}
return instructions;
}

Expand Down Expand Up @@ -549,6 +558,10 @@ private Instructions formatUpdateEngine(UpdateEngine engine) {
instructions.setMultiShardAutocommit(engine.isMultiShardAutocommit());
instructions.setQuery(SQLUtils.toMySqlString(engine.getQuery(), SQLUtils.NOT_FORMAT_OPTION));
instructions.setTableName(engine.getTableName());
List<Object> values = getValueList(engine.getVtPlanValueList());
if (values != null) {
instructions.setValueList(values);
}
return instructions;
}

Expand All @@ -566,6 +579,10 @@ private Instructions formatDeleteEngine(DeleteEngine engine) {
instructions.setMultiShardAutocommit(engine.isMultiShardAutocommit());
instructions.setQuery(SQLUtils.toMySqlString(engine.getQuery(), SQLUtils.NOT_FORMAT_OPTION));
instructions.setTableName(engine.getTableName());
List<Object> values = getValueList(engine.getVtPlanValueList());
if (values != null) {
instructions.setValueList(values);
}
return instructions;
}

Expand All @@ -588,6 +605,100 @@ private String formatEvalResult(EvalEngine.EvalResult evalResult) {
return str;
}

private List<Object> getValueList(List<VtPlanValue> vtPlanValueList) {
if (CollectionUtils.isEmpty(vtPlanValueList)) {
return null;
}
List<Object> values = new ArrayList<>();
VtPlanValue vtPlanValue1 = vtPlanValueList.get(0);
if (vtPlanValue1.getVtValue() != null && !vtPlanValue1.getVtValue().isNull() && CollectionUtils.isEmpty(vtPlanValue1.getVtPlanValueList())) {
values.add(getVtValue(vtPlanValue1.getVtValue()));
}
if (vtPlanValue1.getVtValue() != null && vtPlanValue1.getVtValue().isNull() && StringUtils.isNotEmpty(vtPlanValue1.getKey())) {
values.add(":" + vtPlanValue1.getKey());
}
if (vtPlanValue1.getVtValue() == null && StringUtils.isNotEmpty(vtPlanValue1.getListKey())) {
values.add("::" + vtPlanValue1.getListKey());
}

List<String> invalues = new ArrayList<>();
for (VtPlanValue vtPlanValue : vtPlanValue1.getVtPlanValueList()) {
if (vtPlanValue.getVtValue() != null && !vtPlanValue.getVtValue().isNull() && CollectionUtils.isEmpty(vtPlanValue.getVtPlanValueList())) {
invalues.add(getVtValue(vtPlanValue.getVtValue()));
}
if (vtPlanValue.getVtValue() != null && vtPlanValue.getVtValue().isNull() && StringUtils.isNotEmpty(vtPlanValue.getKey())) {
invalues.add(":" + vtPlanValue.getKey());
}
if (vtPlanValue.getVtValue() == null && StringUtils.isNotEmpty(vtPlanValue.getListKey())) {
values.add("::" + vtPlanValue.getListKey());
}
}
if (CollectionUtils.isNotEmpty(invalues)) {
String join = String.join(", ", invalues);
values.add("(" + join + ")");
}
return values;
}

private String getVtValue(VtValue vtValue) {
Object object = null;
try {
switch (vtValue.getVtType()) {
case CHAR:
case VARBINARY:
case VARCHAR:
case TEXT:
case TIME:
object = "\"" + vtValue + "\"";
break;
case BIT:
object = vtValue.toBoolean();
break;
case INT8:
case UINT8:
case INT16:
case UINT16:
case INT24:
case UINT24:
case INT32:
object = vtValue.toInt();
break;
case UINT32:
case INT64:
object = vtValue.toLong();
break;
case UINT64:
object = new BigInteger(vtValue.toString());
break;
case DECIMAL:
case FLOAT32:
case FLOAT64:
object = vtValue.toDecimal();
break;
case DATE:
case YEAR:
object = vtValue.toString();
break;
case DATETIME:
case TIMESTAMP:
object = vtValue.toString();
break;
case BLOB:
case BINARY:
object = Hex.encodeHexString(vtValue.getVtValue());
break;
case NULL_TYPE:
object = null;
break;
default:
throw new RuntimeException("unknown data type:" + vtValue.getVtType());
}
} catch (SQLException throwables) {
Assert.fail();
}
return vtValue.getVtType().toString().toUpperCase() + "(" + object + ")";
}

@Data
@AllArgsConstructor
private static class TestCase {
Expand Down Expand Up @@ -727,6 +838,7 @@ public boolean equals(Object o) {
Objects.equal(fieldQuery, that.fieldQuery) &&
Objects.equal(query, that.query) &&
Objects.equal(table, that.table) &&
Objects.equal(valueList, that.valueList) &&
Objects.equal(joinColumnIndexes, that.joinColumnIndexes) &&
Objects.equal(tableName, that.tableName) &&
Objects.equal(columns, that.columns) &&
Expand All @@ -742,8 +854,8 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hashCode(operatorType, variant, aggregates, distinct, groupBy, orderBy, count, keyspace, targetTabletType, multiShardAutocommit, fieldQuery, query, table, joinColumnIndexes,
tableName, columns, strColumns, sysTableKeyspaceExpr, expressions, inputs, targetDestination, isDML, singleShardOnly, shardNameNeeded);
return Objects.hashCode(operatorType, variant, aggregates, distinct, groupBy, orderBy, count, keyspace, targetTabletType, multiShardAutocommit, fieldQuery, query, table, valueList,
joinColumnIndexes, tableName, columns, strColumns, sysTableKeyspaceExpr, expressions, inputs, targetDestination, isDML, singleShardOnly, shardNameNeeded);
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/test/resources/plan/aggr_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"Query": "select count(*), col from user where name = 1",
"Table": "user",
"Values": [
1
"INT64(1)"
],
"Vindex": "user_index"
}
Expand Down Expand Up @@ -463,7 +463,7 @@
"Query": "select name, count(*) as c from user group by name having name = 1 and c = 10",
"Table": "user",
"Values": [
1
"INT64(1)"
],
"Vindex": "user_index"
}
Expand Down Expand Up @@ -1066,7 +1066,7 @@
"Query": "select user.col1 as a from user where user.name = 5 group by a collate utf8_general_ci",
"Table": "user",
"Values": [
5
"INT64(5)"
],
"Vindex": "user_index"
}
Expand Down
7 changes: 2 additions & 5 deletions src/test/resources/plan/dml_delete_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"Query": "delete from music_extra where user_id = 1",
"TableName": "music_extra",
"Values": [
1
"INT64(1)"
],
"Vindex": "user_index"
}
Expand Down Expand Up @@ -171,10 +171,7 @@
"Query": "delete from user_extra where user_id in (1, 2)",
"TableName": "user_extra",
"Values": [
[
1,
2
]
"(INT64(1), INT64(2))"
],
"Vindex": "user_index"
}
Expand Down
15 changes: 6 additions & 9 deletions src/test/resources/plan/dml_update_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"Query": "update user set val = 1 where name = 'foo'",
"TableName": "user",
"Values": [
1
"VARCHAR(\"foo\")"
],
"Vindex": "user_index"
}
Expand All @@ -60,7 +60,7 @@
"Query": "update user as user_alias set val = 1 where user_alias.name = 'foo'",
"TableName": "user",
"Values": [
1
"VARCHAR(\"foo\")"
],
"Vindex": "user_index"
}
Expand All @@ -83,7 +83,7 @@
"Query": "update user set val = 1 where name = 'foo'",
"TableName": "user",
"Values": [
1
"VARCHAR(\"foo\")"
],
"Vindex": "user_index"
}
Expand Down Expand Up @@ -178,7 +178,7 @@
"Query": "update user set val = 1 where name = 'foo' and id = 1",
"TableName": "user",
"Values": [
1
"VARCHAR(\"foo\")"
],
"Vindex": "user_index"
}
Expand All @@ -201,7 +201,7 @@
"Query": "update user set val = 1 where name = 'foo' and name = '1'",
"TableName": "user",
"Values": [
1
"VARCHAR(\"foo\")"
],
"Vindex": "user_index"
}
Expand Down Expand Up @@ -299,10 +299,7 @@
"Query": "update user_extra set val = 1 where user_id in (1, 2)",
"TableName": "user_extra",
"Values": [
[
1,
2
]
"(INT64(1), INT64(2))"
],
"Vindex": "user_index"
}
Expand Down
Loading

0 comments on commit 57b561f

Please sign in to comment.