/
date.go
executable file
·485 lines (385 loc) · 11.9 KB
/
date.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
// 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"
"time"
"github.com/Rock-liyi/p2pdb-store/sql"
"github.com/Rock-liyi/p2pdb-store/sql/expression"
)
// DateAdd adds an interval to a date.
type DateAdd struct {
Date sql.Expression
Interval *expression.Interval
}
var _ sql.FunctionExpression = (*DateAdd)(nil)
// NewDateAdd creates a new date add function.
func NewDateAdd(args ...sql.Expression) (sql.Expression, error) {
if len(args) != 2 {
return nil, sql.ErrInvalidArgumentNumber.New("DATE_ADD", 2, len(args))
}
i, ok := args[1].(*expression.Interval)
if !ok {
return nil, fmt.Errorf("DATE_ADD expects an interval as second parameter")
}
return &DateAdd{args[0], i}, nil
}
// FunctionName implements sql.FunctionExpression
func (d *DateAdd) FunctionName() string {
return "date_add"
}
// Description implements sql.FunctionExpression
func (d *DateAdd) Description() string {
return "adds the interval to the given date."
}
// Children implements the sql.Expression interface.
func (d *DateAdd) Children() []sql.Expression {
return []sql.Expression{d.Date, d.Interval}
}
// Resolved implements the sql.Expression interface.
func (d *DateAdd) Resolved() bool {
return d.Date.Resolved() && d.Interval.Resolved()
}
// IsNullable implements the sql.Expression interface.
func (d *DateAdd) IsNullable() bool {
return true
}
// Type implements the sql.Expression interface.
func (d *DateAdd) Type() sql.Type { return sql.Date }
// WithChildren implements the Expression interface.
func (d *DateAdd) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewDateAdd(children...)
}
// Eval implements the sql.Expression interface.
func (d *DateAdd) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
date, err := d.Date.Eval(ctx, row)
if err != nil {
return nil, err
}
if date == nil {
return nil, nil
}
date, err = sql.Datetime.Convert(date)
if err != nil {
return nil, err
}
delta, err := d.Interval.EvalDelta(ctx, row)
if err != nil {
return nil, err
}
if delta == nil {
return nil, nil
}
return sql.ValidateTime(delta.Add(date.(time.Time))), nil
}
func (d *DateAdd) String() string {
return fmt.Sprintf("DATE_ADD(%s, %s)", d.Date, d.Interval)
}
// DateSub subtracts an interval from a date.
type DateSub struct {
Date sql.Expression
Interval *expression.Interval
}
var _ sql.FunctionExpression = (*DateSub)(nil)
// NewDateSub creates a new date add function.
func NewDateSub(args ...sql.Expression) (sql.Expression, error) {
if len(args) != 2 {
return nil, sql.ErrInvalidArgumentNumber.New("DATE_SUB", 2, len(args))
}
i, ok := args[1].(*expression.Interval)
if !ok {
return nil, fmt.Errorf("DATE_SUB expects an interval as second parameter")
}
return &DateSub{args[0], i}, nil
}
// FunctionName implements sql.FunctionExpression
func (d *DateSub) FunctionName() string {
return "date_sub"
}
// Description implements sql.FunctionExpression
func (d *DateSub) Description() string {
return "subtracts the interval from the given date."
}
// Children implements the sql.Expression interface.
func (d *DateSub) Children() []sql.Expression {
return []sql.Expression{d.Date, d.Interval}
}
// Resolved implements the sql.Expression interface.
func (d *DateSub) Resolved() bool {
return d.Date.Resolved() && d.Interval.Resolved()
}
// IsNullable implements the sql.Expression interface.
func (d *DateSub) IsNullable() bool {
return true
}
// Type implements the sql.Expression interface.
func (d *DateSub) Type() sql.Type { return sql.Date }
// WithChildren implements the Expression interface.
func (d *DateSub) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewDateSub(children...)
}
// Eval implements the sql.Expression interface.
func (d *DateSub) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
date, err := d.Date.Eval(ctx, row)
if err != nil {
return nil, err
}
if date == nil {
return nil, nil
}
date, err = sql.Datetime.Convert(date)
if err != nil {
return nil, err
}
delta, err := d.Interval.EvalDelta(ctx, row)
if err != nil {
return nil, err
}
if delta == nil {
return nil, nil
}
return sql.ValidateTime(delta.Sub(date.(time.Time))), nil
}
func (d *DateSub) String() string {
return fmt.Sprintf("DATE_SUB(%s, %s)", d.Date, d.Interval)
}
// TimestampConversion is a shorthand function for CONVERT(expr, TIMESTAMP)
type TimestampConversion struct {
Date sql.Expression
}
var _ sql.FunctionExpression = (*TimestampConversion)(nil)
// FunctionName implements sql.FunctionExpression
func (t *TimestampConversion) FunctionName() string {
return "timestamp"
}
// Description implements sql.FunctionExpression
func (t *TimestampConversion) Description() string {
return "returns a timestamp value for the expression given (e.g. the string '2020-01-02')."
}
func (t *TimestampConversion) Resolved() bool {
return t.Date == nil || t.Date.Resolved()
}
func (t *TimestampConversion) String() string {
return fmt.Sprintf("TIMESTAMP(%s)", t.Date)
}
func (t *TimestampConversion) Type() sql.Type {
return sql.Timestamp
}
func (t *TimestampConversion) IsNullable() bool {
return false
}
func (t *TimestampConversion) Eval(ctx *sql.Context, r sql.Row) (interface{}, error) {
e, err := t.Date.Eval(ctx, r)
if err != nil {
return nil, err
}
return sql.Timestamp.Convert(e)
}
func (t *TimestampConversion) Children() []sql.Expression {
if t.Date == nil {
return nil
}
return []sql.Expression{t.Date}
}
func (t *TimestampConversion) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewTimestamp(children...)
}
func NewTimestamp(args ...sql.Expression) (sql.Expression, error) {
if len(args) != 1 {
return nil, sql.ErrInvalidArgumentNumber.New("TIMESTAMP", 1, len(args))
}
return &TimestampConversion{args[0]}, nil
}
// DatetimeConversion is a shorthand function for CONVERT(expr, DATETIME)
type DatetimeConversion struct {
Date sql.Expression
}
var _ sql.FunctionExpression = (*DatetimeConversion)(nil)
// FunctionName implements sql.FunctionExpression
func (t *DatetimeConversion) FunctionName() string {
return "datetime"
}
// Description implements sql.FunctionExpression
func (t *DatetimeConversion) Description() string {
return "returns a DATETIME value for the expression given (e.g. the string '2020-01-02')."
}
func (t *DatetimeConversion) Resolved() bool {
return t.Date == nil || t.Date.Resolved()
}
func (t *DatetimeConversion) String() string {
return fmt.Sprintf("DATETIME(%s)", t.Date)
}
func (t *DatetimeConversion) Type() sql.Type {
return sql.Datetime
}
func (t *DatetimeConversion) IsNullable() bool {
return false
}
func (t *DatetimeConversion) Eval(ctx *sql.Context, r sql.Row) (interface{}, error) {
e, err := t.Date.Eval(ctx, r)
if err != nil {
return nil, err
}
return sql.Datetime.Convert(e)
}
func (t *DatetimeConversion) Children() []sql.Expression {
if t.Date == nil {
return nil
}
return []sql.Expression{t.Date}
}
func (t *DatetimeConversion) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewDatetime(children...)
}
// NewDatetime returns a DatetimeConversion instance to handle the sql function "datetime". This is
// not a standard mysql function, but provides a shorthand for datetime conversions.
func NewDatetime(args ...sql.Expression) (sql.Expression, error) {
if len(args) != 1 {
return nil, sql.ErrInvalidArgumentNumber.New("DATETIME", 1, len(args))
}
return &DatetimeConversion{args[0]}, nil
}
// UnixTimestamp converts the argument to the number of seconds since 1970-01-01 00:00:00 UTC.
// With no argument, returns number of seconds since unix epoch for the current time.
type UnixTimestamp struct {
Date sql.Expression
}
var _ sql.FunctionExpression = (*UnixTimestamp)(nil)
func NewUnixTimestamp(args ...sql.Expression) (sql.Expression, error) {
if len(args) > 1 {
return nil, sql.ErrInvalidArgumentNumber.New("UNIX_TIMESTAMP", 1, len(args))
}
if len(args) == 0 {
return &UnixTimestamp{nil}, nil
}
return &UnixTimestamp{args[0]}, nil
}
// FunctionName implements sql.FunctionExpression
func (ut *UnixTimestamp) FunctionName() string {
return "unix_timestamp"
}
// Description implements sql.FunctionExpression
func (ut *UnixTimestamp) Description() string {
return "returns the datetime argument to the number of seconds since the Unix epoch. With no argument, returns the number of seconds since the Unix epoch for the current time."
}
func (ut *UnixTimestamp) Children() []sql.Expression {
if ut.Date != nil {
return []sql.Expression{ut.Date}
}
return nil
}
func (ut *UnixTimestamp) Resolved() bool {
if ut.Date != nil {
return ut.Date.Resolved()
}
return true
}
func (ut *UnixTimestamp) IsNullable() bool {
return true
}
func (ut *UnixTimestamp) Type() sql.Type {
return sql.Float64
}
func (ut *UnixTimestamp) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NewUnixTimestamp(children...)
}
func (ut *UnixTimestamp) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
if ut.Date == nil {
return toUnixTimestamp(ctx.QueryTime())
}
date, err := ut.Date.Eval(ctx, row)
if err != nil {
return nil, err
}
if date == nil {
return nil, nil
}
date, err = sql.Datetime.Convert(date)
if err != nil {
return nil, err
}
return toUnixTimestamp(date.(time.Time))
}
func toUnixTimestamp(t time.Time) (interface{}, error) {
return sql.Float64.Convert(float64(t.Unix()) + float64(t.Nanosecond())/float64(1000000000))
}
func (ut *UnixTimestamp) String() string {
if ut.Date != nil {
return fmt.Sprintf("UNIX_TIMESTAMP(%s)", ut.Date)
} else {
return "UNIX_TIMESTAMP()"
}
}
// FromUnixtime converts the argument to a datetime.
type FromUnixtime struct {
*UnaryFunc
}
var _ sql.FunctionExpression = (*FromUnixtime)(nil)
func NewFromUnixtime(arg sql.Expression) sql.Expression {
return &FromUnixtime{NewUnaryFunc(arg, "FROM_UNIXTIME", sql.Datetime)}
}
// Description implements sql.FunctionExpression
func (r *FromUnixtime) Description() string {
return "formats Unix timestamp as a date."
}
func (r *FromUnixtime) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
val, err := r.EvalChild(ctx, row)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}
n, err := sql.Int64.Convert(val)
if err != nil {
return nil, err
}
return time.Unix(n.(int64), 0), nil
}
func (r *FromUnixtime) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(r, len(children), 1)
}
return NewFromUnixtime(children[0]), nil
}
type CurrDate struct {
NoArgFunc
}
var _ sql.FunctionExpression = CurrDate{}
// Description implements sql.FunctionExpression
func (c CurrDate) Description() string {
return "returns the current date."
}
func NewCurrDate() sql.Expression {
return CurrDate{
NoArgFunc: NoArgFunc{"curdate", sql.LongText},
}
}
func NewCurrentDate() sql.Expression {
return CurrDate{
NoArgFunc: NoArgFunc{"current_date", sql.LongText},
}
}
func currDateLogic(ctx *sql.Context, _ sql.Row) (interface{}, error) {
t := ctx.QueryTime()
return fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day()), nil
}
// Eval implements sql.Expression
func (c CurrDate) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return currDateLogic(ctx, row)
}
// WithChildren implements sql.Expression
func (c CurrDate) WithChildren(children ...sql.Expression) (sql.Expression, error) {
return NoArgFuncWithChildren(c, children)
}