You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by "github-code-scanning[bot] (via GitHub)" <gi...@apache.org> on 2023/06/01 00:48:53 UTC

[GitHub] [datasketches-java] github-code-scanning[bot] commented on a diff in pull request #448: Revert items changes prepare4.1.0

github-code-scanning[bot] commented on code in PR #448:
URL: https://github.com/apache/datasketches-java/pull/448#discussion_r1212451635


##########
src/main/java/org/apache/datasketches/kll/KllHelper.java:
##########
@@ -562,20 +605,24 @@
   static byte[] toCompactByteArrayImpl(final KllSketch sketch) {
     if (sketch.isEmpty()) { return fastEmptyCompactByteArray(sketch); }
     if (sketch.isSingleItem()) { return fastSingleItemCompactByteArray(sketch); }
+
     final byte[] byteArr = new byte[sketch.getCurrentCompactSerializedSizeBytes()];
     final WritableMemory wmem = WritableMemory.writableWrap(byteArr);
     loadFirst8Bytes(sketch, wmem, false);
     if (sketch.getN() == 0) { return byteArr; } //empty
-    final boolean doubleType = (sketch.sketchType == DOUBLES_SKETCH);
+    final KllDoublesSketch dblSk = (sketch.sketchType == DOUBLES_SKETCH) ? (KllDoublesSketch)sketch : null;
+    final KllFloatsSketch fltSk = (sketch.sketchType == FLOATS_SKETCH) ? (KllFloatsSketch)sketch : null;
+    //TODO add items
 
     //load data
     int offset = DATA_START_ADR_SINGLE_ITEM;
     final int[] myLevelsArr = sketch.getLevelsArray();
     if (sketch.getN() == 1) { //single item
-      if (doubleType) {
-        wmem.putDouble(offset,  sketch.getDoubleItemsArray()[myLevelsArr[0]]);
-      } else {
-        wmem.putFloat(offset, sketch.getFloatItemsArray()[myLevelsArr[0]]);
+      switch (sketch.sketchType) {
+        case DOUBLES_SKETCH : wmem.putDouble(offset,  dblSk.getDoubleItemsArray()[myLevelsArr[0]]); break;

Review Comment:
   ## Dereferenced variable may be null
   
   Variable [dblSk](1) may be null at this access because of [this](2) assignment.
   
   [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/550)



##########
src/main/java/org/apache/datasketches/kll/KllFloatsProxy.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static org.apache.datasketches.kll.KllSketch.Error.TGT_IS_READ_ONLY;
+import static org.apache.datasketches.kll.KllSketch.Error.kllSketchThrow;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+abstract class KllFloatsProxy extends KllSketch {
+
+  KllFloatsProxy(final WritableMemory wmem, final MemoryRequestServer memReqSvr) {
+    super(SketchType.FLOATS_SKETCH, wmem, memReqSvr);
+  }
+
+  /**
+   * @return full size of internal items array including garbage.
+   */
+  abstract float[] getFloatItemsArray();
+
+  abstract float getFloatSingleItem();
+
+  abstract float getMaxFloatItem();
+
+  abstract float getMinFloatItem();
+
+  abstract void setFloatItemsArray(float[] floatItems);
+
+  abstract void setFloatItemsArrayAt(int index, float item);
+
+  abstract void setMaxFloatItem(float item);
+
+  abstract void setMinFloatItem(float item);
+
+  /**
+   * Merges another sketch into this one.
+   * Attempting to merge a KllDoublesSketch with a KllFloatsSketch will
+   * throw an exception.
+   * @param other sketch to merge into this one
+   */
+  public final void merge(final KllFloatsSketch other) {

Review Comment:
   ## Confusing overloading of methods
   
   Method KllFloatsProxy.merge(..) could be confused with overloaded method [KllSketch.merge](1), since dispatch depends on static types.
   
   [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/548)



##########
src/main/java/org/apache/datasketches/kll/KllHelper.java:
##########
@@ -665,28 +743,44 @@
     } else {
       sb.append("   Compact Storage Bytes  : ").append(sketch.getCurrentCompactSerializedSizeBytes()).append(Util.LS);
     }
-
-    if (doubleType) {
-      sb.append("   Min Item               : ").append(sketch.getMinDoubleItem()).append(Util.LS);
-      sb.append("   Max Item               : ").append(sketch.getMaxDoubleItem()).append(Util.LS);
-    } else {
-      sb.append("   Min Item               : ").append(sketch.getMinFloatItem()).append(Util.LS);
-      sb.append("   Max Item               : ").append(sketch.getMaxFloatItem()).append(Util.LS);
+    KllDoublesSketch dblSk = null;
+    KllFloatsSketch fltSk = null;
+    //final KllItemsSketch itmSk = null;
+
+    if (sketchType == DOUBLES_SKETCH) {
+      dblSk = (KllDoublesSketch) sketch;
+      sb.append("   Min Item               : ").append(dblSk.getMinDoubleItem()).append(Util.LS);
+      sb.append("   Max Item               : ").append(dblSk.getMaxDoubleItem()).append(Util.LS);
+    } else if (sketchType == FLOATS_SKETCH) {
+      fltSk = (KllFloatsSketch) sketch;
+      sb.append("   Min Item               : ").append(fltSk.getMinFloatItem()).append(Util.LS);
+      sb.append("   Max Item               : ").append(fltSk.getMaxFloatItem()).append(Util.LS);
     }
+//    else {
+//      itmSk = (KllItemsSketch) sketch;
+//      sb.append("   Min Item               : ").append(itmSk.getMinItem()).append(Util.LS);
+//      sb.append("   Max Item               : ").append(itmSk.getMaxItem()).append(Util.LS);
+//    }
     sb.append("### End sketch summary").append(Util.LS);
 
     double[] myDoubleItemsArr = null;
     float[] myFloatItemsArr = null;
-    if (doubleType) {
-      myDoubleItemsArr = sketch.getDoubleItemsArray();
-    } else {
-      myFloatItemsArr = sketch.getFloatItemsArray();
-    }
+    Object[] myItemsArr = null;
+
     if (withLevels) {
       sb.append(outputLevels(k, m, numLevels, levelsArr));
     }
     if (withData) {
-      sb.append(outputData(doubleType, numLevels, levelsArr, myFloatItemsArr, myDoubleItemsArr));
+      if (sketchType == DOUBLES_SKETCH) {
+        myDoubleItemsArr = dblSk.getDoubleItemsArray();
+        sb.append(outputDoublesData(numLevels, levelsArr, myDoubleItemsArr));
+      } else if (sketchType == FLOATS_SKETCH) {
+        myFloatItemsArr = fltSk.getFloatItemsArray();

Review Comment:
   ## Dereferenced variable may be null
   
   Variable [fltSk](1) may be null at this access because of [this](2) assignment.
   
   [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/554)



##########
src/main/java/org/apache/datasketches/kll/KllHelper.java:
##########
@@ -793,18 +907,21 @@
     float[] myNewFloatItemsArr = null;
     double[] myNewDoubleItemsArr = null;
 
-    if (sketch.sketchType == DOUBLES_SKETCH) {
-      minDouble = sketch.getMinDoubleItem();
-      maxDouble = sketch.getMaxDoubleItem();
-      myCurDoubleItemsArr = sketch.getDoubleItemsArray();
+    if (sketchType == DOUBLES_SKETCH) {
+      minDouble = dblSk.getMinDoubleItem();
+      maxDouble = dblSk.getMaxDoubleItem();
+      myCurDoubleItemsArr = dblSk.getDoubleItemsArray();
       //assert we are following a certain growth scheme
       assert myCurDoubleItemsArr.length == myCurTotalItemsCapacity;
-    } else { //FLOATS_SKETCH
-      minFloat = sketch.getMinFloatItem();
-      maxFloat = sketch.getMaxFloatItem();
-      myCurFloatItemsArr = sketch.getFloatItemsArray();
+    } else if (sketchType == FLOATS_SKETCH) {
+      minFloat = fltSk.getMinFloatItem();

Review Comment:
   ## Dereferenced variable may be null
   
   Variable [fltSk](1) may be null at this access because of [this](2) assignment.
   
   [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/555)



##########
src/main/java/org/apache/datasketches/kll/KllDoublesProxy.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.kll;
+
+import static org.apache.datasketches.kll.KllSketch.Error.TGT_IS_READ_ONLY;
+import static org.apache.datasketches.kll.KllSketch.Error.kllSketchThrow;
+
+import org.apache.datasketches.memory.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableMemory;
+
+abstract class KllDoublesProxy extends KllSketch {
+
+  KllDoublesProxy(final WritableMemory wmem, final MemoryRequestServer memReqSvr) {
+    super(SketchType.DOUBLES_SKETCH, wmem, memReqSvr);
+  }
+
+  /**
+   * @return full size of internal items array including garbage.
+   */
+  abstract double[] getDoubleItemsArray();
+
+  abstract double getDoubleSingleItem();
+
+  abstract double getMaxDoubleItem();
+
+  abstract double getMinDoubleItem();
+
+  abstract void setDoubleItemsArray(double[] doubleItems);
+
+  abstract void setDoubleItemsArrayAt(int index, double item);
+
+  abstract void setMaxDoubleItem(double item);
+
+  abstract void setMinDoubleItem(double item);
+
+  /**
+   * Merges another sketch into this one.
+   * Attempting to merge a KllDoublesSketch with a KllFloatsSketch will
+   * throw an exception.
+   * @param other sketch to merge into this one
+   */
+  public final void merge(final KllDoublesSketch other) {

Review Comment:
   ## Confusing overloading of methods
   
   Method KllDoublesProxy.merge(..) could be confused with overloaded method [KllSketch.merge](1), since dispatch depends on static types.
   
   [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/547)



##########
src/main/java/org/apache/datasketches/kll/KllHelper.java:
##########
@@ -590,63 +637,94 @@
       offset += len * Integer.BYTES;
 
       //LOAD MIN, MAX Items FOLLOWED BY ITEMS ARRAY
-      if (doubleType) {
-        wmem.putDouble(offset,sketch.getMinDoubleItem());
-        offset += Double.BYTES;
-        wmem.putDouble(offset, sketch.getMaxDoubleItem());
-        offset += Double.BYTES;
-        wmem.putDoubleArray(offset, sketch.getDoubleItemsArray(), myLevelsArr[0], sketch.getNumRetained());
-      } else {
-        wmem.putFloat(offset, sketch.getMinFloatItem());
-        offset += Float.BYTES;
-        wmem.putFloat(offset, sketch.getMaxFloatItem());
-        offset += Float.BYTES;
-        wmem.putFloatArray(offset, sketch.getFloatItemsArray(), myLevelsArr[0], sketch.getNumRetained());
+      switch (sketch.sketchType) {
+        case DOUBLES_SKETCH : {
+          wmem.putDouble(offset,dblSk.getMinDoubleItem());

Review Comment:
   ## Dereferenced variable may be null
   
   Variable [dblSk](1) may be null at this access because of [this](2) assignment.
   
   [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/552)



##########
src/main/java/org/apache/datasketches/kll/KllHelper.java:
##########
@@ -590,63 +637,94 @@
       offset += len * Integer.BYTES;
 
       //LOAD MIN, MAX Items FOLLOWED BY ITEMS ARRAY
-      if (doubleType) {
-        wmem.putDouble(offset,sketch.getMinDoubleItem());
-        offset += Double.BYTES;
-        wmem.putDouble(offset, sketch.getMaxDoubleItem());
-        offset += Double.BYTES;
-        wmem.putDoubleArray(offset, sketch.getDoubleItemsArray(), myLevelsArr[0], sketch.getNumRetained());
-      } else {
-        wmem.putFloat(offset, sketch.getMinFloatItem());
-        offset += Float.BYTES;
-        wmem.putFloat(offset, sketch.getMaxFloatItem());
-        offset += Float.BYTES;
-        wmem.putFloatArray(offset, sketch.getFloatItemsArray(), myLevelsArr[0], sketch.getNumRetained());
+      switch (sketch.sketchType) {
+        case DOUBLES_SKETCH : {
+          wmem.putDouble(offset,dblSk.getMinDoubleItem());
+          offset += Double.BYTES;
+          wmem.putDouble(offset, dblSk.getMaxDoubleItem());
+          offset += Double.BYTES;
+          wmem.putDoubleArray(offset, dblSk.getDoubleItemsArray(), myLevelsArr[0], sketch.getNumRetained());
+          break;
+        }
+        case FLOATS_SKETCH  : {
+          wmem.putFloat(offset, fltSk.getMinFloatItem());

Review Comment:
   ## Dereferenced variable may be null
   
   Variable [fltSk](1) may be null at this access because of [this](2) assignment.
   
   [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/553)



##########
src/main/java/org/apache/datasketches/kll/KllHelper.java:
##########
@@ -562,20 +605,24 @@
   static byte[] toCompactByteArrayImpl(final KllSketch sketch) {
     if (sketch.isEmpty()) { return fastEmptyCompactByteArray(sketch); }
     if (sketch.isSingleItem()) { return fastSingleItemCompactByteArray(sketch); }
+
     final byte[] byteArr = new byte[sketch.getCurrentCompactSerializedSizeBytes()];
     final WritableMemory wmem = WritableMemory.writableWrap(byteArr);
     loadFirst8Bytes(sketch, wmem, false);
     if (sketch.getN() == 0) { return byteArr; } //empty
-    final boolean doubleType = (sketch.sketchType == DOUBLES_SKETCH);
+    final KllDoublesSketch dblSk = (sketch.sketchType == DOUBLES_SKETCH) ? (KllDoublesSketch)sketch : null;
+    final KllFloatsSketch fltSk = (sketch.sketchType == FLOATS_SKETCH) ? (KllFloatsSketch)sketch : null;
+    //TODO add items
 
     //load data
     int offset = DATA_START_ADR_SINGLE_ITEM;
     final int[] myLevelsArr = sketch.getLevelsArray();
     if (sketch.getN() == 1) { //single item
-      if (doubleType) {
-        wmem.putDouble(offset,  sketch.getDoubleItemsArray()[myLevelsArr[0]]);
-      } else {
-        wmem.putFloat(offset, sketch.getFloatItemsArray()[myLevelsArr[0]]);
+      switch (sketch.sketchType) {
+        case DOUBLES_SKETCH : wmem.putDouble(offset,  dblSk.getDoubleItemsArray()[myLevelsArr[0]]); break;
+        case FLOATS_SKETCH  : wmem.putFloat(offset, fltSk.getFloatItemsArray()[myLevelsArr[0]]); break;

Review Comment:
   ## Dereferenced variable may be null
   
   Variable [fltSk](1) may be null at this access because of [this](2) assignment.
   
   [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/570)



##########
src/main/java/org/apache/datasketches/kll/KllHelper.java:
##########
@@ -849,14 +969,14 @@
     //update our sketch with new expanded spaces
     sketch.setNumLevels(myNewNumLevels);
     sketch.setLevelsArray(myNewLevelsArr);
-    if (sketch.sketchType == DOUBLES_SKETCH) {
-      sketch.setMinDoubleItem(minDouble);
-      sketch.setMaxDoubleItem(maxDouble);
-      sketch.setDoubleItemsArray(myNewDoubleItemsArr);
+    if (sketchType == DOUBLES_SKETCH) {
+      dblSk.setMinDoubleItem(minDouble);
+      dblSk.setMaxDoubleItem(maxDouble);
+      dblSk.setDoubleItemsArray(myNewDoubleItemsArr);
     } else { //Float sketch
-      sketch.setMinFloatItem(minFloat);
-      sketch.setMaxFloatItem(maxFloat);
-      sketch.setFloatItemsArray(myNewFloatItemsArr);
+      fltSk.setMinFloatItem(minFloat);

Review Comment:
   ## Dereferenced variable may be null
   
   Variable [fltSk](1) may be null at this access because of [this](2) assignment.
   
   [Show more details](https://github.com/apache/datasketches-java/security/code-scanning/556)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org