Role hierarchy in Snowflake - Time & Space Complexity
We want to understand how the time to check permissions grows when roles are arranged in a hierarchy in Snowflake.
Specifically, how does the number of roles affect the time to find all permissions for a user?
Analyze the time complexity of the following operation sequence.
-- Create roles
CREATE ROLE role1;
CREATE ROLE role2;
GRANT ROLE role1 TO ROLE role2;
GRANT ROLE role2 TO USER some_user;
-- Check all roles granted to a user
SHOW GRANTS TO USER some_user;
This sequence creates roles and grants one role to another, forming a hierarchy. Then it checks all roles granted to a user, including inherited roles.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Checking role grants recursively to find all inherited roles.
- How many times: Once per role in the hierarchy during permission resolution.
As the number of roles in the hierarchy grows, the system checks each linked role to gather permissions.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 role checks |
| 100 | About 100 role checks |
| 1000 | About 1000 role checks |
Pattern observation: The number of checks grows roughly in direct proportion to the number of roles linked in the hierarchy.
Time Complexity: O(n)
This means the time to find all permissions grows linearly with the number of roles in the hierarchy.
[X] Wrong: "Checking permissions is instant no matter how many roles exist."
[OK] Correct: Each role linked in the hierarchy must be checked, so more roles mean more work.
Understanding how role hierarchies affect permission checks shows you can reason about system behavior as it scales, a key skill in cloud infrastructure.
"What if roles could have multiple parent roles instead of just one? How would the time complexity change?"
Practice
Solution
Step 1: Understand role hierarchy concept
Role hierarchy means roles can inherit permissions from other roles, simplifying access management.Step 2: Identify correct description
Only Let roles inherit permissions from other roles describes this inheritance feature correctly.Final Answer:
Let roles inherit permissions from other roles -> Option CQuick Check:
Role hierarchy = permission inheritance [OK]
- Confusing role hierarchy with user password management
- Thinking role hierarchy controls data storage
- Mixing role hierarchy with query scheduling
Solution
Step 1: Recall Snowflake grant syntax
To make one role inherit another, you grant the parent role to the child role using: GRANT ROLE parent_role TO ROLE child_role;Step 2: Match syntax to options
GRANT ROLE parent_role TO ROLE child_role; matches the correct syntax exactly.Final Answer:
GRANT ROLE parent_role TO ROLE child_role; -> Option AQuick Check:
Grant parent to child = correct syntax [OK]
- Reversing parent and child roles in the grant statement
- Incorrect order of keywords in the command
- Missing 'TO ROLE' keyword
GRANT ROLE analyst TO ROLE senior_analyst;GRANT ROLE senior_analyst TO ROLE manager;If a user is assigned the role
manager, which roles' permissions does the user have?Solution
Step 1: Trace role inheritance
Manager role inherits senior_analyst, which inherits analyst. So manager includes all permissions from senior_analyst and analyst.Step 2: Determine user permissions
User with manager role has permissions from manager, senior_analyst, and analyst roles.Final Answer:
Manager, senior_analyst, and analyst -> Option BQuick Check:
Role inheritance includes all ancestor roles [OK]
- Assuming user only has direct role permissions
- Ignoring transitive inheritance in role hierarchy
- Confusing child and parent roles
GRANT ROLE analyst TO senior_analyst;What is wrong with this command?
Solution
Step 1: Check syntax for granting roles
The correct syntax requires 'TO ROLE' before the role receiving the grant.Step 2: Identify missing keyword
The command misses 'ROLE' keyword before 'senior_analyst'. It should be: GRANT ROLE analyst TO ROLE senior_analyst;Final Answer:
Missing 'ROLE' keyword before 'senior_analyst' -> Option DQuick Check:
Grant syntax requires 'TO ROLE' [OK]
- Omitting 'ROLE' keyword after TO
- Thinking roles cannot be granted to roles
- Using quotes unnecessarily around role names
intern inherits from analyst, and analyst inherits from manager. Which sequence of commands correctly sets this up?Solution
Step 1: Understand inheritance direction
Child role inherits from parent role by granting parent role TO child role.Step 2: Apply to given roles
Intern inherits analyst: GRANT ROLE analyst TO ROLE intern;
Analyst inherits manager: GRANT ROLE manager TO ROLE analyst;Step 3: Match sequence to options
GRANT ROLE manager TO ROLE analyst; GRANT ROLE analyst TO ROLE intern; matches the correct order and syntax.Final Answer:
GRANT ROLE manager TO ROLE analyst; GRANT ROLE analyst TO ROLE intern; -> Option AQuick Check:
Grant parent TO child for inheritance [OK]
- Reversing parent and child roles in grants
- Granting child TO parent instead of parent TO child
- Mixing order of commands
Ready to prove it?
Pass the test and this pattern moves unknown
