Skip to content

Commit c9eebc9

Browse files
author
James Cor
committed
more fixing for unix_timestmap
1 parent b37ee95 commit c9eebc9

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

enginetest/queries/script_queries.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5133,6 +5133,8 @@ CREATE TABLE tab3 (
51335133
Name: "UNIX_TIMESTAMP function preserves trailing 0s",
51345134
SetUpScript: []string{
51355135
"SET time_zone = '+07:00';",
5136+
"create table t (d0 datetime(0), d1 datetime(1), d2 datetime(2), d3 datetime(3), d4 datetime(4), d5 datetime(5), d6 datetime(6));",
5137+
"insert into t values ('2020-01-02 12:34:56.123456', '2020-01-02 12:34:56.123456', '2020-01-02 12:34:56.123456', '2020-01-02 12:34:56.123456', '2020-01-02 12:34:56.123456', '2020-01-02 12:34:56.123456', '2020-01-02 12:34:56.123456')",
51365138
},
51375139
Assertions: []ScriptTestAssertion{
51385140
{
@@ -5153,6 +5155,12 @@ CREATE TABLE tab3 (
51535155
{"981178496.123457"},
51545156
},
51555157
},
5158+
{
5159+
Query: "select unix_timestamp(d0), unix_timestamp(d1), unix_timestamp(d2), unix_timestamp(d3), unix_timestamp(d4), unix_timestamp(d5), unix_timestamp(d6) from t;",
5160+
Expected: []sql.Row{
5161+
{"1577943296", "1577943296.1", "1577943296.12", "1577943296.123", "1577943296.1235", "1577943296.12346", "1577943296.123456"},
5162+
},
5163+
},
51565164
},
51575165
},
51585166

sql/expression/convert.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ package expression
1717
import (
1818
"encoding/hex"
1919
"fmt"
20-
"strconv"
20+
"github.com/dolthub/vitess/go/sqltypes"
21+
"strconv"
2122
"strings"
2223
"time"
2324

@@ -155,7 +156,7 @@ func (c *Convert) Type() sql.Type {
155156
case ConvertToDate:
156157
return types.Date
157158
case ConvertToDatetime:
158-
return types.DatetimeMaxPrecision
159+
return types.MustCreateDatetimeType(sqltypes.Datetime, c.typeLength)
159160
case ConvertToDecimal:
160161
if c.cachedDecimalType == nil {
161162
c.cachedDecimalType = createConvertedDecimalType(c.typeLength, c.typeScale, true)

sql/expression/function/date.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,18 @@ var _ sql.CollationCoercible = (*UnixTimestamp)(nil)
406406
const MaxUnixTimeMicroSecs = 32536771199999999
407407

408408
// noEval returns true if the expression contains an expression that cannot be evaluated without sql.Context or sql.Row.
409-
func noEval(expr sql.Expression) bool {
410-
var hasBadExpr bool
409+
func canEval(expr sql.Expression) bool {
410+
// Convert Timezone is not evaluable
411+
evaluable := true
411412
transform.InspectExpr(expr, func(e sql.Expression) bool {
412413
switch e.(type) {
413-
case *expression.GetField:
414-
hasBadExpr = true
415-
case *ConvertTz:
416-
hasBadExpr = true
414+
case *expression.GetField, *ConvertTz:
415+
evaluable = false
416+
return true
417417
}
418-
return hasBadExpr
418+
return false
419419
})
420-
return hasBadExpr
420+
return evaluable
421421
}
422422

423423
func getNowExpr(expr sql.Expression) *Now {
@@ -436,7 +436,7 @@ func evalNowType(now *Now) sql.Type {
436436
if now.prec == nil {
437437
return types.Int64
438438
}
439-
if noEval(now.prec) {
439+
if !canEval(now.prec) {
440440
return types.MustCreateDecimalType(19, 6)
441441
}
442442
prec, pErr := now.prec.Eval(nil, nil)
@@ -463,7 +463,10 @@ func NewUnixTimestamp(args ...sql.Expression) (sql.Expression, error) {
463463
}
464464

465465
arg := args[0]
466-
if noEval(arg) {
466+
if dtType, isDtType := arg.Type().(sql.DatetimeType); isDtType {
467+
return &UnixTimestamp{Date: arg, typ: types.MustCreateDecimalType(19, uint8(dtType.Precision()))}, nil
468+
}
469+
if !canEval(arg) {
467470
return &UnixTimestamp{Date: arg, typ: types.MustCreateDecimalType(19, 6)}, nil
468471
}
469472
if now := getNowExpr(arg); now != nil {

sql/types/datetime.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ func (t datetimeType) MustConvert(v interface{}) interface{} {
350350

351351
// Equals implements the Type interface.
352352
func (t datetimeType) Equals(otherType sql.Type) bool {
353+
if dtType, isDtType := otherType.(sql.DatetimeType); isDtType {
354+
return t.baseType == dtType.Type() && t.precision == dtType.Precision()
355+
}
353356
return t.baseType == otherType.Type()
354357
}
355358

0 commit comments

Comments
 (0)