Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmos: bad SQL generated when projecting out "Value" #33858

Closed
maumar opened this issue May 30, 2024 · 2 comments · Fixed by #33895
Closed

Cosmos: bad SQL generated when projecting out "Value" #33858

maumar opened this issue May 30, 2024 · 2 comments · Fixed by #33895
Assignees
Labels
area-cosmos area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented May 30, 2024

query:

ss.Set<Customer>().Select(c => new { Id = c.CustomerID, Value = c.CompanyName.Contains(c.ContactName) })

Sql we generate:

SELECT c["CustomerID"] AS Id, CONTAINS(c["CompanyName"], c["ContactName"]) AS Value
FROM root c
WHERE (c["Discriminator"] = "Customer")

result:

Message: 
Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: ; Reason: (Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: ; Reason: ({"errors":[{"severity":"Error","location":{"start":78,"end":83},"code":"SC1001","message":"Syntax error, incorrect syntax near 'Value'."}]}););
---- Microsoft.Azure.Cosmos.Query.Core.Exceptions.ExpectedQueryPartitionProviderException : {"errors":[{"severity":"Error","location":{"start":78,"end":83},"code":"SC1001","message":"Syntax error, incorrect syntax near 'Value'."}]}
-------- System.Runtime.InteropServices.COMException : 0x800A0B00

  Stack Trace: 
