Skip to content

Commit 99cae08

Browse files
willmostlyebyhr
andauthored
Remove "Optional[]" from getResourceGroupQueryType string
TrinoQueryProperties.getResourceGroupQueryType() called toString on an Optional. This unintended behavior produced output such as "Optional[DATA_DEFINITION]", complicating rule writing for end users. Co-authored-by: Yuya Ebihara <[email protected]>
1 parent 26dc2fe commit 99cae08

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

gateway-ha/src/main/java/io/trino/gateway/ha/router/StatementUtils.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.trino.gateway.ha.router;
1515

1616
import com.google.common.collect.ImmutableList;
17+
import io.airlift.log.Logger;
1718
import io.trino.sql.tree.AddColumn;
1819
import io.trino.sql.tree.Analyze;
1920
import io.trino.sql.tree.Call;
@@ -89,7 +90,6 @@
8990
import io.trino.sql.tree.Use;
9091

9192
import java.util.Map;
92-
import java.util.Optional;
9393

9494
import static com.google.common.collect.ImmutableMap.toImmutableMap;
9595
import static io.trino.gateway.ha.router.QueryType.ALTER_TABLE_EXECUTE;
@@ -108,6 +108,8 @@
108108
//modified version of io.trino.util.StatementUtils
109109
public final class StatementUtils
110110
{
111+
private static final Logger log = Logger.get(StatementUtils.class);
112+
111113
private StatementUtils() {}
112114

113115
private static final Map<Class<? extends Statement>, StatementTypeInfo<? extends Statement>> STATEMENT_QUERY_TYPES = ImmutableList.<StatementTypeInfo<?>>builder()
@@ -191,13 +193,17 @@ private StatementUtils() {}
191193
.build().stream()
192194
.collect(toImmutableMap(StatementTypeInfo::getStatementType, identity()));
193195

194-
public static Optional<QueryType> getQueryType(Statement statement)
196+
public static String getResourceGroupQueryType(Statement statement)
195197
{
196198
if (statement instanceof ExplainAnalyze) {
197-
return getQueryType(((ExplainAnalyze) statement).getStatement());
199+
return getResourceGroupQueryType(((ExplainAnalyze) statement).getStatement());
200+
}
201+
StatementTypeInfo<? extends Statement> statementTypeInfo = STATEMENT_QUERY_TYPES.get(statement.getClass());
202+
if (statementTypeInfo != null) {
203+
return statementTypeInfo.getQueryType().toString();
198204
}
199-
return Optional.ofNullable(STATEMENT_QUERY_TYPES.get(statement.getClass()))
200-
.map(StatementTypeInfo::getQueryType);
205+
log.warn("Unsupported statement type: %s", statement.getClass());
206+
return "UNKNOWN";
201207
}
202208

203209
private static <T extends Statement> StatementTypeInfo<T> basicStatement(Class<T> statementType, QueryType queryType)

gateway-ha/src/main/java/io/trino/gateway/ha/router/TrinoQueryProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ else if (statement instanceof ExecuteImmediate executeImmediate) {
206206
}
207207

208208
queryType = statement.getClass().getSimpleName();
209-
resourceGroupQueryType = StatementUtils.getQueryType(statement).toString();
209+
resourceGroupQueryType = StatementUtils.getResourceGroupQueryType(statement);
210210
ImmutableSet.Builder<QualifiedName> tableBuilder = ImmutableSet.builder();
211211
ImmutableSet.Builder<String> catalogBuilder = ImmutableSet.builder();
212212
ImmutableSet.Builder<String> schemaBuilder = ImmutableSet.builder();

gateway-ha/src/test/java/io/trino/gateway/ha/router/TestRoutingGroupSelector.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ void testTrinoQueryPropertiesQueryType()
178178
assertThat(routingGroupSelector.findRoutingGroup(mockRequest)).isEqualTo("type-group");
179179
}
180180

181+
@Test
182+
void testTrinoQueryPropertiesResourceGroupQueryType()
183+
throws IOException
184+
{
185+
RoutingGroupSelector routingGroupSelector =
186+
RoutingGroupSelector.byRoutingRulesEngine("src/test/resources/rules/routing_rules_trino_query_properties.yml", requestAnalyzerConfig);
187+
HttpServletRequest mockRequest = prepareMockRequest();
188+
when(mockRequest.getReader()).thenReturn(new BufferedReader(new StringReader("CREATE TABLE cat.schem.foo (c1 int)")));
189+
190+
assertThat(routingGroupSelector.findRoutingGroup(mockRequest)).isEqualTo("resource-group-type-group");
191+
}
192+
181193
@Test
182194
void testTrinoQueryPropertiesAlternateStatementFormat()
183195
throws IOException

gateway-ha/src/test/resources/rules/routing_rules_trino_query_properties.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ condition: |
3232
actions:
3333
- "result.put(\"routingGroup\", \"type-group\")"
3434
---
35+
name: "resource-group-query-type"
36+
description: "test table type"
37+
condition: |
38+
trinoQueryProperties.getResourceGroupQueryType().equals("DATA_DEFINITION")
39+
actions:
40+
- "result.put(\"routingGroup\", \"resource-group-type-group\")"
41+
---
3542
name: "prepared-statement-header"
3643
description: "test execute with multiple prepared statements"
3744
condition: |

0 commit comments

Comments
 (0)