Github user wengyanqing commented on a diff in the pull request: https://github.com/apache/incubator-hawq/pull/1356#discussion_r184327414 --- Diff: contrib/vexecutor/tuplebatch.c --- @@ -93,91 +85,119 @@ tbSerializationSize(TupleBatch tb) //get skip tag size len += sizeof( bool ) * tb->nrows; + int vtypeSz = VTYPESIZE(tb->nrows); //get all un-null columns data size for(int i = 0;i < tb->ncols; i++ ) { if(tb->datagroup[i]) { len += sizeof(int); - len += vtypeSize(tb->datagroup[i]); + len += vtypeSz; } } return len; } -unsigned char * +MemTuple tbSerialization(TupleBatch tb ) { + MemTuple ret; size_t len = 0; size_t tmplen = 0; //calculate total size for TupleBatch size_t size = tbSerializationSize(tb); + size = (size + 0x8) & (~0x7); - unsigned char *buffer = palloc(size); + ret = palloc0(size); + unsigned char *buffer = ret->PRIVATE_mt_bits; //copy TupleBatch header memcpy(buffer,&size,sizeof(size_t)); + buffer += sizeof(size_t); tmplen = offsetof(TupleBatchData ,skip); - memcpy(buffer+len,tb,tmplen); - len += tmplen; + memcpy(buffer,tb,tmplen); + buffer +=tmplen; tmplen = sizeof(bool) * tb->nrows; - memcpy(buffer+len,tb->skip,tmplen); - len += tmplen; + memcpy(buffer,tb->skip,tmplen); + buffer += tmplen; for(int i = 0;i < tb->ncols; i++ ) { if(tb->datagroup[i]) { - memcpy(buffer+len,&i,sizeof(int)); - len += sizeof(int); + memcpy(buffer,&i,sizeof(int)); + buffer += sizeof(int); + + unsigned char* ptr = buffer; + memcpy(ptr,tb->datagroup[i],offsetof(vtype,isnull)); + ptr+= offsetof(vtype,isnull); + + tmplen = VDATUMSZ(tb->nrows); + memcpy(ptr,tb->datagroup[i]->values, tmplen); + ptr += tmplen; - tmplen = GetVFunc(tb->datagroup[i]->elemtype)->serialization(tb->datagroup[i],buffer + len); - len += tmplen; + tmplen = ISNULLSZ(tb->nrows); + memcpy(ptr,tb->datagroup[i]->isnull,tmplen); + buffer += VTYPESIZE(tb->nrows); } } - return buffer; + memtuple_set_size(ret,NULL,size); + return ret; } TupleBatch tbDeserialization(unsigned char *buffer) { size_t buflen; - memcpy(&buflen,buffer,sizeof(size_t)); + size_t len = 0; + size_t tmplen = 0; + tmplen = sizeof(size_t); + memcpy(&buflen,buffer,tmplen); + len += tmplen; if(buflen < sizeof(TupleBatchData)) return NULL; - size_t len = 0; - size_t tmplen = 0; TupleBatch tb = palloc0(sizeof(TupleBatchData)); //deserial tb main data tmplen = offsetof(TupleBatchData,skip); - memcpy(tb,buffer+len,tmplen); + memcpy(tb,buffer + len,tmplen); len += tmplen; //deserial member value -- skip if(tb->nrows != 0) { - tb->skip = palloc(sizeof(bool) * tb->nrows); + tmplen = sizeof(bool) * tb->nrows; + tb->skip = palloc(tmplen); memcpy(tb->skip,buffer+len,tmplen); len += tmplen; } //deserial member value -- datagroup if(tb->ncols != 0) { - tb->datagroup = palloc0(sizeof(vheader*) * tb->ncols); int colid; - while (len < buflen) + tmplen = sizeof(vtype*) * tb->ncols; + tb->datagroup = palloc0(tmplen); + while (((len + 0x8) & (~0x7)) < buflen) --- End diff -- What's the meaning of these magic number ? It's better to have a comment or use a literal const.
---