|
| 1 | +import Foundation |
| 2 | + |
| 3 | +public enum BigQueryStatementPolicy { |
| 4 | + public static let jobCreationMode = "JOB_CREATION_OPTIONAL" |
| 5 | + |
| 6 | + // A stopped statement's job reference arrives within this wait, so its cancel can follow. |
| 7 | + public static let serverWaitMilliseconds = 2_000 |
| 8 | + |
| 9 | + public static let stopGrace: Duration = .seconds(1) |
| 10 | + |
| 11 | + public static let stoppedRequestLimit: Duration = .seconds(10) |
| 12 | + |
| 13 | + // The server caps an answer at 10 MB. |
| 14 | + public static let answerByteLimit = 32 * 1_024 * 1_024 |
| 15 | + |
| 16 | + // jobs.query answers a statement the server refuses with these, where jobs.insert reported them on the job. |
| 17 | + public static func isStatementRefusal(status: Int) -> Bool { |
| 18 | + status == 400 || status == 403 || status == 404 || status == 409 |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +public struct BigQueryStatementRequest<Parameter: Encodable & Sendable>: Encodable, Sendable { |
| 23 | + public struct Dataset: Encodable, Sendable { |
| 24 | + public let projectId: String |
| 25 | + public let datasetId: String |
| 26 | + |
| 27 | + public init(projectId: String, datasetId: String) { |
| 28 | + self.projectId = projectId |
| 29 | + self.datasetId = datasetId |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public let query: String |
| 34 | + public let useLegacySql: Bool |
| 35 | + public let jobCreationMode: String |
| 36 | + // A retry with the same id returns the first request's statement instead of running it again. |
| 37 | + public let requestId: String |
| 38 | + public let maxResults: Int |
| 39 | + public let timeoutMs: Int |
| 40 | + public let location: String? |
| 41 | + public let defaultDataset: Dataset? |
| 42 | + public let maximumBytesBilled: String? |
| 43 | + public let parameterMode: String? |
| 44 | + public let queryParameters: [Parameter]? |
| 45 | + public let jobTimeoutMs: String? |
| 46 | + |
| 47 | + public init( |
| 48 | + query: String, |
| 49 | + requestId: String, |
| 50 | + maxResults: Int, |
| 51 | + location: String?, |
| 52 | + defaultDataset: Dataset?, |
| 53 | + maximumBytesBilled: String?, |
| 54 | + parameterMode: String?, |
| 55 | + queryParameters: [Parameter]?, |
| 56 | + jobTimeoutMs: String? |
| 57 | + ) { |
| 58 | + self.query = query |
| 59 | + self.useLegacySql = false |
| 60 | + self.jobCreationMode = BigQueryStatementPolicy.jobCreationMode |
| 61 | + self.requestId = requestId |
| 62 | + self.maxResults = maxResults |
| 63 | + self.timeoutMs = BigQueryStatementPolicy.serverWaitMilliseconds |
| 64 | + self.location = location |
| 65 | + self.defaultDataset = defaultDataset |
| 66 | + self.maximumBytesBilled = maximumBytesBilled |
| 67 | + self.parameterMode = parameterMode |
| 68 | + self.queryParameters = queryParameters |
| 69 | + self.jobTimeoutMs = jobTimeoutMs |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +public enum BigQueryStatementProgress: Equatable, Sendable { |
| 74 | + case finished(jobId: String?) |
| 75 | + case running(jobId: String) |
| 76 | + case unreadable |
| 77 | + |
| 78 | + // A statement that finished without a job cannot page, so a page token with no job cannot be followed. |
| 79 | + public init(jobComplete: Bool?, jobId: String?, pageToken: String?) { |
| 80 | + if jobComplete == false { |
| 81 | + self = jobId.map { .running(jobId: $0) } ?? .unreadable |
| 82 | + } else if jobId == nil, pageToken != nil { |
| 83 | + self = .unreadable |
| 84 | + } else { |
| 85 | + self = .finished(jobId: jobId) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static func jobToCancel(jobComplete: Bool?, jobId: String?) -> String? { |
| 90 | + guard case .running(let jobId) = BigQueryStatementProgress(jobComplete: jobComplete, jobId: jobId, pageToken: nil) |
| 91 | + else { return nil } |
| 92 | + return jobId |
| 93 | + } |
| 94 | +} |
0 commit comments