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 : SqlServerMathRoundTranslator wrong argument index results in returning incorrect data or throwing InvalidCastException #7600

Closed
rpawlaszek opened this issue Feb 12, 2017 · 3 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@rpawlaszek
Copy link
Contributor

rpawlaszek commented Feb 12, 2017

Trying to use Math.Round in the query gives InvalidCastException

Exception message: An exception occurred in the database while iterating the results of a query.
Stack trace:
System.InvalidCastException: Specified cast is not valid..
   w lambda_method(Closure , ValueBuffer )
   w System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   w Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()

Steps to reproduce

Follow Entity Framework Core docs, build the model, populate Blogs table with few entities and then used this to query:

            using (var db = new BloggingContext())
            {
                LogToConsole(db);
                var query = db.Blogs
                            .Select(b => Math.Round(Math.PI, b.Rating));
                            
                foreach (var item in query)
                {
                    Console.WriteLine($"RESULT is {item}");
                }
            }

The console logging shows what went wrong:

select double Round(
    value: 3,14159265358979,
    digits: b.Rating
)
'
TRACKED: False
(QueryContext queryContext) => IEnumerable<double> _Select(
    source: IEnumerable<ValueBuffer> _ShapedQuery(
        queryContext: queryContext,
        shaperCommandContext: SelectExpression:
            SELECT ROUND([b].[Rating], [b].[Rating])
            FROM [Blogs] AS [b]
        ,
        shaper: ValueBufferShaper
    )
    ,
    selector: (ValueBuffer b) => (double) object b.get_Item(0)
)

The line

SELECT ROUND([b].[Rating], [b].[Rating])

should be

SELECT ROUND(3.14159265358979, [b].[Rating])

and is there because of a bug in SqlServerMathRoundTranslator.cs#L34. There should be

: new[] { methodCallExpression.Arguments[0], methodCallExpression.Arguments[1] })

I did that as I wanted to check whether mathemathical functions are run on the server side. And actually Math.Round wasn't the one I was looking for, but checked them all. I guess I should create another Issue with this, but could you implement trigonometric functions + Math.Sqrt, Math.Log, Math.Log10, Math.Exp as well?

Further technical details

EF Core version: 1.1.0 (but present in all, as I see)
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 Pro
IDE: Visual Studio 2015

@maumar maumar changed the title SqlServerMathRoundTranslator wrong argument index results in InvalidCastException Query : SqlServerMathRoundTranslator wrong argument index results in InvalidCastException Feb 12, 2017
@maumar maumar self-assigned this Feb 12, 2017
maumar added a commit that referenced this issue Feb 12, 2017
…dex results in InvalidCastException

Fixing typo in the Round translator.
@rowanmiller rowanmiller added this to the 1.1.1 milestone Feb 13, 2017
@maumar maumar changed the title Query : SqlServerMathRoundTranslator wrong argument index results in InvalidCastException Query : SqlServerMathRoundTranslator wrong argument index results in returning incorrect data or throwing InvalidCastException Feb 13, 2017
@maumar
Copy link
Contributor

maumar commented Feb 13, 2017

Justification for 1.1.1

Impact: Medium. Issue was found by a customer and it may cause incorrect results for simple queries. Severity is mitigated by the fact that scope of the issue is quite narrow (only one method is affected).

Risk: None - fix is one-character change (correcting of a typo), scoped to only one method call (Math.Round)

maumar added a commit that referenced this issue Feb 13, 2017
…dex results in returning incorrect data or throwing InvalidCastException

Fixing typo in the Round translator.
@Eilon
Copy link
Member

Eilon commented Feb 15, 2017

This patch bug is approved. Please use the normal code review process w/ a PR and make sure the fix is in the correct branch, then close the bug and mark it as done.

@divega divega added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 16, 2017
@smitpatel
Copy link
Member

✅ Verified

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

No branches or pull requests

6 participants