-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] introduce theta_sketch aggregate function. (#3723)
- Loading branch information
Showing
8 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
paimon-common/src/main/java/org/apache/paimon/utils/ThetaSketch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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.paimon.utils; | ||
|
||
import org.apache.paimon.annotation.VisibleForTesting; | ||
|
||
import org.apache.datasketches.memory.Memory; | ||
import org.apache.datasketches.theta.Sketches; | ||
import org.apache.datasketches.theta.Union; | ||
import org.apache.datasketches.theta.UpdateSketch; | ||
|
||
/** A compressed bitmap for 32-bit integer. */ | ||
public class ThetaSketch { | ||
|
||
public static byte[] union(byte[] sketchBytes1, byte[] sketchBytes2) { | ||
Union union = Sketches.setOperationBuilder().buildUnion(); | ||
union.union(Memory.wrap(sketchBytes1)); | ||
union.union(Memory.wrap(sketchBytes2)); | ||
return union.getResult().toByteArray(); | ||
} | ||
|
||
@VisibleForTesting | ||
public static byte[] sketchOf(int... values) { | ||
UpdateSketch updateSketch = UpdateSketch.builder().build(); | ||
for (int value : values) { | ||
updateSketch.update(value); | ||
} | ||
return updateSketch.compact().toByteArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...core/src/main/java/org/apache/paimon/mergetree/compact/aggregate/FieldThetaSketchAgg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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.paimon.mergetree.compact.aggregate; | ||
|
||
import org.apache.paimon.types.VarBinaryType; | ||
import org.apache.paimon.utils.ThetaSketch; | ||
|
||
/** ThetaSketch aggregate a field of a row. */ | ||
public class FieldThetaSketchAgg extends FieldAggregator { | ||
|
||
public static final String NAME = "theta_sketch"; | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public FieldThetaSketchAgg(VarBinaryType dataType) { | ||
super(dataType); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public Object agg(Object accumulator, Object inputField) { | ||
if (accumulator == null && inputField == null) { | ||
return null; | ||
} | ||
|
||
if (accumulator == null || inputField == null) { | ||
return accumulator == null ? inputField : accumulator; | ||
} | ||
|
||
return ThetaSketch.union((byte[]) accumulator, (byte[]) inputField); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters