public override CosmosResultSetIterator CreateItemQueryAsStream( CosmosSqlQueryDefinition sqlQueryDefinition, int maxConcurrency, object partitionKey = null, int?maxItemCount = null, string continuationToken = null, CosmosQueryRequestOptions requestOptions = null) { requestOptions = requestOptions ?? new CosmosQueryRequestOptions(); requestOptions.MaxConcurrency = maxConcurrency; requestOptions.EnableCrossPartitionQuery = true; requestOptions.RequestContinuation = continuationToken; requestOptions.MaxItemCount = maxItemCount; requestOptions.PartitionKey = partitionKey; IDocumentQueryExecutionContext documentQueryExecution = new CosmosQueryExecutionContextFactory( client: this.queryClient, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(CosmosQueryResponse), sqlQuerySpec: sqlQueryDefinition.ToSqlQuerySpec(), queryRequestOptions: requestOptions, resourceLink: this.container.LinkUri, isContinuationExpected: true, allowNonValueAggregateQuery: true, correlatedActivityId: Guid.NewGuid()); return(new CosmosResultSetIteratorCore( maxItemCount, continuationToken, requestOptions, this.QueryRequestExecutor, documentQueryExecution)); }
public override FeedIterator <T> CreateItemQuery <T>( CosmosSqlQueryDefinition sqlQueryDefinition, int maxConcurrency, int?maxItemCount = null, string continuationToken = null, QueryRequestOptions requestOptions = null) { requestOptions = requestOptions ?? new QueryRequestOptions(); requestOptions.EnableCrossPartitionQuery = true; requestOptions.RequestContinuation = continuationToken; requestOptions.MaxItemCount = maxItemCount; requestOptions.MaxConcurrency = maxConcurrency; CosmosQueryExecutionContext cosmosQueryExecution = new CosmosQueryExecutionContextFactory( client: this.queryClient, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(T), sqlQuerySpec: sqlQueryDefinition.ToSqlQuerySpec(), queryRequestOptions: requestOptions, resourceLink: this.LinkUri, isContinuationExpected: true, allowNonValueAggregateQuery: true, correlatedActivityId: Guid.NewGuid()); return(new FeedIteratorCore <T>( maxItemCount, continuationToken, requestOptions, this.NextResultSetAsync <T>, cosmosQueryExecution)); }
public override FeedIterator <T> GetItemQueryIterator <T>( QueryDefinition sqlQueryDefinition, string continuationToken = null, QueryRequestOptions requestOptions = null) { requestOptions = requestOptions ?? new QueryRequestOptions(); CosmosQueryExecutionContext cosmosQueryExecution = new CosmosQueryExecutionContextFactory( client: this.queryClient, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(T), sqlQuerySpec: sqlQueryDefinition.ToSqlQuerySpec(), continuationToken: continuationToken, queryRequestOptions: requestOptions, resourceLink: this.LinkUri, isContinuationExpected: true, allowNonValueAggregateQuery: true, correlatedActivityId: Guid.NewGuid()); return(new FeedIteratorCore <T>( requestOptions.MaxItemCount, continuationToken, requestOptions, this.NextResultSetAsync <T>, cosmosQueryExecution)); }
public async Task TestCosmosQueryPartitionKeyDefinition() { PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); QueryRequestOptions queryRequestOptions = new QueryRequestOptions { Properties = new Dictionary <string, object>() { { "x-ms-query-partitionkey-definition", partitionKeyDefinition } } }; SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(@"select * from t where t.something = 42 "); bool allowNonValueAggregateQuery = true; bool isContinuationExpected = true; CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); CancellationToken cancellationtoken = cancellationTokenSource.Token; Mock <CosmosQueryClient> client = new Mock <CosmosQueryClient>(); client.Setup(x => x.GetCachedContainerQueryPropertiesAsync(It.IsAny <Uri>(), It.IsAny <Cosmos.PartitionKey?>(), cancellationtoken)).Returns(Task.FromResult(new ContainerQueryProperties("mockContainer", null, partitionKeyDefinition))); client.Setup(x => x.ByPassQueryParsing()).Returns(false); client.Setup(x => x.GetPartitionedQueryExecutionInfoAsync( sqlQuerySpec, partitionKeyDefinition, true, isContinuationExpected, allowNonValueAggregateQuery, false, // has logical partition key cancellationtoken)).Throws(new InvalidOperationException("Verified that the PartitionKeyDefinition was correctly set. Cancel the rest of the query")); CosmosQueryExecutionContextFactory.InputParameters inputParameters = new CosmosQueryExecutionContextFactory.InputParameters() { SqlQuerySpec = sqlQuerySpec, InitialUserContinuationToken = null, MaxBufferedItemCount = queryRequestOptions?.MaxBufferedItemCount, MaxConcurrency = queryRequestOptions?.MaxConcurrency, MaxItemCount = queryRequestOptions?.MaxItemCount, PartitionKey = queryRequestOptions?.PartitionKey, Properties = queryRequestOptions?.Properties }; CosmosQueryContext cosmosQueryContext = new CosmosQueryContextCore( client: client.Object, queryRequestOptions: queryRequestOptions, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(QueryResponse), resourceLink: new Uri("dbs/mockdb/colls/mockColl", UriKind.Relative), isContinuationExpected: isContinuationExpected, allowNonValueAggregateQuery: allowNonValueAggregateQuery, correlatedActivityId: new Guid("221FC86C-1825-4284-B10E-A6029652CCA6")); CosmosQueryExecutionContextFactory factory = new CosmosQueryExecutionContextFactory( cosmosQueryContext: cosmosQueryContext, inputParameters: inputParameters); await factory.ExecuteNextAsync(cancellationtoken); }
public async Task TestCosmosQueryPartitionKeyDefinition() { PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); QueryRequestOptions queryRequestOptions = new QueryRequestOptions(); queryRequestOptions.Properties = new Dictionary <string, object>() { { "x-ms-query-partitionkey-definition", partitionKeyDefinition } }; SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(@"select * from t where t.something = 42 "); bool allowNonValueAggregateQuery = true; bool isContinuationExpected = true; CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); CancellationToken cancellationtoken = cancellationTokenSource.Token; Mock <CollectionCache> mockCollectionCache = new Mock <CollectionCache>(); mockCollectionCache.Setup(x => x.ResolveCollectionAsync(It.IsAny <DocumentServiceRequest>(), cancellationtoken)).Returns(Task.FromResult(new ContainerProperties("mockContainer", "/pk"))); Mock <CosmosQueryClient> client = new Mock <CosmosQueryClient>(); client.Setup(x => x.GetCollectionCacheAsync()).Returns(Task.FromResult(mockCollectionCache.Object)); client.Setup(x => x.ByPassQueryParsing()).Returns(false); client.Setup(x => x.GetPartitionedQueryExecutionInfoAsync( sqlQuerySpec, partitionKeyDefinition, true, isContinuationExpected, allowNonValueAggregateQuery, false, // has logical partition key cancellationtoken)).Throws(new InvalidOperationException("Verified that the PartitionKeyDefinition was correctly set. Cancel the rest of the query")); CosmosQueryExecutionContextFactory factory = new CosmosQueryExecutionContextFactory( client: client.Object, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(QueryResponse), sqlQuerySpec: sqlQuerySpec, continuationToken: null, queryRequestOptions: queryRequestOptions, resourceLink: new Uri("dbs/mockdb/colls/mockColl", UriKind.Relative), isContinuationExpected: isContinuationExpected, allowNonValueAggregateQuery: allowNonValueAggregateQuery, correlatedActivityId: new Guid("221FC86C-1825-4284-B10E-A6029652CCA6")); await factory.ExecuteNextAsync(cancellationtoken); }
private CosmosQueryExecutionContext CreateCosmosQueryExecutionContext() { CosmosQueryExecutionContext cosmosQueryExecution = new CosmosQueryExecutionContextFactory( client: this.queryClient, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(T), sqlQuerySpec: DocumentQueryEvaluator.Evaluate(expression), queryRequestOptions: this.cosmosQueryRequestOptions, resourceLink: this.container.LinkUri, isContinuationExpected: true, allowNonValueAggregateQuery: true, correlatedActivityId: Guid.NewGuid()); return(cosmosQueryExecution); }
public async Task GetTargetPartitionKeyRangesAsyncWithFeedRange() { ContainerInternal container = null; try { // Create a container large enough to have at least 2 partitions ContainerResponse containerResponse = await this.database.CreateContainerAsync( id : Guid.NewGuid().ToString(), partitionKeyPath : "/pk", throughput : 15000); container = (ContainerInlineCore)containerResponse; // Get all the partition key ranges to verify there is more than one partition IRoutingMapProvider routingMapProvider = await this.cosmosClient.DocumentClient.GetPartitionKeyRangeCacheAsync(NoOpTrace.Singleton); ContainerQueryProperties containerQueryProperties = new ContainerQueryProperties( containerResponse.Resource.ResourceId, null, containerResponse.Resource.PartitionKey); IReadOnlyList <FeedRange> feedTokens = await container.GetFeedRangesAsync(); Assert.IsTrue(feedTokens.Count > 1, " RUs of the container needs to be increased to ensure at least 2 partitions."); // There should only be one range since we get 1 FeedRange per range foreach (FeedRange feedToken in feedTokens) { List <PartitionKeyRange> partitionKeyRanges = await CosmosQueryExecutionContextFactory.GetTargetPartitionKeyRangesAsync( queryClient : new CosmosQueryClientCore(container.ClientContext, container), resourceLink : container.LinkUri, partitionedQueryExecutionInfo : null, containerQueryProperties : containerQueryProperties, properties : null, feedRangeInternal : feedToken as FeedRangeInternal, NoOpTrace.Singleton); Assert.IsTrue(partitionKeyRanges.Count == 1, "Only 1 partition key range should be selected since the FeedRange represents a single range."); } } finally { await container?.DeleteContainerAsync(); } }
public override FeedIterator <T> GetItemQueryIterator <T>( QueryDefinition queryDefinition, string continuationToken = null, QueryRequestOptions requestOptions = null) { requestOptions = requestOptions ?? new QueryRequestOptions(); if (requestOptions.IsEffectivePartitionKeyRouting) { requestOptions.PartitionKey = null; } if (queryDefinition == null) { FeedStatelessIteratorCore feedStatelessIterator = new FeedStatelessIteratorCore( this.ClientContext, this.LinkUri, resourceType: ResourceType.Document, queryDefinition: null, continuationToken: continuationToken, options: requestOptions); return(new FeedStatelessIteratorCore <T>(feedStatelessIterator, responseCreator: this.ClientContext.ResponseFactory.CreateResultSetQueryResponse <T>)); } CosmosQueryExecutionContext cosmosQueryExecution = new CosmosQueryExecutionContextFactory( client: this.queryClient, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(T), sqlQuerySpec: queryDefinition.ToSqlQuerySpec(), continuationToken: continuationToken, queryRequestOptions: requestOptions, resourceLink: this.LinkUri, isContinuationExpected: true, allowNonValueAggregateQuery: true, correlatedActivityId: Guid.NewGuid()); return(new FeedIteratorCore <T>( requestOptions.MaxItemCount, continuationToken, requestOptions, this.NextResultSetAsync <T>, cosmosQueryExecution)); }
public async Task TestCosmosQueryPartitionKeyDefinition() { PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); QueryRequestOptions queryRequestOptions = new QueryRequestOptions { Properties = new Dictionary <string, object>() { { "x-ms-query-partitionkey-definition", partitionKeyDefinition } } }; SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(@"select * from t where t.something = 42 "); bool allowNonValueAggregateQuery = true; bool isContinuationExpected = true; CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); CancellationToken cancellationtoken = cancellationTokenSource.Token; Mock <CosmosQueryClient> client = new Mock <CosmosQueryClient>(); string exceptionMessage = "Verified that the PartitionKeyDefinition was correctly set. Cancel the rest of the query"; client .Setup(x => x.GetCachedContainerQueryPropertiesAsync(It.IsAny <string>(), It.IsAny <Cosmos.PartitionKey?>(), cancellationtoken)) .ReturnsAsync(new ContainerQueryProperties("mockContainer", null, partitionKeyDefinition)); client .Setup(x => x.ByPassQueryParsing()) .Returns(false); client .Setup(x => x.TryGetPartitionedQueryExecutionInfoAsync( It.IsAny <SqlQuerySpec>(), It.IsAny <PartitionKeyDefinition>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())) .ReturnsAsync(TryCatch <PartitionedQueryExecutionInfo> .FromException( new InvalidOperationException( exceptionMessage))); CosmosQueryExecutionContextFactory.InputParameters inputParameters = new CosmosQueryExecutionContextFactory.InputParameters( sqlQuerySpec: sqlQuerySpec, initialUserContinuationToken: null, initialFeedRange: null, maxConcurrency: queryRequestOptions?.MaxConcurrency, maxItemCount: queryRequestOptions?.MaxItemCount, maxBufferedItemCount: queryRequestOptions?.MaxBufferedItemCount, partitionKey: queryRequestOptions?.PartitionKey, properties: queryRequestOptions?.Properties, partitionedQueryExecutionInfo: null, executionEnvironment: queryRequestOptions?.ExecutionEnvironment, returnResultsInDeterministicOrder: true, forcePassthrough: false, testInjections: queryRequestOptions?.TestSettings); CosmosQueryContext cosmosQueryContext = new CosmosQueryContextCore( client: client.Object, queryRequestOptions: queryRequestOptions, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(QueryResponse), resourceLink: "dbs/mockdb/colls/mockColl", isContinuationExpected: isContinuationExpected, allowNonValueAggregateQuery: allowNonValueAggregateQuery, diagnosticsContext: new CosmosDiagnosticsContextCore(), correlatedActivityId: new Guid("221FC86C-1825-4284-B10E-A6029652CCA6")); IQueryPipelineStage pipelineStage = CosmosQueryExecutionContextFactory.Create( documentContainer: null, cosmosQueryContext, inputParameters); Assert.IsTrue(await pipelineStage.MoveNextAsync()); TryCatch <QueryPage> tryGetPage = pipelineStage.Current; Assert.IsTrue(tryGetPage.Failed); Assert.AreEqual(HttpStatusCode.BadRequest, (tryGetPage.InnerMostException as CosmosException).StatusCode); Assert.IsTrue( (tryGetPage.InnerMostException as CosmosException).ToString().Contains(exceptionMessage), "response error message did not contain the proper substring."); }
public async Task TestCosmosQueryPartitionKeyDefinition() { PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition(); QueryRequestOptions queryRequestOptions = new QueryRequestOptions { Properties = new Dictionary <string, object>() { { "x-ms-query-partitionkey-definition", partitionKeyDefinition } } }; SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(@"select * from t where t.something = 42 "); bool allowNonValueAggregateQuery = true; bool isContinuationExpected = true; CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); CancellationToken cancellationtoken = cancellationTokenSource.Token; Mock <CosmosQueryClient> client = new Mock <CosmosQueryClient>(); string exceptionMessage = "Verified that the PartitionKeyDefinition was correctly set. Cancel the rest of the query"; client .Setup(x => x.GetCachedContainerQueryPropertiesAsync(It.IsAny <Uri>(), It.IsAny <Cosmos.PartitionKey?>(), cancellationtoken)) .ReturnsAsync(new ContainerQueryProperties("mockContainer", null, partitionKeyDefinition)); client .Setup(x => x.ByPassQueryParsing()) .Returns(false); client .Setup(x => x.TryGetPartitionedQueryExecutionInfoAsync( It.IsAny <SqlQuerySpec>(), It.IsAny <PartitionKeyDefinition>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <bool>(), It.IsAny <CancellationToken>())) .ReturnsAsync(TryCatch <PartitionedQueryExecutionInfo> .FromException( new InvalidOperationException( exceptionMessage))); CosmosQueryExecutionContextFactory.InputParameters inputParameters = new CosmosQueryExecutionContextFactory.InputParameters( sqlQuerySpec: sqlQuerySpec, initialUserContinuationToken: null, maxConcurrency: queryRequestOptions?.MaxConcurrency, maxItemCount: queryRequestOptions?.MaxItemCount, maxBufferedItemCount: queryRequestOptions?.MaxBufferedItemCount, partitionKey: queryRequestOptions?.PartitionKey, properties: queryRequestOptions?.Properties, partitionedQueryExecutionInfo: null, executionEnvironment: queryRequestOptions?.ExecutionEnvironment, testInjections: queryRequestOptions?.TestSettings); CosmosQueryContext cosmosQueryContext = new CosmosQueryContextCore( client: client.Object, queryRequestOptions: queryRequestOptions, resourceTypeEnum: ResourceType.Document, operationType: OperationType.Query, resourceType: typeof(QueryResponse), resourceLink: new Uri("dbs/mockdb/colls/mockColl", UriKind.Relative), isContinuationExpected: isContinuationExpected, allowNonValueAggregateQuery: allowNonValueAggregateQuery, correlatedActivityId: new Guid("221FC86C-1825-4284-B10E-A6029652CCA6")); CosmosQueryExecutionContext context = CosmosQueryExecutionContextFactory.Create( cosmosQueryContext, inputParameters); QueryResponseCore queryResponse = await context.ExecuteNextAsync(cancellationtoken); Assert.AreEqual(HttpStatusCode.BadRequest, queryResponse.StatusCode); Assert.IsTrue(queryResponse.ErrorMessage.Contains(exceptionMessage)); }
public override PassThroughQueryTestOutput ExecuteTest(PassThroughQueryTestInput input) { // gets DocumentContainer IMonadicDocumentContainer monadicDocumentContainer = new InMemoryContainer(input.PartitionKeyDefinition); DocumentContainer documentContainer = new DocumentContainer(monadicDocumentContainer); SqlQuerySpec sqlQuerySpec = new SqlQuerySpec(input.Query); // gets query context string databaseId = "db1234"; string resourceLink = string.Format("dbs/{0}/colls", databaseId); CosmosQueryContextCore cosmosQueryContextCore = new CosmosQueryContextCore( client: new TestCosmosQueryClient(), resourceTypeEnum: Documents.ResourceType.Document, operationType: Documents.OperationType.Query, resourceType: typeof(QueryResponseCore), resourceLink: resourceLink, isContinuationExpected: false, allowNonValueAggregateQuery: true, useSystemPrefix: false, correlatedActivityId: Guid.NewGuid()); // gets input parameters QueryRequestOptions queryRequestOptions = new QueryRequestOptions(); CosmosSerializerCore serializerCore = new(); using StreamReader streamReader = new(serializerCore.ToStreamSqlQuerySpec(sqlQuerySpec, Documents.ResourceType.Document)); string sqlQuerySpecJsonString = streamReader.ReadToEnd(); PartitionedQueryExecutionInfo partitionedQueryExecutionInfo = GetPartitionedQueryExecutionInfo(sqlQuerySpecJsonString, input.PartitionKeyDefinition); if (input.PartitionKeyValue == default || input.PartitionKeyValue == Cosmos.PartitionKey.None) { input.PartitionKeyValue = Cosmos.PartitionKey.Null; } CosmosQueryExecutionContextFactory.InputParameters inputParameters = new CosmosQueryExecutionContextFactory.InputParameters( sqlQuerySpec: sqlQuerySpec, initialUserContinuationToken: null, initialFeedRange: null, maxConcurrency: queryRequestOptions.MaxConcurrency, maxItemCount: queryRequestOptions.MaxItemCount, maxBufferedItemCount: queryRequestOptions.MaxBufferedItemCount, partitionKey: input.PartitionKeyValue, properties: queryRequestOptions.Properties, partitionedQueryExecutionInfo: partitionedQueryExecutionInfo, executionEnvironment: null, returnResultsInDeterministicOrder: null, forcePassthrough: true, testInjections: null); IQueryPipelineStage queryPipelineStage = CosmosQueryExecutionContextFactory.Create( documentContainer, cosmosQueryContextCore, inputParameters, NoOpTrace.Singleton); bool result = queryPipelineStage.MoveNextAsync(NoOpTrace.Singleton).Result; Assert.AreEqual(input.ExpectedPassThrough, inputParameters.SqlQuerySpec.PassThrough); Assert.IsNotNull(queryPipelineStage); Assert.IsTrue(result); return(new PassThroughQueryTestOutput(inputParameters.SqlQuerySpec.PassThrough)); }