From 6deb5e1bfde201c6e54ce1959014459d43e7e3b8 Mon Sep 17 00:00:00 2001 From: Gagan Goel Date: Tue, 8 Jan 2019 20:15:04 -0500 Subject: [PATCH] MCOL-2057 Fix truncate(x,d) when d < 0 for unsigned int data types For unsigned data types in the int family (tinyint, smallint, int, bigint), truncate(x, d) now properly applies zeros when d < 0. --- utils/funcexp/func_truncate.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/utils/funcexp/func_truncate.cpp b/utils/funcexp/func_truncate.cpp index b822caacd..2da482ab8 100644 --- a/utils/funcexp/func_truncate.cpp +++ b/utils/funcexp/func_truncate.cpp @@ -136,7 +136,34 @@ uint64_t Func_truncate::getUintVal(Row& row, bool& isNull, CalpontSystemCatalog::ColType& op_ct) { - return parm[0]->data()->getUintVal(row, isNull); + uint64_t val = parm[0]->data()->getUintVal(row, isNull); + + if (isNull) + return val; + + int64_t d = parm[1]->data()->getIntVal(row, isNull); + + if (isNull || d >= 0) + return val; + + uint64_t p = 1; + int64_t i = (-d); + + // Handle overflow since p can't have more than 19 0's + if (i >= 20) + { + val = 0; + } + else + { + while (i--) + p *= 10; + + val /= p; + val *= p; + } + + return val; }