From a328faa8349ab0085e1057c1c09e5392de5ca691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 1 Jul 2020 21:24:27 +0200 Subject: [PATCH] Allow grouping by FastPrimaryKeyExpression --- .../SQLGeneration/SQLQueryGenerator.swift | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 61ebf1c92..296124551 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -500,6 +500,20 @@ struct SQLQueryGenerator: Refinable { return .none } + // Grouping something which is not a table: assume non unique grouping. + guard case let .table(tableName: tableName, alias: alias) = relation.source else { + return .nonUnique + } + + // FastPrimaryKeyExpression? + for expression in groupExpressions { + if + let column = expression as? QualifiedFastPrimaryKeyExpression, + column.alias == alias { + return .unique + } + } + // Grouping by something which is not a column: assume non // unique grouping. // SELECT * FROM player GROUP BY (score + bonus) @@ -508,11 +522,6 @@ struct SQLQueryGenerator: Refinable { return .nonUnique } - // Grouping something which is not a table: assume non unique grouping. - guard case let .table(tableName: tableName, alias: alias) = relation.source else { - return .nonUnique - } - // Grouping by some column(s) which do not come from the main table: // assume non unique grouping. // SELECT * FROM player JOIN team ... GROUP BY team.id