github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/linestring.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  	"strings"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql/expression"
    22  	"github.com/dolthub/go-mysql-server/sql/types"
    23  
    24  	"github.com/dolthub/go-mysql-server/sql"
    25  )
    26  
    27  // LineString is a function that returns a point type containing values Y and Y.
    28  type LineString struct {
    29  	expression.NaryExpression
    30  }
    31  
    32  var _ sql.FunctionExpression = (*LineString)(nil)
    33  var _ sql.CollationCoercible = (*LineString)(nil)
    34  
    35  // NewLineString creates a new LineString.
    36  func NewLineString(args ...sql.Expression) (sql.Expression, error) {
    37  	if len(args) < 2 {
    38  		return nil, sql.ErrInvalidArgumentNumber.New("LineString", "2 or more", len(args))
    39  	}
    40  	return &LineString{expression.NaryExpression{ChildExpressions: args}}, nil
    41  }
    42  
    43  // FunctionName implements sql.FunctionExpression
    44  func (l *LineString) FunctionName() string {
    45  	return "linestring"
    46  }
    47  
    48  // Description implements sql.FunctionExpression
    49  func (l *LineString) Description() string {
    50  	return "returns a new linestring."
    51  }
    52  
    53  // Type implements the sql.Expression interface.
    54  func (l *LineString) Type() sql.Type {
    55  	return types.LineStringType{}
    56  }
    57  
    58  // CollationCoercibility implements the interface sql.CollationCoercible.
    59  func (*LineString) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    60  	return sql.Collation_binary, 5
    61  }
    62  
    63  func (l *LineString) String() string {
    64  	var args = make([]string, len(l.ChildExpressions))
    65  	for i, arg := range l.ChildExpressions {
    66  		args[i] = arg.String()
    67  	}
    68  	return fmt.Sprintf("%s(%s)", l.FunctionName(), strings.Join(args, ","))
    69  }
    70  
    71  // WithChildren implements the Expression interface.
    72  func (l *LineString) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    73  	return NewLineString(children...)
    74  }
    75  
    76  // Eval implements the sql.Expression interface.
    77  func (l *LineString) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    78  	// Allocate array of points
    79  	var points = make([]types.Point, len(l.ChildExpressions))
    80  
    81  	// Go through each argument
    82  	for i, arg := range l.ChildExpressions {
    83  		// Evaluate argument
    84  		val, err := arg.Eval(ctx, row)
    85  		if err != nil {
    86  			return nil, err
    87  		}
    88  		// Must be of type point, throw error otherwise
    89  		switch v := val.(type) {
    90  		case types.Point:
    91  			points[i] = v
    92  		case types.GeometryValue:
    93  			return nil, sql.ErrInvalidArgumentDetails.New(l.FunctionName(), v)
    94  		default:
    95  			return nil, sql.ErrIllegalGISValue.New(v)
    96  		}
    97  	}
    98  
    99  	return types.LineString{Points: points}, nil
   100  }