Skip to content

Commit 5bdb794

Browse files
authored
Merge pull request #413 from ArangoDB-Community/feature3.10/DE-341-query-rules
Added support for new endpoint `GET /_api/query/rules` that returns the available optimizer rules for AQL queries.
2 parents b469117 + 2446f44 commit 5bdb794

File tree

8 files changed

+140
-3
lines changed

8 files changed

+140
-3
lines changed

arangodb-net-standard.Test/AqlFunctionApi/AqlFunctionApiClientTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,23 @@ await _fixture.AqlFunctionClient.PutChangeQueryTrackingConfigurationAsync(
225225
Assert.False(putResponse.Error);
226226
Assert.Equal(HttpStatusCode.OK, putResponse.Code);
227227
}
228+
229+
230+
[Fact]
231+
public async Task GetQueryRulesAsync_ShouldSucceed()
232+
{
233+
if (_fixture.VersionMajor >= 3 && _fixture.VersionMinor >= 10)
234+
{
235+
var getResponse =
236+
await _fixture.AqlFunctionClient.GetQueryRulesAsync();
237+
238+
Assert.NotNull(getResponse);
239+
Assert.All(getResponse, x =>
240+
{
241+
Assert.NotNull(x.Name);
242+
Assert.NotNull(x.Flags);
243+
});
244+
}
245+
}
228246
}
229247
}

arangodb-net-standard.Test/AqlFunctionApi/AqlFunctionApiClientTestFixture.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Threading.Tasks;
66
using System.Collections.Generic;
7+
using ArangoDBNetStandard.AdminApi.Models;
78

89
namespace ArangoDBNetStandardTest.AqlFunctionApi
910
{
@@ -15,6 +16,9 @@ public class AqlFunctionApiClientTestFixture : ApiClientTestFixtureBase
1516
public AqlFunctionApiClient AqlFunctionClient { get; set; }
1617
public string TestCollectionName { get; internal set; } = "Pets";
1718
public string TestAqlQuery { get; internal set; }
19+
public GetServerVersionResponse ServerVersion { get; internal set; }
20+
public int VersionMajor { get; internal set; }
21+
public int VersionMinor { get; internal set; }
1822

1923
public override async Task InitializeAsync()
2024
{
@@ -23,7 +27,17 @@ public override async Task InitializeAsync()
2327
await CreateDatabase(dbName);
2428
var arangoClient = GetArangoDBClient(dbName);
2529
AqlFunctionClient = arangoClient.AqlFunction;
26-
30+
ServerVersion = await arangoClient.Admin.GetServerVersionAsync(
31+
new GetServerVersionQuery()
32+
{
33+
Details = true
34+
});
35+
if (ServerVersion != null)
36+
{
37+
var vParts = ServerVersion.Version.Split(".");
38+
VersionMajor = int.Parse(vParts[0]);
39+
VersionMinor = int.Parse(vParts[1]);
40+
}
2741
try
2842
{
2943
//create one collection for our tests...

arangodb-net-standard/AdminApi/Models/GetServerVersionResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class GetServerVersionResponse
2020
/// <summary>
2121
/// The version of the server.
2222
/// </summary>
23-
public string Version { get; set; }
23+
public string Version { get; set; }
2424

2525
/// <summary>
2626
/// Additional details about the DB server.

arangodb-net-standard/AqlFunctionApi/AqlFunctionApiClient.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ArangoDBNetStandard.AqlFunctionApi.Models;
22
using ArangoDBNetStandard.Serialization;
33
using ArangoDBNetStandard.Transport;
4+
using System.Collections;
45
using System.Collections.Generic;
56
using System.Net;
67
using System.Threading;
@@ -507,5 +508,30 @@ public virtual async Task<List<RunningAqlQuery>> GetCurrentlyRunningAqlQueriesAs
507508
throw await GetApiErrorException(response).ConfigureAwait(false);
508509
}
509510
}
511+
512+
/// <summary>
513+
/// Gets the available optimizer rules for AQL queries
514+
/// GET /_api/query/rules
515+
/// </summary>
516+
/// <param name="token">A CancellationToken to observe while waiting for the task to complete or to cancel the task.</param>
517+
/// <remarks>
518+
/// Returns an array of objects that contain the name of each available
519+
/// rule and its respective flags.
520+
/// </remarks>
521+
/// <returns></returns>
522+
public virtual async Task<List<GetQueryRule>> GetQueryRulesAsync(CancellationToken token = default)
523+
{
524+
string uri = "_api/query/rules";
525+
526+
using (var response = await _transport.GetAsync(uri).ConfigureAwait(false))
527+
{
528+
if (response.IsSuccessStatusCode)
529+
{
530+
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
531+
return DeserializeJsonFromStream<List<GetQueryRule>>(stream);
532+
}
533+
throw await GetApiErrorException(response).ConfigureAwait(false);
534+
}
535+
}
510536
}
511537
}

