Skip to content

Commit abd28c7

Browse files
committed
Fixes to parametrized generalization expressions after review
1 parent 8464d51 commit abd28c7

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

pg_diffix/node_funcs.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
/*
88
* Returns `true` if the node represents a constant from pg_diffix perspective, i.e. `1` or `$1`.
99
*/
10-
extern bool is_simple_constant(Node *node);
10+
extern bool is_stable_expression(Node *node);
1111

12-
extern void get_simple_constant_typed_value(Node *node, ParamListInfo bound_params, Oid *type, Datum *value, bool *isnull);
12+
/*
13+
* Fills `type`, `value` and `isnull` with what the stable expression `node` holds.
14+
* `bound_params` must be provided, since `node` might be a `Param` node.
15+
*/
16+
extern void get_stable_expression_value(Node *node, ParamListInfo bound_params, Oid *type, Datum *value, bool *isnull);
1317

1418
#endif /* PG_DIFFIX_NODE_FUNCS_H */

src/node_funcs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ static ParamExternData *get_param_data(ParamListInfo bound_params, int one_based
1111
return &bound_params->params[one_based_paramid - 1];
1212
}
1313

14-
bool is_simple_constant(Node *node)
14+
bool is_stable_expression(Node *node)
1515
{
1616
return IsA(node, Const) || (IsA(node, Param) && ((Param *)node)->paramkind == PARAM_EXTERN);
1717
}
1818

19-
void get_simple_constant_typed_value(Node *node, ParamListInfo bound_params, Oid *type, Datum *value, bool *isnull)
19+
void get_stable_expression_value(Node *node, ParamListInfo bound_params, Oid *type, Datum *value, bool *isnull)
2020
{
2121
if (IsA(node, Const))
2222
{

src/query/anonymization.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,12 @@ static bool collect_seed_material(Node *node, CollectMaterialContext *context)
378378
append_seed_material(context->material, attribute_name, '.');
379379
}
380380

381-
if (is_simple_constant(node))
381+
if (is_stable_expression(node))
382382
{
383383
Oid type;
384384
Datum value;
385385
bool isnull;
386-
get_simple_constant_typed_value(node, context->bound_params, &type, &value, &isnull);
386+
get_stable_expression_value(node, context->bound_params, &type, &value, &isnull);
387387

388388
if (!is_supported_numeric_type(type))
389389
FAILWITH_LOCATION(exprLocation(node), "Unsupported constant type used in bucket definition!");

src/query/validation.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ static void verify_bucket_expression(Node *node)
228228

229229
for (int i = 1; i < list_length(func_expr->args); i++)
230230
{
231-
if (!is_simple_constant(unwrap_cast((Node *)list_nth(func_expr->args, i))))
231+
if (!is_stable_expression(unwrap_cast((Node *)list_nth(func_expr->args, i))))
232232
FAILWITH_LOCATION(func_expr->location, "Non-primary arguments for a bucket function have to be simple constants.");
233233
}
234234
}
@@ -237,7 +237,7 @@ static void verify_bucket_expression(Node *node)
237237
OpExpr *op_expr = (OpExpr *)node;
238238
FAILWITH_LOCATION(op_expr->location, "Use of operators to define buckets is not supported.");
239239
}
240-
else if (is_simple_constant(node))
240+
else if (is_stable_expression(node))
241241
{
242242
FAILWITH_LOCATION(exprLocation(node), "Simple constants are not allowed as bucket expressions.");
243243
}
@@ -267,11 +267,11 @@ static void verify_bucket_expression(Node *node)
267267
static void verify_substring(FuncExpr *func_expr, ParamListInfo bound_params)
268268
{
269269
Node *node = unwrap_cast(list_nth(func_expr->args, 1));
270-
Assert(is_simple_constant(node)); /* Checked by prior validations */
270+
Assert(is_stable_expression(node)); /* Checked by prior validations */
271271
Oid type;
272272
Datum value;
273273
bool isnull;
274-
get_simple_constant_typed_value(node, bound_params, &type, &value, &isnull);
274+
get_stable_expression_value(node, bound_params, &type, &value, &isnull);
275275

276276
if (DatumGetUInt32(value) != 1)
277277
FAILWITH_LOCATION(exprLocation(node), "Generalization used in the query is not allowed in untrusted access level.");
@@ -294,11 +294,11 @@ static bool is_money_style(double number)
294294
static void verify_bin_size(Node *range_expr, ParamListInfo bound_params)
295295
{
296296
Node *range_node = unwrap_cast(range_expr);
297-
Assert(is_simple_constant(range_node)); /* Checked by prior validations */
297+
Assert(is_stable_expression(range_node)); /* Checked by prior validations */
298298
Oid type;
299299
Datum value;
300300
bool isnull;
301-
get_simple_constant_typed_value(range_node, bound_params, &type, &value, &isnull);
301+
get_stable_expression_value(range_node, bound_params, &type, &value, &isnull);
302302

303303
if (!is_supported_numeric_type(type))
304304
FAILWITH_LOCATION(exprLocation(range_node), "Unsupported constant type used in generalization.");

test/expected/noiseless.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,14 @@ SELECT discount, COUNT(DISTINCT id) FROM test_customers GROUP BY 1;
231231
2 | 4
232232
(4 rows)
233233

234+
----------------------------------------------------------------
235+
-- Prepared statements
236+
----------------------------------------------------------------
237+
PREPARE prepared_floor_by(numeric) AS SELECT diffix.floor_by(discount, $1), count(*) FROM test_customers GROUP BY 1;
238+
EXECUTE prepared_floor_by(2.0);
239+
floor_by | count
240+
----------+-------
241+
0 | 13
242+
2 | 4
243+
(2 rows)
244+

test/sql/noiseless.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,10 @@ SELECT COUNT(DISTINCT planet) FROM test_customers;
7474
-- `low_count_min_threshold` for queries with GROUP BY
7575
SELECT city, COUNT(DISTINCT city) FROM test_customers GROUP BY 1;
7676
SELECT discount, COUNT(DISTINCT id) FROM test_customers GROUP BY 1;
77+
78+
----------------------------------------------------------------
79+
-- Prepared statements
80+
----------------------------------------------------------------
81+
82+
PREPARE prepared_floor_by(numeric) AS SELECT diffix.floor_by(discount, $1), count(*) FROM test_customers GROUP BY 1;
83+
EXECUTE prepared_floor_by(2.0);

0 commit comments

Comments
 (0)