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

Translate Math.PI #25219

Closed
wants to merge 1 commit into from
Closed

Translate Math.PI #25219

wants to merge 1 commit into from

Conversation

Marusyk
Copy link
Member

@Marusyk Marusyk commented Jul 8, 2021

Issue #25049

I've added a new member translator. I expected that it will run after this test

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Where_math_pi(bool async)
{
            return AssertQuery(
                async,
                ss => ss.Set<Product>()
                    .Where(od => Math.Abs(od.ProductID) > Math.PI),
                entryCount: 0);
}

Could you please suggest what I am doing wrong?
It just translates Math.PI to constant without calling my translator. It seems I missed something 🤔

Check.NotNull(logger, nameof(logger));

if (member.Name == nameof(Math.PI)
&& instance?.Type == typeof(double))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math.PI is a static member access. So the instance is always null hence this check fails.
Correct check (apart from matching member.Name) would be member.DeclaringType == typeof(Math)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also check instance is null)

Copy link
Member Author

@Marusyk Marusyk Jul 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I will fix that but first I need to run it. To see that the translator at least runs

@smitpatel
Copy link
Contributor

It just translates Math.PI to constant without calling my translator.

You will also need to mark it as non-evaluatable in parameter extraction. https://github.com/dotnet/efcore/blob/main/src/EFCore.SqlServer/Query/Internal/SqlServerEvaluatableExpressionFilter.cs
In this file return false for Math.PI expression (using usual pattern matching)

@Marusyk
Copy link
Member Author

Marusyk commented Jul 8, 2021

@smitpatel thanks for the advice
The IsEvaluatableExpression catches the following
{[Microsoft.EntityFrameworkCore.Query.QueryRootExpression].Where(od => (3,141592653589793 > Convert(od.ProductID, Double)))}
for test ss => ss.Set<Product>().Where(od => Math.PI > od.ProductID)
Could you please suggest how to return false for Math.PI expression?

@smitpatel
Copy link
Contributor

With that expression tree, we cannot add translation. That expression tree indicates that LINQ compiler inline the constant value when generating expression tree which happens even before the tree reaches EF Core.

cc: @bricelam

@bricelam
Copy link
Contributor

bricelam commented Jul 8, 2021

Lame. Probably not worth translating, then.

@smitpatel
Copy link
Contributor

Closing the PR. Will add notes on the issue. @Marusyk - Thanks for investigating this.

@smitpatel smitpatel closed this Jul 9, 2021
@Marusyk Marusyk deleted the rmarusyk/25049 branch July 9, 2021 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants