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

RC2, using ?? operator in select fails with casting error #5439

Closed
sadjadbp opened this issue May 20, 2016 · 3 comments
Closed

RC2, using ?? operator in select fails with casting error #5439

sadjadbp opened this issue May 20, 2016 · 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

@sadjadbp
Copy link

RC2, using ?? operator in select fails with casting error.

.Select(x => x.NullableBooleanProperty ?? false)

would fail with casting error. But the same like this is ok

.Select(x => x.NullableBooleanProperty == null ? false : x.NullableBooleanProperty.value)

@mikary
Copy link
Contributor

mikary commented May 31, 2016

The invalid cast exception is the result of a general problem with materialization and SQL Server. There are a few different ways to compensate for this issue:

  1. Compensate in SQL generation by always casting bool literals to BIT (original proposed solution in Fix #5439 - Select null coalesce bool throws InvalidCastException #5523)
    • This is the most general solution to the problem, but introduces unnecessary CASTs into the SQL where results are not required.
    • The new behavior may be introduced through the RelationalSqlGenerationHelper or SqlServerSqlGenerationHelper (Possible to move all bit casts to SQL Server implementation, probably requires a "providers-beware").
    • The current implementation for DefaultQuerySqlGenerator.VisitConditional does similar compensation for this issue and could be simplified by this implementation
  2. Detect and compensate for bool casts in materialization
    • Adds additional check / cast logic when using the ValueBuffer that could prevent the exception from being thrown in all int to bool cast cases, but potentially has a negitive performance impact on a very hot path
  3. Modify the DefaultQuerySqlGenerator and or SqlServerQuerySqlGenerator to special case on bool values
    • Introduces logic in SQL generation to compensate for bool values in the projection expressions
    • May not be feasible without modifying the expression due to AliasExpressions (generating a cast around an alias results in invalid SQL)
  4. Alter the expression before SQL generation (i.e. in RelationalProjectionExpressionVisitor)
    • Requires introducing a new Expression node / ExpressionType to provide a Convert expression that can be distinguished from existing converts (current SQL generation ignores Convert operations to avoid unnecessary casts)

@smitpatel
Copy link
Contributor

Already fixed in RTM. Adding test.

@smitpatel
Copy link
Contributor

dupe of #5652

@rowanmiller rowanmiller removed the pri0 label Jul 6, 2016
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 11, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
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

5 participants