Skip to content

Commit 941460f

Browse files
committed
Add Boolean node
Before, SQL-level boolean constants were represented by a string with a cast, and internal Boolean values in DDL commands were usually represented by Integer nodes. This takes the place of both of these uses, making the intent clearer and having some amount of type safety. Reviewed-by: Pavel Stehule <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
1 parent ca86a63 commit 941460f

File tree

20 files changed

+211
-128
lines changed

20 files changed

+211
-128
lines changed

contrib/postgres_fdw/postgres_fdw.c

+16-16
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ enum FdwModifyPrivateIndex
103103
FdwModifyPrivateTargetAttnums,
104104
/* Length till the end of VALUES clause (as an Integer node) */
105105
FdwModifyPrivateLen,
106-
/* has-returning flag (as an Integer node) */
106+
/* has-returning flag (as a Boolean node) */
107107
FdwModifyPrivateHasReturning,
108108
/* Integer list of attribute numbers retrieved by RETURNING */
109109
FdwModifyPrivateRetrievedAttrs
@@ -122,11 +122,11 @@ enum FdwDirectModifyPrivateIndex
122122
{
123123
/* SQL statement to execute remotely (as a String node) */
124124
FdwDirectModifyPrivateUpdateSql,
125-
/* has-returning flag (as an Integer node) */
125+
/* has-returning flag (as a Boolean node) */
126126
FdwDirectModifyPrivateHasReturning,
127127
/* Integer list of attribute numbers retrieved by RETURNING */
128128
FdwDirectModifyPrivateRetrievedAttrs,
129-
/* set-processed flag (as an Integer node) */
129+
/* set-processed flag (as a Boolean node) */
130130
FdwDirectModifyPrivateSetProcessed
131131
};
132132

@@ -280,9 +280,9 @@ typedef struct PgFdwAnalyzeState
280280
*/
281281
enum FdwPathPrivateIndex
282282
{
283-
/* has-final-sort flag (as an Integer node) */
283+
/* has-final-sort flag (as a Boolean node) */
284284
FdwPathPrivateHasFinalSort,
285-
/* has-limit flag (as an Integer node) */
285+
/* has-limit flag (as a Boolean node) */
286286
FdwPathPrivateHasLimit
287287
};
288288

