-
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
DbFunctions compared to NULL are ignored and break the query builder #18547
Comments
…are applied One way to solve #18547
See #11295 (comment) on how to write translation for JSON_VALUE. Further, don't pass |
That seems to work yes, except in our actual application builder.Property(e => e.MetaData)
.HasConversion(v => JsonConvert.SerializeObject(v, new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore}),
v => JsonConvert.DeserializeObject<MetaData>(v, new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore})); This is why I had to rewrite your suggested solution, it doesn't know what Anyway, is there a reason that my solution breaks though? I don't see what causes the issue and breaking the whole query builder. |
You declare your The solution breaks due to a bug. We assume that if FUNC(A,B) == null only when either A or B is null. In your case since you passed constant value none of them is null so we simplified it to false. That is one more reason to use actual metadata then string column name via constant expression as by using constant you lose all the metadata EF knows about. (In complex query it will generate invalid SQL too if column gets aliased.) |
I tried setting it to Object but then I run into this:
Also when I set it to the My solution is just a workaround until full JSON support lands, I'm only using it sparingly where needed, and for now I removed the conditions on the DB level and do them in memory instead. |
Hence you need to use this
|
Ah OK now I see, that makes sense, works perfectly! |
Resolves #17543 - Queries really slow due to null checks Resolves #18525 - Query: optimize binary expression AndAlso and OrElse where left and right are the same Resolves #18547 - DbFunctions compared to NULL are ignored and break the query builder #17543 Before when rewriting null comparisons we would always remove possibility of nulls in the resulting expression. This is not always needed, specifically in predicates, where it doesn't really matter if expression returns false or null - the result is the same. Now we detect those cases and apply "simplified" null expansion which should lead to better performance. #18525 Null semantics initially expands expressions to a verbose form which then gets simplified in query optimizer. One of the optimizations added is when we do AND/OR where both sides are the same. Other small improvements have been added in this change also. #18525 Previously we assumed that function can only be null if at least one of its arguments is null. This is the case for most built-in functions, but not all. This also goes for user defined functions which can return arbitrary results. Fix is to treat all functions as potentially nullable. This leads to worse queries is some cases, but is also mitigated by other improvements added along side. In the future we will provide metadata to better determine given function's nullability. Also fixed a few small bug found along the way - incorrect Equals comparison for SqlExpression, for cases when TypeMapping was null.
Resolves #17543 - Queries really slow due to null checks Resolves #18525 - Query: optimize binary expression AndAlso and OrElse where left and right are the same Resolves #18547 - DbFunctions compared to NULL are ignored and break the query builder #17543 Before when rewriting null comparisons we would always remove possibility of nulls in the resulting expression. This is not always needed, specifically in predicates, where it doesn't really matter if expression returns false or null - the result is the same. Now we detect those cases and apply "simplified" null expansion which should lead to better performance. #18525 Null semantics initially expands expressions to a verbose form which then gets simplified in query optimizer. One of the optimizations added is when we do AND/OR where both sides are the same. Other small improvements have been added in this change also. #18525 Previously we assumed that function can only be null if at least one of its arguments is null. This is the case for most built-in functions, but not all. This also goes for user defined functions which can return arbitrary results. Fix is to treat all functions as potentially nullable. This leads to worse queries is some cases, but is also mitigated by other improvements added along side. In the future we will provide metadata to better determine given function's nullability. Also fixed a few small bug found along the way - incorrect Equals comparison for SqlExpression, for cases when TypeMapping was null. Additional refactoring: - removed the second pass of sql optimizations (we do it later when sniffing parameter values anyway) - consolidated NullComparisonTransformingExpressionVisitor into SqlExpressionOptimizingExpressionVisitor
Resolves #17543 - Queries really slow due to null checks Resolves #18525 - Query: optimize binary expression AndAlso and OrElse where left and right are the same Resolves #18547 - DbFunctions compared to NULL are ignored and break the query builder #17543 Before when rewriting null comparisons we would always remove possibility of nulls in the resulting expression. This is not always needed, specifically in predicates, where it doesn't really matter if expression returns false or null - the result is the same. Now we detect those cases and apply "simplified" null expansion which should lead to better performance. #18525 Null semantics initially expands expressions to a verbose form which then gets simplified in query optimizer. One of the optimizations added is when we do AND/OR where both sides are the same. Other small improvements have been added in this change also. #18547 Previously we assumed that function can only be null if at least one of its arguments is null. This is the case for most built-in functions, but not all. This also goes for user defined functions which can return arbitrary results. Fix is to treat all functions as potentially nullable. This leads to worse queries is some cases, but is also mitigated by other improvements added along side. In the future we will provide metadata to better determine given function's nullability. Also fixed a few small bug found along the way - incorrect Equals comparison for SqlExpression, for cases when TypeMapping was null. Additional refactoring: - removed the second pass of sql optimizations (we do it later when sniffing parameter values anyway) - consolidated NullComparisonTransformingExpressionVisitor into SqlExpressionOptimizingExpressionVisitor
DbFunctions that are comparing to NULL are ignored in the query and even break other (valid) conditions.
For example:
When I run:
context.Courses.FirstOrDefault(m => DataContext.JsonValue("MetaData", "$.TestParam") == "Test 123");
Works as expected, but when I run:
context.Courses.FirstOrDefault(m => DataContext.JsonValue("MetaData", "$.TestParam") == null);
it doesn't work, the generated query is odd as well:
When chaining multiple conditions, like this:
context.Courses.FirstOrDefault(m => m.CourseName == "Test 1" && DataContext.JsonValue("MetaData", "$.TestParam") == null);
It gives the same result.
Here is a demo repo:
test-repo.zip
The text was updated successfully, but these errors were encountered: