You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by pv...@apache.org on 2026/06/23 15:28:46 UTC

(iceberg) branch main updated: ORC: Fix lower/upper bounds for timestamp_ns columns in OrcMetrics (#16922)

This is an automated email from the ASF dual-hosted git repository.

pvary pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/main by this push:
     new ef07755dd3 ORC: Fix lower/upper bounds for timestamp_ns columns in OrcMetrics (#16922)
ef07755dd3 is described below

commit ef07755dd32b59e62346e8599753f75076e57151
Author: Eunbin Son <58...@users.noreply.github.com>
AuthorDate: Wed Jun 24 00:28:38 2026 +0900

    ORC: Fix lower/upper bounds for timestamp_ns columns in OrcMetrics (#16922)
---
 .../org/apache/iceberg/orc/TestOrcMetrics.java     | 35 ++++++++++++++++++++++
 .../java/org/apache/iceberg/orc/OrcMetrics.java    | 20 +++++++++----
 2 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/data/src/test/java/org/apache/iceberg/orc/TestOrcMetrics.java b/data/src/test/java/org/apache/iceberg/orc/TestOrcMetrics.java
index 940cf45222..c1647bb716 100644
--- a/data/src/test/java/org/apache/iceberg/orc/TestOrcMetrics.java
+++ b/data/src/test/java/org/apache/iceberg/orc/TestOrcMetrics.java
@@ -22,6 +22,8 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.File;
 import java.io.IOException;
+import java.time.LocalDateTime;
+import java.time.temporal.ChronoUnit;
 import java.util.Map;
 import java.util.UUID;
 import org.apache.iceberg.FileFormat;
@@ -31,6 +33,7 @@ import org.apache.iceberg.MetricsConfig;
 import org.apache.iceberg.ParameterizedTestExtension;
 import org.apache.iceberg.Schema;
 import org.apache.iceberg.TestMetrics;
+import org.apache.iceberg.data.GenericRecord;
 import org.apache.iceberg.data.Record;
 import org.apache.iceberg.data.orc.GenericOrcWriter;
 import org.apache.iceberg.io.FileAppender;
@@ -39,7 +42,10 @@ import org.apache.iceberg.io.OutputFile;
 import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
 import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
 import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Conversions;
 import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+import org.junit.jupiter.api.TestTemplate;
 import org.junit.jupiter.api.extension.ExtendWith;
 
 /** Test Metrics for ORC. */
@@ -107,6 +113,35 @@ public class TestOrcMetrics extends TestMetrics {
     return BINARY_TYPES.contains(type.typeId());
   }
 
+  @TestTemplate
+  public void timestampNanoBoundsKeepNanoPrecision() throws IOException {
+    Types.TimestampNanoType nanoType = Types.TimestampNanoType.withoutZone();
+    Schema schema = new Schema(Types.NestedField.optional(1, "tsNano", nanoType));
+
+    // sub-microsecond nanos that would change if truncated to micros
+    LocalDateTime lower = LocalDateTime.parse("1970-01-01T00:00:00.000001500");
+    LocalDateTime upper = LocalDateTime.parse("2024-01-02T03:04:05.123456789");
+
+    GenericRecord lowerRecord = GenericRecord.create(schema);
+    lowerRecord.set(0, lower);
+    GenericRecord upperRecord = GenericRecord.create(schema);
+    upperRecord.set(0, upper);
+
+    Metrics metrics = getMetrics(schema, lowerRecord, upperRecord);
+
+    LocalDateTime epoch = LocalDateTime.parse("1970-01-01T00:00:00");
+    long expectedLower = ChronoUnit.NANOS.between(epoch, lower);
+    long expectedUpper = ChronoUnit.NANOS.between(epoch, upper);
+
+    long actualLower = Conversions.fromByteBuffer(nanoType, metrics.lowerBounds().get(1));
+    long actualUpper = Conversions.fromByteBuffer(nanoType, metrics.upperBounds().get(1));
+
+    assertThat(actualLower).isEqualTo(expectedLower);
+    assertThat(actualUpper).isEqualTo(expectedUpper);
+    // guard against the micros regression: the bound must not be ~1000x smaller
+    assertThat(actualLower).isEqualTo(1500L);
+  }
+
   @Override
   protected <T> void assertBounds(
       int fieldId, Type type, T lowerBound, T upperBound, MetricsWithStats metricsWithStats) {
diff --git a/orc/src/main/java/org/apache/iceberg/orc/OrcMetrics.java b/orc/src/main/java/org/apache/iceberg/orc/OrcMetrics.java
index 867828820d..7498d6e155 100644
--- a/orc/src/main/java/org/apache/iceberg/orc/OrcMetrics.java
+++ b/orc/src/main/java/org/apache/iceberg/orc/OrcMetrics.java
@@ -21,6 +21,9 @@ package org.apache.iceberg.orc;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.sql.Timestamp;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.time.temporal.ChronoUnit;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -267,9 +270,7 @@ public class OrcMetrics {
       TimestampColumnStatistics tColStats = (TimestampColumnStatistics) columnStats;
       Timestamp minValue = tColStats.getMinimumUTC();
       min =
-          Optional.ofNullable(minValue)
-              .map(v -> DateTimeUtil.microsFromInstant(v.toInstant()))
-              .orElse(null);
+          Optional.ofNullable(minValue).map(v -> timestampBound(type, v.toInstant())).orElse(null);
     } else if (columnStats instanceof BooleanColumnStatistics) {
       BooleanColumnStatistics booleanStats = (BooleanColumnStatistics) columnStats;
       min = booleanStats.getFalseCount() <= 0;
@@ -322,9 +323,7 @@ public class OrcMetrics {
       TimestampColumnStatistics tColStats = (TimestampColumnStatistics) columnStats;
       Timestamp maxValue = tColStats.getMaximumUTC();
       max =
-          Optional.ofNullable(maxValue)
-              .map(v -> DateTimeUtil.microsFromInstant(v.toInstant()))
-              .orElse(null);
+          Optional.ofNullable(maxValue).map(v -> timestampBound(type, v.toInstant())).orElse(null);
     } else if (columnStats instanceof BooleanColumnStatistics) {
       BooleanColumnStatistics booleanStats = (BooleanColumnStatistics) columnStats;
       max = booleanStats.getTrueCount() > 0;
@@ -333,6 +332,15 @@ public class OrcMetrics {
         Conversions.toByteBuffer(type, truncateIfNeeded(Bound.UPPER, type, max, metricsMode)));
   }
 
+  private static long timestampBound(Type type, Instant instant) {
+    // timestamp_ns columns store bounds as nanoseconds from epoch, while timestamp columns store
+    // microseconds (see Conversions for TIMESTAMP and TIMESTAMP_NANO).
+    if (type.typeId() == Type.TypeID.TIMESTAMP_NANO) {
+      return ChronoUnit.NANOS.between(DateTimeUtil.EPOCH, instant.atOffset(ZoneOffset.UTC));
+    }
+    return DateTimeUtil.microsFromInstant(instant);
+  }
+
   private static Object replaceNaN(double value, double replacement) {
     return Double.isNaN(value) ? replacement : value;
   }