Skip to content

Commit 3df6a9b

Browse files
committed
Added queryText regex pattern to match in resource group selectors
1 parent a0c208a commit 3df6a9b

File tree

17 files changed

+134
-28
lines changed

17 files changed

+134
-28
lines changed

core/trino-main/src/main/java/io/trino/dispatcher/DispatchManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ private <C> void createQueryInternal(QueryId queryId, Span querySpan, Slug slug,
235235
sessionContext.getSource(),
236236
sessionContext.getClientTags(),
237237
sessionContext.getResourceEstimates(),
238+
query,
238239
queryType));
239240

240241
// apply system default session properties (does not override user set properties)

core/trino-spi/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,20 @@
319319
<old>method long[] io.trino.spi.PageSorter::sort(java.util.List&lt;io.trino.spi.type.Type&gt;, java.util.List&lt;io.trino.spi.Page&gt;, java.util.List&lt;java.lang.Integer&gt;, java.util.List&lt;io.trino.spi.connector.SortOrder&gt;, int)</old>
320320
<new>method java.util.Iterator&lt;io.trino.spi.Page&gt; io.trino.spi.PageSorter::sort(java.util.List&lt;io.trino.spi.type.Type&gt;, java.util.List&lt;io.trino.spi.Page&gt;, java.util.List&lt;java.lang.Integer&gt;, java.util.List&lt;io.trino.spi.connector.SortOrder&gt;, int)</new>
321321
</item>
322+
<item>
323+
<ignore>true</ignore>
324+
<code>java.method.numberOfParametersChanged</code>
325+
<old>method void io.trino.spi.resourcegroups.SelectionCriteria::&lt;init&gt;(boolean, java.lang.String, java.util.Set&lt;java.lang.String&gt;, java.util.Optional&lt;java.lang.String&gt;, java.util.Set&lt;java.lang.String&gt;, io.trino.spi.session.ResourceEstimates, java.util.Optional&lt;java.lang.String&gt;)</old>
326+
<new>method void io.trino.spi.resourcegroups.SelectionCriteria::&lt;init&gt;(boolean, java.lang.String, java.util.Set&lt;java.lang.String&gt;, java.lang.String, java.util.Optional&lt;java.lang.String&gt;, java.util.Optional&lt;java.lang.String&gt;, java.util.Set&lt;java.lang.String&gt;, io.trino.spi.session.ResourceEstimates, java.lang.String, java.util.Optional&lt;java.lang.String&gt;)</new>
327+
<justification>Added queryText before queryType parameter</justification>
328+
</item>
329+
<item>
330+
<ignore>true</ignore>
331+
<code>java.method.removed</code>
332+
<old>method void io.trino.spi.resourcegroups.SelectionCriteria::&lt;init&gt;(boolean, java.lang.String, java.util.Set&lt;java.lang.String&gt;, java.lang.String, java.util.Optional&lt;java.lang.String&gt;, java.util.Optional&lt;java.lang.String&gt;, java.util.Set&lt;java.lang.String&gt;, io.trino.spi.session.ResourceEstimates, java.util.Optional&lt;java.lang.String&gt;)</old>
333+
<justification>Added queryText before queryType parameter</justification>
334+
</item>
335+
322336
</differences>
323337
</revapi.differences>
324338
</analysisConfiguration>

core/trino-spi/src/main/java/io/trino/spi/resourcegroups/SelectionCriteria.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public final class SelectionCriteria
3131
private final Optional<String> source;
3232
private final Set<String> clientTags;
3333
private final ResourceEstimates resourceEstimates;
34+
private final String queryText;
3435
private final Optional<String> queryType;
3536

