[Tarantool-patches] [PATCH v2 3/3] sql: replace MEM-type flags by enum mem_type

Mergen Imeev imeevma at tarantool.org
Mon May 24 13:56:30 MSK 2021


Hi! Thank you for the review! My answers and diff below.

On Fri, May 21, 2021 at 08:59:14PM +0200, Vladislav Shpilevoy wrote:
> Hi! Thanks for the fixes!
> 
> See 2 small comments below.
> 
> > diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
> > index b6ff6397f..f7788021d 100644
> > --- a/src/box/sql/mem.c
> > +++ b/src/box/sql/mem.c
> > @@ -1155,11 +1214,11 @@ mem_get_str0(const struct Mem *mem, const char **s)
> >  int
> >  mem_get_bin(const struct Mem *mem, const char **s)
> >  {
> > -	if ((mem->flags & MEM_Str) != 0) {
> > +	if (mem->type == MEM_TYPE_STR) {
> >  		*s = mem->n > 0 ? mem->z : NULL;
> >  		return 0;
> >  	}
> > -	if ((mem->flags & MEM_Blob) == 0 || (mem->flags & MEM_Zero) != 0)
> > +	if (mem->type != MEM_TYPE_BIN || (mem->flags & MEM_Zero) != 0)
> >  		return -1;
> 
> 1. AFAIR, it is not possible to have MEM_Zero for non-bin. Which makes
> the second condition unreachable, right? The same in mem_len().
> 
It is true that it should be not possible to have MEM_Zero flag for
non-VARBINARY value. However, here it is checked that the value is VARBINARY
and if it is, the flag is checked. I think there is nothing wrong here.

Still, In mem_len() I removed one condition and added assert instead.

> >  	*s = mem->z;
> >  	return 0;
> > @@ -1644,14 +1717,15 @@ mem_bit_not(const struct Mem *mem, struct Mem *result)
> >  		return -1;
> >  	}
> >  	result->u.i = ~i;
> > -	result->flags = result->u.i < 0 ? MEM_Int : MEM_UInt;
> > +	result->type = result->u.i < 0 ? MEM_TYPE_INT : MEM_TYPE_UINT;
> > +	assert(result->flags == 0);
> >  	return 0;
> >  }
> >  
> >  int
> >  mem_cmp_bool(const struct Mem *a, const struct Mem *b, int *result)
> >  {
> > -	if ((a->flags & b->flags & MEM_Bool) == 0)
> > +	if (a->type != MEM_TYPE_BOOL || b->type != MEM_TYPE_BOOL)
> 
> 2. You could leave the old way so as to avoid || (branching).
> 
Thanks, fixed. Also fixed in one more place.

> >  		return -1;
> >  	if (a->u.b == b->u.b)
> >  		*result = 0;


Diff:

diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index f7788021d..1bd70c37f 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1229,7 +1229,8 @@ mem_len(const struct Mem *mem, uint32_t *len)
 {
 	if (!mem_is_bytes(mem))
 		return -1;
-	if (mem->type == MEM_TYPE_BIN && (mem->flags & MEM_Zero) != 0)
+	assert((mem->flags & MEM_Zero) == 0 || mem->type == MEM_TYPE_BIN);
+	if ((mem->flags & MEM_Zero) != 0)
 		*len = mem->n + mem->u.nZero;
 	else
 		*len = mem->n;
@@ -1725,7 +1726,7 @@ mem_bit_not(const struct Mem *mem, struct Mem *result)
 int
 mem_cmp_bool(const struct Mem *a, const struct Mem *b, int *result)
 {
-	if (a->type != MEM_TYPE_BOOL || b->type != MEM_TYPE_BOOL)
+	if ((a->type & b->type & MEM_TYPE_BOOL) == 0)
 		return -1;
 	if (a->u.b == b->u.b)
 		*result = 0;
@@ -2345,7 +2346,7 @@ sqlMemCompare(const Mem * pMem1, const Mem * pMem2, const struct coll * pColl)
 		       (int)(type1 == MEM_TYPE_NULL);
 
 	if (((type1 | type2) & MEM_TYPE_BOOL) != 0) {
-		if (type1 == MEM_TYPE_BOOL && type2 == MEM_TYPE_BOOL) {
+		if ((type1 & type2 & MEM_TYPE_BOOL) != 0) {
 			if (pMem1->u.b == pMem2->u.b)
 				return 0;
 			if (pMem1->u.b)


More information about the Tarantool-patches mailing list