static int bool_float(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObBoolType); out.set_float(static_cast<float> (in.get_bool())); return OB_SUCCESS; }
static int mtime_float(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObModifyTimeType); out.set_float(static_cast<float> (in.get_mtime())); return OB_SUCCESS; }
static int pdatetime_float(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObPreciseDateTimeType); out.set_float(static_cast<float> (in.get_precise_datetime())); return OB_SUCCESS; }
TEST_F(ObObjCastTest, float_to_xxx) { ObExprObj in; char buf[128]; float cases[] = {-1.1f, 0.0f, 1.1f}; const char *dec_expected[] = {"-1.100000", "0", "1.100000"}; for (int64_t i = 0; i < static_cast<int64_t>(ARRAYSIZEOF(cases)); ++i) { float v = cases[i]; in.set_float(v); snprintf(buf, 128, "%f", v); MY_EXPECT(ObFloatType, in, ObNullType, v, buf); MY_EXPECT(ObFloatType, in, ObIntType, v, buf); MY_EXPECT(ObFloatType, in, ObFloatType, v, buf); MY_EXPECT(ObFloatType, in, ObDoubleType, v, buf); MY_EXPECT(ObFloatType, in, ObDateTimeType, v, buf); MY_EXPECT(ObFloatType, in, ObPreciseDateTimeType, v, buf); MY_EXPECT(ObFloatType, in, ObVarcharType, v, buf); MY_EXPECT(ObFloatType, in, ObSeqType, v, buf); MY_EXPECT(ObFloatType, in, ObCreateTimeType, v, buf); MY_EXPECT(ObFloatType, in, ObModifyTimeType, v, buf); MY_EXPECT(ObFloatType, in, ObExtendType, v, buf); MY_EXPECT(ObFloatType, in, ObBoolType, v, buf); MY_EXPECT(ObFloatType, in, ObDecimalType, v, dec_expected[i]); } }
static int varchar_float(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { int ret = OB_SUCCESS; UNUSED(params); OB_ASSERT(in.get_type() == ObVarcharType); float value = 0.0f; ret = varchar_scanf(in, 1, "%f", &value); if (OB_SUCCESS == ret) { out.set_float(value); } else { out.set_float(0.0f); ret = OB_SUCCESS; } return ret; }
static int decimal_float(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObDecimalType); // decimal -> string -> float char buf[ObNumber::MAX_PRINTABLE_SIZE]; memset(buf, 0, ObNumber::MAX_PRINTABLE_SIZE); in.get_decimal().to_string(buf, ObNumber::MAX_PRINTABLE_SIZE); float v = 0.0f; if (1 == sscanf(buf, "%f", &v)) { out.set_float(v); } else { out.set_float(0.0f); } return OB_SUCCESS; }