Skip to content

Commit a4efc2d

Browse files
ralonsohkarelyatin
authored andcommitted
[PostgreSQL] Subnet entity with ServiceType grouped by both tables
The SQL clause "GROUP BY" in PostgreSQL requires the presence of all fields provided in the "SELECT" clause; this is not happening in MariaDB not MySQL. Since [1] (and the related patch [2] that are part of the same bug fix), when a resource with RBAC registers is selected, the "GROUP BY" clause is added to reduce the number of returned registers. The filed used is "id", present in all the RBAC controlled resources ('network', 'qospolicy', 'securitygroup', 'addressscope', 'subnetpool', 'addressgroup'). That is in opposition to what was stated in the first paragraph "requires the presence of all fields provided in the "SELECT" clause". However it is possible to group only by a primary key [3]. In [4] a prior change was introduced that modifies the "Subnet" entity to include a child table "SubnetServiceType". That introduces two new fields ("SubnetServiceType" fields) that are not present in the "GROUP BY" clause, causing the reported error in the LP bug. This patch is adding these two fields (that form the composite primary key of "SubnetServiceType" table) to the "GROUP BY" clause, when needed; if the query is executed by an administrator, the RBAC query won't be performed (an administrator has always permissions) and the "GROUP BY" clause won't be issued. [1]https://review.opendev.org/c/openstack/neutron-lib/+/884878 [2]https://review.opendev.org/c/openstack/neutron/+/884877 [3]https://learnsql.com/blog/must-appear-in-group-by-clause/ [4]https://review.opendev.org/c/openstack/neutron/+/744512 Closes-Bug: #2028003 Change-Id: I18e65d79e56fe5995076eb9166da23fc14c92fc5 (cherry picked from commit c831771) (cherry picked from commit 96267a2) Conflicts: neutron/objects/subnet.py
1 parent edf33d2 commit a4efc2d

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

neutron/objects/subnet.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ def query_filter_service_subnets(cls, query, service_type):
178178
# service type when DHCP is enabled on the subnet.
179179
and_(Subnet.enable_dhcp.is_(True),
180180
service_type == const.DEVICE_OWNER_DHCP)))
181+
182+
if query._group_by_clauses:
183+
# If the "Subnet" query has a "GROUP BY" clause (that happens when
184+
# a non-admin user has executed the query, that requires the join
185+
# of the RBAC registers), it is needed to add the
186+
# "SubnetServiceType" fields to this clause too.
187+
query = query.group_by(ServiceType.subnet_id,
188+
ServiceType.service_type)
189+
181190
return query.from_self(Subnet)
182191

183192

0 commit comments

Comments
 (0)