Skip to content

Commit 3981555

Browse files
authored
Fail when groupBy or topN queries try to use hourly granularity (#54)
1 parent b98e220 commit 3981555

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

Sources/DataTransferObjects/Query/CustomQuery+CompileDown.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public extension CustomQuery {
2525
throw QueryGenerationError.compilationStatusError
2626
}
2727

28+
// If we're not a super-org, disallow running groupBy and topN queries in hourly granularity
29+
// because these currently produce misleading or wrong data sometimes.
30+
// Remove this after the issue is fixed.
31+
if !isSuperOrg, granularity == .hour, [.topN, .groupBy].contains(queryType) {
32+
throw QueryGenerationError.notImplemented(reason: "This query can't be calculated in hourly granularity. Please choose daily or monthly instead.")
33+
}
34+
2835
// Make an editable copy of self
2936
var query = self
3037

Tests/QueryGenerationTests/CompileDownTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,46 @@ final class CompileDownTests: XCTestCase {
314314
let compiledQuery = try precompiledQuery.compileToRunnableQuery()
315315
XCTAssertEqual(compiledQuery.dataSource?.name, "some-unknown-namespace")
316316
}
317+
318+
func testAllowsHourlyGranularityForTimeseries() throws {
319+
let intervals: [QueryTimeInterval] = [
320+
.init(beginningDate: Date(iso8601String: "2023-04-01T00:00:00.000Z")!, endDate: Date(iso8601String: "2023-05-31T00:00:00.000Z")!),
321+
]
322+
let query = CustomQuery(queryType: .timeseries, intervals: intervals, granularity: .hour)
323+
XCTAssertNoThrow(try query.precompile(organizationAppIDs: [appID1, appID2], isSuperOrg: false))
324+
}
325+
326+
func testAllowsDailyGranularityForTopN() throws {
327+
let intervals: [QueryTimeInterval] = [
328+
.init(beginningDate: Date(iso8601String: "2023-04-01T00:00:00.000Z")!, endDate: Date(iso8601String: "2023-05-31T00:00:00.000Z")!),
329+
]
330+
let query = CustomQuery(queryType: .topN, intervals: intervals, granularity: .day)
331+
XCTAssertNoThrow(try query.precompile(organizationAppIDs: [appID1, appID2], isSuperOrg: false))
332+
}
333+
334+
func testAllowsDailyGranularityForGroupBy() throws {
335+
let intervals: [QueryTimeInterval] = [
336+
.init(beginningDate: Date(iso8601String: "2023-04-01T00:00:00.000Z")!, endDate: Date(iso8601String: "2023-05-31T00:00:00.000Z")!),
337+
]
338+
let query = CustomQuery(queryType: .groupBy, intervals: intervals, granularity: .day)
339+
XCTAssertNoThrow(try query.precompile(organizationAppIDs: [appID1, appID2], isSuperOrg: false))
340+
}
341+
342+
func testDisallowsHourlyQueriesForTopN() throws {
343+
let intervals: [QueryTimeInterval] = [
344+
.init(beginningDate: Date(iso8601String: "2023-04-01T00:00:00.000Z")!, endDate: Date(iso8601String: "2023-05-31T00:00:00.000Z")!),
345+
]
346+
let query = CustomQuery(queryType: .topN, intervals: intervals, granularity: .hour)
347+
348+
XCTAssertThrowsError(try query.precompile(organizationAppIDs: [appID1, appID2], isSuperOrg: false))
349+
}
350+
351+
func testDisallowsHourlyQueriesForGroupBy() throws {
352+
let intervals: [QueryTimeInterval] = [
353+
.init(beginningDate: Date(iso8601String: "2023-04-01T00:00:00.000Z")!, endDate: Date(iso8601String: "2023-05-31T00:00:00.000Z")!),
354+
]
355+
let query = CustomQuery(queryType: .groupBy, intervals: intervals, granularity: .hour)
356+
357+
XCTAssertThrowsError(try query.precompile(organizationAppIDs: [appID1, appID2], isSuperOrg: false))
358+
}
317359
}

0 commit comments

Comments
 (0)