Skip to content

Commit

Permalink
Deal with no applicable restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
winsmith committed May 31, 2023
1 parent 45d3fc9 commit 02c4b6c
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions Sources/DataTransferObjects/Query/CustomQuery+CompileDown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,9 @@ public extension CustomQuery {
}

// Add restrictionsFilter
if let restrictions = query.restrictions {
// Only apply those restrictions that actually are inside the query intervals
var applicableRestrictions = Set<QueryTimeInterval>()
for queryInterval in query.intervals ?? [] {
for restrictionInterval in restrictions {
let isOverlapping = (queryInterval.beginningDate <= restrictionInterval.endDate) && (restrictionInterval.beginningDate <= queryInterval.endDate)
if isOverlapping {
applicableRestrictions.insert(restrictionInterval)
}
}
}

if applicableRestrictions.isEmpty {
query.restrictions = nil
} else {
query.restrictions = applicableRestrictions.sorted { $0 < $1 }
query.filter = query.filter && Filter.not(.init(field: Filter.interval(.init(dimension: "__time", intervals: query.restrictions ?? []))))
}
if let applicableRestrictions = Self.getApplicableRestrictions(from: query) {
query.restrictions = applicableRestrictions
query.filter = query.filter && Filter.not(.init(field: Filter.interval(.init(dimension: "__time", intervals: applicableRestrictions))))
}

// Update compilationStatus so the next steps in the pipeline are sure the query has been compiled
Expand Down Expand Up @@ -155,4 +140,25 @@ extension CustomQuery {

return Filter.or(.init(fields: filters))
}

static func getApplicableRestrictions(from query: CustomQuery) -> [QueryTimeInterval]? {
guard let restrictions = query.restrictions else { return nil }

// Only apply those restrictions that actually are inside the query intervals
var applicableRestrictions = Set<QueryTimeInterval>()
for queryInterval in query.intervals ?? [] {
for restrictionInterval in restrictions {
let isOverlapping = (queryInterval.beginningDate <= restrictionInterval.endDate) && (restrictionInterval.beginningDate <= queryInterval.endDate)
if isOverlapping {
applicableRestrictions.insert(restrictionInterval)
}
}
}

if applicableRestrictions.isEmpty {
return nil
} else {
return applicableRestrictions.sorted { $0 < $1 }
}
}
}

0 comments on commit 02c4b6c

Please sign in to comment.