Skip to content

Commit

Permalink
opt: add testcases showing stats problem
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
RaduBerinde committed Aug 21, 2018
1 parent 7855663 commit e1401e9
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
80 changes: 80 additions & 0 deletions pkg/sql/opt/memo/testdata/stats/join
Original file line number Diff line number Diff line change
Expand Up @@ -689,3 +689,83 @@ inner-join (merge)
├── right ordering: +5
└── filters [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ]), fd=(1)==(5), (5)==(1)]
└── a = e [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ])]

exec-ddl
CREATE TABLE uvw (u INT, v INT, w INT)
----
TABLE uvw
├── u int
├── v int
├── w int
├── rowid int not null (hidden)
└── INDEX primary
└── rowid int not null (hidden)

exec-ddl
CREATE TABLE xyz (x INT, y INT, z INT)
----
TABLE xyz
├── x int
├── y int
├── z int
├── rowid int not null (hidden)
└── INDEX primary
└── rowid int not null (hidden)

# Verify that two equivalent formulations of a join lead to similar statistics.
# In the first case, x=10 is pushed down; in the second case it is part of the
# ON condition. The latter formulation happens in practice when we convert to
# lookup join (we incorporate the filter back into the ON condition).
# TODO(radu): the second case has orders of magnitude smaller row count; fix
# this.

norm disable=(PushFilterIntoJoinLeftAndRight,PushFilterIntoJoinLeft,PushFilterIntoJoinRight,MapFilterIntoJoinLeft,MapFilterIntoJoinRight)
SELECT * FROM (SELECT * FROM uvw WHERE w=1) JOIN (SELECT * FROM xyz WHERE x=10) ON u=x
----
inner-join
├── columns: u:1(int!null) v:2(int) w:3(int!null) x:5(int!null) y:6(int) z:7(int)
├── stats: [rows=1.429009, distinct(1)=1, distinct(5)=1]
├── fd: ()-->(1,3,5), (1)==(5), (5)==(1)
├── select
│ ├── columns: u:1(int) v:2(int) w:3(int!null)
│ ├── stats: [rows=1.42857143, distinct(1)=1.42813399, distinct(3)=1]
│ ├── fd: ()-->(3)
│ ├── scan uvw
│ │ ├── columns: u:1(int) v:2(int) w:3(int)
│ │ └── stats: [rows=1000, distinct(1)=700, distinct(3)=700]
│ └── filters [type=bool, outer=(3), constraints=(/3: [/1 - /1]; tight), fd=()-->(3)]
│ └── w = 1 [type=bool, outer=(3), constraints=(/3: [/1 - /1]; tight)]
├── select
│ ├── columns: x:5(int!null) y:6(int) z:7(int)
│ ├── stats: [rows=1.42857143, distinct(5)=1]
│ ├── fd: ()-->(5)
│ ├── scan xyz
│ │ ├── columns: x:5(int) y:6(int) z:7(int)
│ │ └── stats: [rows=1000, distinct(5)=700]
│ └── filters [type=bool, outer=(5), constraints=(/5: [/10 - /10]; tight), fd=()-->(5)]
│ └── x = 10 [type=bool, outer=(5), constraints=(/5: [/10 - /10]; tight)]
└── filters [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ]), fd=(1)==(5), (5)==(1)]
└── u = x [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ])]

norm disable=(PushFilterIntoJoinLeftAndRight,PushFilterIntoJoinLeft,PushFilterIntoJoinRight,MapFilterIntoJoinLeft,MapFilterIntoJoinRight)
SELECT * FROM (SELECT * FROM uvw WHERE w=1) JOIN xyz ON u=x AND x=10
----
inner-join
├── columns: u:1(int!null) v:2(int) w:3(int!null) x:5(int!null) y:6(int) z:7(int)
├── stats: [rows=0.0029154519, distinct(1)=0.0029154519, distinct(5)=0.0029154519]
├── fd: ()-->(1,3,5), (1)==(5), (5)==(1)
├── select
│ ├── columns: u:1(int) v:2(int) w:3(int!null)
│ ├── stats: [rows=1.42857143, distinct(1)=1.42813399, distinct(3)=1]
│ ├── fd: ()-->(3)
│ ├── scan uvw
│ │ ├── columns: u:1(int) v:2(int) w:3(int)
│ │ └── stats: [rows=1000, distinct(1)=700, distinct(3)=700]
│ └── filters [type=bool, outer=(3), constraints=(/3: [/1 - /1]; tight), fd=()-->(3)]
│ └── w = 1 [type=bool, outer=(3), constraints=(/3: [/1 - /1]; tight)]
├── scan xyz
│ ├── columns: x:5(int) y:6(int) z:7(int)
│ └── stats: [rows=1000, distinct(5)=700]
└── filters [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: [/10 - /10]), fd=()-->(1,5), (1)==(5), (5)==(1)]
├── u = x [type=bool, outer=(1,5), constraints=(/1: (/NULL - ]; /5: (/NULL - ])]
└── x = 10 [type=bool, outer=(5), constraints=(/5: [/10 - /10]; tight)]
67 changes: 67 additions & 0 deletions pkg/sql/opt/memo/testdata/stats/select
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,70 @@ select
└── filters [type=bool, outer=(1), constraints=(/1: [/0 - /99]; tight)]
├── x >= 0 [type=bool, outer=(1), constraints=(/1: [/0 - ]; tight)]
└── x < 100 [type=bool, outer=(1), constraints=(/1: (/NULL - /99]; tight)]