3637
public SelectionCriteria(
@@ -42,6 +43,7 @@ public SelectionCriteria(
4243
Optional<String> source,
4344
Set<String> clientTags,
4445
ResourceEstimates resourceEstimates,
46+
String queryText,
4547
Optional<String> queryType)
4648
{
4749
this.authenticated = authenticated;
@@ -52,6 +54,7 @@ public SelectionCriteria(
5254
this.source = requireNonNull(source, "source is null");
5355
this.clientTags = Set.copyOf(requireNonNull(clientTags, "clientTags is null"));
5456
this.resourceEstimates = requireNonNull(resourceEstimates, "resourceEstimates is null");
57+
this.queryText = requireNonNull(queryText, "queryText is null");
5558
this.queryType = requireNonNull(queryType, "queryType is null");
5659
}
5760

@@ -95,6 +98,11 @@ public ResourceEstimates getResourceEstimates()
9598
return resourceEstimates;
9699
}
97100

101+
public String getQueryText()
102+
{
103+
return queryText;
104+
}
105+
98106
public Optional<String> getQueryType()
99107
{
100108
return queryType;
@@ -112,6 +120,7 @@ public String toString()
112120
.add("source=" + source)
113121
.add("clientTags=" + clientTags)
114122
.add("resourceEstimates=" + resourceEstimates)
123+
.add("queryText=" + queryText)
115124
.add("queryType=" + queryType)
116125
.toString();
117126
}

docs/src/main/sphinx/admin/resource-groups.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ documentation](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java
181181

182182
- `source` (optional): Java regex to match against source string.
183183

184+
- `queryText` (optional): regex to match against the SQL query string.
185+
184186
- `queryType` (optional): string to match against the type of the query submitted:
185187

186188
- `SELECT`: [SELECT](/sql/select) queries.

plugin/trino-resource-group-managers/src/main/java/io/trino/plugin/resourcegroups/AbstractResourceConfigurationManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ protected List<ResourceGroupSelector> buildSelectors(ManagerSpec managerSpec)
107107
spec.getSourceRegex(),
108108
spec.getClientTags(),
109109
spec.getResourceEstimate(),
110+
spec.getQueryText(),
110111
spec.getQueryType(),
111112
spec.getGroup()));
112113
}

plugin/trino-resource-group-managers/src/main/java/io/trino/plugin/resourcegroups/SelectorSpec.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class SelectorSpec
3131
private final Optional<Pattern> originalUserRegex;
3232
private final Optional<Pattern> authenticatedUserRegex;
3333
private final Optional<Pattern> sourceRegex;
34+
private final Optional<Pattern> queryText;
3435
private final Optional<String> queryType;
3536
private final Optional<List<String>> clientTags;
3637
private final Optional<SelectorResourceEstimate> selectorResourceEstimate;
@@ -43,6 +44,7 @@ public SelectorSpec(
4344
@JsonProperty("originalUser") Optional<Pattern> originalUserRegex,
4445
@JsonProperty("authenticatedUser") Optional<Pattern> authenticatedUserRegex,
4546
@JsonProperty("source") Optional<Pattern> sourceRegex,
47+
@JsonProperty("queryText") Optional<Pattern> queryText,
4648
@JsonProperty("queryType") Optional<String> queryType,
4749
@JsonProperty("clientTags") Optional<List<String>> clientTags,
4850
@JsonProperty("selectorResourceEstimate") Optional<SelectorResourceEstimate> selectorResourceEstimate,
@@ -53,6 +55,7 @@ public SelectorSpec(
5355
this.originalUserRegex = requireNonNull(originalUserRegex, "originalUserRegex is null");
5456
this.authenticatedUserRegex = requireNonNull(authenticatedUserRegex, "authenticatedUserRegex is null");
5557
this.sourceRegex = requireNonNull(sourceRegex, "sourceRegex is null");
58+
this.queryText = requireNonNull(queryText, "queryText is null");
5659
this.queryType = requireNonNull(queryType, "queryType is null");
5760
this.clientTags = requireNonNull(clientTags, "clientTags is null");
5861
this.selectorResourceEstimate = requireNonNull(selectorResourceEstimate, "selectorResourceEstimate is null");
@@ -84,6 +87,11 @@ public Optional<Pattern> getSourceRegex()
8487
return sourceRegex;
8588
}
8689

90+
public Optional<Pattern> getQueryText()
91+
{
92+
return queryText;
93+
}
94+
8795
public Optional<String> getQueryType()
8896
{
8997
return queryType;
@@ -124,6 +132,7 @@ public boolean equals(Object other)
124132
authenticatedUserRegex.map(Pattern::flags).equals(that.authenticatedUserRegex.map(Pattern::flags)) &&
125133
sourceRegex.map(Pattern::pattern).equals(that.sourceRegex.map(Pattern::pattern))) &&
126134
sourceRegex.map(Pattern::flags).equals(that.sourceRegex.map(Pattern::flags)) &&
135+
queryText.equals(that.queryText) &&
127136
queryType.equals(that.queryType) &&
128137
clientTags.equals(that.clientTags);
129138
}
@@ -143,6 +152,7 @@ public int hashCode()
143152
authenticatedUserRegex.map(Pattern::flags),
144153
sourceRegex.map(Pattern::pattern),
145154
sourceRegex.map(Pattern::flags),
155+
queryText,
146156
queryType,
147157
clientTags);
148158
}
@@ -162,6 +172,7 @@ public String toString()
162172
.add("authenticatedUserFlags", authenticatedUserRegex.map(Pattern::flags))
163173
.add("sourceRegex", sourceRegex)
164174
.add("sourceFlags", sourceRegex.map(Pattern::flags))
175+
.add("queryText", queryText)
165176
.add("queryType", queryType)
166177
.add("clientTags", clientTags)
167178
.toString();

