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

Query: Additional function mapping capabilities #4319

Open
4 of 13 tasks
rowanmiller opened this issue Jan 15, 2016 · 17 comments
Open
4 of 13 tasks

Query: Additional function mapping capabilities #4319

rowanmiller opened this issue Jan 15, 2016 · 17 comments
Labels
area-query area-relational-mapping composite-issue A grouping of multiple related issues into one issue type-enhancement
Milestone

Comments

@rowanmiller
Copy link
Contributor

rowanmiller commented Jan 15, 2016

This would allow functions in the database to be used in LINQ queries. This includes:

We could support a number of patterns for this:

Once database functions can be mapped to methods, we could accept those methods in other places besides LINQ queries, e.g. to specify default values, in set based updates (once we have the feature), etc.

@rowanmiller rowanmiller added this to the Backlog milestone Jan 15, 2016
@rowanmiller rowanmiller changed the title Query: User Defined Functions Query: User Defined Functions (including TVFs) Jan 15, 2016
@divega divega changed the title Query: User Defined Functions (including TVFs) Query: Additional function mapping capabilities Jun 29, 2017
@divega
Copy link
Contributor

divega commented Jun 29, 2017

Renaming since scalar functions mapping support has been added in 2.0 preview 2. Some of the remaining scenarios listed in this issue could be easier to tackle based on the work done in #7368.

cc @pmiddleton @anpete

@pmiddleton
Copy link
Contributor

I am going to start looking into TVF next. I had a mostly working version back in January which I am going to revisit.

User-defined aggregate functions have the same syntax as scalar functions so they "should just work". 🙈 I can give it a try this weekend to verify.

@pmiddleton
Copy link
Contributor

I looked at using the existing UDF support for aggregates over the long weekend. Everything does work when the full query can be translated. The main issue is that without group by translation support the aggregate is lost.

Aggregates can still be used in queries without group by, but the usefulness of that will be rather limited.

Once group by translation is added we should be good to go without any modifications needed.

@divega
Copy link
Contributor

divega commented Feb 28, 2018

@pmiddleton curious if you are still looking at TVFs and other things you were planning to look into. If there is anything we can help with let us know.

@pmiddleton
Copy link
Contributor

@divega - yes I have been working on those items.

I have the following things currently working.

  • Bootstrapable Scalar Functions
  • Bootstrapable TVFs
  • Nestable DbFunction calls on both types of bootstrapped methods (pass a function as a parameter to the bootstrapped function)
  • Full query support for TVFs via Cross Join
  • TVF integration with Query Types. You can either directly materialize the QT from the TVF, or use it as part of a larger query.

I have started on Outer Apply for TVFs. The Linq syntax is in place and valid Sql is being produce, but it is currently not optimal as it is using a cross apply with a subquery. I need to replace that with an Outer Apply directly to the TVF.

Things are finally settling down at work (we just had the release of a 3 year long project) so I have had more time to work on this at night.

I can create a wip pr tomorrow so the team can see what I have going on. I still need finish the outer apply, clean up the code, and add more unit tests.

I believe that the changes I had to make are going to introduce too many breaking changes for 2.1, but I will let the team make that determination.

Let me know what you guys think once you see it.

@aaronhudon
Copy link

@pmiddleton Can you suggest the current workaround in 2.1 on how to call a TVF (with parameters)?

@pmiddleton
Copy link
Contributor

@aaronhudon - Your only options in 2.1 will be running raw sql using either FromSql or Database.Query.

@aaronhudon
Copy link

@pmiddleton Thanks. I'm doing this now. Since this causes a subquery like `SELECT * FROM TVF...`` the query plan performance is like 99% worse than selecting directly from the TVF.

@pmiddleton
Copy link
Contributor

@aaronhudon - I'm not understanding where the subquery is coming from. Did you build the whole query yourself or is part of it being generated by EF? If so can you just build the whole query yourself to avoid where this subquery is coming from.

@aaronhudon
Copy link

Unfortunately, yes, I'll have to write the entire SQL instead of taking advantage of the nice LINQ syntax for the where clauses (and potentially joins to other entity objects)

The subquery comes from my DbContext exposing an IQueryable<T> method like

internal DbQuery<Foo> FooTvf {get;set;}

public IQueryable<Foo> Get(DateTime param)
{
    return FooTvf.FromSql($"SELECT * FROM tvf_xxxx({param})");
}

Then using this Get method from the dbContext:
dbContext.Get(someDate).Where(x => x.a == blah)

The resulting query looks something like:

SELECT m.a, m.b, m.c, m.d
FROM (
    SELECT * FROM tvf_xxx(param)
) as m
WHERE m.a = [query params inserted here]

@pmiddleton
Copy link
Contributor

How often are you composing on your Get method? If you are just tacking on that same where clause then you can move the whole query into the FromSql call. If you are composing a lot of different queries then you are stuck for now.

@aaronhudon
Copy link

I'm stuck for now :( I'll watch this thread (?) for updates on support for TVFs

@pmiddleton
Copy link
Contributor

yes. I don't know if tvf will make 3.0. The team is rewriting the query pipeline so it's going to be based on when that gets to stable point and my free time to work on updating my existing PR.

@aaronhudon
Copy link

@pmiddleton Rewrite? :-/ which thread has the query pipeline rewrite discussion?

@MartinJohns
Copy link

@pmiddleton Is there any update on this? Could you please link the issue @aaronhudon asked about?

@pmiddleton
Copy link
Contributor

@MartinJohns - See my response on #11129

@jscarle
Copy link

jscarle commented Jul 6, 2024

Related: #33788

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query area-relational-mapping composite-issue A grouping of multiple related issues into one issue type-enhancement
Projects
None yet
Development

No branches or pull requests

8 participants