Does SurrealDB permission evaluation impact result set counts in aggregate queries?
0 reputation · 07 Feb 2021, 23:02 UTC
0 reputation · 07 Feb 2021, 23:02 UTC
SurrealDB utilizes DEFINE TABLE and DEFINE FIELD statements to implement fine-grained access control. These permissions are evaluated during the query execution phase, effectively acting as a filter on the records returned to the user based on the authenticated session's properties.
When executing aggregate functions—such as count() or sum()—on a table with complex record-level permissions, there is a need to understand how the engine handles the filtered result set. Specifically, it is unclear if the aggregation occurs before or after the permission filter is applied to the underlying data.
count() function return the total number of records in the table or only the records the user is permitted to see?30775 reputation · 08 Feb 2021, 00:42 UTC
Yes, SurrealDB permission evaluation directly impacts result set counts in aggregate queries. Aggregate functions—including count(), sum(), and avg()—operate only on the records that the authenticated user is permitted to access.
In SurrealDB, permissions defined via DEFINE TABLE or DEFINE FIELD are not applied as a post-processing step after data retrieval. Instead, they are integrated into the query planning phase as implicit filters. Effectively, the engine treats permission policies as hidden WHERE clauses that are appended to the query before the aggregation occurs.
This leads to the following behaviors:
SELECT count() operation returns the number of records the user is authorized to see, not the total number of records existing in the table.SELECT permissions for any individual records within that table, the aggregate result will be zero or an empty set.SUPERUSER role (or those operating under a NONE permission schema) bypass these filters to see the absolute total of all records.To verify this behavior in your environment (assuming SurrealDB v1.x or v2.x), you can use the following scoped test:
status = 'active').DEFINE TABLE test PERMISSIONS FOR SELECT WHERE status = 'active';SELECT count() FROM test; (Expected result: 3).Diagnostic Note: To provide a more specific optimization recommendation, please clarify if you are using DEFINE FIELD permissions in addition to DEFINE TABLE permissions, as field-level restrictions can further impact specific aggregate functions like sum().
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.