findepi commented on code in PR #10288: URL: https://github.com/apache/iceberg/pull/10288#discussion_r1694014828
########## spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/actions/NDVSketchGenerator.java: ########## @@ -0,0 +1,125 @@ +/* + * 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.iceberg.spark.actions; + +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.datasketches.theta.Sketch; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Table; +import org.apache.iceberg.data.Record; +import org.apache.iceberg.puffin.Blob; +import org.apache.iceberg.puffin.StandardBlobTypes; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.spark.SparkReadOptions; +import org.apache.iceberg.spark.SparkValueConverter; +import org.apache.iceberg.types.Conversions; +import org.apache.iceberg.types.Types; +import org.apache.spark.api.java.JavaPairRDD; +import org.apache.spark.sql.Column; +import org.apache.spark.sql.Dataset; +import org.apache.spark.sql.Row; +import org.apache.spark.sql.SparkSession; +import org.apache.spark.sql.functions; +import scala.Tuple2; + +public class NDVSketchGenerator { + + private NDVSketchGenerator() {} + + public static final String APACHE_DATASKETCHES_THETA_V1_NDV_PROPERTY = "ndv"; + + static List<Blob> generateNDVSketchesAndBlobs( + SparkSession spark, Table table, long snapshotId, Set<String> columnsToBeAnalyzed) { + Map<Integer, ThetaSketchJavaSerializable> columnToSketchMap = + computeNDVSketches(spark, table, snapshotId, columnsToBeAnalyzed); + return generateBlobs(table, columnsToBeAnalyzed, columnToSketchMap, snapshotId); + } + + private static List<Blob> generateBlobs( + Table table, + Set<String> columns, + Map<Integer, ThetaSketchJavaSerializable> sketchMap, + long snapshotId) { + return columns.stream() + .map( + columnName -> { + Types.NestedField field = table.schema().findField(columnName); + Sketch sketch = sketchMap.get(field.fieldId()).getSketch(); + long ndv = (long) sketch.getEstimate(); Review Comment: @szehon-ho This is a good question. It looks like we cut a corner here. The value is an estimate of an integer value (number of distinct values ∈ ℕ∪{0}). In this context, fractional part of a double is significant only for small numbers (≤10). However, for small NDV, the theta sketch might actually be exact, so I don't think floating point value really adds significantly more information, and cutting this corner was justified. if we choose to store fractional values, we should define representation. For example, should scientific notation be supported? @karuppayya Trino currently expects the value to be an exact integer value not exceeding 2⁶³-1. The code is here https://github.com/trinodb/trino/blob/90bfd3520fac6b64a11632028a62740e65b974de/plugin/trino-iceberg/src/main/java/io/trino/plugin/iceberg/TableStatisticsReader.java#L262 if we choose to store fractional values, this code would need to be updated. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org