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

     1  // Copyright 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 function
    16  
    17  import (
    18  	"fmt"
    19  
    20  	gmstime "github.com/dolthub/go-mysql-server/internal/time"
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  	"github.com/dolthub/go-mysql-server/sql/types"
    23  )
    24  
    25  type ConvertTz struct {
    26  	dt     sql.Expression
    27  	fromTz sql.Expression
    28  	toTz   sql.Expression
    29  }
    30  
    31  var _ sql.FunctionExpression = (*ConvertTz)(nil)
    32  var _ sql.CollationCoercible = (*ConvertTz)(nil)
    33  
    34  // NewConvertTz returns an implementation of the CONVERT_TZ() function.
    35  func NewConvertTz(dt, fromTz, toTz sql.Expression) sql.Expression {
    36  	return &ConvertTz{
    37  		dt:     dt,
    38  		fromTz: fromTz,
    39  		toTz:   toTz,
    40  	}
    41  }
    42  
    43  // FunctionName implements sql.FunctionExpression
    44  func (c *ConvertTz) FunctionName() string {
    45  	return "convert_tz"
    46  }
    47  
    48  // Description implements the sql.FunctionExpression interface.
    49  func (c *ConvertTz) Description() string {
    50  	return "converts a datetime value dt from the time zone given by from_tz to the time zone given by to_tz and returns the resulting value."
    51  }
    52  
    53  // Resolved implements the sql.Expression interface.
    54  func (c *ConvertTz) Resolved() bool {
    55  	return c.dt.Resolved() && c.fromTz.Resolved() && c.toTz.Resolved()
    56  }
    57  
    58  // String implements the sql.Expression interface.
    59  func (c *ConvertTz) String() string {
    60  	return fmt.Sprintf("%s(%s,%s,%s)", c.FunctionName(), c.dt, c.fromTz, c.toTz)
    61  }
    62  
    63  // Type implements the sql.Expression interface.
    64  func (c *ConvertTz) Type() sql.Type {
    65  	return types.DatetimeMaxPrecision
    66  }
    67  
    68  // CollationCoercibility implements the interface sql.CollationCoercible.
    69  func (*ConvertTz) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    70  	return sql.Collation_binary, 5
    71  }
    72  
    73  // IsNullable implements the sql.Expression interface.
    74  func (c *ConvertTz) IsNullable() bool {
    75  	return true
    76  }
    77  
    78  // Eval implements the sql.Expression interface.
    79  func (c *ConvertTz) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    80  	from, err := c.fromTz.Eval(ctx, row)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	to, err := c.toTz.Eval(ctx, row)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	dt, err := c.dt.Eval(ctx, row)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	// If either the date, or the timezones/offsets are not correct types we return NULL.
    96  	datetime, err := types.DatetimeMaxPrecision.ConvertWithoutRangeCheck(dt)
    97  	if err != nil {
    98  		return nil, nil
    99  	}
   100  
   101  	fromStr, ok := from.(string)
   102  	if !ok {
   103  		return nil, nil
   104  	}
   105  
   106  	if fromStr == "SYSTEM" {
   107  		fromStr = gmstime.SystemTimezoneOffset()
   108  	}
   109  
   110  	toStr, ok := to.(string)
   111  	if !ok {
   112  		return nil, nil
   113  	}
   114  
   115  	if toStr == "SYSTEM" {
   116  		toStr = gmstime.SystemTimezoneOffset()
   117  	}
   118  
   119  	converted, success := gmstime.ConvertTimeZone(datetime, fromStr, toStr)
   120  	if !success {
   121  		return nil, nil
   122  	}
   123  
   124  	return types.DatetimeMaxPrecision.ConvertWithoutRangeCheck(converted)
   125  }
   126  
   127  // Children implements the sql.Expression interface.
   128  func (c *ConvertTz) Children() []sql.Expression {
   129  	return []sql.Expression{c.dt, c.fromTz, c.toTz}
   130  }
   131  
   132  // WithChildren implements the sql.Expression interface.
   133  func (c *ConvertTz) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   134  	if len(children) != 3 {
   135  		return nil, sql.ErrInvalidChildrenNumber.New(c, len(children), 3)
   136  	}
   137  
   138  	return NewConvertTz(children[0], children[1], children[2]), nil
   139  }