-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calculate Iceberg NDV with a Theta sketch
Iceberg specification defines the Apache DataSketches's Theta as the common data sketch for keeping track of distinct values in a table. This change replaces the use of HLL within Iceberg's ANALYZE with Theta sketch. The follow-up work is to store the serialized compact form of the sketch inside the Iceberg Puffin statistics file, but this requires Iceberg API changes, which are still in progress. A side effect of this change is that complex types (array, map, row) can no longer be analyzed: Trino can calculate a HyperLogLog for these types, while Iceberg does not specify binary representation for these types, which is required to feed data into a Theta sketch. However, NDV for complex types is not as useful as it is for scalar types, so this shouldn't matter in practice.
- Loading branch information
Showing
7 changed files
with
269 additions
and
15 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
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
32 changes: 32 additions & 0 deletions
32
plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/aggregation/DataSketchState.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,32 @@ | ||
/* | ||
* Licensed 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 io.trino.plugin.iceberg.aggregation; | ||
|
||
import io.trino.spi.function.AccumulatorState; | ||
import io.trino.spi.function.AccumulatorStateMetadata; | ||
import org.apache.datasketches.theta.CompactSketch; | ||
import org.apache.datasketches.theta.UpdateSketch; | ||
|
||
@AccumulatorStateMetadata(stateSerializerClass = DataSketchStateSerializer.class) | ||
public interface DataSketchState | ||
extends AccumulatorState | ||
{ | ||
UpdateSketch getUpdateSketch(); | ||
|
||
void setUpdateSketch(UpdateSketch value); | ||
|
||
CompactSketch getCompactSketch(); | ||
|
||
void setCompactSketch(CompactSketch value); | ||
} |
73 changes: 73 additions & 0 deletions
73
...-iceberg/src/main/java/io/trino/plugin/iceberg/aggregation/DataSketchStateSerializer.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,73 @@ | ||
/* | ||
* Licensed 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 io.trino.plugin.iceberg.aggregation; | ||
|
||
import io.airlift.slice.Slice; | ||
import io.airlift.slice.Slices; | ||
import io.trino.spi.block.Block; | ||
import io.trino.spi.block.BlockBuilder; | ||
import io.trino.spi.function.AccumulatorStateSerializer; | ||
import io.trino.spi.type.Type; | ||
import org.apache.datasketches.memory.WritableMemory; | ||
import org.apache.datasketches.theta.CompactSketch; | ||
|
||
import java.util.Optional; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static io.trino.spi.type.VarbinaryType.VARBINARY; | ||
|
||
public class DataSketchStateSerializer | ||
implements AccumulatorStateSerializer<DataSketchState> | ||
{ | ||
@Override | ||
public Type getSerializedType() | ||
{ | ||
return VARBINARY; | ||
} | ||
|
||
@Override | ||
public void serialize(DataSketchState state, BlockBuilder out) | ||
{ | ||
serializeToVarbinary(state, out); | ||
} | ||
|
||
public static void serializeToVarbinary(DataSketchState state, BlockBuilder out) | ||
{ | ||
if (state.getUpdateSketch() == null && state.getCompactSketch() == null) { | ||
out.appendNull(); | ||
} | ||
else { | ||
checkArgument(state.getUpdateSketch() == null || state.getCompactSketch() == null, "A state must not have both transient accumulator and combined form set"); | ||
CompactSketch compactSketch = Optional.ofNullable(state.getCompactSketch()) | ||
.orElseGet(() -> state.getUpdateSketch().compact()); | ||
Slice slice = Slices.wrappedBuffer(compactSketch.toByteArray()); | ||
VARBINARY.writeSlice(out, slice); | ||
} | ||
} | ||
|
||
@Override | ||
public void deserialize(Block block, int index, DataSketchState state) | ||
{ | ||
if (!block.isNull(index)) { | ||
state.setCompactSketch(deserialize(block, index)); | ||
} | ||
} | ||
|
||
public static CompactSketch deserialize(Block block, int index) | ||
{ | ||
checkArgument(!block.isNull(index), "Value is null"); | ||
Slice slice = VARBINARY.getSlice(block, index); | ||
return CompactSketch.heapify(WritableMemory.writableWrap(slice.getBytes())); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
...iceberg/src/main/java/io/trino/plugin/iceberg/aggregation/IcebergThetaSketchForStats.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,124 @@ | ||
/* | ||
* Licensed 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 io.trino.plugin.iceberg.aggregation; | ||
|
||
import io.trino.spi.block.Block; | ||
import io.trino.spi.block.BlockBuilder; | ||
import io.trino.spi.function.AggregationFunction; | ||
import io.trino.spi.function.AggregationState; | ||
import io.trino.spi.function.BlockIndex; | ||
import io.trino.spi.function.BlockPosition; | ||
import io.trino.spi.function.CombineFunction; | ||
import io.trino.spi.function.InputFunction; | ||
import io.trino.spi.function.OutputFunction; | ||
import io.trino.spi.function.SqlType; | ||
import io.trino.spi.function.TypeParameter; | ||
import io.trino.spi.type.StandardTypes; | ||
import io.trino.spi.type.Type; | ||
import org.apache.datasketches.Family; | ||
import org.apache.datasketches.theta.SetOperation; | ||
import org.apache.datasketches.theta.Sketch; | ||
import org.apache.datasketches.theta.Union; | ||
import org.apache.datasketches.theta.UpdateSketch; | ||
import org.apache.iceberg.types.Conversions; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
import static com.google.common.base.Verify.verify; | ||
import static io.trino.plugin.iceberg.IcebergTypes.convertTrinoValueToIceberg; | ||
import static io.trino.plugin.iceberg.TypeConverter.toIcebergType; | ||
import static io.trino.spi.type.TypeUtils.readNativeValue; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
@AggregationFunction(value = IcebergThetaSketchForStats.NAME, hidden = true) | ||
public final class IcebergThetaSketchForStats | ||
{ | ||
private IcebergThetaSketchForStats() {} | ||
|
||
public static final String NAME = "$iceberg_theta_stat"; | ||
|
||
@InputFunction | ||
@TypeParameter("T") | ||
public static void input(@TypeParameter("T") Type type, @AggregationState DataSketchState state, @BlockPosition @SqlType("T") Block block, @BlockIndex int index) | ||
{ | ||
verify(!block.isNull(index), "Input function is not expected to be called on a NULL input"); | ||
|
||
Object trinoValue = readNativeValue(type, block, index); | ||
org.apache.iceberg.types.Type icebergType = toIcebergType(type); | ||
Object icebergValue = convertTrinoValueToIceberg(type, trinoValue); | ||
ByteBuffer byteBuffer = Conversions.toByteBuffer(icebergType, icebergValue); | ||
requireNonNull(byteBuffer, "byteBuffer is null"); // trino value isn't null | ||
byte[] bytes = getBytes(byteBuffer); | ||
getOrCreateUpdateSketch(state).update(bytes); | ||
} | ||
|
||
@CombineFunction | ||
public static void combine(@AggregationState DataSketchState state, @AggregationState DataSketchState otherState) | ||
{ | ||
Union union = SetOperation.builder().buildUnion(); | ||
addIfPresent(union, state.getUpdateSketch()); | ||
addIfPresent(union, state.getCompactSketch()); | ||
addIfPresent(union, otherState.getUpdateSketch()); | ||
addIfPresent(union, otherState.getCompactSketch()); | ||
|
||
state.setUpdateSketch(null); | ||
state.setCompactSketch(union.getResult()); | ||
} | ||
|
||
@OutputFunction(StandardTypes.VARBINARY) | ||
public static void output(@AggregationState DataSketchState state, BlockBuilder out) | ||
{ | ||
if (state.getUpdateSketch() == null && state.getCompactSketch() == null) { | ||
getOrCreateUpdateSketch(state); | ||
} | ||
DataSketchStateSerializer.serializeToVarbinary(state, out); | ||
} | ||
|
||
private static UpdateSketch getOrCreateUpdateSketch(@AggregationState DataSketchState state) | ||
{ | ||
UpdateSketch sketch = state.getUpdateSketch(); | ||
if (sketch == null) { | ||
// Must match Iceberg table statistics specification | ||
// https://iceberg.apache.org/puffin-spec/#apache-datasketches-theta-v1-blob-type | ||
sketch = UpdateSketch.builder() | ||
.setFamily(Family.ALPHA) | ||
.build(); | ||
state.setUpdateSketch(sketch); | ||
} | ||
return sketch; | ||
} | ||
|
||
private static void addIfPresent(Union union, @Nullable Sketch input) | ||
{ | ||
if (input != null) { | ||
union.union(input); | ||
} | ||
} | ||
|
||
private static byte[] getBytes(ByteBuffer byteBuffer) | ||
{ | ||
int length = byteBuffer.remaining(); | ||
if (byteBuffer.hasArray() && byteBuffer.arrayOffset() == 0) { | ||
byte[] bytes = byteBuffer.array(); | ||
if (bytes.length == length) { | ||
return bytes; | ||
} | ||
} | ||
byte[] bytes = new byte[length]; | ||
byteBuffer.get(bytes); | ||
return bytes; | ||
} | ||
} |
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