From 54fcfb20c517dfe48d528b9f6677273549be70b7 Mon Sep 17 00:00:00 2001 From: Andrew Hutchings Date: Tue, 27 Sep 2016 17:31:05 +0100 Subject: [PATCH] Fix decimal to date/datetime casting --- utils/dataconvert/dataconvert.cpp | 9 ++++++++- utils/funcexp/func_cast.cpp | 31 ++++++++----------------------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/utils/dataconvert/dataconvert.cpp b/utils/dataconvert/dataconvert.cpp index 327a21ba3..7f93821f7 100644 --- a/utils/dataconvert/dataconvert.cpp +++ b/utils/dataconvert/dataconvert.cpp @@ -1805,6 +1805,14 @@ int64_t DataConvert::intToDate(int64_t data) //snprintf( buf, 10, "%llu", (long long unsigned int)data); //string date = buf; char buf[21] = {0}; + Date aday; + if (data == 0) + { + aday.year = 0; + aday.month = 0; + aday.day = 0; + return *(reinterpret_cast(&aday)); + } snprintf( buf, 15, "%llu", (long long unsigned int)data); string year, month, day, hour, min, sec, msec; @@ -1866,7 +1874,6 @@ int64_t DataConvert::intToDate(int64_t data) default: return -1; } - Date aday; if (year.empty()) { // MMDD format. assume current year diff --git a/utils/funcexp/func_cast.cpp b/utils/funcexp/func_cast.cpp index 2c032e99b..0bf45a3fc 100644 --- a/utils/funcexp/func_cast.cpp +++ b/utils/funcexp/func_cast.cpp @@ -510,20 +510,12 @@ int32_t Func_cast_date::getDateIntVal(rowgroup::Row& row, case execplan::CalpontSystemCatalog::DECIMAL: case execplan::CalpontSystemCatalog::UDECIMAL: { - if (parm[0]->data()->resultType().scale != 0) - { + val = dataconvert::DataConvert::intToDate(parm[0]->data()->getIntVal(row, isNull)); + if (val == -1) isNull = true; - break; - } else - { - val = dataconvert::DataConvert::intToDate(parm[0]->data()->getIntVal(row, isNull)); - if (val == -1) - isNull = true; - else - return val; - break; - } + return val; + break; } case execplan::CalpontSystemCatalog::VARCHAR: case execplan::CalpontSystemCatalog::CHAR: @@ -715,18 +707,11 @@ int64_t Func_cast_datetime::getDatetimeIntVal(rowgroup::Row& row, case execplan::CalpontSystemCatalog::DECIMAL: case execplan::CalpontSystemCatalog::UDECIMAL: { - if (parm[0]->data()->resultType().scale) - { - val = dataconvert::DataConvert::intToDatetime(parm[0]->data()->getIntVal(row, isNull)); - if (val == -1) - isNull = true; - else - return val; - } - else - { + val = dataconvert::DataConvert::intToDatetime(parm[0]->data()->getIntVal(row, isNull)); + if (val == -1) isNull = true; - } + else + return val; break; } case execplan::CalpontSystemCatalog::VARCHAR: