go-mysql-server icon indicating copy to clipboard operation
go-mysql-server copied to clipboard

Functional Dependency anaylsis on simple left join doesn't behave as expected.

Open nicktobey opened this issue 1 year ago • 1 comments

Put a breakpoint in lookupJoinSelectivity before the call to sql.NewLookupFDs

Run the following SQL:

drop table if exists uv;
CREATE table uv (u int primary key, v int);
insert into uv values (0,0),
	(1,1),
	(2,2),
	(3,3);

drop table if exists pq;
CREATE table pq (p int primary key, q int);
insert into pq values
  (0,0),
  (1,1),
  (2,2),
  (3,3);


  select * from uv
  left join pq on u = p;

As part of costing joins, we attempt to detect lookups that we know will always return at most one row. This is a perfect candidate of such a lookup: We use u as the key into a lookup on the primary key index pq.p. This always has at most one result. So we expect that fds.HasMax1Row() within lookupJoinSelectivity should return true. But it returns false instead, and we fail to prioritize this execution plan.

nicktobey avatar Jul 24 '23 22:07 nicktobey

I think my default approach for LEFT JOIN was heavy handed, left this TODO which I think applies to your example https://github.com/dolthub/go-mysql-server/blob/main/sql/func_deps.go#L647

max-hoffman avatar Jul 24 '23 23:07 max-hoffman