mao-liu commented on code in PR #767:
URL: https://github.com/apache/incubator-xtable/pull/767#discussion_r2605818307


##########
xtable-core/src/main/java/org/apache/xtable/paimon/PaimonDataFileExtractor.java:
##########
@@ -78,10 +93,154 @@ private String toFullPhysicalPath(FileStoreTable table, 
ManifestEntry entry) {
     }
   }
 
-  private List<ColumnStat> toColumnStats(DataFileMeta file) {
-    // TODO: Implement logic to extract column stats from the file meta
-    // https://github.com/apache/incubator-xtable/issues/755
-    return Collections.emptyList();
+  private List<ColumnStat> toColumnStats(DataFileMeta file, InternalSchema 
internalSchema) {
+    List<ColumnStat> columnStats = new ArrayList<>();
+    Map<String, InternalField> fieldMap =
+        internalSchema.getAllFields().stream()
+            .collect(Collectors.toMap(InternalField::getPath, f -> f));
+
+    // stats for all columns are present in valueStats, we can safely ignore 
file.keyStats() - TODO: validate this assumption
+    SimpleStats valueStats = file.valueStats();
+    if (valueStats != null) {
+      //      log.info("Processing valueStats: {}", valueStats.toRow());
+      List<String> colNames = file.valueStatsCols();
+      //      log.info("valueStatsCols: {}", colNames);
+      if (colNames == null || colNames.isEmpty()) {
+        // if column names are not present, we assume all columns in the 
schema are present in the same order as the schema - TODO: validate this 
assumption
+        colNames =
+            internalSchema.getAllFields().stream()
+                .map(InternalField::getPath)
+                .collect(Collectors.toList());
+      }
+
+      if (colNames.size() != valueStats.minValues().getFieldCount()) {
+        // paranoia check - this should never happen, but if the code reaches 
here, then there is a bug! Please file a bug report
+        throw new ReadException(
+            String.format(
+                "Mismatch between column stats names and values arity: 
names=%d, values=%d",
+                colNames.size(), valueStats.minValues().getFieldCount()));
+      }
+
+      extractStats(columnStats, valueStats, colNames, fieldMap, 
file.rowCount());
+    }
+
+    return columnStats;
+  }
+
+  private void extractStats(
+      List<ColumnStat> columnStats,
+      SimpleStats stats,
+      List<String> colNames,
+      Map<String, InternalField> fieldMap,
+      long rowCount) {
+    BinaryRow minValues = stats.minValues();
+    BinaryRow maxValues = stats.maxValues();
+    BinaryArray nullCounts = stats.nullCounts();
+
+    //    log.info("Extracting stats for columns: {}", colNames);
+    //    log.info("minValues: arity={}, {}", minValues.getFieldCount(), 
minValues);
+    //    log.info("maxValues: arity={}, {}", maxValues.getFieldCount(), 
maxValues);
+    //    log.info("fieldMap: {}", fieldMap.toString());
+
+    for (int i = 0; i < colNames.size(); i++) {
+      String colName = colNames.get(i);
+      InternalField field = fieldMap.get(colName);
+      if (field == null) {
+        continue;
+      }
+
+      // Check if we already have stats for this field
+      boolean alreadyExists =
+          columnStats.stream().anyMatch(cs -> 
cs.getField().getPath().equals(colName));
+      if (alreadyExists) {
+        continue;
+      }
+
+      InternalType type = field.getSchema().getDataType();
+      Object min = getValue(minValues, i, type, field.getSchema());
+      Object max = getValue(maxValues, i, type, field.getSchema());
+      Long nullCount = (nullCounts != null && i < nullCounts.size()) ? 
nullCounts.getLong(i) : 0L;
+
+      //      log.info(
+      //          "Column: {}, Index: {}, Min: {}, Max: {}, NullCount: {}",
+      //          colName,
+      //          i,
+      //          min,
+      //          max,
+      //          nullCount);
+
+      columnStats.add(
+          ColumnStat.builder()
+              .field(field)
+              .range(Range.vector(min, max))
+              .numNulls(nullCount)
+              .numValues(rowCount)
+              .build());
+    }
+  }
+
+  private Object getValue(BinaryRow row, int index, InternalType type, 
InternalSchema fieldSchema) {
+    if (row.isNullAt(index)) {
+      return null;
+    }
+    switch (type) {
+      case BOOLEAN:
+        return row.getBoolean(index);
+      case INT:
+      case DATE:
+        return row.getInt(index);
+      case LONG:
+        return row.getLong(index);
+      case TIMESTAMP:
+      case TIMESTAMP_NTZ:
+        int tsPrecision;
+        InternalSchema.MetadataValue tsPrecisionEnum =
+            (InternalSchema.MetadataValue)
+                
fieldSchema.getMetadata().get(InternalSchema.MetadataKey.TIMESTAMP_PRECISION);
+        if (tsPrecisionEnum == InternalSchema.MetadataValue.MILLIS) {
+          tsPrecision = 3;
+        } else if (tsPrecisionEnum == InternalSchema.MetadataValue.MICROS) {
+          tsPrecision = 6;
+        } else if (tsPrecisionEnum == InternalSchema.MetadataValue.NANOS) {
+          tsPrecision = 9;
+        } else {
+          log.warn(
+              "Field idx={}, name={} does not have 
MetadataKey.TIMESTAMP_PRECISION set, defaulting to default precision",
+              index,
+              fieldSchema.getName());
+          tsPrecision = TimestampType.DEFAULT_PRECISION;
+        }
+        // TODO: BinaryRow.getTimestamp().toInstant() is deprecated (use 
LocalZoneTimestamp), but BinaryRow does not have a method to get 
LocalZoneTimestamp?
+        Instant timestamp = row.getTimestamp(index, tsPrecision).toInstant();
+        long tsMillis = timestamp.toEpochMilli();
+
+        // according to docs for org.apache.xtable.model.stat.Range, timestamp 
is stored as millis
+        // or micros - even if precision is higher than micros, return micros

Review Comment:
   Q: to xtable maintainers - is this assumption valid?



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

Reply via email to