From 75952ee8da709507144c677b8110af4e37c9df08 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 5 Apr 2022 22:53:32 +0200 Subject: [PATCH] PostgreSQL: Group summaries: Fix type resolution for UNION MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Type resolution for UNION (CASE and related constructs) in PostgreSQL works by taking the UNIONed queries 2 at a time, i.e. the first TWO queries are resolved, then it takes the result and resolves it with the next query and so on. Previously, when using Hostgroup as the first query and Host (or Service) as the next query, PostgreSQL encounters two NULL values for the host_handled (or service_handled) column, resulting in the type text. This leads to the following error: UNION types text and boolenum cannot be matched. Now Hostgroup is the last query that ensures there are not two consecutive NULL values for host_handled (or service_handled). NOTE: BREAKING: This change breaks other code that extracts the "base query" which depends on Hostgroup or Servicegroup being the first query in the getUnions() array, which has yet to be fixed. In preparation for a possible fix that could simply use the last query instead of the first, the Servicegroup query has also been moved to the end. --- library/Icingadb/Model/Hostgroupsummary.php | 32 +++++++++---------- .../Icingadb/Model/ServicegroupSummary.php | 26 +++++++-------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/library/Icingadb/Model/Hostgroupsummary.php b/library/Icingadb/Model/Hostgroupsummary.php index 8c050ce6e..68c4a3e6b 100644 --- a/library/Icingadb/Model/Hostgroupsummary.php +++ b/library/Icingadb/Model/Hostgroupsummary.php @@ -99,22 +99,6 @@ public function getDefaultSort() public function getUnions() { $unions = [ - [ - Hostgroup::class, - [], - [ - 'hostgroup_id' => 'hostgroup.id', - 'hostgroup_name' => 'hostgroup.name', - 'hostgroup_display_name' => 'hostgroup.display_name', - 'host_id' => new Expression('NULL'), - 'host_state' => new Expression('NULL'), - 'host_handled' => new Expression('NULL'), - 'host_severity' => new Expression('0'), - 'service_id' => new Expression('NULL'), - 'service_state' => new Expression('NULL'), - 'service_handled' => new Expression('NULL') - ] - ], [ Host::class, [ @@ -152,6 +136,22 @@ public function getUnions() 'service_state' => 'state.soft_state', 'service_handled' => 'state.is_handled' ] + ], + [ + Hostgroup::class, + [], + [ + 'hostgroup_id' => 'hostgroup.id', + 'hostgroup_name' => 'hostgroup.name', + 'hostgroup_display_name' => 'hostgroup.display_name', + 'host_id' => new Expression('NULL'), + 'host_state' => new Expression('NULL'), + 'host_handled' => new Expression('NULL'), + 'host_severity' => new Expression('0'), + 'service_id' => new Expression('NULL'), + 'service_state' => new Expression('NULL'), + 'service_handled' => new Expression('NULL') + ] ] ]; diff --git a/library/Icingadb/Model/ServicegroupSummary.php b/library/Icingadb/Model/ServicegroupSummary.php index f434a9787..1b5232ce3 100644 --- a/library/Icingadb/Model/ServicegroupSummary.php +++ b/library/Icingadb/Model/ServicegroupSummary.php @@ -75,19 +75,6 @@ public function getDefaultSort() public function getUnions() { $unions = [ - [ - Servicegroup::class, - [], - [ - 'servicegroup_id' => 'servicegroup.id', - 'servicegroup_name' => 'servicegroup.name', - 'servicegroup_display_name' => 'servicegroup.display_name', - 'service_id' => new Expression('NULL'), - 'service_state' => new Expression('NULL'), - 'service_handled' => new Expression('NULL'), - 'service_severity' => new Expression('0') - ] - ], [ Service::class, [ @@ -103,6 +90,19 @@ public function getUnions() 'service_handled' => 'state.is_handled', 'service_severity' => 'state.severity' ] + ], + [ + Servicegroup::class, + [], + [ + 'servicegroup_id' => 'servicegroup.id', + 'servicegroup_name' => 'servicegroup.name', + 'servicegroup_display_name' => 'servicegroup.display_name', + 'service_id' => new Expression('NULL'), + 'service_state' => new Expression('NULL'), + 'service_handled' => new Expression('NULL'), + 'service_severity' => new Expression('0') + ] ] ];