Skip to content

Commit

Permalink
evalengine: Implement MAKEDATE and MAKETIME. (#12938)
Browse files Browse the repository at this point in the history
This adds the `MAKEDATE` and `MAKETIME` functions. As always, this also
uncovers some other small issues in how we parse things. This time it's
that we don't allow the full range of valid values for hours when
parsing the integer representation for a `time` type.

Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink authored Apr 20, 2023
1 parent 15c0292 commit d1c332e
Show file tree
Hide file tree
Showing 7 changed files with 506 additions and 2 deletions.
4 changes: 2 additions & 2 deletions go/mysql/datetime/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ func ParseTimeInt64(i int64) (t Time, ok bool) {
return t, false
}

t.hour = uint16(i % 100)
if i/100 != 0 {
if i > 838 {
return t, false
}
t.hour = uint16(i)
if neg {
t.hour |= negMask
}
Expand Down
24 changes: 24 additions & 0 deletions go/vt/vtgate/evalengine/cached_size.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions go/vt/vtgate/evalengine/compiler_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,101 @@ func (asm *assembler) Fn_HOUR() {
}, "FN HOUR TIME(SP-1)")
}

func (asm *assembler) Fn_MAKEDATE() {
asm.adjustStack(-1)
asm.emit(func(env *ExpressionEnv) int {
y := env.vm.stack[env.vm.sp-1].(*evalInt64)
yd := env.vm.stack[env.vm.sp-2].(*evalInt64)

t := yearDayToTime(y.i, yd.i)
if t.IsZero() {
env.vm.stack[env.vm.sp-2] = nil
} else {
env.vm.stack[env.vm.sp-2] = env.vm.arena.newEvalDate(datetime.FromStdTime(t).Date)
}
env.vm.sp--
return 1
}, "FN MAKEDATE INT64(SP-2) INT64(SP-1)")
}

func (asm *assembler) Fn_MAKETIME_i() {
asm.adjustStack(-2)
asm.emit(func(env *ExpressionEnv) int {
h := env.vm.stack[env.vm.sp-3].(*evalInt64)
m := env.vm.stack[env.vm.sp-2].(*evalInt64)
s := env.vm.stack[env.vm.sp-1].(*evalInt64)

i, ok := makeTime_i(h.i, m.i, s.i)
if !ok {
env.vm.stack[env.vm.sp-3] = nil
env.vm.sp -= 2
return 1
}
t, ok := datetime.ParseTimeInt64(i)
if !ok {
env.vm.stack[env.vm.sp-3] = nil
env.vm.sp -= 2
return 1
}

env.vm.stack[env.vm.sp-3] = env.vm.arena.newEvalTime(t, 0)
env.vm.sp -= 2
return 1
}, "FN MAKETIME INT64(SP-3) INT64(SP-2) INT64(SP-1)")
}

func (asm *assembler) Fn_MAKETIME_d() {
asm.adjustStack(-2)
asm.emit(func(env *ExpressionEnv) int {
h := env.vm.stack[env.vm.sp-3].(*evalInt64)
m := env.vm.stack[env.vm.sp-2].(*evalInt64)
s := env.vm.stack[env.vm.sp-1].(*evalDecimal)

d, ok := makeTime_d(h.i, m.i, s.dec)
if !ok {
env.vm.stack[env.vm.sp-3] = nil
env.vm.sp -= 2
return 1
}
t, l, ok := datetime.ParseTimeDecimal(d, s.length, -1)
if !ok {
env.vm.stack[env.vm.sp-3] = nil
env.vm.sp -= 2
return 1
}

env.vm.stack[env.vm.sp-3] = env.vm.arena.newEvalTime(t, l)
env.vm.sp -= 2
return 1
}, "FN MAKETIME INT64(SP-3) INT64(SP-2) DECIMAL(SP-1)")
}

func (asm *assembler) Fn_MAKETIME_f() {
asm.adjustStack(-2)
asm.emit(func(env *ExpressionEnv) int {
h := env.vm.stack[env.vm.sp-3].(*evalInt64)
m := env.vm.stack[env.vm.sp-2].(*evalInt64)
s := env.vm.stack[env.vm.sp-1].(*evalFloat)

f, ok := makeTime_f(h.i, m.i, s.f)
if !ok {
env.vm.stack[env.vm.sp-3] = nil
env.vm.sp -= 2
return 1
}
t, l, ok := datetime.ParseTimeFloat(f, -1)
if !ok {
env.vm.stack[env.vm.sp-3] = nil
env.vm.sp -= 2
return 1
}

env.vm.stack[env.vm.sp-3] = env.vm.arena.newEvalTime(t, l)
env.vm.sp -= 2
return 1
}, "FN MAKETIME INT64(SP-3) INT64(SP-2) FLOAT(SP-1)")
}

func (asm *assembler) Fn_MICROSECOND() {
asm.emit(func(env *ExpressionEnv) int {
if env.vm.stack[env.vm.sp-1] == nil {
Expand Down
8 changes: 8 additions & 0 deletions go/vt/vtgate/evalengine/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ func TestCompilerSingle(t *testing.T) {
expression: `WEEK(date '2014-10-26', 6)`,
result: `INT64(44)`,
},
{
expression: `MAKEDATE(cast('invalid' as json), NULL)`,
result: `NULL`,
},
{
expression: `MAKETIME(NULL, '', cast('invalid' as json))`,
result: `NULL`,
},
}

for _, tc := range testCases {
Expand Down
Loading

0 comments on commit d1c332e

Please sign in to comment.