Skip to content

Commit

Permalink
Refactor usage of VariableSegment (#33755)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 21, 2024
1 parent ef6c151 commit fbbbd85
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ public ASTNode visitSetCharacter(final SetCharacterContext ctx) {
int stopIndex = null == ctx.CHARSET() ? ctx.SET(1).getSymbol().getStopIndex() : ctx.CHARSET().getSymbol().getStopIndex();
// TODO Consider setting all three system variables: character_set_client, character_set_results, character_set_connection
VariableSegment variable = new VariableSegment(startIndex, stopIndex, (null == ctx.CHARSET()) ? "character_set_client" : ctx.CHARSET().getText());
String assignValue = (null == ctx.DEFAULT()) ? ctx.charsetName().getText() : ctx.DEFAULT().getText();
String assignValue = null == ctx.DEFAULT() ? ctx.charsetName().getText() : ctx.DEFAULT().getText();
VariableAssignSegment characterSet = new VariableAssignSegment(startIndex, stopIndex, variable, assignValue);
DorisSetStatement result = new DorisSetStatement();
result.getVariableAssigns().add(characterSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ private ConnectionContext mockConnectionContext() {
}

private SetStatement prepareSetStatement() {
VariableSegment maxConnectionVariableSegment = new VariableSegment(0, 0, "max_connections");
maxConnectionVariableSegment.setScope("global");
VariableSegment maxConnectionVariableSegment = new VariableSegment(0, 0, "max_connections", "global");
VariableAssignSegment setGlobalMaxConnectionAssignSegment = new VariableAssignSegment(0, 0, maxConnectionVariableSegment, "151");
VariableSegment characterSetClientSegment = new VariableSegment(0, 0, "character_set_client");
VariableAssignSegment setCharacterSetClientVariableSegment = new VariableAssignSegment(0, 0, characterSetClientSegment, "'utf8mb4'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class MySQLSystemVariableQueryExecutorTest {
void assertTryGetSystemVariableQueryExecutorWithOtherExpressionProjection() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setProjections(new ProjectionsSegment(0, 0));
VariableSegment variable = new VariableSegment(0, 0, "max_connections");
variable.setScope("session");
VariableSegment variable = new VariableSegment(0, 0, "max_connections", "session");
selectStatement.getProjections().getProjections().add(new ExpressionProjectionSegment(0, 0, "@@session.max_connections", variable));
selectStatement.getProjections().getProjections().add(new ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("some_column"))));
assertFalse(MySQLSystemVariableQueryExecutor.tryGetSystemVariableQueryExecutor(selectStatement).isPresent());
Expand All @@ -66,11 +65,9 @@ void assertTryGetSystemVariableQueryExecutorWithUnknownVariable() {
void assertTryGetSystemVariableQueryExecutorAndExecuteWithCorrectScope() throws SQLException {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setProjections(new ProjectionsSegment(0, 0));
VariableSegment maxConnectionsVariable = new VariableSegment(0, 0, "max_connections");
maxConnectionsVariable.setScope("global");
VariableSegment maxConnectionsVariable = new VariableSegment(0, 0, "max_connections", "global");
selectStatement.getProjections().getProjections().add(new ExpressionProjectionSegment(0, 0, "@@global.max_connections", maxConnectionsVariable));
VariableSegment warningCountVariable = new VariableSegment(0, 0, "warning_count");
warningCountVariable.setScope("session");
VariableSegment warningCountVariable = new VariableSegment(0, 0, "warning_count", "session");
ExpressionProjectionSegment warningCountProjection = new ExpressionProjectionSegment(0, 0, "@@session.warning_count", warningCountVariable);
warningCountProjection.setAlias(new AliasSegment(0, 0, new IdentifierValue("session_warning")));
selectStatement.getProjections().getProjections().add(warningCountProjection);
Expand All @@ -96,8 +93,7 @@ void assertTryGetSystemVariableQueryExecutorAndExecuteWithCorrectScope() throws
void assertExecuteWithIncorrectScope() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();
selectStatement.setProjections(new ProjectionsSegment(0, 0));
VariableSegment variable = new VariableSegment(0, 0, "max_connections");
variable.setScope("session");
VariableSegment variable = new VariableSegment(0, 0, "max_connections", "session");
selectStatement.getProjections().getProjections().add(new ExpressionProjectionSegment(0, 0, "@@session.max_connections", variable));
Optional<DatabaseAdminExecutor> executor = MySQLSystemVariableQueryExecutor.tryGetSystemVariableQueryExecutor(selectStatement);
assertTrue(executor.isPresent());
Expand Down

0 comments on commit fbbbd85

Please sign in to comment.