From aa3195cbcc0a254044e7fc66f16a35b11f96cf1d Mon Sep 17 00:00:00 2001 From: Jeffrey Macko Date: Wed, 28 May 2025 12:20:15 +0200 Subject: [PATCH] Add support for distinct jsonArray --- .../SQLite/JSONFunctions.swift | 8 +++++-- .../JSONFunctionsTests.swift | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift b/Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift index 79d46afb..0651363e 100644 --- a/Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift +++ b/Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift @@ -27,14 +27,16 @@ extension QueryExpression where QueryValue: Codable & QueryBindable & Sendable { /// ``` /// /// - Parameters: + /// - isDistinct: An boolean to enable the `DISTINCT` clause to apply to the aggregation. /// - order: An `ORDER BY` clause to apply to the aggregation. /// - filter: A `FILTER` clause to apply to the aggregation. /// - Returns: A JSON array aggregate of this expression. public func jsonGroupArray( + isDistinct: Bool = false, order: (some QueryExpression)? = Bool?.none, filter: (some QueryExpression)? = Bool?.none ) -> some QueryExpression<[QueryValue].JSONRepresentation> { - AggregateFunction("json_group_array", self, order: order, filter: filter) + AggregateFunction("json_group_array", isDistinct: isDistinct, self, order: order, filter: filter) } } @@ -85,14 +87,16 @@ extension PrimaryKeyedTableDefinition where QueryValue: Codable & Sendable { /// } /// /// - Parameters: + /// - isDistinct: An boolean to enable the `DISTINCT` clause to apply to the aggregation. /// - order: An `ORDER BY` clause to apply to the aggregation. /// - filter: A `FILTER` clause to apply to the aggregation. /// - Returns: A JSON array aggregate of this table. public func jsonGroupArray( + isDistinct: Bool = false, order: (some QueryExpression)? = Bool?.none, filter: (some QueryExpression)? = Bool?.none ) -> some QueryExpression<[QueryValue].JSONRepresentation> { - AggregateFunction("json_group_array", jsonObject, order: order, filter: filter) + AggregateFunction("json_group_array", isDistinct: isDistinct, jsonObject, order: order, filter: filter) } private var jsonObject: some QueryExpression { diff --git a/Tests/StructuredQueriesTests/JSONFunctionsTests.swift b/Tests/StructuredQueriesTests/JSONFunctionsTests.swift index c1aaecd6..fd4b5dd2 100644 --- a/Tests/StructuredQueriesTests/JSONFunctionsTests.swift +++ b/Tests/StructuredQueriesTests/JSONFunctionsTests.swift @@ -40,6 +40,30 @@ extension SnapshotTests { } } + @Test func jsonGroupArrayDisctinct() { + assertQuery( + Reminder.select { + $0.priority.jsonGroupArray(isDistinct: true) + } + ) { + """ + SELECT json_group_array(DISTINCT "reminders"."priority") + FROM "reminders" + """ + } results: { + """ + ┌────────────────┐ + │ [ │ + │ [0]: nil, │ + │ [1]: .high, │ + │ [2]: .low, │ + │ [3]: .medium │ + │ ] │ + └────────────────┘ + """ + } + } + @Test func jsonArrayLength() { assertQuery( Reminder.select {