Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
50784: opt: add ScanPrivate.IsUnfiltered() helper function r=RaduBerinde a=mgartner

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.

Related context: #49933 (comment)

Release note: None

50825: geogfn: apply bounding box calculations for DWithin r=sumeerbhola a=otan

Expand a BoundingRect by a given radius (using s2.Cap) and apply
intersection to do fairly cheap bounding box calculations before doing
the expensive DWithin logic.

Release note: None

50881: Makefile: change GEOS symlink to a copy r=petermattis a=otan

Instead of symlinking to the correct GEOS directory, force a copy
following the symlinks instead. This dramatically simplifies release
scripts from needing to follow symlinks to add the file to the
archive/Docker container.

Release note: None

50894: mutations: remove session variable setting from postgres mutator r=mjibson a=rohany

Fixes #48087.

This PR ensures that SQLSmith doesn't send session variable setting
statements to Postgres in various comparison tests.

Release note: None

Co-authored-by: Marcus Gartner <[email protected]>
Co-authored-by: Oliver Tan <[email protected]>
Co-authored-by: Rohan Yadav <[email protected]>
  • Loading branch information
4 people committed Jul 1, 2020
5 parents 09ef9ef + 4943780 + 51ddf02 + 40f919a + e14a1ad commit 54c7f57
Show file tree
Hide file tree
Showing 19 changed files with 267 additions and 243 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,8 @@ $(LIBSNAPPY): $(SNAPPY_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
$(LIBGEOS): $(GEOS_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
@uptodate $@ $(GEOS_SRC_DIR) || $(MAKE) --no-print-directory -C $(GEOS_DIR) geos_c
mkdir -p $(DYN_LIB_DIR)
ln -sf $(GEOS_DIR)/lib/lib{geos,geos_c}.$(DYN_EXT) $(DYN_LIB_DIR)
rm -f $(DYN_LIB_DIR)/lib{geos,geos_c}.$(DYN_EXT)
cp -L $(GEOS_DIR)/$(if $(target-is-windows),bin,lib)/lib{geos,geos_c}.$(DYN_EXT) $(DYN_LIB_DIR)

$(LIBPROJ): $(PROJ_DIR)/Makefile bin/uptodate .ALWAYS_REBUILD
@uptodate $@ $(PROJ_SRC_DIR) || $(MAKE) --no-print-directory -C $(PROJ_DIR) proj
Expand Down
7 changes: 6 additions & 1 deletion pkg/geo/geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (g *Geography) AsS2(emptyBehavior EmptyBehavior) ([]s2.Region, error) {
return S2RegionsFromGeom(geomRepr, emptyBehavior)
}

// BoundingRect returns the bounding rectangle of the given Geography.
// BoundingRect returns the bounding s2.Rect of the given Geography.
func (g *Geography) BoundingRect() s2.Rect {
bbox := g.spatialObject.BoundingBox
if bbox == nil {
Expand All @@ -484,6 +484,11 @@ func (g *Geography) BoundingRect() s2.Rect {
}
}

// BoundingCap returns the bounding s2.Cap of the given Geography.
func (g *Geography) BoundingCap() s2.Cap {
return g.BoundingRect().CapBound()
}

// IsLinearRingCCW returns whether a given linear ring is counter clock wise.
// See 2.07 of http://www.faqs.org/faqs/graphics/algorithms-faq/.
// "Find the lowest vertex (or, if there is more than one vertex with the same lowest coordinate,
Expand Down
12 changes: 6 additions & 6 deletions pkg/geo/geogfn/distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import (
"github.com/golang/geo/s2"
)

// SpheroidErrorFraction is an error fraction to compensate for using a sphere
// to calculate the distance for what is actually a spheroid. The distance
// calculation has an error that is bounded by (2 * spheroid.Flattening)%.
// This 5% margin is pretty safe.
const SpheroidErrorFraction = 0.05

// Distance returns the distance between geographies a and b on a sphere or spheroid.
// Returns a geo.EmptyGeometryError if any of the Geographies are EMPTY.
func Distance(
Expand Down Expand Up @@ -217,12 +223,6 @@ func distanceGeographyRegions(
return minDistance, nil
}

// SpheroidErrorFraction is an error fraction to compensate for using a sphere
// to calculate the distance for what is actually a spheroid. The distance
// calculation has an error that is bounded by (2 * spheroid.Flattening)%.
// This 5% margin is pretty safe.
const SpheroidErrorFraction = 0.05

// geographyMinDistanceUpdater finds the minimum distance using a sphere.
// Methods will return early if it finds a minimum distance <= stopAfterLE.
type geographyMinDistanceUpdater struct {
Expand Down
17 changes: 13 additions & 4 deletions pkg/geo/geogfn/dwithin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package geogfn
import (
"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/errors"
"github.com/golang/geo/s1"
)

// DWithin returns whether a is within distance d of b, i.e. Distance(a, b) <= d.
Expand All @@ -26,6 +27,18 @@ func DWithin(
if distance < 0 {
return false, errors.Newf("dwithin distance cannot be less than zero")
}
spheroid, err := a.Spheroid()
if err != nil {
return false, err
}

angleToExpand := s1.Angle(distance / spheroid.SphereRadius)
if useSphereOrSpheroid == UseSpheroid {
angleToExpand *= (1 + SpheroidErrorFraction)
}
if !a.BoundingCap().Expanded(angleToExpand).Intersects(b.BoundingCap()) {
return false, nil
}

aRegions, err := a.AsS2(geo.EmptyBehaviorError)
if err != nil {
Expand All @@ -41,10 +54,6 @@ func DWithin(
}
return false, err
}
spheroid, err := a.Spheroid()
if err != nil {
return false, err
}
maybeClosestDistance, err := distanceGeographyRegions(spheroid, useSphereOrSpheroid, aRegions, bRegions, distance)
if err != nil {
return false, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/mutations/mutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func postgresMutator(rng *rand.Rand, q string) string {
var postgresStatementMutator MultiStatementMutation = func(rng *rand.Rand, stmts []tree.Statement) (mutated []tree.Statement, changed bool) {
for _, stmt := range stmts {
switch stmt := stmt.(type) {
case *tree.SetClusterSetting:
case *tree.SetClusterSetting, *tree.SetVar:
continue
case *tree.CreateTable:
if stmt.Interleave != nil {
Expand Down
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
Loading

0 comments on commit 54c7f57

Please sign in to comment.