From a4132a00db9ba9100fda0e76d92bc0612087276f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 19 Jan 2025 14:40:01 +0100 Subject: [PATCH] Clarify code that aims at fixing #1357 --- GRDB/QueryInterface/SQL/SQLRelation.swift | 2 +- GRDB/QueryInterface/SQL/SQLSelection.swift | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index 469c845bf..691889cb3 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -636,7 +636,7 @@ extension SQLRelation { } // - guard selection.allSatisfy(\.isTriviallyCountable) else { + if selection.contains(where: \.requiresTrivialCount) { return try fetchTrivialCount(db) } diff --git a/GRDB/QueryInterface/SQL/SQLSelection.swift b/GRDB/QueryInterface/SQL/SQLSelection.swift index a0f29120d..c616c701b 100644 --- a/GRDB/QueryInterface/SQL/SQLSelection.swift +++ b/GRDB/QueryInterface/SQL/SQLSelection.swift @@ -245,15 +245,25 @@ extension SQLSelection { } } + /// Returns whether this selection MUST be counted with a "trivial" + /// count: `SELECT COUNT(*) FROM (SELECT ...)`. + /// /// Supports SQLRelation.fetchCount. /// /// See - var isTriviallyCountable: Bool { + var requiresTrivialCount: Bool { switch impl { case .aliasedExpression, .literal: - return false - case .allColumns, .qualifiedAllColumns, .expression: + // Trivial count is required. + // + // For example, the WHERE clause here requires the aliased + // column to be preserved in the counting request: + // SELECT *, column AS alt FROM player WHERE alt return true + case .allColumns, .qualifiedAllColumns: + return false + case .expression: + return false } } }