Skip to content

Commit

Permalink
opt: add ScanPrivate.IsUnfiltered() helper function
Browse files Browse the repository at this point in the history
This commit adds a new method to `ScanPrivate`, `IsUnfiltered`, which
returns true if the scan is guaranteed to produce all rows in the table.
It returns false if it may not return all rows because it has a limit,
it is constrained, or it scans a partial index.

This commit updates a few lines of code that were manually performing
these checks.

Release note: None
  • Loading branch information
mgartner committed Jul 1, 2020
1 parent 0672a96 commit 4943780
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 230 deletions.
10 changes: 10 additions & 0 deletions pkg/sql/opt/memo/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,16 @@ func (s *ScanPrivate) IsCanonical() bool {
s.HardLimit == 0
}

// IsUnfiltered returns true if the ScanPrivate will produce all rows in the
// table.
func (s *ScanPrivate) IsUnfiltered(md *opt.Metadata) bool {
_, isPartialIndex := md.Table(s.Table).Index(s.Index).Predicate()
return !isPartialIndex &&
(s.Constraint == nil || s.Constraint.IsUnconstrained()) &&
s.InvertedConstraint == nil &&
s.HardLimit == 0
}

// IsLocking returns true if the ScanPrivate is configured to use a row-level
// locking mode. This can be the case either because the Scan is in the scope of
// a SELECT .. FOR [KEY] UPDATE/SHARE clause or because the Scan was configured
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/opt/memo/multiplicity_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func deriveUnfilteredCols(in RelExpr) opt.ColSet {
// non-null foreign key relation - rows in kr imply rows in xy. However,
// the columns from xy are not output columns, so in order to see that this
// is the case we must bubble up non-output columns.
baseTable := t.Memo().Metadata().Table(t.Table)
_, isPartialIndex := baseTable.Index(t.Index).Predicate()
if t.HardLimit == 0 && t.Constraint == nil && !isPartialIndex {
md := t.Memo().Metadata()
baseTable := md.Table(t.Table)
if t.IsUnfiltered(md) {
for i, cnt := 0, baseTable.ColumnCount(); i < cnt; i++ {
unfilteredCols.Add(t.Table.ColumnID(i))
}
Expand Down
60 changes: 30 additions & 30 deletions pkg/sql/opt/opbench/testdata/scan-lineitem.csv
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
rows,num_cols,estimated,actual
1000000,1,1170000.020000,0.834084
2000000,1,2340000.020000,1.489845
3000000,1,3510000.020000,2.211772
4000000,1,4680000.020000,2.787555
5000000,1,5850000.020000,3.727968
6000000,1,7020000.020000,4.538592
1000000,2,1180000.020000,0.856709
2000000,2,2360000.020000,1.689832
3000000,2,3540000.020000,2.624644
4000000,2,4720000.020000,3.354893
5000000,2,5900000.020000,4.149957
6000000,2,7080000.020000,5.126522
1000000,3,1190000.020000,0.948950
2000000,3,2380000.020000,1.844214
3000000,3,3570000.020000,2.819678
4000000,3,4760000.020000,3.689639
5000000,3,5950000.020000,4.598679
6000000,3,7140000.020000,5.589131
1000000,4,1200000.020000,0.963368
2000000,4,2400000.020000,1.907634
3000000,4,3600000.020000,2.819860
4000000,4,4800000.020000,3.835917
5000000,4,6000000.020000,4.603531
6000000,4,7200000.020000,5.676630
1000000,16,1320000.020000,1.951303
2000000,16,2640000.020000,3.845009
3000000,16,3960000.020000,5.823685
4000000,16,5280000.020000,7.786907
5000000,16,6600000.020000,9.746737
6000000,16,7920000.020000,11.603873
1000000,1,1170000.010000,0.834084
2000000,1,2340000.010000,1.489845
3000000,1,3510000.010000,2.211772
4000000,1,4680000.010000,2.787555
5000000,1,5850000.010000,3.727968
6000000,1,7020000.010000,4.538592
1000000,2,1180000.010000,0.856709
2000000,2,2360000.010000,1.689832
3000000,2,3540000.010000,2.624644
4000000,2,4720000.010000,3.354893
5000000,2,5900000.010000,4.149957
6000000,2,7080000.010000,5.126522
1000000,3,1190000.010000,0.948950
2000000,3,2380000.010000,1.844214
3000000,3,3570000.010000,2.819678
4000000,3,4760000.010000,3.689639
5000000,3,5950000.010000,4.598679
6000000,3,7140000.010000,5.589131
1000000,4,1200000.010000,0.963368
2000000,4,2400000.010000,1.907634
3000000,4,3600000.010000,2.819860
4000000,4,4800000.010000,3.835917
5000000,4,6000000.010000,4.603531
6000000,4,7200000.010000,5.676630
1000000,16,1320000.010000,1.951303
2000000,16,2640000.010000,3.845009
3000000,16,3960000.010000,5.823685
4000000,16,5280000.010000,7.786907
5000000,16,6600000.010000,9.746737
6000000,16,7920000.010000,11.603873
48 changes: 24 additions & 24 deletions pkg/sql/opt/opbench/testdata/scan-orders.csv
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
rows,num_cols,estimated,actual
250000,1,275000.020000,0.234685
500000,1,550000.020000,0.355673
750000,1,825000.020000,0.536125
1000000,1,1100000.020000,0.735832
1250000,1,1375000.020000,0.909200
1500000,1,1650000.020000,1.036382
250000,3,280000.020000,0.226567
500000,3,560000.020000,0.441384
750000,3,840000.020000,0.611747
1000000,3,1120000.020000,0.881042
1250000,3,1400000.020000,1.092393
1500000,3,1680000.020000,1.314116
250000,6,287500.020000,0.292452
500000,6,575000.020000,0.496511
750000,6,862500.020000,0.817809
1000000,6,1150000.020000,1.162477
1250000,6,1437500.020000,1.451609
1500000,6,1725000.020000,1.760463
250000,9,295000.020000,0.360331
500000,9,590000.020000,0.626732
750000,9,885000.020000,0.992292
1000000,9,1180000.020000,1.368585
1250000,9,1475000.020000,1.727139
1500000,9,1770000.020000,2.056275
250000,1,275000.010000,0.234685
500000,1,550000.010000,0.355673
750000,1,825000.010000,0.536125
1000000,1,1100000.010000,0.735832
1250000,1,1375000.010000,0.909200
1500000,1,1650000.010000,1.036382
250000,3,280000.010000,0.226567
500000,3,560000.010000,0.441384
750000,3,840000.010000,0.611747
1000000,3,1120000.010000,0.881042
1250000,3,1400000.010000,1.092393
1500000,3,1680000.010000,1.314116
250000,6,287500.010000,0.292452
500000,6,575000.010000,0.496511
750000,6,862500.010000,0.817809
1000000,6,1150000.010000,1.162477
1250000,6,1437500.010000,1.451609
1500000,6,1725000.010000,1.760463
250000,9,295000.010000,0.360331
500000,9,590000.010000,0.626732
750000,9,885000.010000,0.992292
1000000,9,1180000.010000,1.368585
1250000,9,1475000.010000,1.727139
1500000,9,1770000.010000,2.056275
36 changes: 18 additions & 18 deletions pkg/sql/opt/opbench/testdata/sort-lineitem.csv
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
rows,num_cols,estimated,actual
1000000,1,1588631.401386,1.467528
2000000,1,3217262.772773,3.007519
3000000,1,4860991.894203,5.827569
4000000,1,6514525.515546,7.106306
5000000,1,8175349.696421,8.982024
6000000,1,9841983.758405,10.749726
1000000,2,1598631.401386,1.665857
2000000,2,3237262.772773,4.036806
3000000,2,4890991.894203,6.109568
4000000,2,6554525.515546,8.072787
5000000,2,8225349.696421,9.743525
6000000,2,9901983.758405,12.756862
1000000,3,1608631.401386,2.786447
2000000,3,3257262.772773,4.812669
3000000,3,4920991.894203,7.417335
4000000,3,6594525.515546,11.088666
5000000,3,8275349.696421,12.703216
6000000,3,9961983.758405,14.615791
1000000,1,1588631.391386,1.467528
2000000,1,3217262.762773,3.007519
3000000,1,4860991.884203,5.827569
4000000,1,6514525.505546,7.106306
5000000,1,8175349.686421,8.982024
6000000,1,9841983.748405,10.749726
1000000,2,1598631.391386,1.665857
2000000,2,3237262.762773,4.036806
3000000,2,4890991.884203,6.109568
4000000,2,6554525.505546,8.072787
5000000,2,8225349.686421,9.743525
6000000,2,9901983.748405,12.756862
1000000,3,1608631.391386,2.786447
2000000,3,3257262.762773,4.812669
3000000,3,4920991.884203,7.417335
4000000,3,6594525.505546,11.088666
5000000,3,8275349.686421,12.703216
6000000,3,9961983.748405,14.615791
120 changes: 60 additions & 60 deletions pkg/sql/opt/opbench/testdata/tpch-hash-join.csv
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
lineitem_rows,supplier_rows,estimated,actual
1000000,1000,1059539.161521,0.632029
1000000,2000,1061578.273042,0.570772
1000000,3000,1063617.384562,0.598398
1000000,4000,1065656.496083,0.617674
1000000,5000,1067695.607604,0.646713
1000000,6000,1069734.719125,0.670244
1000000,7000,1071773.830646,0.696637
1000000,8000,1073812.942167,0.714715
1000000,9000,1075852.053687,0.751317
1000000,10000,1077891.165208,0.768779
2000000,1000,2118035.773042,1.138285
2000000,2000,2121071.496083,1.138734
2000000,3000,2124107.219125,1.282786
2000000,4000,2127142.942167,1.280636
2000000,5000,2130178.665208,1.312094
2000000,6000,2133214.388250,1.349938
2000000,7000,2136250.111292,1.419143
2000000,8000,2139285.834333,1.464272
2000000,9000,2142321.557375,1.517473
2000000,10000,2145357.280417,1.665660
3000000,1000,3176532.384562,1.723738
3000000,2000,3180564.719125,1.846815
3000000,3000,3184597.053687,1.898967
3000000,4000,3188629.388250,2.007566
3000000,5000,3192661.722812,2.114909
3000000,6000,3196694.057375,2.131143
3000000,7000,3200726.391937,2.195418
3000000,8000,3204758.726500,2.289942
3000000,9000,3208791.061062,2.392428
3000000,10000,3212823.395625,2.583456
4000000,1000,4235028.996083,2.372955
4000000,2000,4240057.942167,2.577938
4000000,3000,4245086.888250,2.751925
4000000,4000,4250115.834333,2.841285
4000000,5000,4255144.780417,2.787714
4000000,6000,4260173.726500,2.886389
4000000,7000,4265202.672583,3.053209
4000000,8000,4270231.618667,3.089117
4000000,9000,4275260.564750,3.175184
4000000,10000,4280289.510833,3.333599
5000000,1000,5293525.607604,2.949502
5000000,2000,5299551.165208,3.096689
5000000,3000,5305576.722812,3.231915
5000000,4000,5311602.280417,3.415585
5000000,5000,5317627.838021,3.470487
5000000,6000,5323653.395625,3.691811
5000000,7000,5329678.953229,3.747602
5000000,8000,5335704.510833,3.808640
5000000,9000,5341730.068437,4.090677
5000000,10000,5347755.626041,4.145000
6000000,1000,6352022.219125,3.547718
6000000,2000,6359044.388250,3.729800
6000000,3000,6366066.557375,3.814457
6000000,4000,6373088.726500,4.056065
6000000,5000,6380110.895625,4.093838
6000000,6000,6387133.064750,4.332647
6000000,7000,6394155.233875,4.503540
6000000,8000,6401177.403000,4.591593
6000000,9000,6408199.572125,4.831284
6000000,10000,6415221.741250,5.117229
1000000,1000,1059539.141521,0.632029
1000000,2000,1061578.253042,0.570772
1000000,3000,1063617.364562,0.598398
1000000,4000,1065656.476083,0.617674
1000000,5000,1067695.587604,0.646713
1000000,6000,1069734.699125,0.670244
1000000,7000,1071773.810646,0.696637
1000000,8000,1073812.922167,0.714715
1000000,9000,1075852.033687,0.751317
1000000,10000,1077891.145208,0.768779
2000000,1000,2118035.753042,1.138285
2000000,2000,2121071.476083,1.138734
2000000,3000,2124107.199125,1.282786
2000000,4000,2127142.922167,1.280636
2000000,5000,2130178.645208,1.312094
2000000,6000,2133214.368250,1.349938
2000000,7000,2136250.091292,1.419143
2000000,8000,2139285.814333,1.464272
2000000,9000,2142321.537375,1.517473
2000000,10000,2145357.260417,1.665660
3000000,1000,3176532.364562,1.723738
3000000,2000,3180564.699125,1.846815
3000000,3000,3184597.033687,1.898967
3000000,4000,3188629.368250,2.007566
3000000,5000,3192661.702812,2.114909
3000000,6000,3196694.037375,2.131143
3000000,7000,3200726.371937,2.195418
3000000,8000,3204758.706500,2.289942
3000000,9000,3208791.041062,2.392428
3000000,10000,3212823.375625,2.583456
4000000,1000,4235028.976083,2.372955
4000000,2000,4240057.922167,2.577938
4000000,3000,4245086.868250,2.751925
4000000,4000,4250115.814333,2.841285
4000000,5000,4255144.760417,2.787714
4000000,6000,4260173.706500,2.886389
4000000,7000,4265202.652583,3.053209
4000000,8000,4270231.598667,3.089117
4000000,9000,4275260.544750,3.175184
4000000,10000,4280289.490833,3.333599
5000000,1000,5293525.587604,2.949502
5000000,2000,5299551.145208,3.096689
5000000,3000,5305576.702812,3.231915
5000000,4000,5311602.260417,3.415585
5000000,5000,5317627.818021,3.470487
5000000,6000,5323653.375625,3.691811
5000000,7000,5329678.933229,3.747602
5000000,8000,5335704.490833,3.808640
5000000,9000,5341730.048437,4.090677
5000000,10000,5347755.606041,4.145000
6000000,1000,6352022.199125,3.547718
6000000,2000,6359044.368250,3.729800
6000000,3000,6366066.537375,3.814457
6000000,4000,6373088.706500,4.056065
6000000,5000,6380110.875625,4.093838
6000000,6000,6387133.044750,4.332647
6000000,7000,6394155.213875,4.503540
6000000,8000,6401177.383000,4.591593
6000000,9000,6408199.552125,4.831284
6000000,10000,6415221.721250,5.117229
20 changes: 10 additions & 10 deletions pkg/sql/opt/opbench/testdata/tpch-lookup-join.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
supplier_rows,estimated,actual
1000,1225129.551626,0.956943
2000,2450259.073253,1.718082
3000,3675388.594879,2.448982
4000,4900518.116506,3.256364
5000,6125647.638132,3.989822
6000,7350777.159759,4.934962
7000,8575906.681385,5.711155
8000,9801036.203012,6.269443
9000,11026165.724638,7.126775
10000,12251295.246265,7.803261
1000,1225129.541626,0.956943
2000,2450259.063253,1.718082
3000,3675388.584879,2.448982
4000,4900518.106506,3.256364
5000,6125647.628132,3.989822
6000,7350777.149759,4.934962
7000,8575906.671385,5.711155
8000,9801036.193012,6.269443
9000,11026165.714638,7.126775
10000,12251295.236265,7.803261
Loading

0 comments on commit 4943780

Please sign in to comment.