plugin/trino-resource-group-managers/src/main/java/io/trino/plugin/resourcegroups/StaticSelector.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public StaticSelector(
5353
Optional<Pattern> sourceRegex,
5454
Optional<List<String>> clientTags,
5555
Optional<SelectorResourceEstimate> selectorResourceEstimate,
56+
Optional<Pattern> queryText,
5657
Optional<String> queryType,
5758
ResourceGroupIdTemplate group)
5859
{
@@ -63,6 +64,7 @@ public StaticSelector(
6364
requireNonNull(sourceRegex, "sourceRegex is null");
6465
requireNonNull(clientTags, "clientTags is null");
6566
requireNonNull(selectorResourceEstimate, "selectorResourceEstimate is null");
67+
requireNonNull(queryText, "queryText is null");
6668
requireNonNull(queryType, "queryType is null");
6769
this.group = requireNonNull(group, "group is null");
6870

@@ -86,6 +88,7 @@ public StaticSelector(
8688
}))
8789
.add(userGroupRegex.map(userGroupRegexValue ->
8890
new BasicMatcher(criteria -> criteria.getUserGroups().stream().anyMatch(userGroup -> userGroupRegexValue.matcher(userGroup).matches()))))
91+
.add(queryText.map(queryTextValue -> new PatternMatcher(variableNames, queryTextValue, SelectionCriteria::getQueryText)))
8992
.add(queryType.map(queryTypeValue ->
9093
new BasicMatcher(criteria -> queryTypeValue.equalsIgnoreCase(criteria.getQueryType().orElse("")))))
9194
.add(selectorResourceEstimate.map(selectorResourceEstimateValue ->

plugin/trino-resource-group-managers/src/main/java/io/trino/plugin/resourcegroups/db/DbResourceGroupConfigurationManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ private synchronized Map.Entry<ManagerSpec, Map<ResourceGroupIdTemplate, Resourc
388388
selectorRecord.getOriginalUserRegex(),
389389
selectorRecord.getAuthenticatedUserRegex(),
390390
selectorRecord.getSourceRegex(),
391+
selectorRecord.getQueryText(),
391392
selectorRecord.getQueryType(),
392393
selectorRecord.getClientTags(),
393394
selectorRecord.getSelectorResourceEstimate(),

plugin/trino-resource-group-managers/src/main/java/io/trino/plugin/resourcegroups/db/ResourceGroupsDao.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public interface ResourceGroupsDao
6262
@UseRowMapper(ResourceGroupSpecBuilder.Mapper.class)
6363
List<ResourceGroupSpecBuilder> getResourceGroups(@Bind("environment") String environment);
6464

65-
@SqlQuery("SELECT S.resource_group_id, S.priority, S.user_regex, S.source_regex, S.original_user_regex, S.authenticated_user_regex, S.query_type, S.client_tags, S.selector_resource_estimate, S.user_group_regex\n" +
65+
@SqlQuery("SELECT S.resource_group_id, S.priority, S.user_regex, S.source_regex, S.original_user_regex, S.authenticated_user_regex, S.query_text, S.query_type, S.client_tags, S.selector_resource_estimate, S.user_group_regex\n" +
6666
"FROM selectors S\n" +
6767
"JOIN resource_groups R ON (S.resource_group_id = R.resource_group_id)\n" +
6868
"WHERE R.environment = :environment\n" +
@@ -78,6 +78,7 @@ public interface ResourceGroupsDao
7878
" original_user_regex VARCHAR(512),\n" +
7979
" authenticated_user_regex VARCHAR(512),\n" +
8080
" source_regex VARCHAR(512),\n" +
81+
" query_text VARCHAR(512),\n" +
8182
" query_type VARCHAR(512),\n" +
8283
" client_tags VARCHAR(512),\n" +
8384
" selector_resource_estimate VARCHAR(1024),\n" +

plugin/trino-resource-group-managers/src/main/java/io/trino/plugin/resourcegroups/db/SelectorRecord.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class SelectorRecord
3838
private final Optional<Pattern> originalUserRegex;
3939
private final Optional<Pattern> authenticatedUserRegex;
4040
private final Optional<Pattern> sourceRegex;
41+
private final Optional<Pattern> queryText;
4142
private final Optional<String> queryType;
4243
private final Optional<List<String>> clientTags;
4344
private final Optional<SelectorResourceEstimate> selectorResourceEstimate;
@@ -50,6 +51,7 @@ public SelectorRecord(
5051
Optional<Pattern> originalUserRegex,
5152
Optional<Pattern> authenticatedUserRegex,
5253
Optional<Pattern> sourceRegex,
54+
Optional<Pattern> queryText,
5355
Optional<String> queryType,
5456
Optional<List<String>> clientTags,
5557
Optional<SelectorResourceEstimate> selectorResourceEstimate)
@@ -61,6 +63,7 @@ public SelectorRecord(
6163
this.originalUserRegex = requireNonNull(originalUserRegex, "originalUserRegex is null");
6264
this.authenticatedUserRegex = requireNonNull(authenticatedUserRegex, "authenticatedUserRegex is null");
6365
this.sourceRegex = requireNonNull(sourceRegex, "sourceRegex is null");
66+
this.queryText = requireNonNull(queryText, "queryText is null");
6467
this.queryType = requireNonNull(queryType, "queryType is null");
6568
this.clientTags = clientTags.map(ImmutableList::copyOf);
6669
this.selectorResourceEstimate = requireNonNull(selectorResourceEstimate, "selectorResourceEstimate is null");
@@ -101,6 +104,11 @@ public Optional<Pattern> getSourceRegex()
101104
return sourceRegex;
102105
}
103106

107+
public Optional<Pattern> getQueryText()
108+
{
109+
return queryText;
110+
}
111+
104112
public Optional<String> getQueryType()
105113
{
106114
return queryType;
@@ -134,6 +142,7 @@ public SelectorRecord map(ResultSet resultSet, StatementContext context)
134142
Optional.ofNullable(resultSet.getString("original_user_regex")).map(Pattern::compile),
135143
Optional.ofNullable(resultSet.getString("authenticated_user_regex")).map(Pattern::compile),
136144
Optional.ofNullable(resultSet.getString("source_regex")).map(Pattern::compile),
145+
Optional.ofNullable(resultSet.getString("query_text")).map(Pattern::compile),
137146
Optional.ofNullable(resultSet.getString("query_type")),
138147
Optional.ofNullable(resultSet.getString("client_tags")).map(LIST_STRING_CODEC::fromJson),
139148
Optional.ofNullable(resultSet.getString("selector_resource_estimate")).map(SELECTOR_RESOURCE_ESTIMATE_JSON_CODEC::fromJson));

0 commit comments

Comments
 (0)