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?35275 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.
35,275 reputation · 08 Feb 2021, 10:03 UTC
When a user lacks SELECT permission on a specific field, SurrealDB still allows the row to be counted by COUNT(*) because the permission check is applied to the field value, not to the row’s existence. However, aggregates that read that field—such as SUM(field) or AVG(field)—return NULL (or raise an error in strict permission mode) for each inaccessible row, which can cause the overall aggregate to be NULL if all rows are inaccessible. This distinction means COUNT(*) may show a non‑zero total while SUM(field) shows NULL even though the same set of rows passed the table‑level filter.