Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ArrayIndexOutOfBounds in MergedSortedRecordsSupplier #782

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* | __/\ V /| | || (_| | |_| | |_) |
* \___| \_/ |_|\__\__,_|____/|____/
*
* Copyright (c) 2023-2024
* Copyright (c) 2023-2025
*
* Licensed under the Business Source License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,7 @@

import io.evitadb.api.requestResponse.EvitaResponseExtraResult;
import io.evitadb.core.cache.payload.CachePayloadHeader;
import io.evitadb.core.query.QueryPlanningContext;
import io.evitadb.core.query.QueryExecutionContext;
import io.evitadb.core.query.algebra.Formula;
import io.evitadb.core.query.response.TransactionalDataRelatedStructure;
import io.evitadb.core.query.sort.SortedRecordsSupplierFactory.SortedRecordsProvider;
Expand Down Expand Up @@ -60,7 +60,7 @@ public interface CacheableSorter extends TransactionalDataRelatedStructure, Sort

/**
* Returns copy of this computer with same configuration but new method handle that references callback that
* will be called when {@link #sortAndSlice(QueryPlanningContext, Formula, int, int, int[], int)} method is first executed
* will be called when {@link #sortAndSlice(QueryExecutionContext, Formula, int, int, int[], int)} method is first executed
* and memoized result is available.
*/
@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* | __/\ V /| | || (_| | |_| | |_) |
* \___| \_/ |_|\__\__,_|____/|____/
*
* Copyright (c) 2023-2024
* Copyright (c) 2023-2025
*
* Licensed under the Business Source License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,9 +72,15 @@ public int sortAndSlice(
int peak,
@Nullable IntConsumer skippedRecordsConsumer
) {
return executionWrapper.applyAsInt(
() -> ConditionalSorter.getFirstApplicableSorter(queryContext, sorter)
.sortAndSlice(queryContext, input, startIndex, endIndex, result, peak, skippedRecordsConsumer)
);
final Sorter firstApplicableSorter = ConditionalSorter.getFirstApplicableSorter(queryContext, sorter);
if (firstApplicableSorter == null) {
return 0;
} else {
return executionWrapper.applyAsInt(
() -> firstApplicableSorter.sortAndSlice(
queryContext, input, startIndex, endIndex, result, peak, skippedRecordsConsumer
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* | __/\ V /| | || (_| | |_| | |_) |
* \___| \_/ |_|\__\__,_|____/|____/
*
* Copyright (c) 2023-2024
* Copyright (c) 2023-2025
*
* Licensed under the Business Source License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,7 @@
import io.evitadb.index.bitmap.Bitmap;
import io.evitadb.index.bitmap.RoaringBitmapBackedBitmap;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.roaringbitmap.BatchIterator;
import org.roaringbitmap.RoaringBatchIterator;
import org.roaringbitmap.RoaringBitmap;
Expand Down Expand Up @@ -91,12 +92,14 @@ private static PartialSortResult fetchSlice(

// skip previous pages quickly
if (toSkip > 0) {
toSkip -= bufferPeak;
}
if (skippedRecordsConsumer != null && skip > 0) {
for (int i = 0; i < bufferPeak + toSkip; i++) {
skippedRecordsConsumer.accept(preSortedRecordIds[buffer[i]]);
if (skippedRecordsConsumer != null) {
// skip records in buffer, cap really read records in the buffer
final int skippedInBuffer = Math.max(0, Math.min(toSkip, bufferPeak));
for (int i = 0; i < skippedInBuffer; i++) {
skippedRecordsConsumer.accept(preSortedRecordIds[buffer[i]]);
}
}
toSkip -= bufferPeak;
}

// now we are on the page
Expand Down Expand Up @@ -235,13 +238,15 @@ public int sortAndSlice(
} else {
final int[] buffer = queryContext.borrowBuffer();
try {
final SkippingRecordConsumer delegateConsumer = new SkippingRecordConsumer(skippedRecordsConsumer);
final SortResult sortResult = collectPartialResults(
queryContext, selectedRecordIds, startIndex, endIndex, result, peak, buffer, skippedRecordsConsumer
queryContext, selectedRecordIds, startIndex, endIndex, result, peak, buffer, delegateConsumer
);
return returnResultAppendingUnknown(
queryContext, sortResult.notSortedRecords(),
unknownRecordIdsSorter,
startIndex, endIndex,
Math.max(0, startIndex - delegateConsumer.getCounter()),
Math.max(0, endIndex - delegateConsumer.getCounter()),
result, sortResult.peak(), buffer
);
} finally {
Expand Down Expand Up @@ -336,4 +341,22 @@ private record SortResult(
) {
}

/**
* The SkippingRecordConsumer is an implementation of the {@link IntConsumer} interface that wraps an optional delegate
* {@code IntConsumer} and tracks the count of records processed. This class is intended to be used for scenarios
* where certain records need to be tracked, possibly skipped, or processed with optional side effects.
*/
@RequiredArgsConstructor
private static class SkippingRecordConsumer implements IntConsumer {
@Nullable private final IntConsumer delegate;
@Getter private int counter = 0;

@Override
public void accept(int value) {
if (this.delegate != null) {
this.delegate.accept(value);
}
this.counter++;
}
}
}
Loading