github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/spatial/point.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/types"
    22  )
    23  
    24  // Point is a function that returns a point type containing values Y and Y.
    25  type Point struct {
    26  	X sql.Expression
    27  	Y sql.Expression
    28  }
    29  
    30  var _ sql.FunctionExpression = (*Point)(nil)
    31  var _ sql.CollationCoercible = (*Point)(nil)
    32  
    33  // NewPoint creates a new point expression.
    34  func NewPoint(e1, e2 sql.Expression) sql.Expression {
    35  	return &Point{e1, e2}
    36  }
    37  
    38  // FunctionName implements sql.FunctionExpression
    39  func (p *Point) FunctionName() string {
    40  	return "point"
    41  }
    42  
    43  // Description implements sql.FunctionExpression
    44  func (p *Point) Description() string {
    45  	return "returns a new point."
    46  }
    47  
    48  // Children implements the sql.Expression interface.
    49  func (p *Point) Children() []sql.Expression {
    50  	return []sql.Expression{p.X, p.Y}
    51  }
    52  
    53  // Resolved implements the sql.Expression interface.
    54  func (p *Point) Resolved() bool {
    55  	return p.X.Resolved() && p.Y.Resolved()
    56  }
    57  
    58  // IsNullable implements the sql.Expression interface.
    59  func (p *Point) IsNullable() bool {
    60  	return p.X.IsNullable() || p.Y.IsNullable()
    61  }
    62  
    63  // Type implements the sql.Expression interface.
    64  func (p *Point) Type() sql.Type {
    65  	return types.PointType{}
    66  }
    67  
    68  // CollationCoercibility implements the interface sql.CollationCoercible.
    69  func (*Point) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    70  	return sql.Collation_binary, 5
    71  }
    72  
    73  func (p *Point) String() string {
    74  	return fmt.Sprintf("%s(%s,%s)", p.FunctionName(), p.X.String(), p.Y.String())
    75  }
    76  
    77  // WithChildren implements the Expression interface.
    78  func (p *Point) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    79  	if len(children) != 2 {
    80  		return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 2)
    81  	}
    82  	return NewPoint(children[0], children[1]), nil
    83  }
    84  
    85  // Eval implements the sql.Expression interface.
    86  func (p *Point) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    87  	// Evaluate X
    88  	x, err := p.X.Eval(ctx, row)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	if x == nil {
    93  		return nil, nil
    94  	}
    95  
    96  	// Convert to float64
    97  	_x, _, err := types.Float64.Convert(x)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	// Evaluate Y
   103  	y, err := p.Y.Eval(ctx, row)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  	if y == nil {
   108  		return nil, nil
   109  	}
   110  
   111  	// Convert to float64
   112  	_y, _, err := types.Float64.Convert(y)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	return types.Point{X: _x.(float64), Y: _y.(float64)}, nil
   118  }