Skip to content

Commit

Permalink
Adjust protocolType to sqlParserType in SQLTranslator (#28906)
Browse files Browse the repository at this point in the history
  • Loading branch information
strongduanmu authored Oct 31, 2023
1 parent b3cf6f9 commit dcf1aca
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private QueryContext createQueryContext() {
when(result.getSql()).thenReturn("SELECT ?");
when(result.getParameters()).thenReturn(Collections.singletonList(1));
CommonSQLStatementContext sqlStatementContext = mock(CommonSQLStatementContext.class);
when(sqlStatementContext.getDatabaseType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "H2"));
when(result.getSqlStatementContext()).thenReturn(sqlStatementContext);
when(result.getHintValueContext()).thenReturn(new HintValueContext());
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ void assertRewrite() {
when(database.getProtocolType()).thenReturn(databaseType);
Map<String, StorageUnit> storageUnits = mockStorageUnits(databaseType);
when(database.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits);
CommonSQLStatementContext sqlStatementContext = mock(CommonSQLStatementContext.class);
when(sqlStatementContext.getDatabaseType()).thenReturn(databaseType);
QueryContext queryContext = mock(QueryContext.class);
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
GenericSQLRewriteResult actual = new GenericSQLRewriteEngine(rule, database, mock(RuleMetaData.class))
.rewrite(new SQLRewriteContext(database, mock(CommonSQLStatementContext.class), "SELECT 1", Collections.emptyList(), mock(ConnectionContext.class),
new HintValueContext()), mock(QueryContext.class));
.rewrite(new SQLRewriteContext(database, sqlStatementContext, "SELECT 1", Collections.emptyList(), mock(ConnectionContext.class),
new HintValueContext()), queryContext);
assertThat(actual.getSqlRewriteUnit().getSql(), is("SELECT 1"));
assertThat(actual.getSqlRewriteUnit().getParameters(), is(Collections.emptyList()));
}
Expand All @@ -66,9 +70,14 @@ void assertRewriteStorageTypeIsEmpty() {
when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
when(database.getSchemas()).thenReturn(Collections.singletonMap("test", mock(ShardingSphereSchema.class)));
when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap());
CommonSQLStatementContext sqlStatementContext = mock(CommonSQLStatementContext.class);
DatabaseType databaseType = mock(DatabaseType.class);
when(sqlStatementContext.getDatabaseType()).thenReturn(databaseType);
QueryContext queryContext = mock(QueryContext.class);
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
GenericSQLRewriteResult actual = new GenericSQLRewriteEngine(rule, database, mock(RuleMetaData.class))
.rewrite(new SQLRewriteContext(database, mock(CommonSQLStatementContext.class), "SELECT 1", Collections.emptyList(), mock(ConnectionContext.class),
new HintValueContext()), mock(QueryContext.class));
.rewrite(new SQLRewriteContext(database, sqlStatementContext, "SELECT 1", Collections.emptyList(), mock(ConnectionContext.class),
new HintValueContext()), queryContext);
assertThat(actual.getSqlRewriteUnit().getSql(), is("SELECT 1"));
assertThat(actual.getSqlRewriteUnit().getParameters(), is(Collections.emptyList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ class RouteSQLRewriteEngineTest {
void assertRewriteWithStandardParameterBuilder() {
DatabaseType databaseType = mock(DatabaseType.class);
ShardingSphereDatabase database = mockDatabase(databaseType);
SQLRewriteContext sqlRewriteContext =
new SQLRewriteContext(database, mock(CommonSQLStatementContext.class), "SELECT ?", Collections.singletonList(1), mock(ConnectionContext.class), new HintValueContext());
CommonSQLStatementContext sqlStatementContext = mock(CommonSQLStatementContext.class);
when(sqlStatementContext.getDatabaseType()).thenReturn(databaseType);
SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(database, sqlStatementContext, "SELECT ?", Collections.singletonList(1), mock(ConnectionContext.class), new HintValueContext());
RouteUnit routeUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
RouteContext routeContext = new RouteContext();
routeContext.getRouteUnits().add(routeUnit);
QueryContext queryContext = mock(QueryContext.class);
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
RouteSQLRewriteResult actual = new RouteSQLRewriteEngine(
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, mock(QueryContext.class));
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, queryContext);
assertThat(actual.getSqlRewriteUnits().size(), is(1));
assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("SELECT ?"));
assertThat(actual.getSqlRewriteUnits().get(routeUnit).getParameters(), is(Collections.singletonList(1)));
Expand All @@ -85,15 +88,18 @@ void assertRewriteWithStandardParameterBuilderWhenNeedAggregateRewrite() {
when(statementContext.getOrderByContext().getItems()).thenReturn(Collections.emptyList());
when(statementContext.getPaginationContext().isHasPagination()).thenReturn(false);
DatabaseType databaseType = mock(DatabaseType.class);
when(statementContext.getDatabaseType()).thenReturn(databaseType);
ShardingSphereDatabase database = mockDatabase(databaseType);
SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(database, statementContext, "SELECT ?", Collections.singletonList(1), mock(ConnectionContext.class), new HintValueContext());
RouteContext routeContext = new RouteContext();
RouteUnit firstRouteUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
RouteUnit secondRouteUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_1")));
routeContext.getRouteUnits().add(firstRouteUnit);
routeContext.getRouteUnits().add(secondRouteUnit);
QueryContext queryContext = mock(QueryContext.class);
when(queryContext.getSqlStatementContext()).thenReturn(statementContext);
RouteSQLRewriteResult actual = new RouteSQLRewriteEngine(
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, mock(QueryContext.class));
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, queryContext);
assertThat(actual.getSqlRewriteUnits().size(), is(1));
assertThat(actual.getSqlRewriteUnits().get(firstRouteUnit).getSql(), is("SELECT ? UNION ALL SELECT ?"));
assertThat(actual.getSqlRewriteUnits().get(firstRouteUnit).getParameters(), is(Arrays.asList(1, 1)));
Expand All @@ -107,14 +113,17 @@ void assertRewriteWithGroupedParameterBuilderForBroadcast() {
when(statementContext.getGroupedParameters()).thenReturn(Collections.singletonList(Collections.singletonList(1)));
when(statementContext.getOnDuplicateKeyUpdateParameters()).thenReturn(Collections.emptyList());
DatabaseType databaseType = mock(DatabaseType.class);
when(statementContext.getDatabaseType()).thenReturn(databaseType);
ShardingSphereDatabase database = mockDatabase(databaseType);
SQLRewriteContext sqlRewriteContext =
new SQLRewriteContext(database, statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1), mock(ConnectionContext.class), new HintValueContext());
RouteUnit routeUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
RouteContext routeContext = new RouteContext();
routeContext.getRouteUnits().add(routeUnit);
QueryContext queryContext = mock(QueryContext.class);
when(queryContext.getSqlStatementContext()).thenReturn(statementContext);
RouteSQLRewriteResult actual = new RouteSQLRewriteEngine(
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, mock(QueryContext.class));
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, queryContext);
assertThat(actual.getSqlRewriteUnits().size(), is(1));
assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("INSERT INTO tbl VALUES (?)"));
assertThat(actual.getSqlRewriteUnits().get(routeUnit).getParameters(), is(Collections.singletonList(1)));
Expand All @@ -128,6 +137,7 @@ void assertRewriteWithGroupedParameterBuilderForRouteWithSameDataNode() {
when(statementContext.getGroupedParameters()).thenReturn(Collections.singletonList(Collections.singletonList(1)));
when(statementContext.getOnDuplicateKeyUpdateParameters()).thenReturn(Collections.emptyList());
DatabaseType databaseType = mock(DatabaseType.class);
when(statementContext.getDatabaseType()).thenReturn(databaseType);
ShardingSphereDatabase database = mockDatabase(databaseType);
SQLRewriteContext sqlRewriteContext =
new SQLRewriteContext(database, statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1), mock(ConnectionContext.class), new HintValueContext());
Expand All @@ -136,8 +146,10 @@ void assertRewriteWithGroupedParameterBuilderForRouteWithSameDataNode() {
routeContext.getRouteUnits().add(routeUnit);
// TODO check why data node is "ds.tbl_0", not "ds_0.tbl_0"
routeContext.getOriginalDataNodes().add(Collections.singletonList(new DataNode("ds.tbl_0")));
QueryContext queryContext = mock(QueryContext.class);
when(queryContext.getSqlStatementContext()).thenReturn(statementContext);
RouteSQLRewriteResult actual = new RouteSQLRewriteEngine(
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, mock(QueryContext.class));
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, queryContext);
assertThat(actual.getSqlRewriteUnits().size(), is(1));
assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("INSERT INTO tbl VALUES (?)"));
assertThat(actual.getSqlRewriteUnits().get(routeUnit).getParameters(), is(Collections.singletonList(1)));
Expand All @@ -151,15 +163,18 @@ void assertRewriteWithGroupedParameterBuilderForRouteWithEmptyDataNode() {
when(statementContext.getGroupedParameters()).thenReturn(Collections.singletonList(Collections.singletonList(1)));
when(statementContext.getOnDuplicateKeyUpdateParameters()).thenReturn(Collections.emptyList());
DatabaseType databaseType = mock(DatabaseType.class);
when(statementContext.getDatabaseType()).thenReturn(databaseType);
ShardingSphereDatabase database = mockDatabase(databaseType);
SQLRewriteContext sqlRewriteContext =
new SQLRewriteContext(database, statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1), mock(ConnectionContext.class), new HintValueContext());
RouteUnit routeUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
RouteContext routeContext = new RouteContext();
routeContext.getRouteUnits().add(routeUnit);
routeContext.getOriginalDataNodes().add(Collections.emptyList());
QueryContext queryContext = mock(QueryContext.class);
when(queryContext.getSqlStatementContext()).thenReturn(statementContext);
RouteSQLRewriteResult actual = new RouteSQLRewriteEngine(
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, mock(QueryContext.class));
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, queryContext);
assertThat(actual.getSqlRewriteUnits().size(), is(1));
assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("INSERT INTO tbl VALUES (?)"));
assertThat(actual.getSqlRewriteUnits().get(routeUnit).getParameters(), is(Collections.singletonList(1)));
Expand All @@ -173,15 +188,18 @@ void assertRewriteWithGroupedParameterBuilderForRouteWithNotSameDataNode() {
when(statementContext.getGroupedParameters()).thenReturn(Collections.singletonList(Collections.singletonList(1)));
when(statementContext.getOnDuplicateKeyUpdateParameters()).thenReturn(Collections.emptyList());
DatabaseType databaseType = mock(DatabaseType.class);
when(statementContext.getDatabaseType()).thenReturn(databaseType);
ShardingSphereDatabase database = mockDatabase(databaseType);
SQLRewriteContext sqlRewriteContext =
new SQLRewriteContext(database, statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1), mock(ConnectionContext.class), new HintValueContext());
RouteUnit routeUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
RouteContext routeContext = new RouteContext();
routeContext.getRouteUnits().add(routeUnit);
routeContext.getOriginalDataNodes().add(Collections.singletonList(new DataNode("ds_1.tbl_1")));
QueryContext queryContext = mock(QueryContext.class);
when(queryContext.getSqlStatementContext()).thenReturn(statementContext);
RouteSQLRewriteResult actual = new RouteSQLRewriteEngine(
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, mock(QueryContext.class));
new SQLTranslatorRule(new SQLTranslatorRuleConfiguration()), database, mock(RuleMetaData.class)).rewrite(sqlRewriteContext, routeContext, queryContext);
assertThat(actual.getSqlRewriteUnits().size(), is(1));
assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("INSERT INTO tbl VALUES (?)"));
assertTrue(actual.getSqlRewriteUnits().get(routeUnit).getParameters().isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public SQLTranslatorRule(final SQLTranslatorRuleConfiguration ruleConfig) {
*/
public String translate(final String sql, final QueryContext queryContext, final DatabaseType storageType, final ShardingSphereDatabase database,
final RuleMetaData globalRuleMetaData) {
DatabaseType protocolType = database.getProtocolType();
if (protocolType.equals(storageType) || null == storageType) {
DatabaseType sqlParserType = queryContext.getSqlStatementContext().getDatabaseType();
if (sqlParserType.equals(storageType) || null == storageType) {
return sql;
}
try {
Expand Down
Loading

0 comments on commit dcf1aca

Please sign in to comment.