@@ -18,51 +18,51 @@ NJson::TJsonValue TSortableBatchPosition::DebugJson() const {
18
18
return result;
19
19
}
20
20
21
- std::optional<TSortableBatchPosition::TFoundPosition> TSortableBatchPosition::FindPosition (TRWSortableBatchPosition& position,
22
- const ui64 posStartExt, const ui64 posFinishExt, const TSortableBatchPosition& forFound, const bool greater ) {
21
+ std::optional<TSortableBatchPosition::TFoundPosition> TSortableBatchPosition::FindBound (TRWSortableBatchPosition& position,
22
+ const ui64 posStartExt, const ui64 posFinishExt, const TSortableBatchPosition& forFound, const bool upper ) {
23
23
ui64 posStart = posStartExt;
24
24
ui64 posFinish = posFinishExt;
25
25
auto guard = position.CreateAsymmetricAccessGuard ();
26
+ const auto cond = upper ?
27
+ [](const std::partial_ordering cmp) {
28
+ return cmp == std::partial_ordering::greater;
29
+ } :
30
+ [](const std::partial_ordering cmp) {
31
+ return cmp == std::partial_ordering::greater || cmp == std::partial_ordering::equivalent;
32
+ };
33
+
26
34
{
27
35
AFL_VERIFY (guard.InitSortingPosition (posStart));
28
36
auto cmp = position.Compare (forFound);
29
- if (cmp == std::partial_ordering::greater) {
30
- return TFoundPosition::Greater (posStart);
31
- } else if (cmp == std::partial_ordering::equivalent) {
32
- return TFoundPosition::Equal (posStart);
37
+ if (cond (cmp)) {
38
+ return TFoundPosition (posStart, cmp);
33
39
}
34
40
}
35
41
{
36
42
AFL_VERIFY (guard.InitSortingPosition (posFinish));
37
43
auto cmp = position.Compare (forFound);
38
- if (cmp == std::partial_ordering::less) {
39
- return TFoundPosition::Less (posFinish);
40
- } else if (cmp == std::partial_ordering::equivalent) {
41
- return TFoundPosition::Equal (posFinish);
44
+ if (!cond (cmp)) {
45
+ return std::nullopt;
42
46
}
43
47
}
44
- while (posFinish > posStart + 1 ) {
48
+ while (posFinish != posStart + 1 ) {
49
+ AFL_VERIFY (posFinish > posStart + 1 )(" finish" , posFinish)(" start" , posStart);
45
50
AFL_VERIFY (guard.InitSortingPosition (0.5 * (posStart + posFinish)));
46
51
const auto comparision = position.Compare (forFound);
47
- if (comparision == std::partial_ordering::less) {
48
- posStart = position.Position ;
49
- } else if (comparision == std::partial_ordering::greater) {
52
+ if (cond (comparision)) {
50
53
posFinish = position.Position ;
51
54
} else {
52
- return TFoundPosition::Equal ( position.Position ) ;
55
+ posStart = position.Position ;
53
56
}
54
57
}
55
- AFL_VERIFY (posFinish != posStart);
56
- if (greater) {
57
- AFL_VERIFY (guard.InitSortingPosition (posFinish));
58
- return TFoundPosition::Greater (posFinish);
59
- } else {
60
- AFL_VERIFY (guard.InitSortingPosition (posStart));
61
- return TFoundPosition::Less (posStart);
62
- }
58
+ AFL_VERIFY (posFinish == posStart + 1 )(" finish" , posFinish)(" start" , posStart);
59
+ AFL_VERIFY (guard.InitSortingPosition (posFinish));
60
+ const auto comparision = position.Compare (forFound);
61
+ AFL_VERIFY (cond (comparision));
62
+ return TFoundPosition (posFinish, comparision);
63
63
}
64
64
65
- std::optional<TSortableBatchPosition::TFoundPosition> TSortableBatchPosition::FindPosition (const std::shared_ptr<arrow::RecordBatch>& batch,
65
+ std::optional<TSortableBatchPosition::TFoundPosition> TSortableBatchPosition::FindBound (const std::shared_ptr<arrow::RecordBatch>& batch,
66
66
const TSortableBatchPosition& forFound, const bool greater, const std::optional<ui32> includedStartPosition) {
67
67
if (!batch || !batch->num_rows ()) {
68
68
return {};
@@ -77,12 +77,11 @@ std::optional<TSortableBatchPosition::TFoundPosition> TSortableBatchPosition::Fi
77
77
}
78
78
79
79
TRWSortableBatchPosition position = forFound.BuildRWPosition (batch, posStart);
80
- return FindPosition (position, posStart, posFinish, forFound, greater);
80
+ return FindBound (position, posStart, posFinish, forFound, greater);
81
81
}
82
82
83
83
NKikimr::NArrow::NMerger::TRWSortableBatchPosition TSortableBatchPosition::BuildRWPosition (const bool needData, const bool deepCopy) const {
84
- return TRWSortableBatchPosition (Position, RecordsCount, ReverseSort,
85
- deepCopy ? Sorting->BuildCopy (Position) : Sorting,
84
+ return TRWSortableBatchPosition (Position, RecordsCount, ReverseSort, deepCopy ? Sorting->BuildCopy (Position) : Sorting,
86
85
(needData && Data) ? (deepCopy ? Data->BuildCopy (Position) : Data) : nullptr );
87
86
}
88
87
@@ -96,9 +95,15 @@ NKikimr::NArrow::NMerger::TRWSortableBatchPosition TSortableBatchPosition::Build
96
95
}
97
96
98
97
TSortableBatchPosition::TFoundPosition TRWSortableBatchPosition::SkipToLower (const TSortableBatchPosition& forFound) {
98
+ AFL_VERIFY (RecordsCount);
99
99
const ui32 posStart = Position;
100
- auto pos = FindPosition (*this , posStart, ReverseSort ? 0 : (RecordsCount - 1 ), forFound, true );
101
- AFL_VERIFY (pos)(" cursor" , DebugJson ())(" found" , forFound.DebugJson ());
100
+ AFL_VERIFY (!ReverseSort)(" reason" , " unimplemented" );
101
+ auto pos = FindBound (*this , posStart, RecordsCount - 1 , forFound, false );
102
+ if (!pos) {
103
+ auto guard = CreateAsymmetricAccessGuard ();
104
+ AFL_VERIFY (guard.InitSortingPosition (RecordsCount - 1 ));
105
+ return TFoundPosition (RecordsCount - 1 , Compare (forFound));
106
+ }
102
107
if (ReverseSort) {
103
108
AFL_VERIFY (Position <= posStart)(" pos" , Position)(" pos_skip" , pos->GetPosition ())(" reverse" , true );
104
109
} else {
@@ -133,8 +138,7 @@ TSortableScanData::TSortableScanData(
133
138
BuildPosition (position);
134
139
}
135
140
136
- TSortableScanData::TSortableScanData (
137
- const ui64 position, const std::shared_ptr<arrow::RecordBatch>& batch) {
141
+ TSortableScanData::TSortableScanData (const ui64 position, const std::shared_ptr<arrow::RecordBatch>& batch) {
138
142
for (auto && c : batch->columns ()) {
139
143
Columns.emplace_back (std::make_shared<NAccessor::TTrivialArray>(c));
140
144
}
0 commit comments