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

SelectTransformBuilder doesn't handle subqueries. Cannot make any kind of subquery projection #1924

Closed
rwasef1830 opened this issue Oct 12, 2021 · 7 comments

Comments

@rwasef1830
Copy link
Contributor

Given this class:

class User 
{
    public Guid Id { get; set; }
    public List<Guid> RoleIds { get; set; }
}
var session = store.QuerySession();
session.Query<User>(x => new { x.Id, FirstRoleId = x.RoleIds.FirstOrDefault() });

This query does not work and throws an exception, it appears to be only expecting sub-member expressions from the RoleIds collection which doesn't work because it's a collection of scalar values.

PR with failing unit test coming.

@rwasef1830 rwasef1830 changed the title Cannot project from a collection of scalar values inside a document SelectTransformBuilder doesn't handle subqueries. Cannot make any kind of subquery projection Oct 12, 2021
rwasef1830 added a commit to rwasef1830/marten that referenced this issue Oct 12, 2021
@rwasef1830
Copy link
Contributor Author

rwasef1830 commented Oct 12, 2021

Ok, so I have this entirely crazy idea of synthesizing a subquery with primary key join and running the entire expression tree processing for the subquery. It might work but perhaps not in all cases... hmmm...

aka, generating a query like this:

SELECT
       d.data->>'Id' AS Id,
       (SELECT id.data->>'RoleIds' FROM mt_doc_user AS id WHERE id.id = d.id LIMIT 1) AS RoleId
FROM mt_doc_user AS d

Perhaps in a future release, some subquery collection operations could be translated to direct jsonb functions and eliminate the self-table join, but for now getting it to work in the first place is going to go a long way.

@rwasef1830
Copy link
Contributor Author

rwasef1830 commented Oct 12, 2021

This is closer to what Marten generates:

SELECT
       jsonb_build_object(
            'Id', d.data->>'Id',
            'RoleId', (SELECT id.data->'RoleIds'->>0 FROM mt_doc_user AS id WHERE id.id = d.id LIMIT 1)
       )
FROM mt_doc_user AS d

@rwasef1830
Copy link
Contributor Author

This needs a refactor to LinqHandlerBuilder to extract out a class that builds Sql and takes a processed model directly.
Then another refactor is needed to SelectTransformBuilder.TargetObject so that it can accept correlated subquery sql generating type.

@rwasef1830
Copy link
Contributor Author

This will be a tiny bit easier to do after #1927 is merged.

@jeremydmiller
Copy link
Member

It's not a bug per se, just something that's never been implemented.

@jeremydmiller jeremydmiller added this to the 7.0.0 milestone Sep 8, 2023
@jeremydmiller
Copy link
Member

Beating this for now w/ the ability to use array indexers and calling that good enough.

@jeremydmiller
Copy link
Member

I'm closing this one. We're not going any farther for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants