Indian-boult commented on code in PR #1014:
URL: https://github.com/apache/incubator-xtable/pull/1014#discussion_r4656927461


##########
xtable-core/src/main/java/org/apache/xtable/delta/DeltaStatsUtils.java:
##########
@@ -179,6 +180,24 @@ public static FileStats parseColumnStatsFromJson(String 
statsJson, List<Internal
     }
   }
 
+  /**
+   * Maps each field's path to the path its stats are keyed by. With column 
mapping, Delta keys the
+   * stats by physical name, so a path segment is the field's storage name 
when one is set. There is
+   * no fallback to the logical name, which can be another column's physical 
name after renames.
+   * Parents must come before their children, as in {@link 
InternalSchema#getAllFields()}.
+   */
+  private static Map<String, String> getPhysicalPaths(List<InternalField> 
fields) {
+    Map<String, String> physicalPaths = new HashMap<>();
+    for (InternalField field : fields) {
+      String name = field.getStorageName() != null ? field.getStorageName() : 
field.getName();
+      String parentPath = field.getParentPath();
+      physicalPaths.put(
+          field.getPath(),
+          StringUtils.isEmpty(parentPath) ? name : 
physicalPaths.get(parentPath) + "." + name);

Review Comment:
   Done in 8c2fa30. `getPhysicalPaths` now builds each path with 
`SchemaUtils.getFullyQualifiedPath`.



##########
xtable-core/src/test/java/org/apache/xtable/delta/TestDeltaStatsExtractor.java:
##########
@@ -220,6 +220,108 @@ void convertNullStatsToInternalRepresentation() {
     assertEquals(0, actualNumRecords);
   }
 
+  @Test
+  void convertStatsWithColumnMappingPhysicalNames() {
+    // With column mapping, the stats are keyed by physical name at every 
level of nesting.
+    List<InternalField> fields =
+        Arrays.asList(
+            columnMappedField("top_level_string", "col-1f2e", null, 
InternalType.STRING),
+            columnMappedField("nested", "col-3d4c", null, InternalType.RECORD),
+            columnMappedField("int_field", "col-5b6a", "nested", 
InternalType.INT));
+    String stats =
+        "{\"numRecords\":100,"
+            + 
"\"minValues\":{\"col-1f2e\":\"a\",\"col-3d4c\":{\"col-5b6a\":1}},"
+            + 
"\"maxValues\":{\"col-1f2e\":\"b\",\"col-3d4c\":{\"col-5b6a\":2}},"
+            + "\"nullCount\":{\"col-1f2e\":3,\"col-3d4c\":{\"col-5b6a\":4}}}";
+    List<ColumnStat> expected =
+        Arrays.asList(
+            ColumnStat.builder()
+                .field(fields.get(0))
+                .numValues(100)
+                .numNulls(3L)
+                .range(Range.vector("a", "b"))
+                .build(),
+            ColumnStat.builder()
+                .field(fields.get(2))
+                .numValues(100)
+                .numNulls(4L)
+                .range(Range.vector(1, 2))
+                .build());
+    assertEquals(expected, extractColumnStats(stats, fields));
+  }
+
+  @Test
+  void convertStatsWithColumnMappingSwappedNames() {
+    // Column mapping enabled on an existing table keeps the old names as 
physical names. After a
+    // and b swap names, each logical name is the other column's physical name.
+    List<InternalField> fields =
+        Arrays.asList(
+            columnMappedField("a", "b", null, InternalType.INT),
+            columnMappedField("b", "a", null, InternalType.INT));
+    String stats =
+        "{\"numRecords\":100,"
+            + "\"minValues\":{\"a\":1,\"b\":10},"
+            + "\"maxValues\":{\"a\":2,\"b\":20},"
+            + "\"nullCount\":{\"a\":0,\"b\":5}}";
+    List<ColumnStat> expected =
+        Arrays.asList(
+            ColumnStat.builder()
+                .field(fields.get(0))
+                .numValues(100)
+                .numNulls(5L)
+                .range(Range.vector(10, 20))
+                .build(),
+            ColumnStat.builder()
+                .field(fields.get(1))
+                .numValues(100)
+                .numNulls(0L)
+                .range(Range.vector(1, 2))
+                .build());
+    assertEquals(expected, extractColumnStats(stats, fields));
+  }
+
+  @Test
+  void convertStatsWithColumnMappingReAddedColumn() {
+    // After renaming a to a_old, a_old keeps physical name a and a re-added a 
gets a new physical
+    // name. A file written before the re-add has stats keyed by a, but none 
for the new a. The new
+    // a is a DATE, so the INT stats of a_old do not parse as its stats.
+    List<InternalField> fields =
+        Arrays.asList(
+            columnMappedField("a_old", "a", null, InternalType.INT),
+            columnMappedField("a", "col-1f2e", null, InternalType.DATE));
+    String stats =
+        "{\"numRecords\":100,"
+            + "\"minValues\":{\"a\":1},"
+            + "\"maxValues\":{\"a\":2},"
+            + "\"nullCount\":{\"a\":0}}";
+    List<ColumnStat> expected =
+        Collections.singletonList(
+            ColumnStat.builder()
+                .field(fields.get(0))
+                .numValues(100)
+                .numNulls(0L)
+                .range(Range.vector(1, 2))
+                .build());
+    assertEquals(expected, extractColumnStats(stats, fields));
+  }
+
+  private static List<ColumnStat> extractColumnStats(String stats, 
List<InternalField> fields) {

Review Comment:
   Done in 8c2fa30. The helper now returns the `FileStats` from 
`getColumnStatsForFile`, which already holds the column stats and `numRecords`, 
so no extra Pair type is needed. `roundTripStatsConversion`, 
`convertStatsToInternalRepresentation` and 
`convertNullStatsToInternalRepresentation` use it now, and the column mapping 
tests check the record count as well.



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