Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
49217: invertedexpr: add SpanExpressionProto and conversion r=sumeerbhola a=sumeerbhola

function

Release note: None:

49237: builtins: implement ST_WKBToSQL and ST_WKTToSQL r=sumeerbhola a=otan

Resolves cockroachdb#48821.
Resolves cockroachdb#48822.

Release note (sql change): Implement the ST_WKBToSQL and ST_WKTToSQL
functions.

49408: sqlbase: add a random datum for enum family r=rohany a=rohany

Release note: None

Co-authored-by: sumeerbhola <[email protected]>
Co-authored-by: Oliver Tan <[email protected]>
Co-authored-by: Rohan Yadav <[email protected]>
  • Loading branch information
4 people committed May 21, 2020
4 parents cedfdd8 + 422c137 + d5c6597 + 47fc965 commit 0085cf4
Show file tree
Hide file tree
Showing 10 changed files with 1,184 additions and 45 deletions.
4 changes: 4 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,10 @@ has no relationship with the commit order of concurrent transactions.</p>
<tr><td><a name="st_within"></a><code>st_within(geometry_a: geometry, geometry_b: geometry) &rarr; <a href="bool.html">bool</a></code></td><td><span class="funcdesc"><p>Returns true if geometry_a is completely inside geometry_b.</p>
<p>This function utilizes the GEOS module.</p>
<p>This function will automatically use any available index.</p>
</span></td></tr>
<tr><td><a name="st_wkbtosql"></a><code>st_wkbtosql(val: <a href="bytes.html">bytes</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns the Geometry from a WKB representation.</p>
</span></td></tr>
<tr><td><a name="st_wkttosql"></a><code>st_wkttosql(val: <a href="string.html">string</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns the Geometry from a WKT or EWKT representation.</p>
</span></td></tr></tbody>
</table>

Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/geospatial
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ INSERT INTO parse_test (geom, geog) VALUES
(ST_GeomFromEWKB(decode('0101000000000000000000F03F000000000000F03F', 'hex')), ST_GeogFromEWKB(decode('0101000000000000000000F03F000000000000F03F', 'hex'))),
(st_geomfromgeojson('null':::jsonb), st_geogfromgeojson('null':::jsonb))

query BB
SELECT
st_geomfromwkb(decode('0101000000000000000000F03F000000000000F03F', 'hex')) = st_wkbtosql(decode('0101000000000000000000F03F000000000000F03F', 'hex')),
st_geomfromtext('POINT(1.0 2.0)') = st_wkttosql('POINT(1.0 2.0)')
----
true true

query TTTTTTTT
SELECT
ST_AsText(geom),
Expand Down
57 changes: 42 additions & 15 deletions pkg/sql/opt/invertedexpr/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type EncInvertedVal []byte
// inverted indexes, where some columns of the primary key will appear before
// the inverted column.

// InvertedSpan is a span of the inverted index. Represents [start, end)
// InvertedSpan is a span of the inverted index. Represents [start, end).
type InvertedSpan struct {
start, end EncInvertedVal
}
Expand All @@ -114,18 +114,6 @@ func (s InvertedSpan) IsSingleVal() bool {
bytes.Equal(s.start, s.end[:len(s.end)-1])
}

// SetOperator is an operator on sets.
type SetOperator int

const (
// None is used in a SpanExpression with no children.
None SetOperator = iota
// SetUnion unions the children.
SetUnion
// SetIntersection intersects the children.
SetIntersection
)

// InvertedExpression is the interface representing an expression or sub-expression
// to be evaluated on the inverted index. Any implementation can be used in the
// builder functions And() and Or(), but in practice there are two useful
Expand Down Expand Up @@ -220,9 +208,12 @@ type InvertedExpression interface {
SetNotTight()
}

// TODO(sumeer): functions to serialize/deserialize SpanExpressions.

// SpanExpression is an implementation of InvertedExpression.
//
// TODO(sumeer): after integration and experimentation with optimizer costing,
// decide if we can eliminate the generality of the InvertedExpression
// interface. If we don't need that generality, we can merge SpanExpression
// and SpanExpressionProto.
type SpanExpression struct {
// Tight mirrors the definition of IsTight().
Tight bool
Expand Down Expand Up @@ -340,6 +331,42 @@ func formatSpan(b *strings.Builder, span InvertedSpan) {
strconv.Quote(string(end)), spanEndOpenOrClosed)
}

// ToProto constructs a SpanExpressionProto for execution. It should
// be called on an expression tree that contains only *SpanExpressions.
func (s *SpanExpression) ToProto() *SpanExpressionProto {
if s == nil {
return nil
}
proto := &SpanExpressionProto{
SpansToRead: getProtoSpans(s.SpansToRead),
Node: *s.getProtoNode(),
}
return proto
}

func getProtoSpans(spans []InvertedSpan) []SpanExpressionProto_Span {
out := make([]SpanExpressionProto_Span, 0, len(spans))
for i := range spans {
out = append(out, SpanExpressionProto_Span{
Start: spans[i].start,
End: spans[i].end,
})
}
return out
}

func (s *SpanExpression) getProtoNode() *SpanExpressionProto_Node {
node := &SpanExpressionProto_Node{
FactoredUnionSpans: getProtoSpans(s.FactoredUnionSpans),
Operator: s.Operator,
}
if node.Operator != None {
node.Left = s.Left.(*SpanExpression).getProtoNode()
node.Right = s.Right.(*SpanExpression).getProtoNode()
}
return node
}

// NonInvertedColExpression is an expression to use for parts of the
// user expression that do not involve the inverted index.
type NonInvertedColExpression struct{}
Expand Down
23 changes: 19 additions & 4 deletions pkg/sql/opt/invertedexpr/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/treeprinter"
"github.com/cockroachdb/datadriven"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/require"
)

Expand All @@ -26,9 +27,9 @@ Format for the datadriven test:
new-span-leaf name=<name> tight=<true|false> span=<start>[,<end>]
----
<spanExpression as string>
<SpanExpression as string>
Creates a new leaf spanExpression with the given name
Creates a new leaf SpanExpression with the given name
new-unknown-leaf name=<name> tight=<true|false>
----
Expand All @@ -42,15 +43,21 @@ new-non-inverted-leaf name=<name>
and result=<name> left=<name> right=<name>
----
<spanExpression as string>
<SpanExpression as string>
Ands the left and right expressions and stores the result
or result=<name> left=<name> right=<name>
----
<spanExpression as string>
<SpanExpression as string>
Ors the left and right expressions and stores the result
to-proto name=<name>
----
<SpanExpressionProto as string>
Converts the SpanExpression to SpanExpressionProto
*/

func getSpan(t *testing.T, d *datadriven.TestData) InvertedSpan {
Expand Down Expand Up @@ -164,6 +171,14 @@ func TestExpression(t *testing.T) {
expr := Or(left, right)
exprsByName[name] = expr
return toString(expr)
case "to-proto":
var name string
d.ScanArgs(t, "name", &name)
expr := exprsByName[name]
if expr == nil {
expr = (*SpanExpression)(nil)
}
return proto.MarshalTextString(expr.(*SpanExpression).ToProto())
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
Expand Down
Loading

0 comments on commit 0085cf4

Please sign in to comment.