Exemplo n.º 1
0
 static int double_bool(const ObObjCastParams &params, const ObExprObj &in, ObExprObj &out)
 {
     UNUSED(params);
     OB_ASSERT(in.get_type() == ObDoubleType);
     out.set_bool(static_cast<bool> (in.get_double()));
     return OB_SUCCESS;
 }
Exemplo n.º 2
0
 static int double_pdatetime(const ObObjCastParams &params, const ObExprObj &in, ObExprObj &out)
 {
     UNUSED(params);
     OB_ASSERT(in.get_type() == ObDoubleType);
     out.set_precise_datetime(static_cast<ObPreciseDateTime> (in.get_double()));
     return OB_SUCCESS;
 }
Exemplo n.º 3
0
 static int double_decimal(const ObObjCastParams &params, 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;
 }
Exemplo n.º 4
0
 static int double_varchar(const ObObjCastParams &params, const ObExprObj &in, ObExprObj &out)
 {
     UNUSED(params);
     OB_ASSERT(in.get_type() == ObDoubleType);
     return varchar_printf(out, "%f", in.get_double());
 }
Exemplo n.º 5
0
  BLOCK_FUNC()
  {
    ObExprObj t1;
    ObExprObj t2;
    ObExprObj res;
    int64_t i1 = 0;
    int64_t i2 = 0;
    const int64_t buf_len = ObNumber::MAX_PRINTABLE_SIZE;
    char res_buf[buf_len];
    int err = OB_SUCCESS;

    // int add int
    t1.set_int(1);
    t2.set_int(2);
    EXPECT_EQ(OB_SUCCESS, t1.add(t2, res));
    ASSERT_EQ(OB_SUCCESS, res.get_int(i2));
    ASSERT_EQ(3, i2);
    // int add dec
    t1.set_int(1);
    t2.set_decimal("2.2");
    EXPECT_EQ(OB_SUCCESS, t1.add(t2, res));
    ASSERT_EQ(OB_SUCCESS, res.get_decimal(res_buf, buf_len));
    ASSERT_STREQ("3.2", res_buf);
    // dec add dec
    t1.set_decimal("10");
    t2.set_decimal("2.2");
    EXPECT_EQ(OB_SUCCESS, t1.add(t2, res));
    ASSERT_EQ(OB_SUCCESS, res.get_decimal(res_buf, buf_len));
    ASSERT_STREQ("12.2", res_buf);

    // int sub int
    t1.set_int(4);
    t2.set_int(2);
    EXPECT_EQ(OB_SUCCESS, t1.sub(t2, res));
    ASSERT_EQ(OB_SUCCESS, res.get_int(i2));
    ASSERT_EQ(2, i2);
    // int sub dec
    t1.set_int(4);
    t2.set_decimal("3.0");
    EXPECT_EQ(OB_SUCCESS, t1.sub(t2, res));
    EXPECT_EQ(OB_SUCCESS, t1.sub(t2, res));
    ASSERT_EQ(OB_SUCCESS, res.get_decimal(res_buf, buf_len));
    ASSERT_STREQ("1.0", res_buf);
    // dec sub dec
    t1.set_decimal("4");
    t2.set_decimal("2.0");
    EXPECT_EQ(OB_SUCCESS, t1.sub(t2, res));
    ASSERT_EQ(OB_SUCCESS, res.get_decimal(res_buf, buf_len));
    ASSERT_STREQ("2.0", res_buf);

    // int mul int
    t1.set_int(4);
    t2.set_int(2);
    EXPECT_EQ(OB_SUCCESS, t1.mul(t2, res));
    ASSERT_EQ(OB_SUCCESS, res.get_int(i2));
    ASSERT_EQ(8, i2);
    // int mul dec
    t1.set_int(4);
    t2.set_decimal("2.0");
    EXPECT_EQ(OB_SUCCESS, t1.mul(t2, res));
    ASSERT_EQ(OB_SUCCESS, res.get_decimal(res_buf, buf_len));
    ASSERT_STREQ("8.0", res_buf);
    // dec mul dec
    t1.set_decimal("4");
    t2.set_decimal("2.0");
    EXPECT_EQ(OB_SUCCESS, t1.mul(t2, res));
    ASSERT_EQ(OB_SUCCESS, res.get_decimal(res_buf, buf_len));
    ASSERT_STREQ("8.0", res_buf);

    // int div int
    t1.set_int(6);
    t2.set_int(2);
    EXPECT_EQ(OB_SUCCESS, t1.div(t2, res, false));
    ASSERT_EQ(OB_SUCCESS, res.get_decimal(res_buf, buf_len));
    ASSERT_STREQ("3.00000000000000000000000000000000000000", res_buf);
    EXPECT_EQ(OB_SUCCESS, t1.div(t2, res, true));
    double d = 0.0;
    ASSERT_EQ(OB_SUCCESS, res.get_double(d));
    ASSERT_EQ(3.0, d);
    // int div dec
    t1.set_int(6);
    t2.set_decimal("2.0");
    EXPECT_EQ(OB_SUCCESS, t1.div(t2, res, false));
    ASSERT_EQ(OB_SUCCESS, res.get_decimal(res_buf, buf_len));
    ASSERT_STREQ("3.00000000000000000000000000000000000000", res_buf);
    // dec div dec
    t1.set_decimal("6");
    t2.set_decimal("2.0");
    EXPECT_EQ(OB_SUCCESS, t1.div(t2, res, false));
    ASSERT_EQ(OB_SUCCESS, res.get_decimal(res_buf, buf_len));
    ASSERT_STREQ("3.00000000000000000000000000000000000000", res_buf);

    t1.set_int(5);
    t2.set_int(0);
    EXPECT_TRUE(OB_SUCCESS != t1.div(t2, res, false));
    EXPECT_TRUE(true == res.is_null());

    t1.set_decimal("5.0");
    t2.set_int(0);
    EXPECT_TRUE(OB_SUCCESS != t1.div(t2, res, false));
    EXPECT_EQ(true, res.is_null());

    t1.set_int(5);
    t2.set_int(0);
    EXPECT_TRUE(OB_SUCCESS != t1.mod(t2, res));
    EXPECT_EQ(true, res.is_null());

    t1.set_int(5);
    t2.set_int(3);
    EXPECT_EQ(OB_SUCCESS, t1.mod(t2, res));
    err = res.get_int(i1);
    EXPECT_TRUE(OB_SUCCESS == err);
    EXPECT_TRUE(i1 == 2);

    t1.set_int(5);
    t2.set_int(12);
    EXPECT_EQ(OB_SUCCESS, t1.mod(t2, res));
    err = res.get_int(i1);
    EXPECT_TRUE(OB_SUCCESS == err);
    EXPECT_TRUE(i1 == 5);
  }