Skip to content

Commit

Permalink
Fixed the CodeQL issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
leerho committed Nov 16, 2023
1 parent 4747f67 commit 9237a28
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
20 changes: 10 additions & 10 deletions src/main/java/org/apache/datasketches/partitions/Partitioner.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
/**
* A partitioning process that can partition very large data sets into thousands to millions
* of partitions of approximately the same size.
* @param T the data type
* @param S the quantiles sketch that implements both QuantilesGenericAPI and PartitioningFeature.
* @param <T> the data type
* @param <S> the quantiles sketch that implements both QuantilesGenericAPI and PartitioningFeature.
*/
//@SuppressWarnings("unused")
public class Partitioner<T, S extends QuantilesGenericAPI<T> & PartitioningFeature<T>> {
Expand Down Expand Up @@ -108,7 +108,7 @@ public List<PartitionBoundsRow<T>> partition(final S sk) {
final int partsPerSk = (int)round(pow(guessNumParts, 1.0 / numLevels));
this.partitionsPerSk = min(partsPerSk, maxPartsPerSk);
final GenericPartitionBoundaries<T> gpb = sk.getPartitionBoundaries(partitionsPerSk, criteria);
final StackElement<T> se = new StackElement<>(gpb, stack.size() + 1, 0, "1");
final StackElement<T> se = new StackElement<>(gpb, 0, "1");
stack.push(se);
partitionSearch(stack);
return finalPartitionList;
Expand Down Expand Up @@ -136,8 +136,8 @@ private void partitionSearch(final Stack<StackElement<T>> stack) {
final S sk = fillReq.getRange(row.lowerBound, row.upperBound, row.rule);
final GenericPartitionBoundaries<T> gpb2 = sk.getPartitionBoundaries(this.partitionsPerSk, criteria);
final int level = stack.size() + 1;
final String partId = se.partId + "." + se.part + "," + level;
final StackElement<T> se2 = new StackElement<>(gpb2, level, 0, partId);
final String partId = se.levelPartId + "." + se.part + "," + level;
final StackElement<T> se2 = new StackElement<>(gpb2, 0, partId);
stack.push(se2);
partitionSearch(stack);
}
Expand All @@ -156,12 +156,12 @@ private void partitionSearch(final Stack<StackElement<T>> stack) {
public static class StackElement<T> {
public final GenericPartitionBoundaries<T> gpb;
public int part;
public String partId;
public String levelPartId;

public StackElement(final GenericPartitionBoundaries<T> gpb, final int level, final int part, final String partId) {
public StackElement(final GenericPartitionBoundaries<T> gpb, final int part, final String levelPartId) {
this.gpb = gpb;
this.part = part;
this.partId = partId;
this.levelPartId = levelPartId;
}
}

Expand All @@ -170,7 +170,7 @@ public StackElement(final GenericPartitionBoundaries<T> gpb, final int level, fi
*/
public static class PartitionBoundsRow<T> {
public int part;
public String partId;
public String levelPartId;
public long approxNumDeltaItems;
public BoundsRule rule;
public T lowerBound;
Expand All @@ -179,7 +179,7 @@ public static class PartitionBoundsRow<T> {
public PartitionBoundsRow(final StackElement<T> se) {
final GenericPartitionBoundaries<T> gpb = se.gpb;
this.part = se.part;
this.partId = se.partId + "." + part;
this.levelPartId = se.levelPartId + "." + part;
final QuantileSearchCriteria searchCrit = gpb.getSearchCriteria();
final T[] boundaries = gpb.getBoundaries();
final int numParts = gpb.getNumPartitions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void outputList(final List<PartitionBoundsRow<String>> list) {
double sumSqErr = 0;
for (int i = 0; i < numParts; i++) {
final PartitionBoundsRow<String> row = list.get(i);
printf(dFmt, row.partId , (i + 1), row.lowerBound, row.upperBound, row.approxNumDeltaItems, row.rule.name());
printf(dFmt, row.levelPartId , (i + 1), row.lowerBound, row.upperBound, row.approxNumDeltaItems, row.rule.name());
size = row.approxNumDeltaItems;
sumSizes += size;
sumAbsRelErr += Math.abs(size / meanPartSize - 1.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Comparator;
import java.util.Random;

import org.apache.datasketches.common.SketchesArgumentException;
import org.apache.datasketches.quantiles.ItemsSketch;

/**
Expand All @@ -52,8 +53,11 @@ public ItemsSketchFillRequestLongAsString(final int k, final long totalN) {
public ItemsSketch<String> getRange(final String lowerQuantile, final String upperQuantile,
final BoundsRule bounds) {
final ItemsSketch<String> sk = ItemsSketch.getInstance(String.class, k, Comparator.naturalOrder());
final long lower = Long.parseLong(lowerQuantile.trim());
final long upper = Long.parseLong(upperQuantile.trim());
long upper, lower;
try {
lower = Long.parseLong(lowerQuantile.trim());
upper = Long.parseLong(upperQuantile.trim());
} catch (NumberFormatException e) { throw new SketchesArgumentException(e.toString()); }
if (bounds == INCLUDE_BOTH) {
for (long i = lower; i <= upper; i++) { sk.update(getString(i, numDigits)); }
} else if (bounds == INCLUDE_UPPER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Random;

import org.apache.datasketches.common.ArrayOfStringsSerDe;
import org.apache.datasketches.common.SketchesArgumentException;
import org.apache.datasketches.kll.KllItemsSketch;

/**
Expand All @@ -53,8 +54,11 @@ public KllItemsSketchFillRequestLongAsString(final int k, final long totalN) {
public KllItemsSketch<String> getRange(final String lowerQuantile, final String upperQuantile,
final BoundsRule bounds) {
KllItemsSketch<String> sk = KllItemsSketch.newHeapInstance(k, Comparator.naturalOrder(), new ArrayOfStringsSerDe());
long lower = Long.parseLong(lowerQuantile.trim());
long upper = Long.parseLong(upperQuantile.trim());
long upper, lower;
try {
lower = Long.parseLong(lowerQuantile.trim());
upper = Long.parseLong(upperQuantile.trim());
} catch (NumberFormatException e) { throw new SketchesArgumentException(e.toString()); }
if (bounds == INCLUDE_BOTH) {
for (long i = lower; i <= upper; i++) { sk.update(getString(i, numDigits)); }
} else if (bounds == INCLUDE_UPPER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void outputList(final List<PartitionBoundsRow<String>> list) {
double sumSqErr = 0;
for (int i = 0; i < numParts; i++) {
final PartitionBoundsRow<String> row = list.get(i);
printf(dFmt, row.partId , (i + 1), row.lowerBound, row.upperBound, row.approxNumDeltaItems, row.rule.name());
printf(dFmt, row.levelPartId , (i + 1), row.lowerBound, row.upperBound, row.approxNumDeltaItems, row.rule.name());
size = row.approxNumDeltaItems;
sumSizes += size;
sumAbsRelErr += Math.abs(size / meanPartSize - 1.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static java.lang.Math.log;
import static org.apache.datasketches.common.Util.characterPad;

import org.apache.datasketches.common.SketchesArgumentException;

/**
* Creates a string from a positive long value that is orderable in the
* same order as its long value.
Expand All @@ -46,7 +48,11 @@ public static String getString(final long value, final int numDigits) {
* @return the given String back to a long
*/
public static long getLong(final String value) {
return Long.parseLong(value.trim());
long out;
try { out = Long.parseLong(value.trim()); } catch (NumberFormatException e) {
throw new SketchesArgumentException(e.toString());
}
return out;
}

/**
Expand Down

0 comments on commit 9237a28

Please sign in to comment.