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 comparisons for big decimals fail on Sqlite #11036

Closed
ajcvickers opened this issue Feb 22, 2018 · 4 comments
Closed

Query comparisons for big decimals fail on Sqlite #11036

ajcvickers opened this issue Feb 22, 2018 · 4 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

@ajcvickers
Copy link
Member

With this model:

public class Rider
{
    public int Id { get; set; }

    public decimal Mount { get; set; }
}

public class XContext : DbContext
{
    public DbSet<Rider> Riders { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseSqlite("Filename=etetest.db");
}

This works:

[Fact]
public void Main()
{
    using (var context = new XContext())
    {
        context.Database.EnsureDeleted();
        context.Database.EnsureCreated();

        context.Add(
            new Rider
            {
                Mount = 222211110000m
            });

        context.SaveChanges();
    }

    using (var context = new XContext())
    {
        Assert.Equal(1, context.Set<Rider>().Count(e => e.Mount == 222211110000m));
    }
}

But making the value bigger fails:

[Fact]
public void Main()
{
    using (var context = new XContext())
    {
        context.Database.EnsureDeleted();
        context.Database.EnsureCreated();

        context.Add(
            new Rider
            {
                Mount = 3333222211110000m
            });

        context.SaveChanges();
    }

    using (var context = new XContext())
    {
        // Now fails to match
        Assert.Equal(1, context.Set<Rider>().Count(e => e.Mount == 3333222211110000m));
    }
}
@smitpatel
Copy link
Member

Probably type affinity thing mentioned here:

A column with NUMERIC affinity may contain values using all five storage classes. When text data is inserted into a NUMERIC column, the storage class of the text is converted to INTEGER or REAL (in order of preference) if such conversion is lossless and reversible. For conversions between TEXT and REAL storage classes, SQLite considers the conversion to be lossless and reversible if the first 15 significant decimal digits of the number are preserved. If the lossless conversion of TEXT to INTEGER or REAL is not possible then the value is stored using the TEXT storage class.

We store data as TEXT but we generate literal of REAL.
SQL in above case

      SELECT COUNT(*)
      FROM "Riders" AS "e"
      WHERE "e"."Mount" = 3333222211110000.0

So for smaller no equality works fine. For bigger no. type affinity fails and equality fails (probably due to being different types). If we compare with TEXT literal then it works.

@ajcvickers ajcvickers added this to the 2.1.0 milestone Feb 28, 2018
@bricelam
Copy link
Contributor

bricelam commented Mar 1, 2018

We can make this work by using TEXT literals for decimal values, but we might lose the ability to perform other comparisons and arithmetic operations in SQL.

@bricelam bricelam removed this from the 2.1.0 milestone Mar 1, 2018
@bricelam
Copy link
Contributor

bricelam commented Mar 1, 2018

Related to #10534

@bricelam
Copy link
Contributor

bricelam commented Mar 1, 2018

Digging into this, I think we should switch to TEXT literals. I can't foresee any regressions. 🤞 The operators appear do the right(?) thing.

@ajcvickers ajcvickers added this to the 2.1.0 milestone Mar 2, 2018
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Mar 12, 2018
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
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

3 participants