|
| 1 | +// Copyright 2024 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 functions |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/dolthub/doltgresql/utils" |
| 19 | +) |
| 20 | + |
| 21 | +// abs represents the PostgreSQL function of the same name. |
| 22 | +var abs = Function{ |
| 23 | + Name: "abs", |
| 24 | + Overloads: []interface{}{abs_int, abs_float, abs_numeric}, |
| 25 | +} |
| 26 | + |
| 27 | +// abs_int is one of the overloads of abs. |
| 28 | +func abs_int(num IntegerType) (IntegerType, error) { |
| 29 | + if num.IsNull { |
| 30 | + return IntegerType{IsNull: true}, nil |
| 31 | + } |
| 32 | + return IntegerType{Value: utils.Abs(num.Value)}, nil |
| 33 | +} |
| 34 | + |
| 35 | +// abs_float is one of the overloads of abs. |
| 36 | +func abs_float(num FloatType) (FloatType, error) { |
| 37 | + if num.IsNull { |
| 38 | + return FloatType{IsNull: true}, nil |
| 39 | + } |
| 40 | + return FloatType{Value: utils.Abs(num.Value)}, nil |
| 41 | +} |
| 42 | + |
| 43 | +// abs_numeric is one of the overloads of abs. |
| 44 | +func abs_numeric(num NumericType) (NumericType, error) { |
| 45 | + if num.IsNull { |
| 46 | + return NumericType{IsNull: true}, nil |
| 47 | + } |
| 48 | + return NumericType{Value: utils.Abs(num.Value)}, nil |
| 49 | +} |
0 commit comments