rdblue commented on a change in pull request #1221:
URL: https://github.com/apache/iceberg/pull/1221#discussion_r458241058



##########
File path: spark/src/main/java/org/apache/iceberg/spark/source/ReaderUtils.java
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.source;
+
+import org.apache.iceberg.SnapshotSummary;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.spark.SparkSchemaUtil;
+import org.apache.spark.sql.types.StructField;
+import org.apache.spark.sql.types.StructType;
+
+/**
+ * common reader code between spark2 and spark3 module
+ */
+public class ReaderUtils {
+  private ReaderUtils() {
+
+  }
+  /**
+   * guess table size based on table schema and total records.
+   *
+   * @param table iceberg table
+   * @param totalRecords total records in the table
+   * @return approxiate size based on table schema
+   */
+
+  public static long approximateTableSize(Table table, long totalRecords) {
+    if (totalRecords == Long.MAX_VALUE) {
+      return totalRecords;
+    }
+    StructType type = SparkSchemaUtil.convert(table.schema());
+    long approximateSize = 0;
+    for (StructField sparkField : type.fields()) {
+      approximateSize += sparkField.dataType().defaultSize();
+    }
+    return approximateSize * totalRecords;
+  }
+
+  /**
+   * get total records from table metadata using {@link 
SnapshotSummary.TOTAL_RECORDS_PROP}.
+   *
+   * @param table iceberg table
+   * @return total records from table metadata
+   */
+
+  public static long totalRecordsFromMetadata(Table table) {

Review comment:
       I think it would make more sense to pass in a `Snapshot` because the 
caller may be reading a specific snapshot.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to