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 pathtime.go
580 lines (471 loc) · 16.4 KB
/
time.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package function
import (
"errors"
"fmt"
"time"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
)
func getDate(ctx *sql.Context,
u expression.UnaryExpression,
row sql.Row) (interface{}, error) {
val, err := u.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}
date, err := sql.Timestamp.Convert(val)
if err != nil {
date, err = sql.Date.Convert(val)
if err != nil {
date = nil
}
}
return date, nil
}
func getDatePart(ctx *sql.Context,
u expression.UnaryExpression,
row sql.Row,
f func(interface{}) interface{}) (interface{}, error) {
date, err := getDate(ctx, u, row)
if err != nil {
return nil, err
}
return f(date), nil
}
// Year is a function that returns the year of a date.
type Year struct {
expression.UnaryExpression
}
// NewYear creates a new Year UDF.
func NewYear(date sql.Expression) sql.Expression {
return &Year{expression.UnaryExpression{Child: date}}
}
func (y *Year) String() string { return fmt.Sprintf("YEAR(%s)", y.Child) }
// Type implements the Expression interface.
func (y *Year) Type() sql.Type { return sql.Int32 }
// Eval implements the Expression interface.
func (y *Year) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return getDatePart(ctx, y.UnaryExpression, row, year)
}
// WithChildren implements the Expression interface.
func (y *Year) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(y, len(children), 1)
}
return NewYear(children[0]), nil
}
// Month is a function that returns the month of a date.
type Month struct {
expression.UnaryExpression
}
// NewMonth creates a new Month UDF.
func NewMonth(date sql.Expression) sql.Expression {
return &Month{expression.UnaryExpression{Child: date}}
}
func (m *Month) String() string { return fmt.Sprintf("MONTH(%s)", m.Child) }
// Type implements the Expression interface.
func (m *Month) Type() sql.Type { return sql.Int32 }
// Eval implements the Expression interface.
func (m *Month) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return getDatePart(ctx, m.UnaryExpression, row, month)
}
// WithChildren implements the Expression interface.
func (m *Month) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(m, len(children), 1)
}
return NewMonth(children[0]), nil
}
// Day is a function that returns the day of a date.
type Day struct {
expression.UnaryExpression
}
// NewDay creates a new Day UDF.
func NewDay(date sql.Expression) sql.Expression {
return &Day{expression.UnaryExpression{Child: date}}
}
func (d *Day) String() string { return fmt.Sprintf("DAY(%s)", d.Child) }
// Type implements the Expression interface.
func (d *Day) Type() sql.Type { return sql.Int32 }
// Eval implements the Expression interface.
func (d *Day) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return getDatePart(ctx, d.UnaryExpression, row, day)
}
// WithChildren implements the Expression interface.
func (d *Day) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(d, len(children), 1)
}
return NewDay(children[0]), nil
}
// Weekday is a function that returns the weekday of a date where 0 = Monday,
// ..., 6 = Sunday.
type Weekday struct {
expression.UnaryExpression
}
// NewWeekday creates a new Weekday UDF.
func NewWeekday(date sql.Expression) sql.Expression {
return &Weekday{expression.UnaryExpression{Child: date}}
}
func (d *Weekday) String() string { return fmt.Sprintf("WEEKDAY(%s)", d.Child) }
// Type implements the Expression interface.
func (d *Weekday) Type() sql.Type { return sql.Int32 }
// Eval implements the Expression interface.
func (d *Weekday) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return getDatePart(ctx, d.UnaryExpression, row, weekday)
}
// WithChildren implements the Expression interface.
func (d *Weekday) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(d, len(children), 1)
}
return NewWeekday(children[0]), nil
}
// Hour is a function that returns the hour of a date.
type Hour struct {
expression.UnaryExpression
}
// NewHour creates a new Hour UDF.
func NewHour(date sql.Expression) sql.Expression {
return &Hour{expression.UnaryExpression{Child: date}}
}
func (h *Hour) String() string { return fmt.Sprintf("HOUR(%s)", h.Child) }
// Type implements the Expression interface.
func (h *Hour) Type() sql.Type { return sql.Int32 }
// Eval implements the Expression interface.
func (h *Hour) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return getDatePart(ctx, h.UnaryExpression, row, hour)
}
// WithChildren implements the Expression interface.
func (h *Hour) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(h, len(children), 1)
}
return NewHour(children[0]), nil
}
// Minute is a function that returns the minute of a date.
type Minute struct {
expression.UnaryExpression
}
// NewMinute creates a new Minute UDF.
func NewMinute(date sql.Expression) sql.Expression {
return &Minute{expression.UnaryExpression{Child: date}}
}
func (m *Minute) String() string { return fmt.Sprintf("MINUTE(%d)", m.Child) }
// Type implements the Expression interface.
func (m *Minute) Type() sql.Type { return sql.Int32 }
// Eval implements the Expression interface.
func (m *Minute) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return getDatePart(ctx, m.UnaryExpression, row, minute)
}
// WithChildren implements the Expression interface.
func (m *Minute) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(m, len(children), 1)
}
return NewMinute(children[0]), nil
}
// Second is a function that returns the second of a date.
type Second struct {
expression.UnaryExpression
}
// NewSecond creates a new Second UDF.
func NewSecond(date sql.Expression) sql.Expression {
return &Second{expression.UnaryExpression{Child: date}}
}
func (s *Second) String() string { return fmt.Sprintf("SECOND(%s)", s.Child) }
// Type implements the Expression interface.
func (s *Second) Type() sql.Type { return sql.Int32 }
// Eval implements the Expression interface.
func (s *Second) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return getDatePart(ctx, s.UnaryExpression, row, second)
}
// WithChildren implements the Expression interface.
func (s *Second) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 1)
}
return NewSecond(children[0]), nil
}
// DayOfWeek is a function that returns the day of the week from a date where
// 1 = Sunday, ..., 7 = Saturday.
type DayOfWeek struct {
expression.UnaryExpression
}
// NewDayOfWeek creates a new DayOfWeek UDF.
func NewDayOfWeek(date sql.Expression) sql.Expression {
return &DayOfWeek{expression.UnaryExpression{Child: date}}
}
func (d *DayOfWeek) String() string { return fmt.Sprintf("DAYOFWEEK(%s)", d.Child) }
// Type implements the Expression interface.
func (d *DayOfWeek) Type() sql.Type { return sql.Int32 }
// Eval implements the Expression interface.
func (d *DayOfWeek) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return getDatePart(ctx, d.UnaryExpression, row, dayOfWeek)
}
// WithChildren implements the Expression interface.
func (d *DayOfWeek) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(d, len(children), 1)
}
return NewDayOfWeek(children[0]), nil
}
// DayOfYear is a function that returns the day of the year from a date.
type DayOfYear struct {
expression.UnaryExpression
}
// NewDayOfYear creates a new DayOfYear UDF.
func NewDayOfYear(date sql.Expression) sql.Expression {
return &DayOfYear{expression.UnaryExpression{Child: date}}
}
func (d *DayOfYear) String() string { return fmt.Sprintf("DAYOFYEAR(%s)", d.Child) }
// Type implements the Expression interface.
func (d *DayOfYear) Type() sql.Type { return sql.Int32 }
// Eval implements the Expression interface.
func (d *DayOfYear) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return getDatePart(ctx, d.UnaryExpression, row, dayOfYear)
}
// WithChildren implements the Expression interface.
func (d *DayOfYear) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(d, len(children), 1)
}
return NewDayOfYear(children[0]), nil
}
func datePartFunc(fn func(time.Time) int) func(interface{}) interface{} {
return func(v interface{}) interface{} {
if v == nil {
return nil
}
return int32(fn(v.(time.Time)))
}
}
// YearWeek is a function that returns year and week for a date.
// The year in the result may be different from the year in the date argument for the first and the last week of the year.
// Details: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_yearweek
type YearWeek struct {
date sql.Expression
mode sql.Expression
}
// NewYearWeek creates a new YearWeek UDF
func NewYearWeek(args ...sql.Expression) (sql.Expression, error) {
if len(args) == 0 {
return nil, sql.ErrInvalidArgumentNumber.New("YEARWEEK", "1 or more", 0)
}
yw := &YearWeek{date: args[0]}
if len(args) > 1 && args[1].Resolved() && sql.IsInteger(args[1].Type()) {
yw.mode = args[1]
} else {
yw.mode = expression.NewLiteral(0, sql.Int64)
}
return yw, nil
}
func (d *YearWeek) String() string { return fmt.Sprintf("YEARWEEK(%s, %d)", d.date, d.mode) }
// Type implements the Expression interface.
func (d *YearWeek) Type() sql.Type { return sql.Int32 }
// Eval implements the Expression interface.
func (d *YearWeek) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
date, err := getDate(ctx, expression.UnaryExpression{Child: d.date}, row)
if err != nil {
return nil, err
}
yyyy, ok := year(date).(int32)
if !ok {
return nil, errors.New("YEARWEEK: invalid year")
}
mm, ok := month(date).(int32)
if !ok {
return nil, errors.New("YEARWEEK: invalid month")
}
dd, ok := day(date).(int32)
if !ok {
return nil, errors.New("YEARWEEK: invalid day")
}
mode := int64(0)
val, err := d.mode.Eval(ctx, row)
if err != nil {
return nil, err
}
if val != nil {
if i64, err := sql.Int64.Convert(val); err == nil {
if mode, ok = i64.(int64); ok {
mode %= 8 // mode in [0, 7]
}
}
}
yyyy, week := calcWeek(yyyy, mm, dd, weekMode(mode)|weekBehaviourYear)
return (yyyy * 100) + week, nil
}
// Resolved implements the Expression interface.
func (d *YearWeek) Resolved() bool {
return d.date.Resolved() && d.mode.Resolved()
}
// Children implements the Expression interface.
func (d *YearWeek) Children() []sql.Expression { return []sql.Expression{d.date, d.mode} }
// IsNullable implements the Expression interface.
func (d *YearWeek) IsNullable() bool {
return d.date.IsNullable()
}
// WithChildren implements the Expression interface.
func (*YearWeek) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewYearWeek(children...)
}
// Following solution of YearWeek was taken from tidb: https://github.com/pingcap/tidb/blob/master/types/mytime.go
type weekBehaviour int64
const (
// weekBehaviourMondayFirst set Monday as first day of week; otherwise Sunday is first day of week
weekBehaviourMondayFirst weekBehaviour = 1 << iota
// If set, Week is in range 1-53, otherwise Week is in range 0-53.
// Note that this flag is only relevant if WEEK_JANUARY is not set.
weekBehaviourYear
// If not set, Weeks are numbered according to ISO 8601:1988.
// If set, the week that contains the first 'first-day-of-week' is week 1.
weekBehaviourFirstWeekday
)
func (v weekBehaviour) test(flag weekBehaviour) bool {
return (v & flag) != 0
}
func weekMode(mode int64) weekBehaviour {
weekFormat := weekBehaviour(mode & 7)
if (weekFormat & weekBehaviourMondayFirst) == 0 {
weekFormat ^= weekBehaviourFirstWeekday
}
return weekFormat
}
// calcWeekday calculates weekday from daynr, returns 0 for Monday, 1 for Tuesday ...
func calcWeekday(daynr int32, sundayFirstDayOfWeek bool) int32 {
daynr += 5
if sundayFirstDayOfWeek {
daynr++
}
return daynr % 7
}
// calcWeek calculates week and year for the time.
func calcWeek(yyyy, mm, dd int32, wb weekBehaviour) (int32, int32) {
daynr := calcDaynr(yyyy, mm, dd)
firstDaynr := calcDaynr(yyyy, 1, 1)
mondayFirst := wb.test(weekBehaviourMondayFirst)
weekYear := wb.test(weekBehaviourYear)
firstWeekday := wb.test(weekBehaviourFirstWeekday)
weekday := calcWeekday(firstDaynr, !mondayFirst)
week, days := int32(0), int32(0)
if mm == 1 && dd <= 7-weekday {
if !weekYear &&
((firstWeekday && weekday != 0) || (!firstWeekday && weekday >= 4)) {
return yyyy, week
}
weekYear = true
yyyy--
days = calcDaysInYear(yyyy)
firstDaynr -= days
weekday = (weekday + 53*7 - days) % 7
}
if (firstWeekday && weekday != 0) ||
(!firstWeekday && weekday >= 4) {
days = daynr - (firstDaynr + 7 - weekday)
} else {
days = daynr - (firstDaynr - weekday)
}
if weekYear && days >= 52*7 {
weekday = (weekday + calcDaysInYear(yyyy)) % 7
if (!firstWeekday && weekday < 4) ||
(firstWeekday && weekday == 0) {
yyyy++
week = 1
return yyyy, week
}
}
week = days/7 + 1
return yyyy, week
}
// calcDaysInYear calculates days in one year, it works with 0 <= yyyy <= 99.
func calcDaysInYear(yyyy int32) int32 {
if (yyyy&3) == 0 && (yyyy%100 != 0 || (yyyy%400 == 0 && (yyyy != 0))) {
return 366
}
return 365
}
// calcDaynr calculates days since 0000-00-00.
func calcDaynr(yyyy, mm, dd int32) int32 {
if yyyy == 0 && mm == 0 {
return 0
}
delsum := 365*yyyy + 31*(mm-1) + dd
if mm <= 2 {
yyyy--
} else {
delsum -= (mm*4 + 23) / 10
}
return delsum + yyyy/4 - ((yyyy/100+1)*3)/4
}
var (
year = datePartFunc((time.Time).Year)
month = datePartFunc(func(t time.Time) int { return int(t.Month()) })
day = datePartFunc((time.Time).Day)
weekday = datePartFunc(func(t time.Time) int { return (int(t.Weekday()) + 6) % 7 })
hour = datePartFunc((time.Time).Hour)
minute = datePartFunc((time.Time).Minute)
second = datePartFunc((time.Time).Second)
dayOfWeek = datePartFunc(func(t time.Time) int { return int(t.Weekday()) + 1 })
dayOfYear = datePartFunc((time.Time).YearDay)
)
type clock func() time.Time
var defaultClock = time.Now
// Now is a function that returns the current time.
type Now struct {
clock
}
// NewNow returns a new Now node.
func NewNow() sql.Expression {
return &Now{defaultClock}
}
// Type implements the sql.Expression interface.
func (*Now) Type() sql.Type { return sql.Timestamp }
func (*Now) String() string { return "NOW()" }
// IsNullable implements the sql.Expression interface.
func (*Now) IsNullable() bool { return false }
// Resolved implements the sql.Expression interface.
func (*Now) Resolved() bool { return true }
// Children implements the sql.Expression interface.
func (*Now) Children() []sql.Expression { return nil }
// Eval implements the sql.Expression interface.
func (n *Now) Eval(*sql.Context, sql.Row) (interface{}, error) {
return n.clock(), nil
}
// WithChildren implements the Expression interface.
func (n *Now) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 0 {
return nil, sql.ErrInvalidChildrenNumber.New(n, len(children), 0)
}
return n, nil
}
// Date a function takes the DATE part out from a datetime expression.
type Date struct {
expression.UnaryExpression
}
// NewDate returns a new Date node.
func NewDate(date sql.Expression) sql.Expression {
return &Date{expression.UnaryExpression{Child: date}}
}
func (d *Date) String() string { return fmt.Sprintf("DATE(%s)", d.Child) }
// Type implements the Expression interface.
func (d *Date) Type() sql.Type { return sql.Text }
// Eval implements the Expression interface.
func (d *Date) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return getDatePart(ctx, d.UnaryExpression, row, func(v interface{}) interface{} {
if v == nil {
return nil
}
return v.(time.Time).Format("2006-01-02")
})
}
// WithChildren implements the Expression interface.
func (d *Date) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(d, len(children), 1)
}
return NewDate(children[0]), nil
}