@@ -1245,9 +1245,9 @@ postgresGetForeignPlan(PlannerInfo *root,
12451245
*/
12461246
if (best_path->fdw_private)
12471247
{
1248-
has_final_sort = intVal(list_nth(best_path->fdw_private,
1248+
has_final_sort = boolVal(list_nth(best_path->fdw_private,
12491249
FdwPathPrivateHasFinalSort));
1250-
has_limit = intVal(list_nth(best_path->fdw_private,
1250+
has_limit = boolVal(list_nth(best_path->fdw_private,
12511251
FdwPathPrivateHasLimit));
12521252
}
12531253

@@ -1879,7 +1879,7 @@ postgresPlanForeignModify(PlannerInfo *root,
18791879
return list_make5(makeString(sql.data),
18801880
targetAttrs,
18811881
makeInteger(values_end_len),
1882-
makeInteger((retrieved_attrs != NIL)),
1882+
makeBoolean((retrieved_attrs != NIL)),
18831883
retrieved_attrs);
18841884
}
18851885

@@ -1916,7 +1916,7 @@ postgresBeginForeignModify(ModifyTableState *mtstate,
19161916
FdwModifyPrivateTargetAttnums);
19171917
values_end_len = intVal(list_nth(fdw_private,
19181918
FdwModifyPrivateLen));
1919-
has_returning = intVal(list_nth(fdw_private,
1919+
has_returning = boolVal(list_nth(fdw_private,
19201920
FdwModifyPrivateHasReturning));
19211921
retrieved_attrs = (List *) list_nth(fdw_private,
19221922
FdwModifyPrivateRetrievedAttrs);
@@ -2567,9 +2567,9 @@ postgresPlanDirectModify(PlannerInfo *root,
25672567
* Items in the list must match enum FdwDirectModifyPrivateIndex, above.
25682568
*/
25692569
fscan->fdw_private = list_make4(makeString(sql.data),
2570-
makeInteger((retrieved_attrs != NIL)),
2570+
makeBoolean((retrieved_attrs != NIL)),
25712571
retrieved_attrs,
2572-
makeInteger(plan->canSetTag));
2572+
makeBoolean(plan->canSetTag));
25732573

25742574
/*
25752575
* Update the foreign-join-related fields.
@@ -2667,11 +2667,11 @@ postgresBeginDirectModify(ForeignScanState *node, int eflags)
26672667
/* Get private info created by planner functions. */
26682668
dmstate->query = strVal(list_nth(fsplan->fdw_private,
26692669
FdwDirectModifyPrivateUpdateSql));
2670-
dmstate->has_returning = intVal(list_nth(fsplan->fdw_private,
2670+
dmstate->has_returning = boolVal(list_nth(fsplan->fdw_private,
26712671
FdwDirectModifyPrivateHasReturning));
26722672
dmstate->retrieved_attrs = (List *) list_nth(fsplan->fdw_private,
26732673
FdwDirectModifyPrivateRetrievedAttrs);
2674-
dmstate->set_processed = intVal(list_nth(fsplan->fdw_private,
2674+
dmstate->set_processed = boolVal(list_nth(fsplan->fdw_private,
26752675
FdwDirectModifyPrivateSetProcessed));
26762676

26772677
/* Create context for per-tuple temp workspace. */
@@ -6566,7 +6566,7 @@ add_foreign_ordered_paths(PlannerInfo *root, RelOptInfo *input_rel,
65666566
* Build the fdw_private list that will be used by postgresGetForeignPlan.
65676567
* Items in the list must match order in enum FdwPathPrivateIndex.
65686568
*/
6569-
fdw_private = list_make2(makeInteger(true), makeInteger(false));
6569+
fdw_private = list_make2(makeBoolean(true), makeBoolean(false));
65706570

65716571
/* Create foreign ordering path */
65726572
ordered_path = create_foreign_upper_path(root,
@@ -6797,8 +6797,8 @@ add_foreign_final_paths(PlannerInfo *root, RelOptInfo *input_rel,
67976797
* Build the fdw_private list that will be used by postgresGetForeignPlan.
67986798
* Items in the list must match order in enum FdwPathPrivateIndex.
67996799
*/
6800-
fdw_private = list_make2(makeInteger(has_final_sort),
6801-
makeInteger(extra->limit_needed));
6800+
fdw_private = list_make2(makeBoolean(has_final_sort),
6801+
makeBoolean(extra->limit_needed));
68026802

68036803
/*
68046804
* Create foreign final path; this gets rid of a no-longer-needed outer

src/backend/commands/define.c

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ defGetString(DefElem *def)
5959
return psprintf("%ld", (long) intVal(def->arg));
6060
case T_Float:
6161
return castNode(Float, def->arg)->fval;
62+
case T_Boolean:
63+
return boolVal(def->arg) ? "true" : "false";
6264
case T_String:
6365
return strVal(def->arg);
6466
case T_TypeName:

src/backend/commands/functioncmds.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -813,15 +813,15 @@ compute_function_attributes(ParseState *pstate,
813813
if (transform_item)
814814
*transform = transform_item->arg;
815815
if (windowfunc_item)
816-
*windowfunc_p = intVal(windowfunc_item->arg);
816+
*windowfunc_p = boolVal(windowfunc_item->arg);
817817
if (volatility_item)
818818
*volatility_p = interpret_func_volatility(volatility_item);
819819
if (strict_item)
820-
*strict_p = intVal(strict_item->arg);
820+
*strict_p = boolVal(strict_item->arg);
821821
if (security_item)
822-
*security_definer = intVal(security_item->arg);
822+
*security_definer = boolVal(security_item->arg);
823823
if (leakproof_item)
824-
*leakproof_p = intVal(leakproof_item->arg);
824+
*leakproof_p = boolVal(leakproof_item->arg);
825825
if (set_items)
826826
*proconfig = update_proconfig_value(NULL, set_items);
827827
if (cost_item)
@@ -1417,12 +1417,12 @@ AlterFunction(ParseState *pstate, AlterFunctionStmt *stmt)
14171417
if (volatility_item)
14181418
procForm->provolatile = interpret_func_volatility(volatility_item);
14191419
if (strict_item)
1420-
procForm->proisstrict = intVal(strict_item->arg);
1420+
procForm->proisstrict = boolVal(strict_item->arg);
14211421
if (security_def_item)
1422-
procForm->prosecdef = intVal(security_def_item->arg);
1422+
procForm->prosecdef = boolVal(security_def_item->arg);
14231423
if (leakproof_item)
14241424
{
1425-
procForm->proleakproof = intVal(leakproof_item->arg);
1425+
procForm->proleakproof = boolVal(leakproof_item->arg);
14261426
if (procForm->proleakproof && !superuser())
14271427
ereport(ERROR,
14281428
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),

src/backend/commands/sequence.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ init_params(ParseState *pstate, List *options, bool for_identity,
14011401
/* CYCLE */
14021402
if (is_cycled != NULL)
14031403
{
1404-
seqform->seqcycle = intVal(is_cycled->arg);
1404+
seqform->seqcycle = boolVal(is_cycled->arg);
14051405
Assert(BoolIsValid(seqform->seqcycle));
14061406
seqdataform->log_cnt = 0;
14071407
}
@@ -1739,7 +1739,7 @@ sequence_options(Oid relid)
17391739
options = lappend(options,
17401740
makeDefElem("cache", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqcache)), -1));
17411741
options = lappend(options,
1742-
makeDefElem("cycle", (Node *) makeInteger(pgsform->seqcycle), -1));
1742+
makeDefElem("cycle", (Node *) makeBoolean(pgsform->seqcycle), -1));
17431743
options = lappend(options,
17441744
makeDefElem("increment", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqincrement)), -1));
17451745
options = lappend(options,

src/backend/commands/tsearchcmds.c

+9
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,15 @@ buildDefItem(const char *name, const char *val, bool was_quoted)
17421742
return makeDefElem(pstrdup(name),
17431743
(Node *) makeFloat(pstrdup(val)),
17441744
-1);
1745+
1746+
if (strcmp(val, "true") == 0)
1747+
return makeDefElem(pstrdup(name),
1748+
(Node *) makeBoolean(true),
1749+
-1);
1750+
if (strcmp(val, "false") == 0)
1751+
return makeDefElem(pstrdup(name),
1752+
(Node *) makeBoolean(false),
1753+
-1);
17451754
}
17461755
/* Just make it a string */
17471756
return makeDefElem(pstrdup(name),

src/backend/commands/user.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,17 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
217217
if (dpassword && dpassword->arg)
218218
password = strVal(dpassword->arg);
219219
if (dissuper)
220-
issuper = intVal(dissuper->arg) != 0;
220+
issuper = boolVal(dissuper->arg);
221221
if (dinherit)
222-
inherit = intVal(dinherit->arg) != 0;
222+
inherit = boolVal(dinherit->arg);
223223
if (dcreaterole)
224-
createrole = intVal(dcreaterole->arg) != 0;
224+
createrole = boolVal(dcreaterole->arg);
225225
if (dcreatedb)
226-
createdb = intVal(dcreatedb->arg) != 0;
226+
createdb = boolVal(dcreatedb->arg);
227227
if (dcanlogin)
228-
canlogin = intVal(dcanlogin->arg) != 0;
228+
canlogin = boolVal(dcanlogin->arg);
229229
if (disreplication)
230-
isreplication = intVal(disreplication->arg) != 0;
230+
isreplication = boolVal(disreplication->arg);
231231
if (dconnlimit)
232232
{
233233
connlimit = intVal(dconnlimit->arg);
@@ -245,7 +245,7 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
245245
if (dvalidUntil)
246246
validUntil = strVal(dvalidUntil->arg);
247247
if (dbypassRLS)
248-
bypassrls = intVal(dbypassRLS->arg) != 0;
248+
bypassrls = boolVal(dbypassRLS->arg);
249249

250250
/* Check some permissions first */
251251
if (issuper)
@@ -700,37 +700,37 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
700700
*/
701701
if (dissuper)
702702
{
703-
new_record[Anum_pg_authid_rolsuper - 1] = BoolGetDatum(intVal(dissuper->arg));
703+
new_record[Anum_pg_authid_rolsuper - 1] = BoolGetDatum(boolVal(dissuper->arg));
704704
new_record_repl[Anum_pg_authid_rolsuper - 1] = true;
705705
}
706706

707707
if (dinherit)
708708
{
709-
new_record[Anum_pg_authid_rolinherit - 1] = BoolGetDatum(intVal(dinherit->arg));
709+
new_record[Anum_pg_authid_rolinherit - 1] = BoolGetDatum(boolVal(dinherit->arg));
710710
new_record_repl[Anum_pg_authid_rolinherit - 1] = true;
711711
}
712712

713713
if (dcreaterole)
714714
{
715-
new_record[Anum_pg_authid_rolcreaterole - 1] = BoolGetDatum(intVal(dcreaterole->arg));
715+
new_record[Anum_pg_authid_rolcreaterole - 1] = BoolGetDatum(boolVal(dcreaterole->arg));
716716
new_record_repl[Anum_pg_authid_rolcreaterole - 1] = true;
717717
}
718718

719719
if (dcreatedb)
720720
{
721-
new_record[Anum_pg_authid_rolcreatedb - 1] = BoolGetDatum(intVal(dcreatedb->arg));
721+
new_record[Anum_pg_authid_rolcreatedb - 1] = BoolGetDatum(boolVal(dcreatedb->arg));
722722
new_record_repl[Anum_pg_authid_rolcreatedb - 1] = true;
723723
}
724724

725725
if (dcanlogin)
726726
{
727-
new_record[Anum_pg_authid_rolcanlogin - 1] = BoolGetDatum(intVal(dcanlogin->arg));
727+
new_record[Anum_pg_authid_rolcanlogin - 1] = BoolGetDatum(boolVal(dcanlogin->arg));
728728
new_record_repl[Anum_pg_authid_rolcanlogin - 1] = true;
729729
}
730730

731731
if (disreplication)
732732
{
733-
new_record[Anum_pg_authid_rolreplication - 1] = BoolGetDatum(intVal(disreplication->arg));
733+
new_record[Anum_pg_authid_rolreplication - 1] = BoolGetDatum(boolVal(disreplication->arg));
734734
new_record_repl[Anum_pg_authid_rolreplication - 1] = true;
735735
}
736736

@@ -779,7 +779,7 @@ AlterRole(ParseState *pstate, AlterRoleStmt *stmt)
779779

780780
if (dbypassRLS)
781781
{
782-
new_record[Anum_pg_authid_rolbypassrls - 1] = BoolGetDatum(intVal(dbypassRLS->arg));
782+
new_record[Anum_pg_authid_rolbypassrls - 1] = BoolGetDatum(boolVal(dbypassRLS->arg));
783783
new_record_repl[Anum_pg_authid_rolbypassrls - 1] = true;
784784
}
785785

src/backend/nodes/copyfuncs.c

+16
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,9 @@ _copyA_Const(const A_Const *from)
27502750
case T_Float:
27512751
COPY_STRING_FIELD(val.fval.fval);
27522752
break;
2753+
case T_Boolean:
2754+
COPY_SCALAR_FIELD(val.boolval.boolval);
2755+
break;
27532756
case T_String:
27542757
COPY_STRING_FIELD(val.sval.sval);
27552758
break;
@@ -4949,6 +4952,16 @@ _copyFloat(const Float *from)
49494952
return newnode;
49504953
}
49514954

4955+
static Boolean *
4956+
_copyBoolean(const Boolean *from)
4957+
{
4958+
Boolean *newnode = makeNode(Boolean);
4959+
4960+
COPY_SCALAR_FIELD(boolval);
4961+
4962+
return newnode;
4963+
}
4964+
49524965
static String *
49534966
_copyString(const String *from)
49544967
{
@@ -5356,6 +5369,9 @@ copyObjectImpl(const void *from)
53565369
case T_Float:
53575370
retval = _copyFloat(from);
53585371
break;
5372+
case T_Boolean:
5373+
retval = _copyBoolean(from);
5374+
break;
53595375
case T_String:
53605376
retval = _copyString(from);
53615377
break;

src/backend/nodes/equalfuncs.c

+11
Original file line numberDiff line numberDiff line change
@@ -3138,6 +3138,14 @@ _equalFloat(const Float *a, const Float *b)
31383138
return true;
31393139
}
31403140

3141+
static bool
3142+
_equalBoolean(const Boolean *a, const Boolean *b)
3143+
{
3144+
COMPARE_SCALAR_FIELD(boolval);
3145+
3146+
return true;
3147+
}
3148+
31413149
static bool
31423150
_equalString(const String *a, const String *b)
31433151
{
@@ -3374,6 +3382,9 @@ equal(const void *a, const void *b)
33743382
case T_Float:
33753383
retval = _equalFloat(a, b);
33763384
break;
3385+
case T_Boolean:
3386+
retval = _equalBoolean(a, b);
3387+
break;
33773388
case T_String:
33783389
retval = _equalString(a, b);
33793390
break;

src/backend/nodes/nodeFuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -3577,6 +3577,7 @@ raw_expression_tree_walker(Node *node,
35773577
case T_SQLValueFunction:
35783578
case T_Integer:
35793579
case T_Float:
3580+
case T_Boolean:
35803581
case T_String:
35813582
case T_BitString:
35823583
case T_ParamRef:

src/backend/nodes/outfuncs.c

+8
Original file line numberDiff line numberDiff line change
@@ -3434,6 +3434,12 @@ _outFloat(StringInfo str, const Float *node)
34343434
appendStringInfoString(str, node->fval);
34353435
}
34363436

3437+
static void
3438+
_outBoolean(StringInfo str, const Boolean *node)
3439+
{
3440+
appendStringInfoString(str, node->boolval ? "true" : "false");
3441+
}
3442+
34373443
static void
34383444
_outString(StringInfo str, const String *node)
34393445
{
@@ -3846,6 +3852,8 @@ outNode(StringInfo str, const void *obj)
38463852
_outInteger(str, (Integer *) obj);
38473853
else if (IsA(obj, Float))
38483854
_outFloat(str, (Float *) obj);
3855+
else if (IsA(obj, Boolean))
3856+
_outBoolean(str, (Boolean *) obj);
38493857
else if (IsA(obj, String))
38503858
_outString(str, (String *) obj);
38513859
else if (IsA(obj, BitString))

0 commit comments

Comments
 (0)