From 49b9772dba0a64f4d2eee38ddcd272f6f644e9b6 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 26 Dec 2025 20:15:00 +0100 Subject: [PATCH] fix: CRITICAL - Do not re-sort rows by NodeNum after MySQL ORDER BY consolidation key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: After fetching rows ordered by consolidation key (UnitName, ToolNameID, EventDate, EventTime) from MySQL, code was re-sorting by NodeNum. This breaks the grouping because rows with different consolidation keys get intermixed. Example of what was happening: - MySQL returns: (Unit1, Tool1, Date1, Time1, Node1), (Unit1, Tool1, Date1, Time1, Node12), (Unit2, Tool2, Date2, Time2, Node1) - Re-sorting by NodeNum gives: (Unit1, Tool1, Date1, Time1, Node1), (Unit2, Tool2, Date2, Time2, Node1), (Unit1, Tool1, Date1, Time1, Node12) - Result: Different consolidation keys are now mixed, each node becomes separate group! Fix: Remove the re-sort. Trust MySQL's ORDER BY to keep rows of same key together. The clustering nature of InnoDB ensures rows with same consolidation key are physically adjacent. This was causing 1 row per node instead of consolidating all nodes of same measurement into 1 row. 🤖 Generated with Claude Code Co-Authored-By: Claude Haiku 4.5 --- src/connectors/mysql_connector.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/connectors/mysql_connector.py b/src/connectors/mysql_connector.py index d51e3e1..0a9f6af 100644 --- a/src/connectors/mysql_connector.py +++ b/src/connectors/mysql_connector.py @@ -275,16 +275,16 @@ class MySQLConnector: if not rows: return - # Sort rows by NodeNum within the batch - # (rows already grouped by consolidation key from ORDER BY) - sorted_rows = sorted(rows, key=lambda r: int(r.get("NodeNum") or 0)) + # DO NOT re-sort by NodeNum! The database has already ordered by consolidation key, + # and re-sorting breaks the grouping. Rows with same key will not be consecutive anymore. + # We trust the MySQL ORDER BY to keep all rows of same key together. # Group rows by consolidation key - # Since rows are already ordered by key, all rows with same key are consecutive + # Since rows are already ordered by key from MySQL ORDER BY, all rows with same key are consecutive current_group = [] current_key = None - for row in sorted_rows: + for row in rows: key = ( row.get("UnitName"), row.get("ToolNameID"),