Skip to content

Incorrect result HASH SEMI JOIN with UUID key #9158

Description

@sim1984

WI-V5.0.5.1816 Firebird 5.0 2f91fa0
SubQueryConversion = true

recreate table uuid_1M_a(id binary(16) not null);
recreate table uuid_1M_b(id binary(16) not null);

create index idx_uuid_1M_b_id on uuid_1M_b(id);

set term ^;

execute block
as
  declare variable i int = 0;
begin
  while (i < 1000000) do
  begin
    insert into uuid_1M_a(id) values (gen_uuid());
    i = i + 1;
  end
end^

set term ;^

insert into uuid_1M_b(id)
select a.id from uuid_1M_a a;

commit;
select count(distinct id) from uuid_1M_a;
                COUNT
=====================
              1000000
select count(distinct id) from uuid_1M_b;
                COUNT
=====================
              1000000
set explain on;

select count(*)
from uuid_1M_a a
join uuid_1M_b b on a.id = b.id;
Select Expression
    -> Aggregate
        -> Filter
            -> Hash Join (inner)
                -> Table "UUID_1M_A" as "A" Full Scan
                -> Record Buffer (record length: 41)
                    -> Table "UUID_1M_B" as "B" Full Scan

                COUNT
=====================
              1000000
select count(*)
from uuid_1M_a a
where exists (select * from uuid_1M_b b where a.id = b.id);
Select Expression
    -> Aggregate
        -> Filter
            -> Hash Join (semi)
                -> Table "UUID_1M_A" as "A" Full Scan
                -> Record Buffer (record length: 41)
                    -> Table "UUID_1M_B" as "B" Full Scan

                COUNT
=====================
               999883                                            <----- What ????????????????????

But:

select count(*)
from uuid_1M_a a
where exists (select * from uuid_1M_b b where a.id = b.id and nullif(a.id, b.id) is null);
Sub-query
    -> Filter
        -> Table "UUID_1M_B" as "B" Access By ID
            -> Bitmap
                -> Index "IDX_UUID_1M_B_ID" Range Scan (full match)
Select Expression
    -> Aggregate
        -> Filter
            -> Table "UUID_1M_A" as "A" Full Scan

                COUNT
=====================
              1000000

The issue is reproducible in both version 5.0.5 and version 6.0.

The value 999883 can vary significantly if the test is rerun from scratch. It all depends on the generated UUID. Sometimes, everything works fine.

If SubQueryConversion = false result:

select count(*)
from uuid_1M_a a
where exists (select * from uuid_1M_b b where a.id = b.id);
Sub-query
    -> Filter
        -> Table "UUID_1M_B" as "B" Access By ID
            -> Bitmap
                -> Index "IDX_UUID_1M_B_ID" Range Scan (full match)
Select Expression
    -> Aggregate
        -> Filter
            -> Table "UUID_1M_A" as "A" Full Scan

                COUNT
=====================
              1000000

Activity

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

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions