github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/absval.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 function 16 17 import ( 18 "fmt" 19 20 "github.com/shopspring/decimal" 21 22 "github.com/dolthub/go-mysql-server/sql" 23 "github.com/dolthub/go-mysql-server/sql/expression" 24 ) 25 26 // AbsVal is a function that takes the absolute value of a number 27 type AbsVal struct { 28 expression.UnaryExpression 29 } 30 31 var _ sql.FunctionExpression = (*AbsVal)(nil) 32 var _ sql.CollationCoercible = (*AbsVal)(nil) 33 34 // NewAbsVal creates a new AbsVal expression. 35 func NewAbsVal(e sql.Expression) sql.Expression { 36 return &AbsVal{expression.UnaryExpression{Child: e}} 37 } 38 39 // FunctionName implements sql.FunctionExpression 40 func (t *AbsVal) FunctionName() string { 41 return "abs" 42 } 43 44 // Description implements sql.FunctionExpression 45 func (t *AbsVal) Description() string { 46 return "returns the absolute value of an expression." 47 } 48 49 // Eval implements the Expression interface. 50 func (t *AbsVal) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 51 val, err := t.Child.Eval(ctx, row) 52 53 if err != nil { 54 return nil, err 55 } 56 57 if val == nil { 58 return nil, nil 59 } 60 61 // Fucking Golang 62 switch x := val.(type) { 63 case uint, uint64, uint32, uint16, uint8: 64 return x, nil 65 case int: 66 if x < 0 { 67 return -x, nil 68 } else { 69 return x, nil 70 } 71 case int64: 72 if x < 0 { 73 return -x, nil 74 } else { 75 return x, nil 76 } 77 case int32: 78 if x < 0 { 79 return -x, nil 80 } else { 81 return x, nil 82 } 83 case int16: 84 if x < 0 { 85 return -x, nil 86 } else { 87 return x, nil 88 } 89 case int8: 90 if x < 0 { 91 return -x, nil 92 } else { 93 return x, nil 94 } 95 case float64: 96 if x < 0 { 97 return -x, nil 98 } else { 99 return x, nil 100 } 101 case float32: 102 if x < 0 { 103 return -x, nil 104 } else { 105 return x, nil 106 } 107 case decimal.Decimal: 108 return x.Abs(), nil 109 } 110 111 return nil, nil 112 } 113 114 // String implements the fmt.Stringer interface. 115 func (t *AbsVal) String() string { 116 return fmt.Sprintf("%s(%s)", t.FunctionName(), t.Child.String()) 117 } 118 119 // IsNullable implements the Expression interface. 120 func (t *AbsVal) IsNullable() bool { 121 return t.Child.IsNullable() 122 } 123 124 // WithChildren implements the Expression interface. 125 func (t *AbsVal) WithChildren(children ...sql.Expression) (sql.Expression, error) { 126 if len(children) != 1 { 127 return nil, sql.ErrInvalidChildrenNumber.New(t, len(children), 1) 128 } 129 return NewAbsVal(children[0]), nil 130 } 131 132 // Type implements the Expression interface. 133 func (t *AbsVal) Type() sql.Type { 134 return t.Child.Type() 135 } 136 137 // CollationCoercibility implements the interface sql.CollationCoercible. 138 func (*AbsVal) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 139 return sql.Collation_binary, 5 140 }