arangodb-net-standard/AqlFunctionApi/IAqlFunctionApiClient.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,5 +219,18 @@ Task<QueryTrackingConfiguration> PutChangeQueryTrackingConfigurationAsync(
219219
Task<List<RunningAqlQuery>> GetCurrentlyRunningAqlQueriesAsync(
220220
GetCurrentlyRunningAqlQueriesQuery query = null,
221221
CancellationToken token = default);
222+
223+
/// <summary>
224+
/// Gets the available optimizer rules for AQL queries
225+
/// GET /_api/query/rules
226+
/// </summary>
227+
/// <param name="token">A CancellationToken to observe while waiting for the task to complete or to cancel the task.</param>
228+
/// <remarks>
229+
/// Returns an array of objects that contain the name of each available
230+
/// rule and its respective flags.
231+
/// </remarks>
232+
/// <returns></returns>
233+
Task<List<GetQueryRule>> GetQueryRulesAsync(
234+
CancellationToken token = default);
222235
}
223236
}

arangodb-net-standard/AqlFunctionApi/Models/GetAqlFunctionsResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ public class GetAqlFunctionsResponse
3232
/// </summary>
3333
public IList<AqlFunctionResult> Result { get; set; }
3434
}
35-
}
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.Generic;
2+
3+
namespace ArangoDBNetStandard.AqlFunctionApi.Models
4+
{
5+
/// <summary>
6+
/// Describes an AQL optimizer rule.
7+
/// This is a response item returned by
8+
/// <see cref="IAqlFunctionApiClient.GetQueryRulesAsync(System.Threading.CancellationToken)"/>
9+
/// </summary>
10+
public class GetQueryRule
11+
{
12+
/// <summary>
13+
/// The name of the optimizer rule as seen in query explain outputs.
14+
/// </summary>
15+
public string Name { get; set; }
16+
17+
/// <summary>
18+
/// An object with the properties of the rule.
19+
/// </summary>
20+
public GetQueryRuleFlags Flags { get; set; }
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace ArangoDBNetStandard.AqlFunctionApi.Models
2+
{
3+
/// <summary>
4+
/// Describes the properties of an AQL optimizer rule.
5+
/// </summary>
6+
public class GetQueryRuleFlags
7+
{
8+
/// <summary>
9+
/// Whether the rule is displayed to users.
10+
/// Internal rules are hidden.
11+
/// </summary>
12+
public bool Hidden { get; set; }
13+
14+
/// <summary>
15+
/// Whether the rule is applicable in the
16+
/// cluster deployment mode only.
17+
/// </summary>
18+
public bool ClusterOnly { get; set; }
19+
20+
/// <summary>
21+
/// Whether users are allowed to disable
22+
/// this rule. A few rules are mandatory.
23+
/// </summary>
24+
public bool CanBeDisabled { get; set; }
25+
26+
/// <summary>
27+
/// Whether this rule may create additional
28+
/// query execution plans.
29+
/// </summary>
30+
public bool CanCreateAdditionalPlans { get; set; }
31+
32+
/// <summary>
33+
/// Whether the optimizer considers this
34+
/// rule by default.
35+
/// </summary>
36+
public bool DisabledByDefault { get; set; }
37+
38+
/// <summary>
39+
/// Whether the rule is available in the
40+
/// Enterprise Edition only.
41+
/// </summary>
42+
public bool EnterpriseOnly { get; set; }
43+
}
44+
}

0 commit comments

Comments
 (0)