QueryPartitionProvider.TryGetPartitionedQueryExecutionInfo(String querySpecJsonString, PartitionKeyDefinition partitionKeyDefinition, Boolean requireFormattableOrderByQuery, Boolean isContinuationExpected, Boolean allowNonValueAggregateQuery, Boolean hasLogicalPartitionKey, Boolean allowDCount, Boolean useSystemPrefix, GeospatialType geospatialType)
CosmosQueryClientCore.TryGetPartitionedQueryExecutionInfoAsync(SqlQuerySpec sqlQuerySpec, ResourceType resourceType, PartitionKeyDefinition partitionKeyDefinition, Boolean requireFormattableOrderByQuery, Boolean isContinuationExpected, Boolean allowNonValueAggregateQuery, Boolean hasLogicalPartitionKey, Boolean allowDCount, Boolean useSystemPrefix, GeospatialType geospatialType, CancellationToken cancellationToken)
AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
CosmosQueryClientCore.TryGetPartitionedQueryExecutionInfoAsync(SqlQuerySpec sqlQuerySpec, ResourceType resourceType, PartitionKeyDefinition partitionKeyDefinition, Boolean requireFormattableOrderByQuery, Boolean isContinuationExpected, Boolean allowNonValueAggregateQuery, Boolean hasLogicalPartitionKey, Boolean allowDCount, Boolean useSystemPrefix, GeospatialType geospatialType, CancellationToken cancellationToken)
QueryPlanHandler.TryGetQueryInfoAsync(SqlQuerySpec sqlQuerySpec, ResourceType resourceType, PartitionKeyDefinition partitionKeyDefinition, Boolean hasLogicalPartitionKey, Boolean useSystemPrefix, GeospatialType geospatialType, CancellationToken cancellationToken)
QueryPlanHandler.TryGetQueryPlanAsync(SqlQuerySpec sqlQuerySpec, ResourceType resourceType, PartitionKeyDefinition partitionKeyDefinition, QueryFeatures supportedQueryFeatures, Boolean hasLogicalPartitionKey, Boolean useSystemPrefix, GeospatialType geospatialType, CancellationToken cancellationToken)
AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
QueryPlanHandler.TryGetQueryPlanAsync(SqlQuerySpec sqlQuerySpec, ResourceType resourceType, PartitionKeyDefinition partitionKeyDefinition, QueryFeatures supportedQueryFeatures, Boolean hasLogicalPartitionKey, Boolean useSystemPrefix, GeospatialType geospatialType, CancellationToken cancellationToken)
QueryPlanRetriever.GetQueryPlanWithServiceInteropAsync(CosmosQueryClient queryClient, SqlQuerySpec sqlQuerySpec, ResourceType resourceType, PartitionKeyDefinition partitionKeyDefinition, Boolean hasLogicalPartitionKey, GeospatialType geospatialType, Boolean useSystemPrefix, ITrace trace, CancellationToken cancellationToken)
AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
<32 more frames...>
Task`1.TrySetResult(TResult result)
UnwrapPromise`1.TrySetFromTask(Task task, Boolean lookForOce)
UnwrapPromise`1.ProcessInnerTask(Task task)
Task.RunContinuations(Object continuationObject)
Task.FinishSlow(Boolean userDelegateExecute)
Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
ThreadPoolWorkQueue.Dispatch()
WorkerThread.WorkerThreadStart()
----- Inner Stack Trace -----
----- Inner Stack Trace -----

  Standard Output: 
Compiling query expression: 
'DbSet<Customer>()
    .Select(c => new { 
        Id = c.CustomerID, 
        Value = c.CompanyName.Contains(c.ContactName)
     })'

Generated query execution expression: 
'queryContext => new QueryingEnumerable<<>f__AnonymousType71<string, bool>>(
    (CosmosQueryContext)queryContext, 
    SqlExpressionFactory, 
    QuerySqlGeneratorFactory, 
    [Cosmos.Query.Internal.SelectExpression], 
    Func<QueryContext, JObject, <>f__AnonymousType71<string, bool>>, 
    TestModels.Northwind.NorthwindContext, 
    None, 
    False, 
    True
)'

Executing SQL query for container 'NorthwindContext' in partition 'None' [Parameters=[]]
SELECT c["CustomerID"] AS Id, CONTAINS(c["CompanyName"], c["ContactName"]) AS Value
FROM root c
WHERE (c["Discriminator"] = "Customer")

Executed ReadNext (86.5268 ms, 0 RU) ActivityId='(null)', Container='NorthwindContext', Partition='None', Parameters=[]
SELECT c["CustomerID"] AS Id, CONTAINS(c["CompanyName"], c["ContactName"]) AS Value
FROM root c
WHERE (c["Discriminator"] = "Customer")

An exception occurred while iterating over the results of a query for context type 'Microsoft.EntityFrameworkCore.TestModels.Northwind.NorthwindContext'.
Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: ; Reason: (Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: ; Reason: ({"errors":[{"severity":"Error","location":{"start":78,"end":83},"code":"SC1001","message":"Syntax error, incorrect syntax near 'Value'."}]}););
 ---> Microsoft.Azure.Cosmos.Query.Core.Exceptions.ExpectedQueryPartitionProviderException: {"errors":[{"severity":"Error","location":{"start":78,"end":83},"code":"SC1001","message":"Syntax error, incorrect syntax near 'Value'."}]}
 ---> System.Runtime.InteropServices.COMException (0x800A0B00): 0x800A0B00
   --- End of inner exception stack trace ---

@roji
Copy link
Member

roji commented May 31, 2024

Yeah, I just encountered this while working on the Cosmos query pipeline - Cosmos disallows projecting something with the alias Value using AS (case-insensitive). However, it's possible to use "JSON object projection style" instead:

SELECT VALUE {
    "Id": c["CustomerID"],
    "Value": CONTAINS(c["CompanyName"], c["ContactName"])
}

This is already fixed as part of my upcoming PR on primitive collections.

@roji roji changed the title Cosmos: bad sql generated when using string.Contains in a projection Cosmos: bad SQL generated when projecting out "Value" May 31, 2024
@roji roji added this to the 9.0.0 milestone May 31, 2024
@maumar
Copy link
Contributor Author

maumar commented May 31, 2024

@roji make sure to enable the test that I skipped with this issue number

roji added a commit to roji/efcore that referenced this issue Jun 3, 2024
roji added a commit to roji/efcore that referenced this issue Jun 3, 2024
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 4, 2024
roji added a commit to roji/efcore that referenced this issue Jun 4, 2024
Implements dotnet#25701 for primitive collections
Implements dotnet#25700 for primitive collections
Largely implements dotnet#25765
Fixes dotnet#33858
roji added a commit that referenced this issue Jun 5, 2024
#33895)

Implements #25701 for primitive collections
Implements #25700 for primitive collections
Largely implements #25765
Fixes #33858
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview6 Jun 5, 2024
@roji roji modified the milestones: 9.0.0-preview6, 9.0.0 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-cosmos area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants