github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/st_length.go (about)

     1  // Copyright 2020-2022 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  	"math"
    20  	"strings"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/expression"
    24  	"github.com/dolthub/go-mysql-server/sql/types"
    25  )
    26  
    27  // STLength is a function that returns the STLength of a LineString
    28  type STLength struct {
    29  	expression.NaryExpression
    30  }
    31  
    32  var _ sql.FunctionExpression = (*STLength)(nil)
    33  var _ sql.CollationCoercible = (*STLength)(nil)
    34  
    35  // NewSTLength creates a new STLength expression.
    36  func NewSTLength(args ...sql.Expression) (sql.Expression, error) {
    37  	if len(args) != 1 && len(args) != 2 {
    38  		return nil, sql.ErrInvalidArgumentNumber.New("ST_LENGTH", "1 or 2", len(args))
    39  	}
    40  	return &STLength{expression.NaryExpression{ChildExpressions: args}}, nil
    41  }
    42  
    43  // FunctionName implements sql.FunctionExpression
    44  func (s *STLength) FunctionName() string {
    45  	return "st_length"
    46  }
    47  
    48  // Description implements sql.FunctionExpression
    49  func (s *STLength) Description() string {
    50  	return "returns the length of the given linestring. If given a unit argument, will return the length in those units"
    51  }
    52  
    53  // Type implements the sql.Expression interface.
    54  func (s *STLength) Type() sql.Type {
    55  	return types.Float64
    56  }
    57  
    58  // CollationCoercibility implements the interface sql.CollationCoercible.
    59  func (*STLength) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    60  	return sql.Collation_binary, 5
    61  }
    62  
    63  func (s *STLength) String() string {
    64  	var args = make([]string, len(s.ChildExpressions))
    65  	for i, arg := range s.ChildExpressions {
    66  		args[i] = arg.String()
    67  	}
    68  	return fmt.Sprintf("%s(%s)", s.FunctionName(), strings.Join(args, ","))
    69  }
    70  
    71  // WithChildren implements the Expression interface.
    72  func (s *STLength) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    73  	return NewSTLength(children...)
    74  }
    75  
    76  // calculateLength sums up the line segments formed from a LineString
    77  func calculateLength(l types.LineString) float64 {
    78  	var length float64
    79  	for i := 0; i < len(l.Points)-1; i++ {
    80  		p1 := l.Points[i]
    81  		p2 := l.Points[i+1]
    82  		length += math.Sqrt(math.Pow(p2.X-p1.X, 2) + math.Pow(p2.Y-p1.Y, 2))
    83  	}
    84  	return length
    85  }
    86  
    87  // Eval implements the sql.Expression interface.
    88  func (s *STLength) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    89  	// Evaluate first argument
    90  	v1, err := s.ChildExpressions[0].Eval(ctx, row)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	// Return nil if argument is nil
    96  	if v1 == nil {
    97  		return nil, nil
    98  	}
    99  
   100  	// Return nil if argument is geometry typ, but not linestring
   101  	var l types.LineString
   102  	switch v := v1.(type) {
   103  	case types.LineString:
   104  		l = v
   105  	case types.Point, types.Polygon:
   106  		return nil, nil
   107  	default:
   108  		return nil, sql.ErrInvalidGISData.New(s.FunctionName())
   109  	}
   110  
   111  	// TODO: if SRID is not 0, find geodetic distance
   112  	// If just one argument, return length
   113  	if len(s.ChildExpressions) == 1 {
   114  		return calculateLength(l), nil
   115  	}
   116  
   117  	// TODO: support geodetic distance
   118  	return nil, sql.ErrUnsupportedFeature.New("st_length with non-zero SRID")
   119  }