Skip to content

Commit 42c3633

Browse files
ImeevMAkyukhin
authored andcommitted
sql: introduce cast from STRING to DATETIME
This patch introduces explicit cast from STRING to DATETIME. Follow-up tarantool#6773 NO_DOC=DATETIME has already been introduced. NO_CHANGELOG=DATETIME has already been introduced.
1 parent ddec704 commit 42c3633

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/box/sql/mem.c

+14
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,18 @@ str_to_dec(struct Mem *mem)
858858
return 0;
859859
}
860860

861+
/** Convert MEM from STRING to DATETIME. */
862+
static inline int
863+
str_to_datetime(struct Mem *mem)
864+
{
865+
assert(mem->type == MEM_TYPE_STR);
866+
struct datetime dt;
867+
if (datetime_parse_full(&dt, mem->z, mem->n, 0) <= 0)
868+
return -1;
869+
mem_set_datetime(mem, &dt);
870+
return 0;
871+
}
872+
861873
static inline int
862874
double_to_int(struct Mem *mem)
863875
{
@@ -1457,6 +1469,8 @@ mem_cast_explicit(struct Mem *mem, enum field_type type)
14571469
return bin_to_uuid(mem);
14581470
return -1;
14591471
case FIELD_TYPE_DATETIME:
1472+
if (mem->type == MEM_TYPE_STR)
1473+
return str_to_datetime(mem);
14601474
if (mem->type != MEM_TYPE_DATETIME)
14611475
return -1;
14621476
mem->flags = 0;

test/sql-luatest/datetime_test.lua

+29-4
Original file line numberDiff line numberDiff line change
@@ -931,11 +931,12 @@ end
931931
g.test_datetime_18_3 = function()
932932
g.server:exec(function()
933933
local t = require('luatest')
934+
local dt = require('datetime')
935+
local dt1 = dt.new({year = 2001, month = 1, day = 1, hour = 1})
934936
local sql = [[SELECT CAST('2001-01-01T01:00:00Z' AS DATETIME);]]
935-
local res = [[Type mismatch: can not convert ]]..
936-
[[string('2001-01-01T01:00:00Z') to datetime]]
937-
local _, err = box.execute(sql)
938-
t.assert_equals(err.message, res)
937+
local res = {{dt1}}
938+
local rows = box.execute(sql).rows
939+
t.assert_equals(rows, res)
939940
end)
940941
end
941942

@@ -2362,3 +2363,27 @@ g.test_datetime_31_8 = function()
23622363
t.assert_equals(rows, {{itv3}})
23632364
end)
23642365
end
2366+
2367+
-- Make sure cast from STRING to DATETIME works as intended.
2368+
g.test_datetime_32_1 = function()
2369+
g.server:exec(function()
2370+
local t = require('luatest')
2371+
local dt = require('datetime')
2372+
local dt1 = dt.new({year = 2000, month = 2, day = 29, hour = 1})
2373+
local sql = [[SELECT CAST('2000-02-29T01:00:00Z' AS DATETIME);]]
2374+
local res = {{dt1}}
2375+
local rows = box.execute(sql).rows
2376+
t.assert_equals(rows, res)
2377+
end)
2378+
end
2379+
2380+
g.test_datetime_32_2 = function()
2381+
g.server:exec(function()
2382+
local t = require('luatest')
2383+
local sql = [[SELECT CAST('2001-02-29T01:00:00Z' AS DATETIME);]]
2384+
local _, err = box.execute(sql)
2385+
local res = [[Type mismatch: can not convert ]]..
2386+
[[string('2001-02-29T01:00:00Z') to datetime]]
2387+
t.assert_equals(err.message, res)
2388+
end)
2389+
end

0 commit comments

Comments
 (0)