Skip to content

Commit a609bb6

Browse files
author
James Cor
committed
special case for unix timestamp
1 parent 33b112f commit a609bb6

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

enginetest/queries/queries.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10218,6 +10218,20 @@ from typestable`,
1021810218
{" └─ name: "},
1021910219
},
1022010220
},
10221+
10222+
// check that string timestamps preserve trailing 0s
10223+
{
10224+
Query: "select unix_timestamp('2001-02-03 12:34:56.10');",
10225+
Expected: []sql.Row{
10226+
{"981228896.10"},
10227+
},
10228+
},
10229+
{
10230+
Query: "select unix_timestamp('2001-02-03 12:34:56.000000');",
10231+
Expected: []sql.Row{
10232+
{"981228896.000000"},
10233+
},
10234+
},
1022110235
}
1022210236

1022310237
var KeylessQueries = []QueryTest{

sql/expression/function/date.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,22 @@ func NewUnixTimestamp(args ...sql.Expression) (sql.Expression, error) {
476476
if err != nil || date == nil {
477477
return &UnixTimestamp{Date: arg}, nil
478478
}
479+
// special case: text types with fractional seconds preserve scale
480+
if types.IsText(arg.Type()) {
481+
dateStr := date.(string)
482+
idx := strings.Index(dateStr, ".")
483+
if idx != -1 {
484+
dateStr = strings.TrimSpace(dateStr[idx:])
485+
scale := uint8(len(dateStr) - 1)
486+
if scale > 0 {
487+
typ, tErr := types.CreateDecimalType(19, scale)
488+
if tErr != nil {
489+
return nil, tErr
490+
}
491+
return &UnixTimestamp{Date: arg, typ: typ}, nil
492+
}
493+
}
494+
}
479495
date, _, err = types.DatetimeMaxPrecision.Convert(date)
480496
if err != nil {
481497
return &UnixTimestamp{Date: arg}, nil

0 commit comments

Comments
 (0)