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

Incorrect SQL generation for LTree.MatchesLQuery #2487

Closed
temos opened this issue Aug 29, 2022 · 1 comment · Fixed by #2489
Closed

Incorrect SQL generation for LTree.MatchesLQuery #2487

temos opened this issue Aug 29, 2022 · 1 comment · Fixed by #2489
Assignees
Labels
bug Something isn't working
Milestone

Comments

@temos
Copy link

temos commented Aug 29, 2022

Hello,
I've encountered a weird behavior while using the ltree extension, in particular the Ltree.MatchesLQuery method.

Given the following code

using Microsoft.EntityFrameworkCore;

using var context = new DatabaseContext();

var data = context.Categories.Where(c => c.Path.MatchesLQuery("*." + c.Id));
Console.WriteLine(data.Count()); //PostgresException (0x80004005): 42846: cannot cast type integer to lquery

class DatabaseContext : DbContext
{
    public DbSet<Category> Categories { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql("connection-string-here");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.HasPostgresExtension("ltree");
    }
}

class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public LTree Path { get; set; }
}

the following SQL is generated

SELECT c."Id", c."Name", c."Path"
FROM "Categories" AS c
WHERE c."Path" ~ ('*.' || c."Id"::lquery)

which causes 'Unhandled exception. Npgsql.PostgresException (0x80004005): 42846: cannot cast type integer to lquery' when executed, because only the right side of the concatenation expression inside the MatchesLQuery method call is being cast into lquery.

I would expect the entire concatenation expression inside the MatchesLQuery method call (the string '*.' concatenated with the Id column) to be cast into lquery, not just the right side, producing the following SQL which would work

SELECT c."Id", c."Name", c."Path"
FROM "Categories" AS c
WHERE c."Path" ~ ('*.' || c."Id")::lquery

This only happens when concatenating with a column from the database, not a constant or a parameter. Order also does not matter. If I were to swap the left and right side or put the Id in the middle like this

var data = context.Categories.Where(c => c.Path.MatchesLQuery(c.Id + ".*"));
var dat2 = context.Categories.Where(c => c.Path.MatchesLQuery("*." + c.Id + ".*"));

it would still try to cast the Id to an lquery first and then do the concatenation like this

SELECT c."Id", c."Name", c."Path"
FROM "Categories" AS c
WHERE c."Path" ~ (c."Id"::lquery || '.*');

SELECT c."Id", c."Name", c."Path"
FROM "Categories" AS c
WHERE c."Path" ~ (('*.' || c."Id"::lquery) || '.*');

which of course does not work.

Tested on .NET 6 and 7
Npgsql.EntityFrameworkCore.PostgreSQL 6.0.6 and 7.0.0-preview.7
Npgsql 6.0.6 and 7.0.0-preview.7
Microsoft.EntityFrameworkCore 6.0.8 and 7.0.0-preview.7.22376.2

@roji
Copy link
Member

roji commented Aug 29, 2022

Thanks, definitely seems like a parentheses/precedence issue... Will look into it.

@roji roji added the bug Something isn't working label Aug 29, 2022
@roji roji added this to the 6.0.7 milestone Aug 29, 2022
@roji roji self-assigned this Aug 29, 2022
roji added a commit to roji/efcore.pg that referenced this issue Aug 31, 2022
roji added a commit that referenced this issue Aug 31, 2022
roji added a commit that referenced this issue Aug 31, 2022
Fixes #2487

(cherry picked from commit ddc63fc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants