|
| 1 | +// Copyright 2025 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/dolthub/vitess/go/mysql" |
| 21 | + "github.com/shopspring/decimal" |
| 22 | + |
| 23 | + "github.com/dolthub/go-mysql-server/sql" |
| 24 | + "github.com/dolthub/go-mysql-server/sql/expression" |
| 25 | + "github.com/dolthub/go-mysql-server/sql/types" |
| 26 | +) |
| 27 | + |
| 28 | +// Truncate truncates a number to a specified number of decimal places. |
| 29 | +// If D is 0, the result has no decimal point or fractional part. |
| 30 | +// D can be negative to cause D digits left of the decimal point of the value X to become zero. |
| 31 | +// If X or D is NULL, the function returns NULL. |
| 32 | +// All numbers are rounded toward zero. |
| 33 | +type Truncate struct { |
| 34 | + expression.BinaryExpressionStub |
| 35 | +} |
| 36 | + |
| 37 | +var _ sql.FunctionExpression = (*Truncate)(nil) |
| 38 | +var _ sql.CollationCoercible = (*Truncate)(nil) |
| 39 | + |
| 40 | +// NewTruncate returns a new Truncate expression. |
| 41 | +func NewTruncate(args ...sql.Expression) (sql.Expression, error) { |
| 42 | + argLen := len(args) |
| 43 | + if argLen != 2 { |
| 44 | + return nil, sql.ErrInvalidArgumentNumber.New("TRUNCATE", "2", argLen) |
| 45 | + } |
| 46 | + |
| 47 | + return &Truncate{expression.BinaryExpressionStub{LeftChild: args[0], RightChild: args[1]}}, nil |
| 48 | +} |
| 49 | + |
| 50 | +// FunctionName implements sql.FunctionExpression |
| 51 | +func (t *Truncate) FunctionName() string { |
| 52 | + return "truncate" |
| 53 | +} |
| 54 | + |
| 55 | +// Description implements sql.FunctionExpression |
| 56 | +func (t *Truncate) Description() string { |
| 57 | + return "truncates the number to decimals decimal places." |
| 58 | +} |
| 59 | + |
| 60 | +// Children implements the Expression interface. |
| 61 | +func (t *Truncate) Children() []sql.Expression { |
| 62 | + return t.BinaryExpressionStub.Children() |
| 63 | +} |
| 64 | + |
| 65 | +// Eval implements the Expression interface. |
| 66 | +func (t *Truncate) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { |
| 67 | + val, err := t.LeftChild.Eval(ctx, row) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + if val == nil { |
| 72 | + return nil, nil |
| 73 | + } |
| 74 | + |
| 75 | + val, _, err = types.InternalDecimalType.Convert(ctx, val) |
| 76 | + if err != nil && sql.ErrTruncatedIncorrect.Is(err) { |
| 77 | + ctx.Warn(mysql.ERTruncatedWrongValue, "%s", err.Error()) |
| 78 | + } |
| 79 | + |
| 80 | + prec, err := t.RightChild.Eval(ctx, row) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + if prec == nil { |
| 85 | + return nil, nil |
| 86 | + } |
| 87 | + prec, _, err = types.Int32.Convert(ctx, prec) |
| 88 | + if err != nil { |
| 89 | + if !sql.ErrTruncatedIncorrect.Is(err) { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + ctx.Warn(mysql.ERTruncatedWrongValue, "%s", err.Error()) |
| 93 | + } |
| 94 | + precision := prec.(int32) |
| 95 | + |
| 96 | + // MySQL cuts off at 30 for larger values |
| 97 | + // TODO: these limits are fine only because we can't handle decimals larger than this |
| 98 | + if precision > types.DecimalTypeMaxPrecision { |
| 99 | + precision = types.DecimalTypeMaxPrecision |
| 100 | + } |
| 101 | + if precision < -types.DecimalTypeMaxScale { |
| 102 | + precision = -types.DecimalTypeMaxScale |
| 103 | + } |
| 104 | + |
| 105 | + var res interface{} |
| 106 | + var tmp decimal.Decimal |
| 107 | + |
| 108 | + if precision < 0 { |
| 109 | + // For negative precision, we need to truncate digits to the left of decimal point |
| 110 | + // This is different from the decimal library's Truncate method |
| 111 | + // We need to divide by 10^|precision|, truncate, then multiply back |
| 112 | + multiplier := decimal.NewFromInt(1) |
| 113 | + for i := int32(0); i < -precision; i++ { |
| 114 | + multiplier = multiplier.Mul(decimal.NewFromInt(10)) |
| 115 | + } |
| 116 | + tmp = val.(decimal.Decimal).Div(multiplier).Truncate(0).Mul(multiplier) |
| 117 | + } else { |
| 118 | + // For positive precision, use the standard Truncate method |
| 119 | + tmp = val.(decimal.Decimal).Truncate(precision) |
| 120 | + } |
| 121 | + |
| 122 | + lType := t.LeftChild.Type() |
| 123 | + if types.IsSigned(lType) { |
| 124 | + res, _, err = types.Int64.Convert(ctx, tmp) |
| 125 | + } else if types.IsUnsigned(lType) { |
| 126 | + res, _, err = types.Uint64.Convert(ctx, tmp) |
| 127 | + } else if types.IsFloat(lType) { |
| 128 | + res, _, err = types.Float64.Convert(ctx, tmp) |
| 129 | + } else if types.IsDecimal(lType) { |
| 130 | + res = tmp |
| 131 | + } else if types.IsTextBlob(lType) { |
| 132 | + res, _, err = types.Float64.Convert(ctx, tmp) |
| 133 | + } else { |
| 134 | + // Default to Float64 for unknown types |
| 135 | + res, _, err = types.Float64.Convert(ctx, tmp) |
| 136 | + } |
| 137 | + if err != nil && sql.ErrTruncatedIncorrect.Is(err) { |
| 138 | + ctx.Warn(mysql.ERTruncatedWrongValue, "%s", err.Error()) |
| 139 | + err = nil |
| 140 | + } |
| 141 | + return res, err |
| 142 | +} |
| 143 | + |
| 144 | +// IsNullable implements the Expression interface. |
| 145 | +func (t *Truncate) IsNullable() bool { |
| 146 | + return t.LeftChild.IsNullable() || t.RightChild.IsNullable() |
| 147 | +} |
| 148 | + |
| 149 | +func (t *Truncate) String() string { |
| 150 | + return fmt.Sprintf("%s(%s,%s)", t.FunctionName(), t.LeftChild.String(), t.RightChild.String()) |
| 151 | +} |
| 152 | + |
| 153 | +// Resolved implements the Expression interface. |
| 154 | +func (t *Truncate) Resolved() bool { |
| 155 | + return t.LeftChild.Resolved() && t.RightChild.Resolved() |
| 156 | +} |
| 157 | + |
| 158 | +// Type implements the Expression interface. |
| 159 | +func (t *Truncate) Type() sql.Type { |
| 160 | + return numericRetType(t.LeftChild.Type()) |
| 161 | +} |
| 162 | + |
| 163 | +// CollationCoercibility implements the interface sql.CollationCoercible. |
| 164 | +func (*Truncate) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { |
| 165 | + return sql.Collation_binary, 5 |
| 166 | +} |
| 167 | + |
| 168 | +// WithChildren implements the Expression interface. |
| 169 | +func (t *Truncate) WithChildren(children ...sql.Expression) (sql.Expression, error) { |
| 170 | + return NewTruncate(children...) |
| 171 | +} |
0 commit comments