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

Refactor BroadcastRouteEngineFactory #33505

Merged
merged 1 commit into from
Nov 1, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.tcl.TCLStatement;
import org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLUseStatement;

import java.util.Collection;

Expand Down Expand Up @@ -73,13 +72,17 @@ public static BroadcastRouteEngine newInstance(final BroadcastRule rule, final S
if (!(sqlStatementContext instanceof TableAvailable)) {
return new BroadcastIgnoreRouteEngine();
}
Collection<String> tableNames = ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames();
if (tableNames.isEmpty()) {
return new BroadcastIgnoreRouteEngine();
}
if (sqlStatement instanceof DALStatement) {
return getDALRouteEngine(rule, sqlStatementContext);
return getDALRouteEngine(rule, tableNames);
}
if (sqlStatement instanceof DCLStatement) {
return getDCLRouteEngine(rule, sqlStatementContext);
return getDCLRouteEngine(rule, tableNames);
}
return getDMLRouteEngine(rule, sqlStatementContext, queryContext.getConnectionContext());
return getDMLRouteEngine(rule, sqlStatementContext, queryContext.getConnectionContext(), tableNames);
}

private static BroadcastRouteEngine getCursorRouteEngine(final BroadcastRule rule, final SQLStatementContext sqlStatementContext, final ConnectionContext connectionContext) {
Expand All @@ -98,23 +101,17 @@ private static BroadcastRouteEngine getDDLRouteEngine(final BroadcastRule rule,
return rule.isAllBroadcastTables(tableNames) ? new BroadcastTableBroadcastRouteEngine(tableNames) : new BroadcastIgnoreRouteEngine();
}

private static BroadcastRouteEngine getDALRouteEngine(final BroadcastRule rule, final SQLStatementContext sqlStatementContext) {
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
if (sqlStatement instanceof MySQLUseStatement) {
return new BroadcastIgnoreRouteEngine();
}
Collection<String> tableNames = ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames();
return tableNames.isEmpty() ? new BroadcastIgnoreRouteEngine() : new BroadcastTableBroadcastRouteEngine(rule.filterBroadcastTableNames(tableNames));
private static BroadcastRouteEngine getDALRouteEngine(final BroadcastRule rule, final Collection<String> tableNames) {
return new BroadcastTableBroadcastRouteEngine(rule.filterBroadcastTableNames(tableNames));
}

private static BroadcastRouteEngine getDCLRouteEngine(final BroadcastRule rule, final SQLStatementContext sqlStatementContext) {
Collection<String> tableNames = ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames();
private static BroadcastRouteEngine getDCLRouteEngine(final BroadcastRule rule, final Collection<String> tableNames) {
Collection<String> broadcastTableNames = rule.filterBroadcastTableNames(tableNames);
return broadcastTableNames.isEmpty() ? new BroadcastIgnoreRouteEngine() : new BroadcastTableBroadcastRouteEngine(broadcastTableNames);
}

private static BroadcastRouteEngine getDMLRouteEngine(final BroadcastRule rule, final SQLStatementContext sqlStatementContext, final ConnectionContext connectionContext) {
Collection<String> tableNames = ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames();
private static BroadcastRouteEngine getDMLRouteEngine(final BroadcastRule rule, final SQLStatementContext sqlStatementContext,
final ConnectionContext connectionContext, final Collection<String> tableNames) {
if (rule.isAllBroadcastTables(tableNames)) {
return sqlStatementContext.getSqlStatement() instanceof SelectStatement
? new BroadcastUnicastRouteEngine(sqlStatementContext, tableNames, connectionContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dal.DALStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dcl.DCLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
Expand Down Expand Up @@ -139,22 +140,22 @@ void assertNewInstanceWithDDLStatementAndIsNotAllBroadcastTables() {
}

@Test
void assertNewInstanceWithDALStatementAndNotTableAvailable() {
when(queryContext.getSqlStatementContext().getSqlStatement()).thenReturn(mock(DALStatement.class));
void assertNewInstanceWithoutTableAvailableStatement() {
when(queryContext.getSqlStatementContext().getSqlStatement()).thenReturn(mock(SQLStatement.class));
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastIgnoreRouteEngine.class));
}

@Test
void assertNewInstanceWithDALStatementAndEmptyTables() {
void assertNewInstanceWithEmptyTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(new TablesContext(Collections.emptyList(), databaseType, null));
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DALStatement.class));
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(SQLStatement.class));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastIgnoreRouteEngine.class));
}

@Test
void assertNewInstanceWithDALStatementAndTables() {
void assertNewInstanceWithDALStatement() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
Expand All @@ -164,16 +165,17 @@ void assertNewInstanceWithDALStatementAndTables() {
}

@Test
void assertNewInstanceWithDCLStatementAndEmptyTables() {
void assertNewInstanceWithDCLStatementWithoutBroadcastTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(new TablesContext(Collections.emptyList(), databaseType, null));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("bar_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DCLStatement.class));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastIgnoreRouteEngine.class));
}

@Test
void assertNewInstanceWithDCLStatementAndTables() {
void assertNewInstanceWithDCLStatementWithBroadcastTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
Expand All @@ -185,15 +187,15 @@ void assertNewInstanceWithDCLStatementAndTables() {
@Test
void assertNewInstanceWithDMLStatementAndIsNotAllBroadcastTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.emptyList(), databaseType, null);
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("bar_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DMLStatement.class));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastIgnoreRouteEngine.class));
}

@Test
void assertNewInstanceWithDMLSelectStatementAndIsAllBroadcastTables() {
void assertNewInstanceWithSelectStatementAndIsAllBroadcastTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
Expand All @@ -203,7 +205,7 @@ void assertNewInstanceWithDMLSelectStatementAndIsAllBroadcastTables() {
}

@Test
void assertNewInstanceWithDMLUpdateStatementAndIsAllBroadcastTables() {
void assertNewInstanceWithUpdateStatementAndIsAllBroadcastTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
Expand Down