-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval.go
executable file
·299 lines (257 loc) · 7.39 KB
/
interval.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// 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 expression
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
errors "gopkg.in/src-d/go-errors.v1"
"github.com/Rock-liyi/p2pdb-store/sql"
)
// Interval defines a time duration.
type Interval struct {
UnaryExpression
Unit string
}
// NewInterval creates a new interval expression.
func NewInterval(child sql.Expression, unit string) *Interval {
return &Interval{UnaryExpression{Child: child}, strings.ToUpper(unit)}
}
// Type implements the sql.Expression interface.
func (i *Interval) Type() sql.Type { return sql.Uint64 }
// IsNullable implements the sql.Expression interface.
func (i *Interval) IsNullable() bool { return i.Child.IsNullable() }
// Eval implements the sql.Expression interface.
func (i *Interval) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
panic("Interval.Eval is just a placeholder method and should not be called directly")
}
var (
errInvalidIntervalUnit = errors.NewKind("invalid interval unit: %s")
errInvalidIntervalFormat = errors.NewKind("invalid interval format for %q: %s")
)
// EvalDelta evaluates the expression returning a TimeDelta. This method should
// be used instead of Eval, as this expression returns a TimeDelta, which is not
// a valid value that can be returned in Eval.
func (i *Interval) EvalDelta(ctx *sql.Context, row sql.Row) (*TimeDelta, error) {
val, err := i.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}
var td TimeDelta
if r, ok := unitTextFormats[i.Unit]; ok {
val, err = sql.LongText.Convert(val)
if err != nil {
return nil, err
}
text := val.(string)
if !r.MatchString(text) {
return nil, errInvalidIntervalFormat.New(i.Unit, text)
}
parts := textFormatParts(text, r)
switch i.Unit {
case "DAY_HOUR":
td.Days = parts[0]
td.Hours = parts[1]
case "DAY_MICROSECOND":
td.Days = parts[0]
td.Hours = parts[1]
td.Minutes = parts[2]
td.Seconds = parts[3]
td.Microseconds = parts[4]
case "DAY_MINUTE":
td.Days = parts[0]
td.Hours = parts[1]
td.Minutes = parts[2]
case "DAY_SECOND":
td.Days = parts[0]
td.Hours = parts[1]
td.Minutes = parts[2]
td.Seconds = parts[3]
case "HOUR_MICROSECOND":
td.Hours = parts[0]
td.Minutes = parts[1]
td.Seconds = parts[2]
td.Microseconds = parts[3]
case "HOUR_SECOND":
td.Hours = parts[0]
td.Minutes = parts[1]
td.Seconds = parts[2]
case "HOUR_MINUTE":
td.Hours = parts[0]
td.Minutes = parts[1]
case "MINUTE_MICROSECOND":
td.Minutes = parts[0]
td.Seconds = parts[1]
td.Microseconds = parts[2]
case "MINUTE_SECOND":
td.Minutes = parts[0]
td.Seconds = parts[1]
case "SECOND_MICROSECOND":
td.Seconds = parts[0]
td.Microseconds = parts[1]
case "YEAR_MONTH":
td.Years = parts[0]
td.Months = parts[1]
default:
return nil, errInvalidIntervalUnit.New(i.Unit)
}
} else {
val, err = sql.Int64.Convert(val)
if err != nil {
return nil, err
}
num := val.(int64)
switch i.Unit {
case "DAY":
td.Days = num
case "HOUR":
td.Hours = num
case "MINUTE":
td.Minutes = num
case "SECOND":
td.Seconds = num
case "MICROSECOND":
td.Microseconds = num
case "QUARTER":
td.Months = num * 3
case "MONTH":
td.Months = num
case "WEEK":
td.Days = num * 7
case "YEAR":
td.Years = num
default:
return nil, errInvalidIntervalUnit.New(i.Unit)
}
}
return &td, nil
}
// WithChildren implements the Expression interface.
func (i *Interval) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(i, len(children), 1)
}
return NewInterval(children[0], i.Unit), nil
}
func (i *Interval) String() string {
return fmt.Sprintf("INTERVAL %s %s", i.Child, i.Unit)
}
var unitTextFormats = map[string]*regexp.Regexp{
"DAY_HOUR": regexp.MustCompile(`^(\d+)\s+(\d+)$`),
"DAY_MICROSECOND": regexp.MustCompile(`^(\d+)\s+(\d+):(\d+):(\d+).(\d+)$`),
"DAY_MINUTE": regexp.MustCompile(`^(\d+)\s+(\d+):(\d+)$`),
"DAY_SECOND": regexp.MustCompile(`^(\d+)\s+(\d+):(\d+):(\d+)$`),
"HOUR_MICROSECOND": regexp.MustCompile(`^(\d+):(\d+):(\d+).(\d+)$`),
"HOUR_SECOND": regexp.MustCompile(`^(\d+):(\d+):(\d+)$`),
"HOUR_MINUTE": regexp.MustCompile(`^(\d+):(\d+)$`),
"MINUTE_MICROSECOND": regexp.MustCompile(`^(\d+):(\d+).(\d+)$`),
"MINUTE_SECOND": regexp.MustCompile(`^(\d+):(\d+)$`),
"SECOND_MICROSECOND": regexp.MustCompile(`^(\d+).(\d+)$`),
"YEAR_MONTH": regexp.MustCompile(`^(\d+)-(\d+)$`),
}
func textFormatParts(text string, r *regexp.Regexp) []int64 {
parts := r.FindStringSubmatch(text)
var result []int64
for _, p := range parts[1:] {
// It is safe to igore the error here, because at this point we know
// the string matches the regexp, and that means it can't be an
// invalid number.
n, _ := strconv.ParseInt(p, 10, 64)
result = append(result, n)
}
return result
}
// TimeDelta is the difference between a time and another time.
type TimeDelta struct {
Years int64
Months int64
Days int64
Hours int64
Minutes int64
Seconds int64
Microseconds int64
}
// Add returns the given time plus the time delta.
func (td TimeDelta) Add(t time.Time) time.Time {
return td.apply(t, 1)
}
// Sub returns the given time minus the time delta.
func (td TimeDelta) Sub(t time.Time) time.Time {
return td.apply(t, -1)
}
const (
day = 24 * time.Hour
week = 7 * day
)
func (td TimeDelta) apply(t time.Time, sign int64) time.Time {
y := int64(t.Year())
mo := int64(t.Month())
d := t.Day()
h := t.Hour()
min := t.Minute()
s := t.Second()
ns := t.Nanosecond()
if td.Years != 0 {
y += td.Years * sign
}
if td.Months != 0 {
m := mo + td.Months*sign
if m < 1 {
mo = 12 + (m % 12)
y += m/12 - 1
} else if m > 12 {
mo = m % 12
y += m / 12
} else {
mo = m
}
// Due to the operations done before, month may be zero, which means it's
// december.
if mo == 0 {
mo = 12
}
}
if days := daysInMonth(time.Month(mo), int(y)); days < d {
d = days
}
date := time.Date(int(y), time.Month(mo), d, h, min, s, ns, t.Location())
if td.Days != 0 {
date = date.Add(time.Duration(td.Days) * day * time.Duration(sign))
}
if td.Hours != 0 {
date = date.Add(time.Duration(td.Hours) * time.Hour * time.Duration(sign))
}
if td.Minutes != 0 {
date = date.Add(time.Duration(td.Minutes) * time.Minute * time.Duration(sign))
}
if td.Seconds != 0 {
date = date.Add(time.Duration(td.Seconds) * time.Second * time.Duration(sign))
}
if td.Microseconds != 0 {
date = date.Add(time.Duration(td.Microseconds) * time.Microsecond * time.Duration(sign))
}
return date
}
func daysInMonth(month time.Month, year int) int {
if month == time.December {
return 31
}
date := time.Date(year, month+time.Month(1), 1, 0, 0, 0, 0, time.Local)
return date.Add(-1 * day).Day()
}