diff --git a/core/trino-main/src/main/java/io/trino/operator/VariableWidthData.java b/core/trino-main/src/main/java/io/trino/operator/VariableWidthData.java index 8891d0c8ba49..6e6808d9562b 100644 --- a/core/trino-main/src/main/java/io/trino/operator/VariableWidthData.java +++ b/core/trino-main/src/main/java/io/trino/operator/VariableWidthData.java @@ -31,6 +31,7 @@ import static io.airlift.slice.SizeOf.sizeOfObjectArray; import static io.trino.operator.FlatHash.sumExact; import static java.lang.Math.addExact; +import static java.lang.Math.clamp; import static java.lang.Math.max; import static java.lang.Math.subtractExact; import static java.util.Objects.checkIndex; @@ -117,7 +118,7 @@ public byte[] allocate(byte[] pointer, int pointerOffset, int size) // allocate enough space for 32 values of the current size, or double the current chunk size, whichever is larger int newSize = Ints.saturatedCast(max(size * 32L, openChunk.length * 2L)); // constrain to be between min and max chunk size - newSize = Ints.constrainToRange(newSize, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE); + newSize = clamp(newSize, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE); // jumbo rows get a separate allocation newSize = max(newSize, size); openChunk = new byte[newSize]; diff --git a/core/trino-main/src/main/java/io/trino/operator/aggregation/AbstractMapAggregationState.java b/core/trino-main/src/main/java/io/trino/operator/aggregation/AbstractMapAggregationState.java index a18380534ad5..c283ad4bd7d8 100644 --- a/core/trino-main/src/main/java/io/trino/operator/aggregation/AbstractMapAggregationState.java +++ b/core/trino-main/src/main/java/io/trino/operator/aggregation/AbstractMapAggregationState.java @@ -14,7 +14,6 @@ package io.trino.operator.aggregation; import com.google.common.base.Throwables; -import com.google.common.primitives.Ints; import io.trino.operator.VariableWidthData; import io.trino.spi.TrinoException; import io.trino.spi.block.BlockBuilder; @@ -35,6 +34,7 @@ import static io.trino.operator.VariableWidthData.EMPTY_CHUNK; import static io.trino.operator.VariableWidthData.POINTER_SIZE; import static io.trino.spi.StandardErrorCode.GENERIC_INSUFFICIENT_RESOURCES; +import static java.lang.Math.clamp; import static java.lang.Math.multiplyExact; import static java.nio.ByteOrder.LITTLE_ENDIAN; import static java.util.Objects.checkIndex; @@ -215,7 +215,7 @@ public void setMaxGroupId(int maxGroupId) int currentSize = groupRecordIndex.length; if (requiredSize > currentSize) { - groupRecordIndex = Arrays.copyOf(groupRecordIndex, Ints.constrainToRange(requiredSize * 2, 1024, MAX_ARRAY_SIZE)); + groupRecordIndex = Arrays.copyOf(groupRecordIndex, clamp(requiredSize * 2L, 1024, MAX_ARRAY_SIZE)); Arrays.fill(groupRecordIndex, currentSize, groupRecordIndex.length, -1); } } diff --git a/core/trino-main/src/main/java/io/trino/operator/aggregation/arrayagg/GroupArrayAggregationState.java b/core/trino-main/src/main/java/io/trino/operator/aggregation/arrayagg/GroupArrayAggregationState.java index 84381a9aa0ac..ce0825ac43f8 100644 --- a/core/trino-main/src/main/java/io/trino/operator/aggregation/arrayagg/GroupArrayAggregationState.java +++ b/core/trino-main/src/main/java/io/trino/operator/aggregation/arrayagg/GroupArrayAggregationState.java @@ -13,7 +13,6 @@ */ package io.trino.operator.aggregation.arrayagg; -import com.google.common.primitives.Ints; import io.trino.operator.aggregation.state.AbstractGroupedAccumulatorState; import io.trino.spi.block.BlockBuilder; import io.trino.spi.block.ValueBlock; @@ -25,6 +24,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static io.airlift.slice.SizeOf.instanceSize; import static io.airlift.slice.SizeOf.sizeOf; +import static java.lang.Math.clamp; import static java.lang.Math.toIntExact; public final class GroupArrayAggregationState @@ -60,7 +60,7 @@ public void ensureCapacity(long maxGroupId) checkArgument(maxGroupId + 1 < MAX_ARRAY_SIZE, "Maximum array size exceeded"); int requiredSize = toIntExact(maxGroupId + 1); if (requiredSize > groupHeadPositions.length) { - int newSize = Ints.constrainToRange(requiredSize * 2, 1024, MAX_ARRAY_SIZE); + int newSize = clamp(requiredSize * 2L, 1024, MAX_ARRAY_SIZE); int oldSize = groupHeadPositions.length; groupHeadPositions = Arrays.copyOf(groupHeadPositions, newSize); diff --git a/core/trino-main/src/main/java/io/trino/operator/aggregation/histogram/TypedHistogram.java b/core/trino-main/src/main/java/io/trino/operator/aggregation/histogram/TypedHistogram.java index f4763b6c0037..5881109df43c 100644 --- a/core/trino-main/src/main/java/io/trino/operator/aggregation/histogram/TypedHistogram.java +++ b/core/trino-main/src/main/java/io/trino/operator/aggregation/histogram/TypedHistogram.java @@ -14,7 +14,6 @@ package io.trino.operator.aggregation.histogram; import com.google.common.base.Throwables; -import com.google.common.primitives.Ints; import io.trino.operator.VariableWidthData; import io.trino.spi.TrinoException; import io.trino.spi.block.BlockBuilder; @@ -36,6 +35,7 @@ import static io.trino.operator.VariableWidthData.POINTER_SIZE; import static io.trino.spi.StandardErrorCode.GENERIC_INSUFFICIENT_RESOURCES; import static io.trino.spi.type.BigintType.BIGINT; +import static java.lang.Math.clamp; import static java.lang.Math.multiplyExact; import static java.nio.ByteOrder.LITTLE_ENDIAN; import static java.util.Objects.checkIndex; @@ -167,7 +167,7 @@ public void setMaxGroupId(int maxGroupId) int currentSize = groupRecordIndex.length; if (requiredSize > currentSize) { - groupRecordIndex = Arrays.copyOf(groupRecordIndex, Ints.constrainToRange(requiredSize * 2, 1024, MAX_ARRAY_SIZE)); + groupRecordIndex = Arrays.copyOf(groupRecordIndex, clamp(requiredSize * 2L, 1024, MAX_ARRAY_SIZE)); Arrays.fill(groupRecordIndex, currentSize, groupRecordIndex.length, -1); } } diff --git a/core/trino-main/src/main/java/io/trino/operator/aggregation/listagg/GroupListaggAggregationState.java b/core/trino-main/src/main/java/io/trino/operator/aggregation/listagg/GroupListaggAggregationState.java index 0da363839766..ef381f7aaed5 100644 --- a/core/trino-main/src/main/java/io/trino/operator/aggregation/listagg/GroupListaggAggregationState.java +++ b/core/trino-main/src/main/java/io/trino/operator/aggregation/listagg/GroupListaggAggregationState.java @@ -13,7 +13,6 @@ */ package io.trino.operator.aggregation.listagg; -import com.google.common.primitives.Ints; import io.airlift.slice.DynamicSliceOutput; import io.airlift.slice.SliceOutput; import io.trino.spi.block.ValueBlock; @@ -28,6 +27,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static io.airlift.slice.SizeOf.sizeOf; import static io.trino.operator.VariableWidthData.POINTER_SIZE; +import static java.lang.Math.clamp; import static java.lang.Math.toIntExact; import static java.nio.ByteOrder.LITTLE_ENDIAN; @@ -90,7 +90,7 @@ public void ensureCapacity(long maxGroupId) checkArgument(maxGroupId + 1 < MAX_ARRAY_SIZE, "Maximum array size exceeded"); int requiredSize = toIntExact(maxGroupId + 1); if (requiredSize > groupHeadPositions.length) { - int newSize = Ints.constrainToRange(requiredSize * 2, 1024, MAX_ARRAY_SIZE); + int newSize = clamp(requiredSize * 2L, 1024, MAX_ARRAY_SIZE); int oldSize = groupHeadPositions.length; groupHeadPositions = Arrays.copyOf(groupHeadPositions, newSize); diff --git a/core/trino-main/src/main/java/io/trino/operator/aggregation/minmaxn/TypedHeap.java b/core/trino-main/src/main/java/io/trino/operator/aggregation/minmaxn/TypedHeap.java index 7ba0168077d8..2314add9430c 100644 --- a/core/trino-main/src/main/java/io/trino/operator/aggregation/minmaxn/TypedHeap.java +++ b/core/trino-main/src/main/java/io/trino/operator/aggregation/minmaxn/TypedHeap.java @@ -14,7 +14,6 @@ package io.trino.operator.aggregation.minmaxn; import com.google.common.base.Throwables; -import com.google.common.primitives.Ints; import io.airlift.slice.SizeOf; import io.trino.operator.VariableWidthData; import io.trino.spi.block.BlockBuilder; @@ -36,6 +35,7 @@ import static io.trino.operator.VariableWidthData.getChunkOffset; import static io.trino.operator.VariableWidthData.getValueLength; import static io.trino.operator.VariableWidthData.writePointer; +import static java.lang.Math.clamp; import static java.util.Objects.requireNonNull; public final class TypedHeap @@ -382,7 +382,7 @@ public static VariableWidthData compactIfNecessary(VariableWidthData data, byte[ if (newSize > 0) { int openSliceSize = newSize; if (newSize < MAX_CHUNK_SIZE) { - openSliceSize = Ints.constrainToRange(Ints.saturatedCast(openSliceSize * 2L), MIN_CHUNK_SIZE, MAX_CHUNK_SIZE); + openSliceSize = clamp(openSliceSize * 2L, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE); } moveVariableWidthToNewSlice(data, fixedSizeChunk, fixedRecordSize, fixedRecordPointerOffset, indexStart, recordCount, newSlices, openSliceSize, relocateVariableWidthOffsets); openChunkOffset = newSize; diff --git a/core/trino-main/src/main/java/io/trino/operator/aggregation/multimapagg/AbstractMultimapAggregationState.java b/core/trino-main/src/main/java/io/trino/operator/aggregation/multimapagg/AbstractMultimapAggregationState.java index 0d2d57fffd9a..74b79f418705 100644 --- a/core/trino-main/src/main/java/io/trino/operator/aggregation/multimapagg/AbstractMultimapAggregationState.java +++ b/core/trino-main/src/main/java/io/trino/operator/aggregation/multimapagg/AbstractMultimapAggregationState.java @@ -14,7 +14,6 @@ package io.trino.operator.aggregation.multimapagg; import com.google.common.base.Throwables; -import com.google.common.primitives.Ints; import io.trino.operator.VariableWidthData; import io.trino.operator.aggregation.arrayagg.FlatArrayBuilder; import io.trino.spi.TrinoException; @@ -41,6 +40,7 @@ import static io.trino.operator.VariableWidthData.EMPTY_CHUNK; import static io.trino.operator.VariableWidthData.POINTER_SIZE; import static io.trino.spi.StandardErrorCode.GENERIC_INSUFFICIENT_RESOURCES; +import static java.lang.Math.clamp; import static java.lang.Math.multiplyExact; import static java.nio.ByteOrder.LITTLE_ENDIAN; import static java.util.Objects.checkIndex; @@ -220,7 +220,7 @@ public void setMaxGroupId(int maxGroupId) int currentSize = groupRecordIndex.length; if (requiredSize > currentSize) { - groupRecordIndex = Arrays.copyOf(groupRecordIndex, Ints.constrainToRange(requiredSize * 2, 1024, MAX_ARRAY_SIZE)); + groupRecordIndex = Arrays.copyOf(groupRecordIndex, clamp(requiredSize * 2L, 1024, MAX_ARRAY_SIZE)); Arrays.fill(groupRecordIndex, currentSize, groupRecordIndex.length, -1); } } @@ -417,7 +417,7 @@ private int insert(int keyIndex, int groupId, ValueBlock keyBlock, int keyPositi } if (nextKeyId >= keyHeadPositions.length) { - int newSize = Ints.constrainToRange(nextKeyId * 2, 1024, MAX_ARRAY_SIZE); + int newSize = clamp(nextKeyId * 2L, 1024, MAX_ARRAY_SIZE); int oldSize = keyHeadPositions.length; keyHeadPositions = Arrays.copyOf(keyHeadPositions, newSize); diff --git a/lib/trino-filesystem-azure/src/main/java/io/trino/filesystem/azure/AzureInputStream.java b/lib/trino-filesystem-azure/src/main/java/io/trino/filesystem/azure/AzureInputStream.java index 9de687b3155a..5acc3846eb0f 100644 --- a/lib/trino-filesystem-azure/src/main/java/io/trino/filesystem/azure/AzureInputStream.java +++ b/lib/trino-filesystem-azure/src/main/java/io/trino/filesystem/azure/AzureInputStream.java @@ -23,8 +23,8 @@ import java.io.IOException; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.primitives.Longs.constrainToRange; import static io.trino.filesystem.azure.AzureUtils.handleAzureException; +import static java.lang.Math.clamp; import static java.util.Objects.checkFromIndexSize; import static java.util.Objects.requireNonNull; @@ -129,7 +129,7 @@ public long skip(long n) { ensureOpen(); - long skipSize = constrainToRange(n, 0, fileSize - nextPosition); + long skipSize = clamp(n, 0, fileSize - nextPosition); nextPosition += skipSize; return skipSize; } diff --git a/lib/trino-filesystem-gcs/src/main/java/io/trino/filesystem/gcs/GcsInputStream.java b/lib/trino-filesystem-gcs/src/main/java/io/trino/filesystem/gcs/GcsInputStream.java index e7bb42b4481d..a5fb6460380c 100644 --- a/lib/trino-filesystem-gcs/src/main/java/io/trino/filesystem/gcs/GcsInputStream.java +++ b/lib/trino-filesystem-gcs/src/main/java/io/trino/filesystem/gcs/GcsInputStream.java @@ -23,9 +23,9 @@ import java.nio.ByteBuffer; import java.util.OptionalLong; -import static com.google.common.primitives.Longs.constrainToRange; import static io.trino.filesystem.gcs.GcsUtils.getReadChannel; import static io.trino.filesystem.gcs.GcsUtils.handleGcsException; +import static java.lang.Math.clamp; import static java.util.Objects.checkFromIndexSize; import static java.util.Objects.requireNonNull; @@ -134,7 +134,7 @@ public long skip(long n) throws IOException { ensureOpen(); - long skipSize = constrainToRange(n, 0, fileSize - nextPosition); + long skipSize = clamp(n, 0, fileSize - nextPosition); nextPosition += skipSize; return skipSize; } diff --git a/lib/trino-filesystem-s3/src/main/java/io/trino/filesystem/s3/S3OutputStream.java b/lib/trino-filesystem-s3/src/main/java/io/trino/filesystem/s3/S3OutputStream.java index 8259e2928900..731a56752cc7 100644 --- a/lib/trino-filesystem-s3/src/main/java/io/trino/filesystem/s3/S3OutputStream.java +++ b/lib/trino-filesystem-s3/src/main/java/io/trino/filesystem/s3/S3OutputStream.java @@ -39,7 +39,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import static com.google.common.primitives.Ints.constrainToRange; +import static java.lang.Math.clamp; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.System.arraycopy; @@ -179,7 +179,7 @@ private void ensureCapacity(int extra) int target = max(buffer.length, initialBufferSize); if (target < capacity) { target += target / 2; // increase 50% - target = constrainToRange(target, capacity, partSize); + target = clamp(target, capacity, partSize); } buffer = Arrays.copyOf(buffer, target); memoryContext.setBytes(buffer.length); diff --git a/lib/trino-filesystem/src/main/java/io/trino/filesystem/local/LocalInputStream.java b/lib/trino-filesystem/src/main/java/io/trino/filesystem/local/LocalInputStream.java index 0107daaae5b0..1688f4ff4ab4 100644 --- a/lib/trino-filesystem/src/main/java/io/trino/filesystem/local/LocalInputStream.java +++ b/lib/trino-filesystem/src/main/java/io/trino/filesystem/local/LocalInputStream.java @@ -14,7 +14,6 @@ package io.trino.filesystem.local; import com.google.common.primitives.Ints; -import com.google.common.primitives.Longs; import io.trino.filesystem.Location; import io.trino.filesystem.TrinoInputStream; @@ -26,6 +25,7 @@ import java.io.IOException; import java.io.InputStream; +import static java.lang.Math.clamp; import static java.util.Objects.requireNonNull; class LocalInputStream @@ -135,7 +135,7 @@ public long skip(long length) { ensureOpen(); - length = Longs.constrainToRange(length, 0, fileLength - position); + length = clamp(length, 0, fileLength - position); seek(position + length); return length; } diff --git a/lib/trino-hive-formats/src/main/java/io/trino/hive/formats/line/LineBuffer.java b/lib/trino-hive-formats/src/main/java/io/trino/hive/formats/line/LineBuffer.java index 059e08b03a64..e346adb10829 100644 --- a/lib/trino-hive-formats/src/main/java/io/trino/hive/formats/line/LineBuffer.java +++ b/lib/trino-hive-formats/src/main/java/io/trino/hive/formats/line/LineBuffer.java @@ -21,9 +21,9 @@ import static com.google.common.base.MoreObjects.toStringHelper; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.primitives.Ints.constrainToRange; import static io.airlift.slice.SizeOf.instanceSize; import static io.airlift.slice.SizeOf.sizeOf; +import static java.lang.Math.clamp; public final class LineBuffer extends OutputStream @@ -112,7 +112,7 @@ private void growBufferIfNecessary(int minFreeSpace) throw new IOException("Max line length exceeded: " + newLength); } if (buffer.length < newLength) { - int newSize = constrainToRange(buffer.length * 2, newLength, maxLength); + int newSize = clamp(buffer.length * 2L, newLength, maxLength); buffer = Arrays.copyOf(buffer, newSize); } } diff --git a/plugin/trino-hudi/src/main/java/io/trino/plugin/hudi/split/SizeBasedSplitWeightProvider.java b/plugin/trino-hudi/src/main/java/io/trino/plugin/hudi/split/SizeBasedSplitWeightProvider.java index b7abb9c4d7eb..e94e2d73c373 100644 --- a/plugin/trino-hudi/src/main/java/io/trino/plugin/hudi/split/SizeBasedSplitWeightProvider.java +++ b/plugin/trino-hudi/src/main/java/io/trino/plugin/hudi/split/SizeBasedSplitWeightProvider.java @@ -17,7 +17,7 @@ import io.trino.spi.SplitWeight; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.primitives.Doubles.constrainToRange; +import static java.lang.Math.clamp; import static java.util.Objects.requireNonNull; public class SizeBasedSplitWeightProvider @@ -42,6 +42,6 @@ public SplitWeight calculateSplitWeight(long splitSizeInBytes) { double computedWeight = splitSizeInBytes / standardSplitSizeInBytes; // Clamp the value between the minimum weight and 1.0 (standard weight) - return SplitWeight.fromProportion(constrainToRange(computedWeight, minimumWeight, 1.0)); + return SplitWeight.fromProportion(clamp(computedWeight, minimumWeight, 1.0)); } }