Re: Avoid mix char with bool type in comparisons - Mailing list pgsql-hackers

From Ranier Vilela
Subject Re: Avoid mix char with bool type in comparisons
Date
Msg-id CAEudQAp9NvraQuRJeSc28heUs+18908wXguo4DDyiGMKpMGf9A@mail.gmail.com
Whole thread Raw
In response to Re: Avoid mix char with bool type in comparisons  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Avoid mix char with bool type in comparisons  (Ranier Vilela <ranier.vf@gmail.com>)
List pgsql-hackers
Em sex., 7 de out. de 2022 às 14:44, Tom Lane <tgl@sss.pgh.pa.us> escreveu:
Ranier Vilela <ranier.vf@gmail.com> writes:
> Anyway, it's not *true* that  collectMatchesForHeapRow deals
> only "true" and "false", once that key->entryRes is initialized with
> GIN_FALSE not false.

Look: GinTernaryValue is carefully designed so that it is
representation-compatible with bool, including that GIN_FALSE is
identical to false and GIN_TRUE is identical to true.  I'm quite
uninterested in debating whether that's a good idea or not.
Moreover, there are a ton of other places besides this one where
the GIN code relies on that equivalence, so we'd have to do a
lot more than what's in this patch if we wanted to decouple that.
Just now I checked all the places where GinTernaryValue is used and they all trust using GIN_TRUE, GIN_FALSE and GIN_MAYBE.
The only problematic place I found was at ginget.c and jsonb_gin.c

jsonb_gin.c:
The function execute_jsp_gin_node returns GIN_TRUE if the node->type is JSP_GIN_ENTRY and the value is GIN_MAYBE.
IMO, it should be GIN_FALSE?

case JSP_GIN_ENTRY:
{
int index = node->val.entryIndex;

if (ternary)
return ((GinTernaryValue *) check)[index];
else
return ((bool *) check)[index] ? GIN_TRUE : GIN_FALSE;
}

The array entryRes is used only in ginget.c and ginscan.c.

The GinTernaryValue is used only in:
File contrib\pg_trgm\trgm_gin.c:
File src\backend\access\gin\ginarrayproc.c:
File src\backend\access\gin\ginget.c:
File src\backend\access\gin\ginlogic.c:
File src\backend\access\gin\ginscan.c:
File src\backend\utils\adt\jsonb_gin.c:
File src\backend\utils\adt\tsginidx.c:

regards,
Ranier Vilela

pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: [BUG] parenting a PK constraint to a self-FK one (Was: Self FK oddity when attaching a partition)
Next
From: Andres Freund
Date:
Subject: Re: START_REPLICATION SLOT causing a crash in an assert build