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; }
int ObMetaTable3::MyIterator::check_rowkey_obj_type(int32_t rowkey_obj_idx, const ObObj &obj) const { int ret = OB_SUCCESS; bool found = false; for (int k = 0; k < table_schema_.columns_.count(); ++k) { const ColumnSchema &cschema = table_schema_.columns_.at(k); if (rowkey_obj_idx + 1 == cschema.rowkey_id_) // rowkey_id start from 1 { if (obj.get_type() != cschema.data_type_ && (!obj.is_min_value()) && (!obj.is_max_value())) { TBSYS_LOG(WARN, "invalid meta table row key, cid=%lu ctype=%d obj_type=%d", cschema.column_id_, cschema.data_type_, obj.get_type()); break; } found = true; break; } } // end for if (!found) { TBSYS_LOG(WARN, "rowkey obj not found in table schema, idx=%d", rowkey_obj_idx); ret = OB_ERR_DATA_FORMAT; } return ret; }
TEST(ObObj,Serialize_bool) { ObObj t; t.set_bool(true); int64_t len = t.get_serialize_size(); ASSERT_EQ(len,2); char *buf = (char *)malloc(len); int64_t pos = 0; ASSERT_EQ(t.serialize(buf,len,pos),OB_SUCCESS); ASSERT_EQ(pos,len); ObObj f; pos = 0; ASSERT_EQ(f.deserialize(buf,len,pos),OB_SUCCESS); ASSERT_EQ(pos,len); bool r = false; bool l = false; ASSERT_EQ(f.get_type(),t.get_type()); f.get_bool(r); t.get_bool(l); ASSERT_EQ(r,l); free(buf); }
TEST(ObObj,Serialize_int_add) { ObObj t; t.set_int(9900); int64_t len = t.get_serialize_size(); ASSERT_EQ(len,3); char *buf = (char *)malloc(len); int64_t pos = 0; ASSERT_EQ(t.serialize(buf,len,pos),OB_SUCCESS); ASSERT_EQ(pos,len); ObObj f; pos = 0; ASSERT_EQ(f.deserialize(buf,len,pos),OB_SUCCESS); ASSERT_EQ(pos,len); int64_t r = 0; int64_t l = 0; ASSERT_EQ(f.get_type(),t.get_type()); f.get_int(r); t.get_int(l); ASSERT_EQ(r,l); free(buf); }
int get_rowkey_compatible(const char* buf, const int64_t buf_len, int64_t & pos, const ObRowkeyInfo& info, ObObj* array, int64_t& size, bool& is_binary_rowkey) { int ret = OB_SUCCESS; ObObj obj; int64_t obj_count = 0; ObString str_value; is_binary_rowkey = false; if ( OB_SUCCESS == (ret = obj.deserialize(buf, buf_len, pos)) ) { if (ObIntType == obj.get_type() && (OB_SUCCESS == (ret = obj.get_int(obj_count)))) { // new rowkey format. for (int64_t i = 0; i < obj_count && OB_SUCCESS == ret; ++i) { if (i >= size) { ret = OB_SIZE_OVERFLOW; } else { ret = array[i].deserialize(buf, buf_len, pos); } } if (OB_SUCCESS == ret) size = obj_count; } else if (ObVarcharType == obj.get_type() && OB_SUCCESS == (ret = obj.get_varchar(str_value))) { is_binary_rowkey = true; // old fashion , binary rowkey stream if (size < info.get_size()) { TBSYS_LOG(WARN, "input size=%ld not enough, need rowkey obj size=%ld", size, info.get_size()); ret = OB_SIZE_OVERFLOW; } else if (str_value.length() == 0) { // allow empty binary rowkey , incase min, max range. size = 0; } else if (str_value.length() < info.get_binary_rowkey_length()) { TBSYS_LOG(WARN, "binary rowkey length=%d < need rowkey length=%ld", str_value.length(), info.get_binary_rowkey_length()); ret = OB_SIZE_OVERFLOW; } else { size = info.get_size(); ret = ObRowkeyHelper::binary_rowkey_to_obj_array(info, str_value, array, size); } } } return ret; }
void check_obj(const ObObj& expected, const ObObj& real) { // TODO EXPECT_EQ(expected.get_type(), real.get_type()); int64_t expected_val, real_val; int ret = 0; ret = expected.get_int(expected_val); EXPECT_EQ(0, ret); ret = real.get_int(real_val); EXPECT_EQ(0, ret); EXPECT_EQ(expected_val, real_val); }
int ObStringBuf :: write_obj(const ObObj& obj, ObObj* stored_obj) { int err = OB_SUCCESS; if (NULL != stored_obj) { *stored_obj = obj; } ObObjType type = obj.get_type(); if (ObVarcharType == type) { ObString value; ObString new_value; obj.get_varchar(value); err = write_string(value, &new_value); if (OB_SUCCESS == err) { if (NULL != stored_obj) { stored_obj->set_varchar(new_value); } } } return err; }
TEST(ObObj,Serialize_precise_datetime) { ObObj t1; t1.set_precise_datetime(221348087492); int64_t len = t1.get_serialize_size(); char *buf = (char *)malloc(len); int64_t pos = 0; ASSERT_EQ(t1.serialize(buf,len,pos),OB_SUCCESS); ObObj f; pos = 0; ASSERT_EQ(f.deserialize(buf,len,pos),OB_SUCCESS); ASSERT_EQ(f.get_type(),t1.get_type()); ObPreciseDateTime l = 0; ObPreciseDateTime r = 0; f.get_precise_datetime(l); t1.get_precise_datetime(r); ASSERT_EQ(l,r); free(buf); }
int get_int_obj_value(const char* buf, const int64_t buf_len, int64_t & pos, int64_t & int_value) { int ret = OB_SUCCESS; ObObj obj; if ( OB_SUCCESS == (ret = obj.deserialize(buf, buf_len, pos)) && ObIntType == obj.get_type()) { ret = obj.get_int(int_value); } return ret; }
int get_str_obj_value(const char* buf, const int64_t buf_len, int64_t & pos, ObString & str_value) { int ret = OB_SUCCESS; ObObj obj; if ( OB_SUCCESS == (ret = obj.deserialize(buf, buf_len, pos)) && ObVarcharType == obj.get_type()) { ret = obj.get_varchar(str_value); } return ret; }
int obj_cast(ObObj &cell, const ObObjType expected_type, char* buf, int64_t buf_size, int64_t &used_buf_len) { int ret = OB_SUCCESS; used_buf_len = 0; if (cell.get_type() != expected_type) { ObObjCastParams params; ObExprObj from; ObExprObj to; from.assign(cell); if (ObVarcharType == expected_type) { string buffer; buffer.assign(buf, static_cast<int32_t> (buf_size)); ObObj varchar_cell; varchar_cell.set_varchar(buffer); to.assign(varchar_cell); } if (OB_SUCCESS != (ret = OB_OBJ_CAST[cell.get_type()][expected_type](params, from, to))) { jlog(WARNING, "failed to type cast obj, err=%d", ret); } else if (OB_SUCCESS != (ret = to.to(cell))) { jlog(WARNING, "failed to convert expr_obj to obj, err=%d", ret); } else { if (ObVarcharType == expected_type) { string varchar; cell.get_varchar(varchar); used_buf_len = varchar.length(); // used buffer length for casting to varchar type } } } return ret; }
inline int ObCellArray::copy_obj_(ObObj &dst, const ObObj &src) { int err = OB_SUCCESS; char *tmp_buf = NULL; /// allocate value if (src.get_type() == ObVarcharType) { ObString src_value; ObString dst_value; err = src.get_varchar(src_value); if (OB_SUCCESS == err) { // must assign %src to %dst first, for copy meta data to %dst object. // dst's meta_ members(especially meta_.reserved_) could be random value // if only call dst.set_varchar(); dst = src; if (src_value.length() > 0) { tmp_buf =reinterpret_cast<char*>( page_arena_.alloc(src_value.length())); if (NULL == tmp_buf) { TBSYS_LOG(WARN, "%s", "fail to malloc buffer for varchar value"); err = OB_ALLOCATE_MEMORY_FAILED; } else { allocated_memory_size_ += src_value.length(); memcpy(tmp_buf, src_value.ptr(), src_value.length()); dst_value.assign(tmp_buf,src_value.length()); dst.set_varchar(dst_value); } } else { dst.set_varchar(dst_value); } } } return err; }
int ObSqlGetParam::deserialize_int(const char* buf, const int64_t data_len, int64_t& pos, int64_t& value) const { int ret = OB_SUCCESS; ObObjType type = ObNullType; ObObj obj; ret = obj.deserialize(buf, data_len, pos); if (OB_SUCCESS == ret) { type = obj.get_type(); if (ObIntType == type) { obj.get_int(value); } else { TBSYS_LOG(WARN, "expected deserialize int type, but get type=%d", type); ret = OB_ERROR; } } return ret; }
bool ObSimpleCond::calc(const ObObj & left_operand) const { bool ret = false; // skip nulltype for compare return false switch (logic_operator_) { case LT: { if (ObNullType != left_operand.get_type()) { ret = left_operand < right_operand_; } break; } case LE: { if (ObNullType != left_operand.get_type()) { ret = left_operand <= right_operand_; } break; } case EQ: { ret = left_operand == right_operand_; break; } case NE: { ret = left_operand != right_operand_; break; } case GT: { if (ObNullType != left_operand.get_type()) { ret = left_operand > right_operand_; } break; } case GE: { if (ObNullType != left_operand.get_type()) { ret = left_operand >= right_operand_; } break; } case LIKE: { if (ObNullType != left_operand.get_type()) { ret = find(right_operand_, pattern_sign_, left_operand) >= 0; } break; } default: { TBSYS_LOG(ERROR, "check condition logic type failed:type[%d]", logic_operator_); } } return ret; }
int ObObj::apply(const ObObj &mutation) { int64_t org_type = get_type(); int64_t org_ext = get_ext(); int err = OB_SUCCESS; ObCreateTime create_time = 0; ObModifyTime modify_time = 0; bool is_add = false; bool org_is_add = false; if (ObSeqType == mutation.get_type() || ObMinType >= mutation.get_type() || ObMaxType <= mutation.get_type()) { TBSYS_LOG(WARN,"unsupported type [type:%d]", static_cast<int32_t>(mutation.get_type())); err = OB_INVALID_ARGUMENT; } if (OB_SUCCESS == err && ObExtendType != get_type() && ObNullType != get_type() && ObExtendType != mutation.get_type() && ObNullType != mutation.get_type() && get_type() != mutation.get_type()) { TBSYS_LOG(WARN,"type not coincident [this->type:%d,mutation.type:%d]", static_cast<int>(get_type()), static_cast<int>(mutation.get_type())); err = OB_INVALID_ARGUMENT; } _ObjValue value, mutation_value; if (OB_SUCCESS == err) { bool ext_val_can_change = (ObActionFlag::OP_ROW_DOES_NOT_EXIST == get_ext()) || (ObNullType == get_type()); bool org_is_nop = (ObActionFlag::OP_NOP == get_ext()); switch (mutation.get_type()) { case ObNullType: set_null(); break; case ObVarcharType: *this = mutation; break; case ObIntType: if (ext_val_can_change || org_is_nop) { set_int(0); } err = get_int(value.int_val,org_is_add); if (OB_SUCCESS == err) { err = mutation.get_int(mutation_value.int_val,is_add); } if (OB_SUCCESS == err) { if (is_add) { value.int_val += mutation_value.int_val; } else { value.int_val = mutation_value.int_val; } set_int(value.int_val, (org_is_add || org_is_nop) && is_add); } break; case ObFloatType: if (ext_val_can_change || org_is_nop) { set_float(0); } err = get_float(value.float_val,org_is_add); if (OB_SUCCESS == err) { err = mutation.get_float(mutation_value.float_val, is_add); } if (OB_SUCCESS == err) { if (is_add) { value.float_val += mutation_value.float_val; } else { value.float_val = mutation_value.float_val; } set_float(value.float_val,is_add && (org_is_add || org_is_nop)); } break; case ObDoubleType: if (ext_val_can_change || org_is_nop) { set_double(0); } err = get_double(value.double_val,org_is_add); if (OB_SUCCESS == err) { err = mutation.get_double(mutation_value.double_val,is_add); } if (OB_SUCCESS == err) { if (is_add) { value.double_val += mutation_value.double_val; } else { value.double_val = mutation_value.double_val; } set_double(value.double_val, (org_is_add || org_is_nop) && is_add); } break; case ObDateTimeType: if (ext_val_can_change || org_is_nop) { set_datetime(0); } err = get_datetime(value.second_val,org_is_add); if (OB_SUCCESS == err) { err = mutation.get_datetime(mutation_value.second_val,is_add); } if (OB_SUCCESS == err) { if (is_add) { value.second_val += mutation_value.second_val; } else { value.second_val = mutation_value.second_val; } set_datetime(value.second_val,is_add && (org_is_add || org_is_nop)); } break; case ObPreciseDateTimeType: if (ext_val_can_change || org_is_nop) { set_precise_datetime(0); } err = get_precise_datetime(value.microsecond_val,org_is_add); if (OB_SUCCESS == err) { err = mutation.get_precise_datetime(mutation_value.microsecond_val,is_add); } if (OB_SUCCESS == err) { if (is_add) { value.microsecond_val += mutation_value.microsecond_val; } else { value.microsecond_val = mutation_value.microsecond_val; } set_precise_datetime(value.microsecond_val,is_add && (org_is_add || org_is_nop)); } break; case ObExtendType: switch (mutation.get_ext()) { case ObActionFlag::OP_DEL_ROW: case ObActionFlag::OP_DEL_TABLE: /// used for join, if right row was deleted, set the cell to null set_null(); break; case ObActionFlag::OP_ROW_DOES_NOT_EXIST: /// do nothing break; case ObActionFlag::OP_NOP: if (get_ext() == ObActionFlag::OP_ROW_DOES_NOT_EXIST || get_ext() == ObActionFlag::OP_DEL_ROW) { set_null(); } break; default: TBSYS_LOG(ERROR,"unsupported ext value [value:%ld]", mutation.get_ext()); err = OB_INVALID_ARGUMENT; break; } break; case ObCreateTimeType: err = mutation.get_createtime(create_time); if (OB_SUCCESS == err) { if (ext_val_can_change || org_is_nop) { set_createtime(create_time); } } if (OB_SUCCESS == err) { err = get_createtime(value.create_time_val); } if (OB_SUCCESS == err) { err = mutation.get_createtime(mutation_value.create_time_val); } if (OB_SUCCESS == err) { set_createtime(std::min<ObCreateTime>(value.create_time_val,mutation_value.create_time_val)); } break; case ObModifyTimeType: err = mutation.get_modifytime(modify_time); if (OB_SUCCESS == err) { if (ext_val_can_change || org_is_nop) { set_modifytime(modify_time); } } if (OB_SUCCESS == err) { err = get_modifytime(value.modify_time_val); } if (OB_SUCCESS == err) { err = mutation.get_modifytime(mutation_value.modify_time_val); } if (OB_SUCCESS == err) { set_modifytime(std::max<ObCreateTime>(value.modify_time_val,mutation_value.modify_time_val)); } break; default: /* case ObSeqType: */ TBSYS_LOG(ERROR,"unsupported type [type:%d]", static_cast<int32_t>(mutation.get_type())); err = OB_INVALID_ARGUMENT; break; } } if(OB_SUCCESS != err) { TBSYS_LOG(WARN,"fail to apply [this->type:%ld,this->ext:%ld,mutation->type:%d,mutation->ext:%ld, err:%d]", org_type, org_ext, mutation.get_type(), mutation.get_ext(), err); } return err; }
int ObObj::compare(const ObObj &other) const { int cmp = 0; ObObjType this_type = get_type(); ObObjType other_type = other.get_type(); if (!can_compare(other)) { TBSYS_LOG(ERROR, "can not be compared, this_type=%d other_type=%d", get_type(), other.get_type()); cmp = this_type - other_type; } else { // compare principle : min value < null type < normal object < max value; if (is_min_value()) { // min value == min value and less than any else if (other.is_min_value()) { cmp = 0; } else { cmp = -1; } } else if (is_max_value()) { // max value == max value and great than any else if (other.is_max_value()) { cmp = 0; } else { cmp = 1; } } else if (this_type == ObNullType) { // null type == null type but less than any else type object. // null type > min type if (other.is_min_value()) { // null type > min value cmp = 1; } else if (other_type == ObNullType) { // null type == null type. cmp = 0; } else { // null type < any of normal object type. cmp = -1; } } else if (other.is_min_value() || other_type == ObNullType) { // any of normal type (except null type) > (min value & null type) cmp = 1; } else if (other.is_max_value()) { cmp = -1; } /* else if (this_type == ObNullType || other_type == ObNullType) { cmp = this_type - other_type; } */ else { // okay finally, two object are normal object type. cmp = this->compare_same_type(other); } } return cmp; }
int ObSqlReadParam::deserialize_basic_param(const char * buf, const int64_t data_len, int64_t & pos) { ObObj obj; int ret = OB_SUCCESS; int64_t int_value = 0; // read consistency if (OB_SUCCESS == ret) { ret = obj.deserialize(buf, data_len, pos); if (OB_SUCCESS == ret) { ret = obj.get_int(int_value); if (OB_SUCCESS == ret) { //is read consistency set_is_read_consistency(int_value); } else { TBSYS_LOG(WARN, "fail to get int. obj type=%d", obj.get_type()); } } else { TBSYS_LOG(WARN, "fail to deserialize obj. ret=%d", ret); } } // result cached if (OB_SUCCESS == ret) { ret = obj.deserialize(buf, data_len, pos); if (OB_SUCCESS == ret) { ret = obj.get_int(int_value); if (OB_SUCCESS == ret) { //is read consistency set_is_result_cached(int_value > 0); } else { TBSYS_LOG(WARN, "fail to get int. obj type=%d", obj.get_type()); } } else { TBSYS_LOG(WARN, "fail to deserialize obj. ret=%d", ret); } } // data_version if (OB_SUCCESS == ret) { ret = obj.deserialize(buf, data_len, pos); if (OB_SUCCESS == ret) { ret = obj.get_int(int_value); if (OB_SUCCESS == ret) { // data version set_data_version(int_value); } else { TBSYS_LOG(WARN, "fail to get int. obj type=%d", obj.get_type()); } } else { TBSYS_LOG(WARN, "fail to deserialize obj. ret=%d", ret); } } // table id if (OB_SUCCESS == ret) { ret = obj.deserialize(buf, data_len, pos); if (OB_SUCCESS == ret) { ret = obj.get_int(int_value); if (OB_SUCCESS == ret) { table_id_ = int_value; } else { TBSYS_LOG(WARN, "fail to get int. obj type=%d", obj.get_type()); } } else { TBSYS_LOG(WARN, "fail to deserialize obj. ret=%d", ret); } } // renamed table id if (OB_SUCCESS == ret) { ret = obj.deserialize(buf, data_len, pos); if (OB_SUCCESS == ret) { ret = obj.get_int(int_value); if (OB_SUCCESS == ret) { renamed_table_id_ = int_value; } else { TBSYS_LOG(WARN, "fail to get int. obj type=%d", obj.get_type()); } } else { TBSYS_LOG(WARN, "fail to deserialize obj. ret=%d", ret); } } if (OB_SUCCESS == ret) { if (OB_SUCCESS != (ret = obj.deserialize(buf, data_len, pos))) { TBSYS_LOG(WARN, "fail to deserialize obj:ret[%d]", ret); } else if (OB_SUCCESS != (ret = obj.get_bool(only_static_data_))) { TBSYS_LOG(WARN, "fail to get bool:ret[%d]", ret); } } return ret; }
bool ObObj::operator==(const ObObj &that_obj) const { bool result = false; int err = OB_SUCCESS; if ((get_type() == ObNullType) && (that_obj.get_type() == ObNullType)) { result = true; } else if ((get_type() == ObNullType) || (that_obj.get_type() == ObNullType)) { result = false; } else if (!can_compare(that_obj)) { result = false; TBSYS_LOG(ERROR, "logic error, cann't compare two obj with different type [this.type:%d," "that.type:%d]", get_type(), that_obj.get_type()); } else { _ObjValue this_value; _ObjValue that_value; int64_t this_timestamp = 0; int64_t that_timestamp = 0; ObObjType type = get_type(); switch (type) { case ObIntType: err = get_int(this_value.int_val); if (OB_SUCCESS == err) { err = that_obj.get_int(that_value.int_val); if (OB_SUCCESS == err) { result = this_value.int_val == that_value.int_val; } } break; case ObVarcharType: err = get_varchar(this_value.varchar_val); if (OB_SUCCESS == err) { err = that_obj.get_varchar(that_value.varchar_val); if (OB_SUCCESS == err) { result = this_value.varchar_val == that_value.varchar_val; } } break; case ObFloatType: err = get_float(this_value.float_val); if (OB_SUCCESS == err) { err = that_obj.get_float(that_value.float_val); if (OB_SUCCESS == err) { result = fabsf(this_value.float_val - that_value.float_val) < FLOAT_EPSINON; } } break; case ObDoubleType: err = get_double(this_value.double_val); if (OB_SUCCESS == err) { err = that_obj.get_double(that_value.double_val); if (OB_SUCCESS == err) { result = fabs(this_value.double_val - that_value.double_val) < DOUBLE_EPSINON; } } break; case ObDateTimeType: case ObPreciseDateTimeType: case ObCreateTimeType: case ObModifyTimeType: err = get_timestamp(this_timestamp); if (OB_SUCCESS == err) { err = that_obj.get_timestamp(that_timestamp); if (OB_SUCCESS == err) { result = this_timestamp == that_timestamp; } } break; /*case ObSeqType: /// @todo (wushi [email protected]) sequence break;*/ default: result = OB_ERR_UNEXPECTED; TBSYS_LOG(ERROR,"unexpected obj type [obj.type:%d]", type); break; } } return result; }