-
Notifications
You must be signed in to change notification settings - Fork 0
/
logarithm.go
executable file
·242 lines (205 loc) · 6.49 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package function
import (
"fmt"
"math"
"reflect"
"gopkg.in/src-d/go-errors.v1"
"github.com/Rock-liyi/p2pdb-store/sql"
"github.com/Rock-liyi/p2pdb-store/sql/expression"
)
// 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
}
var _ sql.FunctionExpression = (*LogBase)(nil)
// NewLogBase creates a new LogBase expression.
func NewLogBase(base float64, e sql.Expression) sql.Expression {
return &LogBase{UnaryExpression: expression.UnaryExpression{Child: e}, base: base}
}
// FunctionName implements sql.FunctionExpression
func (l *LogBase) FunctionName() string {
switch l.base {
case float64(math.E):
return "ln"
case float64(10):
return "log10"
case float64(2):
return "log2"
default:
return "log"
}
}
// Description implements sql.FunctionExpression
func (l *LogBase) Description() string {
switch l.base {
case float64(math.E):
return "returns the natural logarithm of X."
case float64(10):
return "returns the base-10 logarithm of X."
case float64(2):
return "returns the base-2 logarithm of X."
default:
return "if called with one parameter, this function returns the natural logarithm of X. If called with two parameters, this function returns the logarithm of X to the base B. If X is less than or equal to 0, or if B is less than or equal to 1, then NULL is returned."
}
}
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
}
var _ sql.FunctionExpression = (*Log)(nil)
// 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
}
}
// FunctionName implements sql.FunctionExpression
func (l *Log) FunctionName() string {
return "log"
}
// Description implements sql.FunctionExpression
func (l *Log) Description() string {
return "if called with one parameter, this function returns the natural logarithm of X. If called with two parameters, this function returns the logarithm of X to the base B. If X is less than or equal to 0, or if B is less than or equal to 1, then NULL is returned."
}
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
}
}