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

Wrong result ordering in LEFT JOIN query #7665

Closed
lukaseder opened this issue Jul 7, 2023 · 3 comments
Closed

Wrong result ordering in LEFT JOIN query #7665

lukaseder opened this issue Jul 7, 2023 · 3 comments

Comments

@lukaseder
Copy link

I'm using Firebird 4.0.2 from here: https://hub.docker.com/r/jacobalberty/firebird, according to:

select rdb$get_context('SYSTEM', 'ENGINE_VERSION')
from rdb$database;

Take this schema:

create table b (x int primary key);
create table a (x int primary key, y int references b);
insert into b values (1);
insert into b values (2);
insert into b values (3);
insert into b values (4);
insert into a values (1, 1);
insert into a values (2, 1);
insert into a values (3, 4);
insert into a values (4, 2);

Now, run this query:

select a.x, b.x
from a
  left outer join b
    on (
      a.y = b.x
      and a.x = b.x
    )
order by a.x

It produces:

|X  |X  |
|---|---|
|1  |1  |
|2  |   |
|4  |   |
|3  |   |

Clearly, the ordering of the last two rows is wrong. It should be:

|X  |X  |
|---|---|
|1  |1  |
|2  |   |
|3  |   |
|4  |   |
@lukaseder
Copy link
Author

This works around the issue:

select a.x, b.x
from a
  left outer join b
    on (
      a.y = b.x
      and a.x = b.x
    )
order by a.x + 1

@mrotteveel
Copy link
Member

@dyemanov Are you sure 5.0 was affected as well, because I could not reproduce it on 5.0.0.1073.

@dyemanov
Copy link
Member

dyemanov commented Jul 7, 2023

@dyemanov Are you sure 5.0 was affected as well, because I could not reproduce it on 5.0.0.1073.

The code is the same, just slightly different index comparison logic is used (which index to use for ORDER BY). So this particular test works fine, but some different test may fail the same way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment