github-advanced-security[bot] commented on code in PR #785:
URL: https://github.com/apache/datasketches-java/pull/785#discussion_r4418231968
##########
src/main/java/org/apache/datasketches/tuple/TupleSketch.java:
##########
@@ -252,11 +266,31 @@
public static <S extends Summary> TupleSketch<S> heapifySketch(
final MemorySegment seg,
final SummaryDeserializer<S> deserializer) {
+ return heapifySketch(seg, deserializer, DEFAULT_UPDATE_SEED);
+ }
+
+ /**
+ * Instantiate a TupleSketch from a given MemorySegment, which was created
with the given update seed.
+ * The seed hash of a non-empty compact image must match the given seed.
+ * An updatable image does not store a seed hash and must have been created
with the default seed.
+ * @param <S> Type of Summary
+ * @param seg MemorySegment object representing a TupleSketch
+ * @param deserializer instance of SummaryDeserializer
+ * @param seed <a href="{@docRoot}/resources/dictionary.html#seed">See
Update Hash Seed</a>
+ * @return TupleSketch created from its MemorySegment representation
+ * @throws org.apache.datasketches.common.SketchesArgumentException if the
seed hashes do not match
+ */
+ public static <S extends Summary> TupleSketch<S> heapifySketch(
+ final MemorySegment seg,
+ final SummaryDeserializer<S> deserializer,
+ final long seed) {
final SerializerDeserializer.SketchType sketchType =
SerializerDeserializer.getSketchType(seg);
if (sketchType == SerializerDeserializer.SketchType.QuickSelectSketch) {
- return new QuickSelectSketch<>(seg, deserializer, null);
+ final QuickSelectSketch<S> sketch = new QuickSelectSketch<>(seg,
deserializer, null);
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [QuickSelectSketch.QuickSelectSketch](1) should be avoided because
it has been deprecated.
[Show more
details](https://github.com/apache/datasketches-java/security/code-scanning/978)
##########
src/test/java/org/apache/datasketches/tuple/TupleCustomSeedTest.java:
##########
@@ -0,0 +1,243 @@
+/*
+ * 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.tuple;
+
+import static org.apache.datasketches.common.Util.DEFAULT_UPDATE_SEED;
+import static org.apache.datasketches.common.Util.computeSeedHash;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.expectThrows;
+
+import java.lang.foreign.MemorySegment;
+
+import org.apache.datasketches.common.ByteArrayUtil;
+import org.apache.datasketches.common.ResizeFactor;
+import org.apache.datasketches.common.SketchesArgumentException;
+import org.apache.datasketches.common.SketchesStateException;
+import org.apache.datasketches.fdt.FdtSketch;
+import org.apache.datasketches.theta.UpdatableThetaSketch;
+import org.apache.datasketches.tuple.adouble.DoubleSummary;
+import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer;
+import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory;
+import org.apache.datasketches.tuple.adouble.DoubleSummarySetOperations;
+import org.apache.datasketches.tuple.adouble.DoubleTupleSketch;
+import org.apache.datasketches.tuple.aninteger.IntegerSummary;
+import org.apache.datasketches.tuple.aninteger.IntegerTupleSketch;
+import org.apache.datasketches.tuple.strings.ArrayOfStringsTupleSketch;
+import org.testng.annotations.Test;
+
+/**
+ * Checks TupleSketches and their set operations created with a custom update
seed.
+ */
+@SuppressWarnings("deprecation")
+public class TupleCustomSeedTest {
+ private static final long SEED = 123L;
+ private static final short SEED_HASH = computeSeedHash(SEED);
+ private static final short DEFAULT_SEED_HASH =
computeSeedHash(DEFAULT_UPDATE_SEED);
+ private static final int SEED_HASH_SHORT = 6;
+
+ private final DoubleSummaryFactory factory = new
DoubleSummaryFactory(DoubleSummary.Mode.Sum);
+ private final DoubleSummarySetOperations dsso = new
DoubleSummarySetOperations();
+ private final DoubleSummaryDeserializer deser = new
DoubleSummaryDeserializer();
+
+ private UpdatableTupleSketch<Double, DoubleSummary> tupleSketch(final long
seed, final int start, final int n) {
+ final UpdatableTupleSketch<Double, DoubleSummary> sk =
+ new UpdatableTupleSketchBuilder<>(factory).setSeed(seed).build();
+ for (int i = start; i < (start + n); i++) { sk.update(i, 1.0); }
+ return sk;
+ }
+
+ private static UpdatableThetaSketch thetaSketch(final long seed, final int
start, final int n) {
+ final UpdatableThetaSketch sk =
UpdatableThetaSketch.builder().setSeed(seed).build();
+ for (int i = start; i < (start + n); i++) { sk.update(i); }
+ return sk;
+ }
+
+ private static short seedHashOf(final byte[] bytes) {
+ return ByteArrayUtil.getShortLE(bytes, SEED_HASH_SHORT);
+ }
+
+ @Test
+ public void checkSeedHashIsCarried() {
+ final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED,
0, 10);
+ assertEquals(sk.getSeedHash(), SEED_HASH);
+ assertEquals(sk.copy().getSeedHash(), SEED_HASH);
+ assertEquals(sk.compact().getSeedHash(), SEED_HASH);
+ assertEquals(seedHashOf(sk.compact().toByteArray()), SEED_HASH);
+ assertEquals(tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).getSeedHash(),
DEFAULT_SEED_HASH);
+ }
+
+ @Test
+ public void checkDefaultSeedHashMatchesCpp() {
+ final byte[] bytes = tupleSketch(DEFAULT_UPDATE_SEED, 0,
10).compact().toByteArray();
+ assertEquals(seedHashOf(bytes), (short) 37836);
+ }
+
+ @Test
+ public void checkSeedChangesHashes() {
+ assertNotEquals(hashOf(tupleSketch(SEED, 0, 1)),
hashOf(tupleSketch(DEFAULT_UPDATE_SEED, 0, 1)));
+ }
+
+ @Test
+ public void checkHeapifyCompact() {
+ final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED,
0, 100);
+ final MemorySegment seg =
MemorySegment.ofArray(sk.compact().toByteArray());
+ expectThrows(SketchesArgumentException.class, () ->
TupleSketch.heapifySketch(seg, deser));
+ final TupleSketch<DoubleSummary> sk2 = TupleSketch.heapifySketch(seg,
deser, SEED);
+ assertEquals(sk2.getEstimate(), sk.getEstimate());
+ assertEquals(sk2.getSeedHash(), SEED_HASH);
+ //default seed image read with a custom seed is rejected
+ final MemorySegment segDef =
MemorySegment.ofArray(tupleSketch(DEFAULT_UPDATE_SEED, 0,
100).compact().toByteArray());
+ expectThrows(SketchesArgumentException.class, () ->
TupleSketch.heapifySketch(segDef, deser, SEED));
+ }
+
+ @Test
+ public void checkHeapifyEmptyCompact() {
+ final byte[] bytes = tupleSketch(SEED, 0, 0).compact().toByteArray();
+ assertEquals(seedHashOf(bytes), (short) 0); //empty image carries zero, as
in theta
+ final MemorySegment seg = MemorySegment.ofArray(bytes);
+ assertTrue(TupleSketch.heapifySketch(seg, deser).isEmpty());
+ final TupleSketch<DoubleSummary> sk = TupleSketch.heapifySketch(seg,
deser, SEED);
+ assertTrue(sk.isEmpty());
+ assertEquals(sk.getSeedHash(), SEED_HASH);
+ }
+
+ @Test
+ public void checkUpdatableSerialization() {
+ final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED,
0, 10);
+ expectThrows(SketchesStateException.class, () -> sk.toByteArray());
+ //the updatable form has no seed hash, so it is read as the default seed
+ final byte[] bytes = tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).toByteArray();
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [QuickSelectSketch.toByteArray](1) should be avoided because it has
been deprecated.
[Show more
details](https://github.com/apache/datasketches-java/security/code-scanning/979)
##########
src/test/java/org/apache/datasketches/tuple/TupleCustomSeedTest.java:
##########
@@ -0,0 +1,243 @@
+/*
+ * 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.tuple;
+
+import static org.apache.datasketches.common.Util.DEFAULT_UPDATE_SEED;
+import static org.apache.datasketches.common.Util.computeSeedHash;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.expectThrows;
+
+import java.lang.foreign.MemorySegment;
+
+import org.apache.datasketches.common.ByteArrayUtil;
+import org.apache.datasketches.common.ResizeFactor;
+import org.apache.datasketches.common.SketchesArgumentException;
+import org.apache.datasketches.common.SketchesStateException;
+import org.apache.datasketches.fdt.FdtSketch;
+import org.apache.datasketches.theta.UpdatableThetaSketch;
+import org.apache.datasketches.tuple.adouble.DoubleSummary;
+import org.apache.datasketches.tuple.adouble.DoubleSummaryDeserializer;
+import org.apache.datasketches.tuple.adouble.DoubleSummaryFactory;
+import org.apache.datasketches.tuple.adouble.DoubleSummarySetOperations;
+import org.apache.datasketches.tuple.adouble.DoubleTupleSketch;
+import org.apache.datasketches.tuple.aninteger.IntegerSummary;
+import org.apache.datasketches.tuple.aninteger.IntegerTupleSketch;
+import org.apache.datasketches.tuple.strings.ArrayOfStringsTupleSketch;
+import org.testng.annotations.Test;
+
+/**
+ * Checks TupleSketches and their set operations created with a custom update
seed.
+ */
+@SuppressWarnings("deprecation")
+public class TupleCustomSeedTest {
+ private static final long SEED = 123L;
+ private static final short SEED_HASH = computeSeedHash(SEED);
+ private static final short DEFAULT_SEED_HASH =
computeSeedHash(DEFAULT_UPDATE_SEED);
+ private static final int SEED_HASH_SHORT = 6;
+
+ private final DoubleSummaryFactory factory = new
DoubleSummaryFactory(DoubleSummary.Mode.Sum);
+ private final DoubleSummarySetOperations dsso = new
DoubleSummarySetOperations();
+ private final DoubleSummaryDeserializer deser = new
DoubleSummaryDeserializer();
+
+ private UpdatableTupleSketch<Double, DoubleSummary> tupleSketch(final long
seed, final int start, final int n) {
+ final UpdatableTupleSketch<Double, DoubleSummary> sk =
+ new UpdatableTupleSketchBuilder<>(factory).setSeed(seed).build();
+ for (int i = start; i < (start + n); i++) { sk.update(i, 1.0); }
+ return sk;
+ }
+
+ private static UpdatableThetaSketch thetaSketch(final long seed, final int
start, final int n) {
+ final UpdatableThetaSketch sk =
UpdatableThetaSketch.builder().setSeed(seed).build();
+ for (int i = start; i < (start + n); i++) { sk.update(i); }
+ return sk;
+ }
+
+ private static short seedHashOf(final byte[] bytes) {
+ return ByteArrayUtil.getShortLE(bytes, SEED_HASH_SHORT);
+ }
+
+ @Test
+ public void checkSeedHashIsCarried() {
+ final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED,
0, 10);
+ assertEquals(sk.getSeedHash(), SEED_HASH);
+ assertEquals(sk.copy().getSeedHash(), SEED_HASH);
+ assertEquals(sk.compact().getSeedHash(), SEED_HASH);
+ assertEquals(seedHashOf(sk.compact().toByteArray()), SEED_HASH);
+ assertEquals(tupleSketch(DEFAULT_UPDATE_SEED, 0, 10).getSeedHash(),
DEFAULT_SEED_HASH);
+ }
+
+ @Test
+ public void checkDefaultSeedHashMatchesCpp() {
+ final byte[] bytes = tupleSketch(DEFAULT_UPDATE_SEED, 0,
10).compact().toByteArray();
+ assertEquals(seedHashOf(bytes), (short) 37836);
+ }
+
+ @Test
+ public void checkSeedChangesHashes() {
+ assertNotEquals(hashOf(tupleSketch(SEED, 0, 1)),
hashOf(tupleSketch(DEFAULT_UPDATE_SEED, 0, 1)));
+ }
+
+ @Test
+ public void checkHeapifyCompact() {
+ final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED,
0, 100);
+ final MemorySegment seg =
MemorySegment.ofArray(sk.compact().toByteArray());
+ expectThrows(SketchesArgumentException.class, () ->
TupleSketch.heapifySketch(seg, deser));
+ final TupleSketch<DoubleSummary> sk2 = TupleSketch.heapifySketch(seg,
deser, SEED);
+ assertEquals(sk2.getEstimate(), sk.getEstimate());
+ assertEquals(sk2.getSeedHash(), SEED_HASH);
+ //default seed image read with a custom seed is rejected
+ final MemorySegment segDef =
MemorySegment.ofArray(tupleSketch(DEFAULT_UPDATE_SEED, 0,
100).compact().toByteArray());
+ expectThrows(SketchesArgumentException.class, () ->
TupleSketch.heapifySketch(segDef, deser, SEED));
+ }
+
+ @Test
+ public void checkHeapifyEmptyCompact() {
+ final byte[] bytes = tupleSketch(SEED, 0, 0).compact().toByteArray();
+ assertEquals(seedHashOf(bytes), (short) 0); //empty image carries zero, as
in theta
+ final MemorySegment seg = MemorySegment.ofArray(bytes);
+ assertTrue(TupleSketch.heapifySketch(seg, deser).isEmpty());
+ final TupleSketch<DoubleSummary> sk = TupleSketch.heapifySketch(seg,
deser, SEED);
+ assertTrue(sk.isEmpty());
+ assertEquals(sk.getSeedHash(), SEED_HASH);
+ }
+
+ @Test
+ public void checkUpdatableSerialization() {
+ final UpdatableTupleSketch<Double, DoubleSummary> sk = tupleSketch(SEED,
0, 10);
+ expectThrows(SketchesStateException.class, () -> sk.toByteArray());
Review Comment:
## CodeQL / Deprecated method or constructor invocation
Invoking [QuickSelectSketch.toByteArray](1) should be avoided because it has
been deprecated.
[Show more
details](https://github.com/apache/datasketches-java/security/code-scanning/980)
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]