Skip to content

Commit b3450d6

Browse files
Merge pull request #429 from diffix/cristian/misc
Skip queries with no rows from anonymization.
2 parents 5f6c25b + b81211f commit b3450d6

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/query/anonymization.c

+21
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,28 @@ static void compile_anonymizing_query(Query *query, List *personal_relations, An
620620
wrap_having_qual(query);
621621
}
622622

623+
static bool are_qualifiers_always_false(Node *quals)
624+
{
625+
if (quals == NULL)
626+
return false;
627+
628+
if (!IsA(quals, Const))
629+
{
630+
quals = eval_const_expressions(NULL, quals);
631+
if (!IsA(quals, Const))
632+
return false;
633+
}
634+
635+
Const *result = (Const *)quals;
636+
Assert(result->consttype == BOOLOID);
637+
return result->constisnull || DatumGetBool(result->constvalue) == false;
638+
}
639+
623640
static bool is_anonymizing_query(Query *query, List *personal_relations)
624641
{
642+
if (are_qualifiers_always_false(query->jointree->quals))
643+
return false; /* No need for anonymization if no rows will be processed. */
644+
625645
ListCell *cell;
626646
foreach (cell, query->rtable)
627647
{
@@ -633,6 +653,7 @@ static bool is_anonymizing_query(Query *query, List *personal_relations)
633653
return true;
634654
}
635655
}
656+
636657
return false;
637658
}
638659

test/expected/misc.out

+15
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,18 @@ EXPLAIN SELECT city FROM test_customers ORDER BY 1;
197197
-> Seq Scan on test_customers
198198
(6 rows)
199199

200+
-- Allow queries that exclude all rows
201+
SELECT FROM test_customers WHERE FALSE;
202+
--
203+
(0 rows)
204+
205+
SELECT TRUE AS "_" FROM test_customers WHERE 1 <> 1 LIMIT 0;
206+
_
207+
---
208+
(0 rows)
209+
210+
SELECT id FROM test_customers WHERE NULL = NULL;
211+
id
212+
----
213+
(0 rows)
214+

test/sql/misc.sql

+5
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,8 @@ EXPLAIN (ANALYZE, SUMMARY false, TIMING false, COSTS true) SELECT name FROM test
9595

9696
-- EXPLAIN prints group/sort names
9797
EXPLAIN SELECT city FROM test_customers ORDER BY 1;
98+
99+
-- Allow queries that exclude all rows
100+
SELECT FROM test_customers WHERE FALSE;
101+
SELECT TRUE AS "_" FROM test_customers WHERE 1 <> 1 LIMIT 0;
102+
SELECT id FROM test_customers WHERE NULL = NULL;

0 commit comments

Comments
 (0)