TEST_F(ObObjCastTest, bool_to_xxx) { ObExprObj in; bool cases[] = {true, false}; const char *varchar_expected[] = {"true", "false"}; const char *dec_expected[] = {"1", "0"}; for (int64_t i = 0; i < static_cast<int64_t>(ARRAYSIZEOF(cases)); ++i) { bool v = cases[i]; in.set_bool(v); MY_EXPECT(ObBoolType, in, ObNullType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObIntType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObFloatType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObDoubleType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObDateTimeType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObPreciseDateTimeType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObVarcharType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObSeqType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObCreateTimeType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObModifyTimeType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObExtendType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObBoolType, v, varchar_expected[i]); MY_EXPECT(ObBoolType, in, ObDecimalType, v, dec_expected[i]); } }
TEST_F(ObObjCastTest, mtime_to_xxx) { ObExprObj in; const char* cases_str[] = {"1970-1-1 8:0:0", "1970-1-1 0:0:0", "2012-9-24 16:59:60"}; int64_t cases[ARRAYSIZEOF(cases_str)]; const char *varchar_expected[] = {"1970-01-01 08:00:00.000000", "1970-01-01 00:00:00.000000", "2012-09-24 17:00:00.000000"}; const char *dec_expected[] = {"0", "-28800000000", "1348477200000000"}; for (int64_t i = 0; i < static_cast<int64_t>(ARRAYSIZEOF(cases)); ++i) { struct tm tm; memset(&tm, 0, sizeof(tm)); strptime(cases_str[i], "%Y-%m-%d %H:%M:%S", &tm); cases[i] = mktime(&tm) * 1000000L; int64_t v = cases[i]; int64_t pv = v / 1000000L; in.set_mtime(v); MY_EXPECT(ObModifyTimeType, in, ObNullType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObIntType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObFloatType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObDoubleType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObDateTimeType, pv, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObPreciseDateTimeType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObVarcharType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObSeqType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObCreateTimeType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObModifyTimeType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObExtendType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObBoolType, v, varchar_expected[i]); MY_EXPECT(ObModifyTimeType, in, ObDecimalType, v, dec_expected[i]); } }
TEST_F(ObObjCastTest, varchar_to_xxx) { ObExprObj in; const char* cases[] = {"1970-1-1 8:0:0", "2012-9-24 16:59:60.000000", "-123456", "-123.456", "true", "false"}; int64_t int_expected[ARRAYSIZEOF(cases)] = {1970, 2012, -123456, -123, 0, 0}; float float_expected[ARRAYSIZEOF(cases)] = {1970.0f, 2012.0f, -123456.0f, -123.456f, 0.0f, 0.0f}; double double_expected[ARRAYSIZEOF(cases)] = {1970.0, 2012.0, -123456.0, -123.456, 0.0, 0.0}; int64_t pdt_expected[ARRAYSIZEOF(cases)] = {0, 1348477200000000, 0, 0, 0, 0}; bool bool_expected[ARRAYSIZEOF(cases)] = {false, false, false, false, true, false}; const char *dec_expected[ARRAYSIZEOF(cases)] = {"0", "0", "-123456", "-123.456", "0", "0"}; for (int64_t i = 0; i < static_cast<int64_t>(ARRAYSIZEOF(cases)); ++i) { ObString v; v.assign_ptr(const_cast<char*>(cases[i]), static_cast<int32_t>(strlen(cases[i]))); in.set_varchar(v); //printf("case %ld: %s\n", i, cases[i]); MY_EXPECT(ObVarcharType, in, ObNullType, int_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObIntType, int_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObFloatType, float_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObDoubleType, double_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObDateTimeType, (pdt_expected[i]/1000000L), cases[i]); MY_EXPECT(ObVarcharType, in, ObPreciseDateTimeType, pdt_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObVarcharType, int_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObSeqType, int_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObCreateTimeType, pdt_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObModifyTimeType, pdt_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObExtendType, int_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObBoolType, bool_expected[i], cases[i]); MY_EXPECT(ObVarcharType, in, ObDecimalType, int_expected[i], dec_expected[i]); } }
TEST_F(ObObjCastTest, double_to_xxx) { ObExprObj in; char buf[128]; double cases[] = {-1.1, 0.0, 1.1}; const char *dec_expected[] = {"-1.100000", "0", "1.100000"}; for (int64_t i = 0; i < static_cast<int64_t>(ARRAYSIZEOF(cases)); ++i) { double v = cases[i]; in.set_double(v); snprintf(buf, 128, "%lf", v); MY_EXPECT(ObDoubleType, in, ObNullType, v, buf); MY_EXPECT(ObDoubleType, in, ObIntType, v, buf); MY_EXPECT(ObDoubleType, in, ObFloatType, v, buf); MY_EXPECT(ObDoubleType, in, ObDoubleType, v, buf); MY_EXPECT(ObDoubleType, in, ObDateTimeType, v, buf); MY_EXPECT(ObDoubleType, in, ObPreciseDateTimeType, v, buf); MY_EXPECT(ObDoubleType, in, ObVarcharType, v, buf); MY_EXPECT(ObDoubleType, in, ObSeqType, v, buf); MY_EXPECT(ObDoubleType, in, ObCreateTimeType, v, buf); MY_EXPECT(ObDoubleType, in, ObModifyTimeType, v, buf); MY_EXPECT(ObDoubleType, in, ObExtendType, v, buf); MY_EXPECT(ObDoubleType, in, ObBoolType, v, buf); MY_EXPECT(ObDoubleType, in, ObDecimalType, v, dec_expected[i]); } }
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 double_mtime(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObDoubleType); out.set_mtime(static_cast<ObModifyTime> (in.get_double())); return OB_SUCCESS; }
static int bool_ctime(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObBoolType); out.set_ctime(static_cast<ObCreateTime> (in.get_bool())); return OB_SUCCESS; }
static int int_datetime(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObIntType); out.set_datetime(static_cast<ObDateTime> (in.get_int())); return OB_SUCCESS; }
static int double_int(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObDoubleType); out.set_int(static_cast<int64_t> (in.get_double())); return OB_SUCCESS; }
static int ctime_float(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObCreateTimeType); out.set_float(static_cast<float> (in.get_ctime())); return OB_SUCCESS; }
static int ctime_datetime(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObCreateTimeType); out.set_datetime(static_cast<ObDateTime> (in.get_ctime() / 1000000L)); return OB_SUCCESS; }
static int float_double(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObFloatType); out.set_double(static_cast<double> (in.get_float())); return OB_SUCCESS; }
int obj_cast(const ObObj &orig_cell, const ObObj &expected_type, ObObj &casted_cell, const ObObj *&res_cell) { int ret = OB_SUCCESS; if (orig_cell.get_type() != expected_type.get_type()) { // type cast ObObjCastParams params; ObExprObj from; ObExprObj to; from.assign(orig_cell); to.assign(casted_cell); if (OB_SUCCESS != (ret = OB_OBJ_CAST[orig_cell.get_type()][expected_type.get_type()](params, from, to))) { jlog(WARNING, "failed to type cast obj, err=%d", ret); } else if (OB_SUCCESS != (ret = to.to(casted_cell))) { jlog(WARNING, "failed to convert expr_obj to obj, err=%d", ret); } else { res_cell = &casted_cell; } } else { res_cell = &orig_cell; } return ret; }
static int decimal_bool(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObDecimalType); out.set_bool(!in.get_decimal().is_zero()); return OB_SUCCESS; }
static int mtime_pdatetime(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObModifyTimeType); out.set_precise_datetime(static_cast<ObPreciseDateTime> (in.get_mtime())); return OB_SUCCESS; }
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_bool(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObModifyTimeType); out.set_bool(static_cast<bool> (in.get_mtime())); return OB_SUCCESS; }
static int int_varchar(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { int ret = OB_SUCCESS; UNUSED(params); OB_ASSERT(in.get_type() == ObIntType); ret = varchar_printf(out, "%ld", in.get_int()); return ret; }
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 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 datetime_varchar(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { int ret = OB_SUCCESS; UNUSED(params); OB_ASSERT(in.get_type() == ObDateTimeType); time_t t = static_cast<time_t> (in.get_datetime()); struct tm gtm; localtime_r(&t, >m); ret = varchar_printf(out, "%04d-%02d-%02d %02d:%02d:%02d", gtm.tm_year + 1900, gtm.tm_mon + 1, gtm.tm_mday, gtm.tm_hour, gtm.tm_min, gtm.tm_sec); return ret; }
TEST_F(ObObjCastTest, null_to_xxx) { ObExprObj in; in.set_null(); ObExprObj out; ObObjCastParams params; for (ObObjType t2 = static_cast<ObObjType>(ObMinType+1); t2 < ObMaxType; t2 = static_cast<ObObjType>(t2 + 1)) { ASSERT_EQ(OB_SUCCESS, OB_OBJ_CAST[ObNullType][t2](params, in, out)); ASSERT_TRUE(out.is_null()); } }
static int varchar_datetime(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { int ret = OB_SUCCESS; UNUSED(params); OB_ASSERT(in.get_type() == ObVarcharType); int year = 0; int month = 0; int day = 0; int hour = 0; int minute = 0; int second = 0; ret = varchar_scanf(in, 6, "%4d-%2d-%2d %2d:%2d:%2d", &year, &month, &day, &hour, &minute, &second); if (OB_SUCCESS != ret) { year = month = day = hour = minute = second = 0; ret = varchar_scanf(in, 3, "%4d-%2d-%2d", &year, &month, &day); } if (OB_SUCCESS != ret) { year = month = day = hour = minute = second = 0; ret = varchar_scanf(in, 3, "%2d:%2d:%2d", &hour, &minute, &second); } if (OB_SUCCESS == ret) { struct tm gtm; memset(>m, 0, sizeof (gtm)); gtm.tm_year = year - 1900; gtm.tm_mon = month - 1; gtm.tm_mday = day; gtm.tm_hour = hour; gtm.tm_min = minute; gtm.tm_sec = second; time_t t = mktime(>m); out.set_datetime(static_cast<ObDateTime> (t)); } else { const string& varchar = in.get_varchar(); jlog(WARNING, "failed to convert string `%.*s' to datetime type", varchar.length(), varchar.data()); out.set_datetime(static_cast<ObDateTime> (0)); ret = OB_SUCCESS; } return ret; }
static int bool_varchar(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { UNUSED(params); OB_ASSERT(in.get_type() == ObBoolType); string varchar; if (in.get_bool()) { varchar.assign("true"); } else { varchar.assign("false"); } out.set_varchar(varchar); return OB_SUCCESS; }
static int decimal_mtime(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { int ret = OB_SUCCESS; UNUSED(params); OB_ASSERT(in.get_type() == ObDecimalType); int64_t i64 = 0; if (OB_SUCCESS != (ret = in.get_decimal().cast_to_int64(i64))) { jlog(WARNING, "failed to cast to int64, err=%d", ret); } else { out.set_mtime(static_cast<ObModifyTime> (i64)); } return ret; }
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; }
void ObExprObjTest::test_not(MyBool t1, MyBool res) { ObExprObj v1 = bool_obj(t1); ObExprObj vres; ASSERT_EQ(OB_SUCCESS, v1.lnot(vres)); switch(res) { case MY_TRUE: ASSERT_TRUE(vres.is_true()); break; case MY_FALSE: ASSERT_TRUE(vres.is_false()); break; default: ASSERT_TRUE(vres.is_null()); break; } }
static int varchar_double(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { int ret = OB_SUCCESS; UNUSED(params); OB_ASSERT(in.get_type() == ObVarcharType); double value = 0.0; ret = varchar_scanf(in, 1, "%lf", &value); if (OB_SUCCESS == ret) { out.set_double(value); } else { out.set_double(0.0); ret = OB_SUCCESS; } return ret; }
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; }
static int decimal_varchar(const ObObjCastParams ¶ms, const ObExprObj &in, ObExprObj &out) { int ret = OB_SUCCESS; UNUSED(params); OB_ASSERT(in.get_type() == ObDecimalType); string varchar = out.get_varchar(); if (varchar.length() < ObNumber::MAX_PRINTABLE_SIZE) { jlog(WARNING, "output buffer for varchar not enough, buf_len=%d", varchar.length()); ret = OB_INVALID_ARGUMENT; } else { int64_t length = in.get_decimal().to_string((char*) varchar.data(), varchar.length()); string varchar2(varchar); out.set_varchar(varchar2); } return ret; }