-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Add DATALENGTH function to SqlServer #20079
Conversation
src/EFCore.SqlServer/Query/Internal/SqlServerDataLengthFunctionTranslator.cs
Outdated
Show resolved
Hide resolved
Also rebase on latest master so that checks can work. Sorry, we had a build break when you checked out. |
5d68521
to
813b04d
Compare
src/EFCore.SqlServer/Query/Internal/SqlServerDataLengthFunctionTranslator.cs
Outdated
Show resolved
Hide resolved
src/EFCore.SqlServer/Query/Internal/SqlServerDataLengthFunctionTranslator.cs
Outdated
Show resolved
Hide resolved
src/EFCore.SqlServer/Query/Internal/SqlServerMethodCallTranslatorProvider.cs
Outdated
Show resolved
Hide resolved
src/EFCore.SqlServer/Extensions/SqlServerDbFunctionsExtensions.cs
Outdated
Show resolved
Hide resolved
@Marusyk - Can you also add tests for other data types for which we are adding overloads? |
Actually, I've tried something like bool? value = true;
var count = context.Orders
.Count(c => c.OrderID < EF.Functions.DataLength(value)); but got
could you please suggest? |
Don't use it on local variable. It will cause parameter extraction to run. Since |
@smitpatel what should I use then? There is no all types in |
6993d2f
to
65d95f4
Compare
Use different entity type. Customer has string, Order has int/date (mapped as datetime). GearOfWar would have DateTimeOffset/TimeSpan/Byte array, fixed/variable size. |
Unfortunately, I can't add more tests for other data types. Because I don't unserstand why this code works fine:
but this one fail
Also I tried
the same error.
@smitpatel, there is no any |
Those properties are ignored in EF Core model. |
As you can see, I added tests to |
This file will tell you what all is ignored in northwind. https://github.com/dotnet/efcore/blob/master/test/EFCore.Specification.Tests/TestModels/Northwind/NorthwindContext.cs |
There are only
|
@maumar to rescue. Let him know which other data types you need. |
Should I really add some tests to EFCore.Specification.Tests and others to EFCore.SqlServer.FunctionalTests? 🤨 @maumar please advise |
@Marusyk - Please add tests to GearsOfWarQuerySqlServerTest file. It is ok to add tests there which are provider specific. The test will run the query an assert the SQL too. https://github.com/dotnet/efcore/blob/master/test/EFCore.Specification.Tests/TestModels/GearsOfWarModel/ shows all the classes mapped in GearsOfWarModel. You can use any entity type as query root and property on them to use DataLength on. Almost all of the properties are mapped, so you would not run into issue like northwind. Let me know if you are unable to find entity type with a specific type. I can point you to specific property to use for the test. |
@smitpatel, Seems the |
You don't add it to EFCore.Specifications.Tests project. You add it to You will utilize a model & context defined in EFCore.Specification.Tests project, which is available in EFCore.SqlServer.FunctionalTests project with SqlServer provider configured and you will write queries there. |
Ok, but you suggested EFCore.Specifications.Tests before
|
Sorry for confusion there. |
@smitpatel sorry, but I think you need to ckeck the source code. Did you see how implemented tests in
You confusing me and waste my time. We spent a lot of time discussing the simple thing. Can anyone else review? |
Yes, currently all the tests there are overrides of the base ones, but they don't need to be. You can define a new test at that level. |
Thanks. I'm trying to follow some conventions, and when I see a file with 7500+ line of similar code, then I need some approval. Before it was confusing, because first we were talking about some I've tried this
got again
Could you please help and suggest what I'm doing wrong? |
@Marusyk The problem you are seeing now happens because test infrastructure tries to execute the query you provided using linq to object, in order to get the expected results. Problem is that DataLength doesn't work in Linq to objects. For those cases we need to provide expected query that can be executed on L2O. here is an example: await AssertQuery(
async,
ss => ss.Set<Mission>().Where(g => g.Id > EF.Functions.DataLength(g.CodeName)),
ss => ss.Set<Mission>().Where(g => g.Id > g.CodeName.Length * 2)); |
Please also add test that projects the DataLength value, e.g. await AssertQueryScalar(
async,
ss => ss.Set<Mission>().Select(m => EF.Functions.DataLength(m.CodeName)),
ss => ss.Set<Mission>().Select(m => (int?)(m.CodeName.Length * 2))); Note that this currently throws. @smitpatel any suggestions where to properly do the compensation for type difference between int and long in this case? |
src/EFCore.SqlServer/Query/Internal/SqlServerDataLengthFunctionTranslator.cs
Show resolved
Hide resolved
I'm closing this PR because don't know how to add tests. await AssertQueryScalar(
async,
ss => ss.Set<Mission>().Select(m => EF.Functions.DataLength(m.CodeName)),
ss => ss.Set<Mission>().Select(m => (int?)(m.CodeName.Length * 2))); then what about other types: double, decimal, DateTimeOffset, ...? How to calculate So, thanks all for your review and sorry for waste your time. |
@Marusyk If you are still interested, I can add the missing tests in the follow up PR. Just rebase on current master and add a test for string property and I will merge. Test infra is quite confusing if one is not familiar with it :( |
I'll rebase. Thanks |
e41bca1
to
6a2e2e3
Compare
Merged. @Marusyk thank you for the contribution! |
@maumar didn't squash 😢 |
Add
DATALENGTH
function to SqlServerFixes #19988
Please review,
Thank you in advance