-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
48552: geo/geomfn: implement ST_Relate and ST_ContainsProperly r=sumeerbhola a=otan Commit on top of #48522. Added ST_ContainsProperly to the optimizer as well that calls it to use Covers. Also update the RFC to claim ST_ContainsProperly as indexed backed. Release note (sql change): Implemented the geometry based builtins `ST_Relate` and `ST_ContainsProperly`. 48659: colexec: fix type schema for LEFT SEMI and LEFT ANTI joins r=yuzefovich a=yuzefovich LEFT SEMI and LEFT ANTI joins output only all the columns from the left, however, we mistakenly put the columns from the right into `result.ColumnTypes`. Fixes: #48622. Release note (bug fix): Previously, CockroachDB could encounter an internal error when a query with LEFT SEMI or LEFT ANTI join was performed via the vectorized execution engine in some cases, and now this has been fixed. This is likely to occur only with `vectorize=on` setting. Co-authored-by: Oliver Tan <[email protected]> Co-authored-by: Yahor Yuzefovich <[email protected]>
- Loading branch information
Showing
15 changed files
with
471 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package geomfn | ||
|
||
import ( | ||
"github.com/cockroachdb/cockroach/pkg/geo" | ||
"github.com/cockroachdb/cockroach/pkg/geo/geos" | ||
"github.com/cockroachdb/cockroach/pkg/util" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
// Relate returns the DE-9IM relation between A and B. | ||
func Relate(a *geo.Geometry, b *geo.Geometry) (string, error) { | ||
if a.SRID() != b.SRID() { | ||
return "", geo.NewMismatchingSRIDsError(a, b) | ||
} | ||
return geos.Relate(a.EWKB(), b.EWKB()) | ||
} | ||
|
||
// MatchesDE9IM checks whether the given DE-9IM relation matches the DE-91M pattern. | ||
// Assumes the relation has been computed, and such has no 'T' and '*' characters. | ||
// See: https://en.wikipedia.org/wiki/DE-9IM. | ||
func MatchesDE9IM(relation string, pattern string) (bool, error) { | ||
if len(relation) != 9 { | ||
return false, errors.Newf("relation %q should be of length 9", relation) | ||
} | ||
if len(pattern) != 9 { | ||
return false, errors.Newf("pattern %q should be of length 9", pattern) | ||
} | ||
for i := 0; i < len(relation); i++ { | ||
matches, err := relationByteMatchesPatternByte(relation[i], pattern[i]) | ||
if err != nil { | ||
return false, err | ||
} | ||
if !matches { | ||
return false, nil | ||
} | ||
} | ||
return true, nil | ||
} | ||
|
||
// relationByteMatchesPatternByte matches a single byte of a DE-9IM relation | ||
// against the DE-9IM pattern. | ||
// Pattern matches are as follows: | ||
// * '*': allow anything. | ||
// * 't'/'T': allow only if the relation is true. This means the relation must be | ||
// '0' (point), '1' (line) or '2' (area) - which is the dimensionality of the | ||
// intersection. | ||
// * 'f'/'F': allow only if relation is also false, which is of the form 'f'/'F'. | ||
func relationByteMatchesPatternByte(r byte, p byte) (bool, error) { | ||
switch util.ToLowerSingleByte(p) { | ||
case '*': | ||
return true, nil | ||
case 't': | ||
if r < '0' || r > '2' { | ||
return false, nil | ||
} | ||
case 'f': | ||
if util.ToLowerSingleByte(r) != 'f' { | ||
return false, nil | ||
} | ||
default: | ||
return false, errors.Newf("unrecognized pattern character: %s", string(p)) | ||
} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2020 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package geomfn | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/geo" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRelate(t *testing.T) { | ||
testCases := []struct { | ||
a *geo.Geometry | ||
b *geo.Geometry | ||
expected string | ||
}{ | ||
{leftRect, rightRect, "FF2F11212"}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
t.Run(fmt.Sprintf("tc:%d", i), func(t *testing.T) { | ||
ret, err := Relate(tc.a, tc.b) | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, ret) | ||
}) | ||
} | ||
} | ||
|
||
func TestMatchesDE9IM(t *testing.T) { | ||
testCases := []struct { | ||
str string | ||
pattern string | ||
expected bool | ||
expectedError string | ||
}{ | ||
{"", "T**FF*FF*", false, `relation "" should be of length 9`}, | ||
{"TTTTTTTTT", "T**FF*FF*T", false, `pattern "T**FF*FF*T" should be of length 9`}, | ||
{"TTTTTTTTT", "T**FF*FF*T", false, `pattern "T**FF*FF*T" should be of length 9`}, | ||
{"000FFF000", "cTTFfFTTT", false, `unrecognized pattern character: c`}, | ||
{"120FFF021", "TTTFfFTTT", true, ""}, | ||
{"02FFFF000", "T**FfFTTT", true, ""}, | ||
{"020F1F010", "TTTFFFTtT", false, ""}, | ||
{"020FFF0f0", "TTTFFFTtT", false, ""}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("%s has pattern %s", tc.str, tc.pattern), func(t *testing.T) { | ||
ret, err := MatchesDE9IM(tc.str, tc.pattern) | ||
if tc.expectedError == "" { | ||
require.NoError(t, err) | ||
require.Equal(t, tc.expected, ret) | ||
} else { | ||
require.EqualError(t, err, tc.expectedError) | ||
} | ||
}) | ||
} | ||
|
||
t.Run("errors if SRIDs mismatch", func(t *testing.T) { | ||
_, err := Relate(mismatchingSRIDGeometryA, mismatchingSRIDGeometryB) | ||
requireMismatchingSRIDError(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.