This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathlogarithm.go
185 lines (156 loc) · 4.55 KB
/
logarithm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package function
import (
"fmt"
"math"
"reflect"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
"gopkg.in/src-d/go-errors.v1"
)
// ErrInvalidArgumentForLogarithm is returned when an invalid argument value is passed to a
// logarithm function
var ErrInvalidArgumentForLogarithm = errors.NewKind("invalid argument value for logarithm: %v")
// NewLogBaseFunc returns LogBase creator function with a specific base.
func NewLogBaseFunc(base float64) func(e sql.Expression) sql.Expression {
return func(e sql.Expression) sql.Expression {
return NewLogBase(base, e)
}
}
// LogBase is a function that returns the logarithm of a value with a specific base.
type LogBase struct {
expression.UnaryExpression
base float64
}
// NewLogBase creates a new LogBase expression.
func NewLogBase(base float64, e sql.Expression) sql.Expression {
return &LogBase{UnaryExpression: expression.UnaryExpression{Child: e}, base: base}
}
func (l *LogBase) String() string {
switch l.base {
case float64(math.E):
return fmt.Sprintf("ln(%s)", l.Child)
case float64(10):
return fmt.Sprintf("log10(%s)", l.Child)
case float64(2):
return fmt.Sprintf("log2(%s)", l.Child)
default:
return fmt.Sprintf("log(%v, %s)", l.base, l.Child)
}
}
// WithChildren implements the Expression interface.
func (l *LogBase) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(l, len(children), 1)
}
return NewLogBase(l.base, children[0]), nil
}
// Type returns the resultant type of the function.
func (l *LogBase) Type() sql.Type {
return sql.Float64
}
// IsNullable implements the sql.Expression interface.
func (l *LogBase) IsNullable() bool {
return l.base == float64(1) || l.base <= float64(0) || l.Child.IsNullable()
}
// Eval implements the Expression interface.
func (l *LogBase) Eval(
ctx *sql.Context,
row sql.Row,
) (interface{}, error) {
v, err := l.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
val, err := sql.Float64.Convert(v)
if err != nil {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(v))
}
return computeLog(val.(float64), l.base)
}
// Log is a function that returns the natural logarithm of a value.
type Log struct {
expression.BinaryExpression
}
// NewLog creates a new Log expression.
func NewLog(args ...sql.Expression) (sql.Expression, error) {
argLen := len(args)
if argLen == 0 || argLen > 2 {
return nil, sql.ErrInvalidArgumentNumber.New("LOG", "1 or 2", argLen)
}
if argLen == 1 {
return &Log{expression.BinaryExpression{Left: expression.NewLiteral(math.E, sql.Float64), Right: args[0]}}, nil
} else {
return &Log{expression.BinaryExpression{Left: args[0], Right: args[1]}}, nil
}
}
func (l *Log) String() string {
return fmt.Sprintf("log(%s, %s)", l.Left, l.Right)
}
// WithChildren implements the Expression interface.
func (l *Log) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewLog(children...)
}
// Children implements the Expression interface.
func (l *Log) Children() []sql.Expression {
return []sql.Expression{l.Left, l.Right}
}
// Type returns the resultant type of the function.
func (l *Log) Type() sql.Type {
return sql.Float64
}
// IsNullable implements the Expression interface.
func (l *Log) IsNullable() bool {
return l.Left.IsNullable() || l.Right.IsNullable()
}
// Eval implements the Expression interface.
func (l *Log) Eval(
ctx *sql.Context,
row sql.Row,
) (interface{}, error) {
left, err := l.Left.Eval(ctx, row)
if err != nil {
return nil, err
}
if left == nil {
return nil, nil
}
lhs, err := sql.Float64.Convert(left)
if err != nil {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(left))
}
right, err := l.Right.Eval(ctx, row)
if err != nil {
return nil, err
}
if right == nil {
return nil, nil
}
rhs, err := sql.Float64.Convert(right)
if err != nil {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(right))
}
// rhs becomes value, lhs becomes base
return computeLog(rhs.(float64), lhs.(float64))
}
func computeLog(v float64, base float64) (float64, error) {
if v <= 0 {
return float64(0), ErrInvalidArgumentForLogarithm.New(v)
}
if base == float64(1) || base <= float64(0) {
return float64(0), ErrInvalidArgumentForLogarithm.New(base)
}
switch base {
case float64(2):
return math.Log2(v), nil
case float64(10):
return math.Log10(v), nil
case math.E:
return math.Log(v), nil
default:
// LOG(BASE,V) is equivalent to LOG(V) / LOG(BASE).
return float64(math.Log(v) / math.Log(base)), nil
}
}