Skip to content

Query: Translate Enumerable.Contains() with byte arrays #4601

Description

@divega

We should translate Enumerable.Contains() for byte arrays, but when we do, we should be careful to update our SQL caching code:

We found while working on caching of FromSql() queries that we are special casing byte arrays so we skip structural comparisons for byte arrays (and also strings and object arrays) when comparing query cache entries. This probably because normally we treat byte arrays as a primitive type throughout our stack but for the case of using a byte array with Enumerable.Contains() each element in the byte array will become a literal in the SQL so we should consider it a different cache entry.

We should also understand if this can be a scenario for strings and object arrays.

A potential fix would be to swap to an approach that special cases Enumerable.Contains() so that we account for the fact that its first argument will not be parameterized.

cc @mikary @anpete

Activity

  1. divega commented on Feb 18, 2016

    @divega
    ContributorAuthor

    @anpete @mikary could you please check what is the impact of the bug? I wonder if subsequent queries will just reuse the SQL from the first query, returning incorrect results or if it is something more benign.

  2. mikary commented on Feb 19, 2016

    @mikary
    Contributor

    Attempted to produce a false positive on the caching and it appears we client eval contains with byte arrays

                using (var context = CreateContext())
                {
                    var x = new byte[] { 1, 2, 3 };
    
                    var first = context.Orders.Where(o => x.Contains<byte>((byte)o.OrderID)).ToList();
    
                    x = new byte[] { 4, 5, 6 };
    
                    first = context.Orders.Where(o => x.Contains<byte>((byte)o.OrderID)).ToList();
                }
    SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
    FROM [Orders] AS [o]
    
    SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
    FROM [Orders] AS [o]

    The same operation with an int array produces two different cache entries:

                using (var context = CreateContext())
                {
                    var x = new int[] { 1, 2, 3 };
    
                    var first = context.Orders.Where(o => x.Contains<int>((int)o.OrderID)).ToList();
    
                    x = new int[] { 4, 5, 6 };
    
                    first = context.Orders.Where(o => x.Contains<int>((int)o.OrderID)).ToList();
                }
    SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
    FROM [Orders] AS [o]
    WHERE [o].[OrderID] IN (1, 2, 3)
    
    SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
    FROM [Orders] AS [o]
    WHERE [o].[OrderID] IN (4, 5, 6)
  3. mikary commented on Feb 19, 2016

    @mikary
    Contributor

    Opened #4605 to track caching and cache pollution aspect of this issue.
    Updating this issue to address byte array client evaluation.

  4. changed the title [-]Query: Enumerable.Contains() with byte arrays[/-] [+]Query: Enumerable.Contains() with byte arrays forces client eval[/+] on Feb 19, 2016
  5. changed the title [-]Query: Enumerable.Contains() with byte arrays forces client eval[/-] [+]Query: Translate Enumerable.Contains() with byte arrays[/+] on Feb 19, 2016
  6. added this to the Backlog milestone on Feb 19, 2016
  7. added 2 commits that reference this issue on Nov 26, 2019
    94c0091
    801e485
  8. modified the milestones: Backlog, 5.0.0 on Nov 26, 2019
  9. 1 remaining item

  10. added a commit that references this issue on Nov 26, 2019
    cf470a0
  11. added a commit that references this issue on Nov 26, 2019
    066b637
  12. added this to the 5.0.0 milestone on Nov 26, 2019
  13. modified the milestones: 5.0.0, on Mar 13, 2020
  14. modified the milestones: , 5.0.0 on Nov 7, 2020
  15. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions