github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/st_dimension.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package spatial 16 17 import ( 18 "fmt" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/expression" 22 "github.com/dolthub/go-mysql-server/sql/types" 23 ) 24 25 // Dimension is a function that converts a spatial type into WKT format (alias for AsText) 26 type Dimension struct { 27 expression.UnaryExpression 28 } 29 30 var _ sql.FunctionExpression = (*Dimension)(nil) 31 var _ sql.CollationCoercible = (*Dimension)(nil) 32 33 // NewDimension creates a new point expression. 34 func NewDimension(e sql.Expression) sql.Expression { 35 return &Dimension{expression.UnaryExpression{Child: e}} 36 } 37 38 // FunctionName implements sql.FunctionExpression 39 func (p *Dimension) FunctionName() string { 40 return "st_dimension" 41 } 42 43 // Description implements sql.FunctionExpression 44 func (p *Dimension) Description() string { 45 return "returns the dimension of the geometry given." 46 } 47 48 // IsNullable implements the sql.Expression interface. 49 func (p *Dimension) IsNullable() bool { 50 return p.Child.IsNullable() 51 } 52 53 // Type implements the sql.Expression interface. 54 func (p *Dimension) Type() sql.Type { 55 return types.Int32 56 } 57 58 // CollationCoercibility implements the interface sql.CollationCoercible. 59 func (*Dimension) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 60 return sql.Collation_binary, 5 61 } 62 63 func (p *Dimension) String() string { 64 return fmt.Sprintf("%s(%s)", p.FunctionName(), p.Child.String()) 65 } 66 67 // WithChildren implements the Expression interface. 68 func (p *Dimension) WithChildren(children ...sql.Expression) (sql.Expression, error) { 69 if len(children) != 1 { 70 return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 1) 71 } 72 return NewDimension(children[0]), nil 73 } 74 75 func FindDimension(g types.GeometryValue) interface{} { 76 switch v := g.(type) { 77 case types.Point, types.MultiPoint: 78 return 0 79 case types.LineString, types.MultiLineString: 80 return 1 81 case types.Polygon, types.MultiPolygon: 82 return 2 83 case types.GeomColl: 84 if len(v.Geoms) == 0 { 85 return nil 86 } 87 maxDim := 0 88 for _, geom := range v.Geoms { 89 dim := FindDimension(geom) 90 if dim == nil { 91 return nil 92 } 93 if dim.(int) > maxDim { 94 maxDim = dim.(int) 95 } 96 } 97 return maxDim 98 default: 99 return nil 100 } 101 } 102 103 // Eval implements the sql.Expression interface. 104 func (p *Dimension) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 105 // Evaluate child 106 val, err := p.Child.Eval(ctx, row) 107 if err != nil { 108 return nil, err 109 } 110 111 // Return nil if geometry is nil 112 if val == nil { 113 return nil, nil 114 } 115 116 // Expect one of the geometry types 117 switch v := val.(type) { 118 case types.GeometryValue: 119 return FindDimension(v), nil 120 default: 121 return nil, sql.ErrInvalidGISData.New("ST_DIMENSION") 122 } 123 }