TEST_F(ObObjCastTest, decimal_to_xxx) { ObExprObj in; const char * cases_str[] = {"0", "123.456", "-789.012"}; ObNumber cases[ARRAYSIZEOF(cases_str)]; int64_t int_expected[ARRAYSIZEOF(cases)] = {0, 123, -789}; float float_expected[ARRAYSIZEOF(cases)] = {0.0f, 123.456f, -789.012f}; double double_expected[ARRAYSIZEOF(cases)] = {0.0, 123.456, -789.012}; for (int64_t i = 0; i < static_cast<int64_t>(ARRAYSIZEOF(cases)); ++i) { //printf("case %ld: %s\n", i, cases_str[i]); cases[i].from(cases_str[i]); in.set_decimal(cases[i]); int64_t v = int_expected[i]; MY_EXPECT(ObDecimalType, in, ObNullType, v, cases_str[i]); MY_EXPECT(ObDecimalType, in, ObIntType, v, cases_str[i]); MY_EXPECT(ObDecimalType, in, ObFloatType, float_expected[i], cases_str[i]); MY_EXPECT(ObDecimalType, in, ObDoubleType, double_expected[i], cases_str[i]); MY_EXPECT(ObDecimalType, in, ObDateTimeType, v, cases_str[i]); MY_EXPECT(ObDecimalType, in, ObPreciseDateTimeType, v, cases_str[i]); MY_EXPECT(ObDecimalType, in, ObVarcharType, v, cases_str[i]); MY_EXPECT(ObDecimalType, in, ObSeqType, v, cases_str[i]); MY_EXPECT(ObDecimalType, in, ObCreateTimeType, v, cases_str[i]); MY_EXPECT(ObDecimalType, in, ObModifyTimeType, v, cases_str[i]); MY_EXPECT(ObDecimalType, in, ObExtendType, v, cases_str[i]); MY_EXPECT(ObDecimalType, in, ObBoolType, v, cases_str[i]); MY_EXPECT(ObDecimalType, in, ObDecimalType, v, cases_str[i]); } }
static int mtime_decimal(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObModifyTimeType); ObNumber num; num.from(static_cast<int64_t> (in.get_mtime())); out.set_decimal(num); return OB_SUCCESS; }
static int int_decimal(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObIntType); ObNumber num; num.from(in.get_int()); out.set_decimal(num); // @todo optimize return OB_SUCCESS; }
static int varchar_decimal(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { int ret = OB_SUCCESS; UNUSED(params); OB_ASSERT(in.get_type() == ObVarcharType); const string &varchar = in.get_varchar(); ObNumber num; if (OB_SUCCESS != (ret = num.from(varchar.data(), varchar.length()))) { jlog(WARNING, "failed to convert varchar to decimal, err=%d varchar=%.*s", ret, varchar.length(), varchar.data()); } else { out.set_decimal(num); // @todo optimize } return OB_SUCCESS; }
static int double_decimal(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { int ret = OB_SUCCESS; UNUSED(params); OB_ASSERT(in.get_type() == ObDoubleType); static const int64_t MAX_DOUBLE_PRINT_SIZE = 64; char buf[MAX_DOUBLE_PRINT_SIZE]; snprintf(buf, MAX_DOUBLE_PRINT_SIZE, "%f", in.get_double()); ObNumber num; if (OB_SUCCESS != (ret = num.from(buf))) { jlog(WARNING, "failed to convert float to decimal, err=%d", ret); } else { out.set_decimal(num); } return ret; }