exec-ddl
CREATE TABLE uvw (u INT, v INT, w INT)
----
TABLE uvw
├── u int
├── v int
├── w int
├── rowid int not null (hidden)
└── INDEX primary
└── rowid int not null (hidden)

# Test selectivity calculations by applying the two constraints in different
# orders.
# TODO(radu): applying both constraints at the same time results in much lower
# estimates.
norm
SELECT * FROM uvw WHERE u=v AND u=10
----
select
├── columns: u:1(int!null) v:2(int!null) w:3(int)
├── stats: [rows=0.00204081633, distinct(1)=0.00204081633, distinct(2)=0.00204081633]
├── fd: ()-->(1,2), (1)==(2), (2)==(1)
├── scan uvw
│ ├── columns: u:1(int) v:2(int) w:3(int)
│ └── stats: [rows=1000, distinct(1)=700, distinct(2)=700]
└── filters [type=bool, outer=(1,2), constraints=(/1: [/10 - /10]; /2: (/NULL - ]), fd=()-->(1,2), (1)==(2), (2)==(1)]
├── u = v [type=bool, outer=(1,2), constraints=(/1: (/NULL - ]; /2: (/NULL - ])]
└── u = 10 [type=bool, outer=(1), constraints=(/1: [/10 - /10]; tight)]

norm disable=MergeSelects
SELECT * FROM (SELECT * FROM uvw WHERE u=10) WHERE u=v
----
select
├── columns: u:1(int!null) v:2(int!null) w:3(int)
├── stats: [rows=1.0003063, distinct(1)=1, distinct(2)=1]
├── fd: ()-->(1,2), (1)==(2), (2)==(1)
├── select
│ ├── columns: u:1(int!null) v:2(int) w:3(int)
│ ├── stats: [rows=1.42857143, distinct(1)=1, distinct(2)=1.42813399]
│ ├── fd: ()-->(1)
│ ├── scan uvw
│ │ ├── columns: u:1(int) v:2(int) w:3(int)
│ │ └── stats: [rows=1000, distinct(1)=700, distinct(2)=700]
│ └── filters [type=bool, outer=(1), constraints=(/1: [/10 - /10]; tight), fd=()-->(1)]
│ └── u = 10 [type=bool, outer=(1), constraints=(/1: [/10 - /10]; tight)]
└── filters [type=bool, outer=(1,2), constraints=(/1: (/NULL - ]; /2: (/NULL - ]), fd=(1)==(2), (2)==(1)]
└── u = v [type=bool, outer=(1,2), constraints=(/1: (/NULL - ]; /2: (/NULL - ])]

norm disable=MergeSelects
SELECT * FROM (SELECT * FROM uvw WHERE u=v) WHERE u=10
----
select
├── columns: u:1(int!null) v:2(int!null) w:3(int)
├── stats: [rows=1, distinct(1)=1]
├── fd: ()-->(1,2), (1)==(2), (2)==(1)
├── select
│ ├── columns: u:1(int!null) v:2(int!null) w:3(int)
│ ├── stats: [rows=1.42857143, distinct(1)=1.42857143, distinct(2)=1.42857143]
│ ├── fd: (1)==(2), (2)==(1)
│ ├── scan uvw
│ │ ├── columns: u:1(int) v:2(int) w:3(int)
│ │ └── stats: [rows=1000, distinct(1)=700, distinct(2)=700]
│ └── filters [type=bool, outer=(1,2), constraints=(/1: (/NULL - ]; /2: (/NULL - ]), fd=(1)==(2), (2)==(1)]
│ └── u = v [type=bool, outer=(1,2), constraints=(/1: (/NULL - ]; /2: (/NULL - ])]
└── filters [type=bool, outer=(1), constraints=(/1: [/10 - /10]; tight), fd=()-->(1)]
└── u = 10 [type=bool, outer=(1), constraints=(/1: [/10 - /10]; tight)]

0 comments on commit e1401e9

Please sign in to comment.