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: incorrect results for a very complex predicate with bitwise operations #30248

Closed
maumar opened this issue Feb 10, 2023 · 3 comments · Fixed by #33875
Closed

Query: incorrect results for a very complex predicate with bitwise operations #30248

maumar opened this issue Feb 10, 2023 · 3 comments · Fixed by #33875
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported type-bug
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented Feb 10, 2023

           // 81 results expected
            var expected = (from e0 in ExpectedData.OperatorEntitiesLong
                            from e1 in ExpectedData.OperatorEntitiesLong
                            from e4 in ExpectedData.OperatorEntitiesLong
                            from e5 in ExpectedData.OperatorEntitiesLong
                            where ((((e1.Value % 2) / e0.Value) & (((e5.Value | e4.Value) - e0.Value) - (e4.Value * e4.Value))) >= (((e1.Value / ~(e5.Value)) % (long)((1 + 1))) % (~(e0.Value) + 1)))
                            select new { Value0 = e0.Value, Value1 = e1.Value, Value4 = e4.Value, Value5 = e5.Value }).ToList();

           // 2 results 
            var actual = (from e0 in context.Set<OperatorEntityLong>()
                            from e1 in context.Set<OperatorEntityLong>()
                            from e4 in context.Set<OperatorEntityLong>()
                            from e5 in context.Set<OperatorEntityLong>()
                            where ((((e1.Value % 2) / e0.Value) & (((e5.Value | e4.Value) - e0.Value) - (e4.Value * e4.Value))) >= (((e1.Value / ~(e5.Value)) % (long)((1 + 1))) % (~(e0.Value) + 1)))
                            select new { Value0 = e0.Value, Value1 = e1.Value, Value4 = e4.Value, Value5 = e5.Value }).ToList();

sql generated:

SELECT [o].[Value] AS [Value0], [o0].[Value] AS [Value1], [o1].[Value] AS [Value4], [o2].[Value] AS [Value5]
FROM [OperatorEntityLong] AS [o]
CROSS JOIN [OperatorEntityLong] AS [o0]
CROSS JOIN [OperatorEntityLong] AS [o1]
CROSS JOIN [OperatorEntityLong] AS [o2]
WHERE ([o0].[Value] % CAST(2 AS bigint)) / [o].[Value] & (([o2].[Value] | [o1].[Value]) - [o].[Value]) - [o1].[Value] * [o1].[Value] >= (([o0].[Value] / ~[o2].[Value]) % CAST(2 AS bigint)) % (~[o].[Value] + CAST(1 AS bigint))
@maumar
Copy link
Contributor Author

maumar commented Feb 10, 2023

correct results are returned when we add parens around everything like so:

SELECT [o].[Value] AS [Value0], [o0].[Value] AS [Value1], [o1].[Value] AS [Value4], [o2].[Value] AS [Value5]
FROM [OperatorEntityLong] AS [o]
CROSS JOIN [OperatorEntityLong] AS [o0]
CROSS JOIN [OperatorEntityLong] AS [o1]
CROSS JOIN [OperatorEntityLong] AS [o2]
--WHERE ([o0].[Value] % CAST(2 AS bigint)) / [o].[Value] & (([o2].[Value] | [o1].[Value]) - [o].[Value]) - [o1].[Value] * [o1].[Value] >= (([o0].[Value] / ~[o2].[Value]) % CAST(2 AS bigint)) % (~[o].[Value] + CAST(1 AS bigint))
WHERE (((([o0].[Value] % CAST(2 AS bigint)) / [o].[Value]) & ((([o2].[Value] | [o1].[Value]) - [o].[Value]) - ([o1].[Value] * [o1].[Value]))) >= ((([o0].[Value] / ~([o2].[Value])) % ((CAST(2 AS bigint)))) % (~([o].[Value]) + CAST(1 AS bigint))))

@maumar
Copy link
Contributor Author

maumar commented Feb 10, 2023

found simpler predicate:

(((((e1.Value & (e0.Value + e0.Value)) & e0.Value) / 1) > (e1.Value & (int)((8 + 2)))) && e2.Value)

sql we generate:

SELECT [o].[Id], [o].[Value], [o0].[Id], [o0].[Value], [o1].[Id], [o1].[Value]
FROM [OperatorEntityInt] AS [o]
CROSS JOIN [OperatorEntityInt] AS [o0]
CROSS JOIN [OperatorEntityBool] AS [o1]
WHERE ([o0].[Value] & [o].[Value] + [o].[Value] & [o].[Value]) / 1 > [o0].[Value] & 10 AND [o1].[Value] = CAST(1 AS bit)
ORDER BY [o].[Id], [o0].[Id], [o1].[Id]

sql that produces correct results:

SELECT [o].[Id], [o].[Value], [o0].[Id], [o0].[Value], [o1].[Id], [o1].[Value]
FROM [OperatorEntityInt] AS [o]
CROSS JOIN [OperatorEntityInt] AS [o0]
CROSS JOIN [OperatorEntityBool] AS [o1]
--WHERE ([o0].[Value] & [o].[Value] + [o].[Value] & [o].[Value]) / 1 > [o0].[Value] & 10 AND [o1].[Value] = CAST(1 AS bit)
WHERE ((((([o0].[Value] & ([o].[Value] + [o].[Value])) & [o].[Value]) / 1) > ([o0].[Value] & ((10)))) AND [o1].[Value] = CAST(1 as bit))
ORDER BY [o].[Id], [o0].[Id], [o1].[Id]

@maumar
Copy link
Contributor Author

maumar commented Feb 10, 2023

related: #30181

@ajcvickers ajcvickers added this to the Backlog milestone Feb 16, 2023
ranma42 added a commit to ranma42/efcore that referenced this issue Jun 2, 2024
Specifically, binary logical operators have the same precedence as `+`/`-`.

The table is now aligned with the referenced document, plus shift operators.
They seem to have the same precedence as `+`/`-` according to manual tests; no
documentation about this is available.

Also, warn about inconsistent and misleading documentation, which might be the
cause of the mismatch between the previous table and the linked documentation.

Fixes dotnet#30248.
ranma42 added a commit to ranma42/efcore that referenced this issue Jun 2, 2024
Specifically, binary logical operators have the same precedence as `+`/`-`.

The table is now aligned with the referenced document, plus shift operators.
They seem to have the same precedence as `+`/`-` according to manual tests; no
documentation about this is available.

Fixes dotnet#30248.
roji pushed a commit that referenced this issue Jun 2, 2024
Specifically, binary logical operators have the same precedence as `+`/`-`.

Fixes #30248
@roji roji modified the milestones: Backlog, 9.0.0 Jun 2, 2024
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 3, 2024
@ajcvickers ajcvickers modified the milestones: 9.0.0, 9.0.0-preview5 Jun 4, 2024
@ajcvickers ajcvickers modified the milestones: 9.0.0-preview5, 9.0.0-preview6 Jun 5, 2024
@roji roji modified the milestones: 9.0.0-preview6, 9.0.0 Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants