Skip to content

Commit 6f0fc05

Browse files
authored
Fix reverseChunked(by:) Method Implementation (#465)
1 parent 43929b0 commit 6f0fc05

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

Sources/PostgresNIO/Data/PostgresData+Numeric.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,10 @@ private extension Collection {
268268
// splits the collection into chunks of the supplied size
269269
// if the collection is not evenly divisible, the first chunk will be smaller
270270
func reverseChunked(by maxSize: Int) -> [SubSequence] {
271-
var lastDistance = 0
272271
var chunkStartIndex = self.startIndex
273272
return stride(from: 0, to: self.count, by: maxSize).reversed().map { current in
274-
let distance = (self.count - current) - lastDistance
275-
lastDistance = distance
276-
let chunkEndOffset = Swift.min(
277-
self.distance(from: chunkStartIndex, to: self.endIndex),
278-
distance
279-
)
280-
let chunkEndIndex = self.index(chunkStartIndex, offsetBy: chunkEndOffset)
273+
let distance = self.count - current
274+
let chunkEndIndex = self.index(self.startIndex, offsetBy: distance)
281275
defer { chunkStartIndex = chunkEndIndex }
282276
return self[chunkStartIndex..<chunkEndIndex]
283277
}

Tests/IntegrationTests/PostgresNIOTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,21 +617,25 @@ final class PostgresNIOTests: XCTestCase {
617617
let a = PostgresNumeric(string: "123456.789123")!
618618
let b = PostgresNumeric(string: "-123456.789123")!
619619
let c = PostgresNumeric(string: "3.14159265358979")!
620+
let d = PostgresNumeric(string: "1234567898765")!
620621
var rows: PostgresQueryResult?
621622
XCTAssertNoThrow(rows = try conn?.query("""
622623
select
623624
$1::numeric as a,
624625
$2::numeric as b,
625-
$3::numeric as c
626+
$3::numeric as c,
627+
$4::numeric as d
626628
""", [
627629
.init(numeric: a),
628630
.init(numeric: b),
629-
.init(numeric: c)
631+
.init(numeric: c),
632+
.init(numeric: d)
630633
]).wait())
631634
let row = rows?.first?.makeRandomAccess()
632635
XCTAssertEqual(row?[data: "a"].decimal, Decimal(string: "123456.789123")!)
633636
XCTAssertEqual(row?[data: "b"].decimal, Decimal(string: "-123456.789123")!)
634637
XCTAssertEqual(row?[data: "c"].decimal, Decimal(string: "3.14159265358979")!)
638+
XCTAssertEqual(row?[data: "d"].decimal, Decimal(string: "1234567898765")!)
635639
}
636640

637641
func testDecimalStringSerialization() {

0 commit comments

Comments
 (0)