Skip to content

Commit 50239ea

Browse files
committed
Prefer the anticipated node_funcs file for common code
1 parent ceefe6a commit 50239ea

File tree

6 files changed

+66
-71
lines changed

6 files changed

+66
-71
lines changed

pg_diffix/node_funcs.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef PG_DIFFIX_NODE_FUNCS_H
2+
#define PG_DIFFIX_NODE_FUNCS_H
3+
4+
#include "nodes/params.h"
5+
#include "nodes/primnodes.h"
6+
7+
/*
8+
* Returns `true` if the node represents a constant from pg_diffix perspective, i.e. `1` or `$1`.
9+
*/
10+
extern bool is_simple_constant(Node *node);
11+
12+
extern void get_simple_constant_typed_value(Node *node, ParamListInfo bound_params, Oid *type, Datum *value, bool *isnull);
13+
14+
#endif /* PG_DIFFIX_NODE_FUNCS_H */

pg_diffix/query/anonymization.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "pg_diffix/aggregation/common.h"
88
#include "pg_diffix/aggregation/noise.h"
9+
#include "pg_diffix/node_funcs.h"
910

1011
/*
1112
* Opaque struct containing references to anonymizing (sub)queries.

pg_diffix/utils.h

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#ifndef PG_DIFFIX_UTILS_H
22
#define PG_DIFFIX_UTILS_H
33

4-
#include "nodes/params.h"
54
#include "nodes/pg_list.h"
6-
#include "nodes/primnodes.h"
75
#include "utils/datum.h"
86

97
/*-------------------------------------------------------------------------
@@ -121,68 +119,4 @@ static inline List *hash_set_union(List *dst_set, const List *src_set)
121119

122120
#endif
123121

124-
/*-------------------------------------------------------------------------
125-
* Node utils
126-
*-------------------------------------------------------------------------
127-
*/
128-
129-
static inline ParamExternData *get_param_data(ParamListInfo bound_params, int one_based_paramid)
130-
{
131-
#if PG_MAJORVERSION_NUM == 13
132-
int paramid = one_based_paramid;
133-
#else
134-
int paramid = one_based_paramid - 1;
135-
#endif
136-
if (bound_params->paramFetch != NULL)
137-
return bound_params->paramFetch(bound_params, paramid, true, NULL);
138-
else
139-
return &bound_params->params[paramid];
140-
}
141-
142-
static inline bool is_simple_constant(Node *node)
143-
{
144-
return IsA(node, Const) || (IsA(node, Param) && ((Param *)node)->paramkind == PARAM_EXTERN);
145-
}
146-
147-
static inline void get_simple_constant_typed_value(Node *node, ParamListInfo bound_params, Oid *type, Datum *value, bool *isnull)
148-
{
149-
if (IsA(node, Const))
150-
{
151-
Const *const_expr = (Const *)node;
152-
*type = const_expr->consttype;
153-
*value = const_expr->constvalue;
154-
*isnull = const_expr->constisnull;
155-
}
156-
else if (IsA(node, Param) && ((Param *)node)->paramkind == PARAM_EXTERN)
157-
{
158-
Param *param_expr = (Param *)node;
159-
ParamExternData *param_data = get_param_data(bound_params, param_expr->paramid);
160-
*type = param_data->ptype;
161-
*value = param_data->value;
162-
*isnull = param_data->isnull;
163-
}
164-
else
165-
{
166-
FAILWITH("Attempted to get simple constant value of non-Const, non-PARAM_EXTERN node");
167-
}
168-
}
169-
170-
static inline int get_simple_constant_location(Node *node)
171-
{
172-
if (IsA(node, Const))
173-
{
174-
Const *const_expr = (Const *)node;
175-
return const_expr->location;
176-
}
177-
else if (IsA(node, Param) && ((Param *)node)->paramkind == PARAM_EXTERN)
178-
{
179-
Param *param_expr = (Param *)node;
180-
return param_expr->location;
181-
}
182-
else
183-
{
184-
FAILWITH("Attempted to get simple constant value of non-Const, non-PARAM_EXTERN node");
185-
}
186-
}
187-
188122
#endif /* PG_DIFFIX_UTILS_H */

src/node_funcs.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "postgres.h"
2+
3+
#include "pg_diffix/node_funcs.h"
4+
#include "pg_diffix/utils.h"
5+
6+
static ParamExternData *get_param_data(ParamListInfo bound_params, int one_based_paramid)
7+
{
8+
#if PG_MAJORVERSION_NUM == 13
9+
int paramid = one_based_paramid;
10+
#else
11+
int paramid = one_based_paramid - 1;
12+
#endif
13+
if (bound_params->paramFetch != NULL)
14+
return bound_params->paramFetch(bound_params, paramid, true, NULL);
15+
else
16+
return &bound_params->params[paramid];
17+
}
18+
19+
bool is_simple_constant(Node *node)
20+
{
21+
return IsA(node, Const) || (IsA(node, Param) && ((Param *)node)->paramkind == PARAM_EXTERN);
22+
}
23+
24+
void get_simple_constant_typed_value(Node *node, ParamListInfo bound_params, Oid *type, Datum *value, bool *isnull)
25+
{
26+
if (IsA(node, Const))
27+
{
28+
Const *const_expr = (Const *)node;
29+
*type = const_expr->consttype;
30+
*value = const_expr->constvalue;
31+
*isnull = const_expr->constisnull;
32+
}
33+
else if (IsA(node, Param) && ((Param *)node)->paramkind == PARAM_EXTERN)
34+
{
35+
Param *param_expr = (Param *)node;
36+
ParamExternData *param_data = get_param_data(bound_params, param_expr->paramid);
37+
*type = param_data->ptype;
38+
*value = param_data->value;
39+
*isnull = param_data->isnull;
40+
}
41+
else
42+
{
43+
FAILWITH("Attempted to get simple constant value of non-Const, non-PARAM_EXTERN node");
44+
}
45+
}

src/query/anonymization.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ static bool collect_seed_material(Node *node, CollectMaterialContext *context)
365365
get_simple_constant_typed_value(node, context->bound_params, &type, &value, &isnull);
366366

367367
if (!is_supported_numeric_type(type))
368-
FAILWITH_LOCATION(get_simple_constant_location(node), "Unsupported constant type used in bucket definition!");
368+
FAILWITH_LOCATION(exprLocation(node), "Unsupported constant type used in bucket definition!");
369369

370370
double const_as_double = numeric_value_to_double(type, value);
371371
char const_as_string[DOUBLE_SHORTEST_DECIMAL_LEN];

src/query/validation.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "utils/lsyscache.h"
1616

1717
#include "pg_diffix/auth.h"
18+
#include "pg_diffix/node_funcs.h"
1819
#include "pg_diffix/oid_cache.h"
1920
#include "pg_diffix/query/allowed_objects.h"
2021
#include "pg_diffix/query/validation.h"
@@ -238,7 +239,7 @@ static void verify_bucket_expression(Node *node)
238239
}
239240
else if (is_simple_constant(node))
240241
{
241-
FAILWITH_LOCATION(get_simple_constant_location(node), "Simple constants are not allowed as bucket expressions.");
242+
FAILWITH_LOCATION(exprLocation(node), "Simple constants are not allowed as bucket expressions.");
242243
}
243244
else if (IsA(node, RelabelType))
244245
{
@@ -273,7 +274,7 @@ static void verify_substring(FuncExpr *func_expr, ParamListInfo bound_params)
273274
get_simple_constant_typed_value(node, bound_params, &type, &value, &isnull);
274275

275276
if (DatumGetUInt32(value) != 1)
276-
FAILWITH_LOCATION(get_simple_constant_location(node), "Generalization used in the query is not allowed in untrusted access level.");
277+
FAILWITH_LOCATION(exprLocation(node), "Generalization used in the query is not allowed in untrusted access level.");
277278
}
278279

279280
/* money-style numbers, i.e. 1, 2, or 5 preceeded by or followed by zeros: ⟨... 0.1, 0.2, 0.5, 1, 2, 5, 10, ...⟩ */
@@ -300,10 +301,10 @@ static void verify_bin_size(Node *range_expr, ParamListInfo bound_params)
300301
get_simple_constant_typed_value(range_node, bound_params, &type, &value, &isnull);
301302

302303
if (!is_supported_numeric_type(type))
303-
FAILWITH_LOCATION(get_simple_constant_location(range_node), "Unsupported constant type used in generalization.");
304+
FAILWITH_LOCATION(exprLocation(range_node), "Unsupported constant type used in generalization.");
304305

305306
if (!is_money_style(numeric_value_to_double(type, value)))
306-
FAILWITH_LOCATION(get_simple_constant_location(range_node), "Generalization used in the query is not allowed in untrusted access level.");
307+
FAILWITH_LOCATION(exprLocation(range_node), "Generalization used in the query is not allowed in untrusted access level.");
307308
}
308309

309310
static void verify_generalization(Node *node, ParamListInfo bound_params)

0 commit comments

Comments
 (0)